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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 10 | #include "GrTexture.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 11 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 14 | #include "GrRenderTarget.h" |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 15 | #include "GrResourceCache.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 16 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 17 | SK_DEFINE_INST_COUNT(GrTexture) |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 18 | GR_DEFINE_RESOURCE_CACHE_TYPE(GrTexture) |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 19 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 20 | /** |
| 21 | * This method allows us to interrupt the normal deletion process and place |
| 22 | * textures back in the texture cache when their ref count goes to zero. |
| 23 | */ |
| 24 | void GrTexture::internal_dispose() const { |
| 25 | |
| 26 | if (this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit) && |
| 27 | NULL != this->INHERITED::getContext()) { |
| 28 | GrTexture* nonConstThis = const_cast<GrTexture *>(this); |
| 29 | this->fRefCnt = 1; // restore ref count to initial setting |
| 30 | |
| 31 | nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit); |
| 32 | nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis); |
| 33 | |
robertphillips@google.com | f41f4d2 | 2012-06-25 17:26:29 +0000 | [diff] [blame] | 34 | // Note: "this" texture might be freed inside addExistingTextureToCache |
| 35 | // if it is purged. |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | |
| 39 | this->INHERITED::internal_dispose(); |
| 40 | } |
| 41 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 42 | bool GrTexture::readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 43 | GrPixelConfig config, void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 44 | size_t rowBytes, uint32_t pixelOpsFlags) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 45 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 46 | GrContext* context = this->getContext(); |
| 47 | if (NULL == context) { |
| 48 | return false; |
| 49 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 50 | return context->readTexturePixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 51 | left, top, width, height, |
| 52 | config, buffer, rowBytes, |
| 53 | pixelOpsFlags); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void GrTexture::writePixels(int left, int top, int width, int height, |
| 57 | GrPixelConfig config, const void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 58 | size_t rowBytes, uint32_t pixelOpsFlags) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 59 | // go through context so that all necessary flushing occurs |
| 60 | GrContext* context = this->getContext(); |
| 61 | if (NULL == context) { |
| 62 | return; |
| 63 | } |
| 64 | context->writeTexturePixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 65 | left, top, width, height, |
| 66 | config, buffer, rowBytes, |
| 67 | pixelOpsFlags); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 68 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 70 | void GrTexture::releaseRenderTarget() { |
| 71 | if (NULL != fRenderTarget) { |
| 72 | GrAssert(fRenderTarget->asTexture() == this); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 73 | GrAssert(fDesc.fFlags & kRenderTarget_GrTextureFlagBit); |
| 74 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 75 | fRenderTarget->onTextureReleaseRenderTarget(); |
| 76 | fRenderTarget->unref(); |
| 77 | fRenderTarget = NULL; |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 78 | |
| 79 | fDesc.fFlags = fDesc.fFlags & |
| 80 | ~(kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit); |
| 81 | fDesc.fSampleCnt = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 85 | void GrTexture::onRelease() { |
| 86 | GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit)); |
| 87 | this->releaseRenderTarget(); |
| 88 | } |
| 89 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 90 | void GrTexture::onAbandon() { |
| 91 | if (NULL != fRenderTarget) { |
| 92 | fRenderTarget->abandon(); |
| 93 | } |
| 94 | } |
| 95 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 96 | void GrTexture::validateDesc() const { |
| 97 | if (NULL != this->asRenderTarget()) { |
| 98 | // This texture has a render target |
| 99 | GrAssert(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 100 | |
| 101 | if (NULL != this->asRenderTarget()->getStencilBuffer()) { |
| 102 | GrAssert(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 103 | } else { |
| 104 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 105 | } |
| 106 | |
| 107 | GrAssert(fDesc.fSampleCnt == this->asRenderTarget()->numSamples()); |
| 108 | } else { |
| 109 | GrAssert(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 110 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 111 | GrAssert(0 == fDesc.fSampleCnt); |
| 112 | } |
| 113 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 114 | |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 115 | // These flags need to fit in <= 8 bits so they can be folded into the texture |
| 116 | // key |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 117 | enum TextureBits { |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 118 | /* |
| 119 | * The kNPOT bit is set when the texture is NPOT and is being repeated |
| 120 | * but the hardware doesn't support that feature. |
| 121 | */ |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 122 | kNPOT_TextureBit = 0x1, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 123 | /* |
| 124 | * The kFilter bit can only be set when the kNPOT flag is set and indicates |
| 125 | * whether the resizing of the texture should use filtering. This is |
| 126 | * to handle cases where the original texture is indexed to disable |
| 127 | * filtering. |
| 128 | */ |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 129 | kFilter_TextureBit = 0x2, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 130 | /* |
| 131 | * The kScratch bit is set if the texture is being used as a scratch |
| 132 | * texture. |
| 133 | */ |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 134 | kScratch_TextureBit = 0x4, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | namespace { |
| 138 | void gen_texture_key_values(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 139 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 140 | const GrTextureDesc& desc, |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 141 | const GrCacheData& cacheData, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 142 | bool scratch, |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 143 | GrCacheID* cacheID) { |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 144 | |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 145 | uint64_t clientKey = cacheData.fClientCacheID; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 146 | |
| 147 | if (scratch) { |
| 148 | // Instead of a client-provided key of the texture contents |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 149 | // we create a key from the descriptor. |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 150 | GrAssert(GrCacheData::kScratch_CacheID == clientKey); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 151 | clientKey = (desc.fFlags << 8) | ((uint64_t) desc.fConfig << 32); |
| 152 | } |
| 153 | |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 154 | cacheID->fPublicID = clientKey; |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 155 | cacheID->fDomain = cacheData.fResourceDomain; |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 156 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 157 | // we assume we only need 16 bits of width and height |
| 158 | // assert that texture creation will fail anyway if this assumption |
| 159 | // would cause key collisions. |
| 160 | GrAssert(gpu->getCaps().fMaxTextureSize <= SK_MaxU16); |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 161 | cacheID->fResourceSpecific32 = desc.fWidth | (desc.fHeight << 16); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 162 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 163 | GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt < 256); |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 164 | cacheID->fResourceSpecific16 = desc.fSampleCnt << 8; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 165 | |
| 166 | if (!gpu->getCaps().fNPOTTextureTileSupport) { |
| 167 | bool isPow2 = GrIsPow2(desc.fWidth) && GrIsPow2(desc.fHeight); |
| 168 | |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 169 | bool tiled = NULL != params && params->isTiled(); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 170 | |
| 171 | if (tiled && !isPow2) { |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 172 | cacheID->fResourceSpecific16 |= kNPOT_TextureBit; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 173 | if (params->isBilerp()) { |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 174 | cacheID->fResourceSpecific16 |= kFilter_TextureBit; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (scratch) { |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 180 | cacheID->fResourceSpecific16 |= kScratch_TextureBit; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 181 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 186 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 187 | const GrTextureDesc& desc, |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 188 | const GrCacheData& cacheData, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 189 | bool scratch) { |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 190 | GrCacheID id(GrTexture::GetResourceType()); |
robertphillips@google.com | 9c2ea84 | 2012-08-13 17:47:59 +0000 | [diff] [blame] | 191 | gen_texture_key_values(gpu, params, desc, cacheData, scratch, &id); |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 192 | |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 193 | uint32_t v[4]; |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 194 | id.toRaw(v); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 195 | return GrResourceKey(v); |
| 196 | } |
| 197 | |
| 198 | bool GrTexture::NeedsResizing(const GrResourceKey& key) { |
| 199 | return 0 != (key.getValue32(3) & kNPOT_TextureBit); |
| 200 | } |
| 201 | |
| 202 | bool GrTexture::IsScratchTexture(const GrResourceKey& key) { |
| 203 | return 0 != (key.getValue32(3) & kScratch_TextureBit); |
| 204 | } |
| 205 | |
| 206 | bool GrTexture::NeedsFiltering(const GrResourceKey& key) { |
| 207 | return 0 != (key.getValue32(3) & kFilter_TextureBit); |
| 208 | } |