epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 9 | #include "GrContext.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 10 | #include "GrDrawTargetCaps.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 11 | #include "GrGpu.h" |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 12 | #include "GrResourceCache.h" |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 13 | #include "GrTexture.h" |
| 14 | #include "GrTexturePriv.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 15 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 16 | void GrTexture::dirtyMipMaps(bool mipMapsDirty) { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 17 | if (mipMapsDirty) { |
| 18 | if (kValid_MipMapsStatus == fMipMapsStatus) { |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 19 | fMipMapsStatus = kAllocated_MipMapsStatus; |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 20 | } |
| 21 | } else { |
| 22 | const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus; |
| 23 | fMipMapsStatus = kValid_MipMapsStatus; |
| 24 | if (sizeChanged) { |
| 25 | // This must not be called until after changing fMipMapsStatus. |
| 26 | this->didChangeGpuMemorySize(); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | size_t GrTexture::gpuMemorySize() const { |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 32 | size_t textureSize; |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 33 | |
| 34 | if (GrPixelConfigIsCompressed(fDesc.fConfig)) { |
krajcevski | 7ef2162 | 2014-07-16 15:21:13 -0700 | [diff] [blame] | 35 | textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight); |
bsalomon | d4cb922 | 2014-08-11 14:19:09 -0700 | [diff] [blame] | 36 | } else { |
| 37 | textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig); |
commit-bot@chromium.org | 6e7ddaa | 2014-05-30 13:55:58 +0000 | [diff] [blame] | 38 | } |
| 39 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 40 | if (this->texturePriv().hasMipMaps()) { |
commit-bot@chromium.org | 11c6b39 | 2014-05-05 19:09:13 +0000 | [diff] [blame] | 41 | // We don't have to worry about the mipmaps being a different size than |
| 42 | // we'd expect because we never change fDesc.fWidth/fHeight. |
| 43 | textureSize *= 2; |
| 44 | } |
| 45 | return textureSize; |
| 46 | } |
| 47 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 48 | void GrTexture::validateDesc() const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 49 | if (this->asRenderTarget()) { |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 50 | // This texture has a render target |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 51 | SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 52 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 53 | if (this->asRenderTarget()->getStencilBuffer()) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 54 | SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrSurfaceFlag)); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 55 | } else { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 56 | SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag)); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 57 | } |
| 58 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 59 | SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples()); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 60 | } else { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 61 | SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag)); |
| 62 | SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 63 | SkASSERT(0 == fDesc.fSampleCnt); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 67 | ////////////////////////////////////////////////////////////////////////////// |
| 68 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 69 | // These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 70 | // key |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 71 | enum TextureFlags { |
| 72 | /** |
| 73 | * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the |
| 74 | * hardware doesn't support that feature. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 75 | */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 76 | kStretchToPOT_TextureFlag = 0x1, |
| 77 | /** |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 78 | * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the |
| 79 | * stretched texture should be bilerped. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 80 | */ |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 81 | kBilerp_TextureFlag = 0x2, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | namespace { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 85 | GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu, |
| 86 | const GrTextureParams* params, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 87 | const GrSurfaceDesc& desc) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 88 | GrResourceKey::ResourceFlags flags = 0; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 89 | bool tiled = params && params->isTiled(); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 90 | if (tiled && !gpu->caps()->npotTextureTileSupport()) { |
tfarina | f9dae78 | 2014-06-06 06:35:28 -0700 | [diff] [blame] | 91 | if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 92 | flags |= kStretchToPOT_TextureFlag; |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 93 | switch(params->filterMode()) { |
| 94 | case GrTextureParams::kNone_FilterMode: |
| 95 | break; |
| 96 | case GrTextureParams::kBilerp_FilterMode: |
| 97 | case GrTextureParams::kMipMap_FilterMode: |
| 98 | flags |= kBilerp_TextureFlag; |
| 99 | break; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 103 | return flags; |
| 104 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 105 | |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 106 | // FIXME: This should be refactored with the code in gl/GrGpuGL.cpp. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 107 | GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) { |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 108 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 109 | // can be drawn to by the outside world without the client having |
| 110 | // to render upside down. |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 111 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag); |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 112 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
| 113 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 114 | } else { |
| 115 | return desc.fOrigin; |
| 116 | } |
| 117 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 118 | } |
| 119 | |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 120 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 121 | GrTexture::GrTexture(GrGpu* gpu, bool isWrapped, const GrSurfaceDesc& desc) |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 122 | : INHERITED(gpu, isWrapped, desc) |
| 123 | , fMipMapsStatus(kNotAllocated_MipMapsStatus) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 124 | |
| 125 | if (!isWrapped) { |
| 126 | this->setScratchKey(GrTexturePriv::ComputeScratchKey(desc)); |
| 127 | } |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 128 | // only make sense if alloc size is pow2 |
| 129 | fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); |
| 130 | fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); |
bsalomon | 744998e | 2014-08-28 09:54:34 -0700 | [diff] [blame] | 131 | } |
commit-bot@chromium.org | e49157f | 2014-05-09 20:46:48 +0000 | [diff] [blame] | 132 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 133 | GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 134 | const GrTextureParams* params, |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 135 | const GrSurfaceDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 136 | const GrCacheID& cacheID) { |
| 137 | GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 138 | return GrResourceKey(cacheID, ResourceType(), flags); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 139 | } |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 140 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 141 | GrResourceKey GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 142 | GrCacheID::Key idKey; |
| 143 | // Instead of a client-provided key of the texture contents we create a key from the |
| 144 | // descriptor. |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 145 | GR_STATIC_ASSERT(sizeof(idKey) >= 16); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 146 | SkASSERT(desc.fHeight < (1 << 16)); |
| 147 | SkASSERT(desc.fWidth < (1 << 16)); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 148 | idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
| 149 | idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
| 150 | idKey.fData32[2] = desc.fFlags; |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 151 | idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 152 | static const int kPadSize = sizeof(idKey) - 16; |
| 153 | GR_STATIC_ASSERT(kPadSize >= 0); |
| 154 | memset(idKey.fData8 + 16, 0, kPadSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 155 | |
| 156 | GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 157 | return GrResourceKey(cacheID, ResourceType(), 0); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 158 | } |
| 159 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 160 | bool GrTexturePriv::NeedsResizing(const GrResourceKey& key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 161 | return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 162 | } |
| 163 | |
bsalomon | afbf2d6 | 2014-09-30 12:18:44 -0700 | [diff] [blame] | 164 | bool GrTexturePriv::NeedsBilerp(const GrResourceKey& key) { |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 165 | return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 166 | } |