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) |
| 18 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | /** |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 20 | * This method allows us to interrupt the normal deletion process and place |
| 21 | * textures back in the texture cache when their ref count goes to zero. |
| 22 | */ |
| 23 | void GrTexture::internal_dispose() const { |
| 24 | |
| 25 | if (this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit) && |
| 26 | NULL != this->INHERITED::getContext()) { |
| 27 | GrTexture* nonConstThis = const_cast<GrTexture *>(this); |
| 28 | this->fRefCnt = 1; // restore ref count to initial setting |
| 29 | |
| 30 | nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit); |
| 31 | nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis); |
| 32 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | // Note: "this" texture might be freed inside addExistingTextureToCache |
robertphillips@google.com | f41f4d2 | 2012-06-25 17:26:29 +0000 | [diff] [blame] | 34 | // if it is purged. |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 35 | return; |
| 36 | } |
| 37 | |
| 38 | this->INHERITED::internal_dispose(); |
| 39 | } |
| 40 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 41 | bool GrTexture::readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 42 | GrPixelConfig config, void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 43 | size_t rowBytes, uint32_t pixelOpsFlags) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 44 | // go through context so that all necessary flushing occurs |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 45 | GrContext* context = this->getContext(); |
| 46 | if (NULL == context) { |
| 47 | return false; |
| 48 | } |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 49 | return context->readTexturePixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 50 | left, top, width, height, |
| 51 | config, buffer, rowBytes, |
| 52 | pixelOpsFlags); |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void GrTexture::writePixels(int left, int top, int width, int height, |
| 56 | GrPixelConfig config, const void* buffer, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 57 | size_t rowBytes, uint32_t pixelOpsFlags) { |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame] | 58 | // go through context so that all necessary flushing occurs |
| 59 | GrContext* context = this->getContext(); |
| 60 | if (NULL == context) { |
| 61 | return; |
| 62 | } |
| 63 | context->writeTexturePixels(this, |
bsalomon@google.com | 0342a85 | 2012-08-20 19:22:38 +0000 | [diff] [blame] | 64 | left, top, width, height, |
| 65 | config, buffer, rowBytes, |
| 66 | pixelOpsFlags); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 67 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 68 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 69 | void GrTexture::releaseRenderTarget() { |
| 70 | if (NULL != fRenderTarget) { |
| 71 | GrAssert(fRenderTarget->asTexture() == this); |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 72 | GrAssert(fDesc.fFlags & kRenderTarget_GrTextureFlagBit); |
| 73 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 74 | fRenderTarget->onTextureReleaseRenderTarget(); |
| 75 | fRenderTarget->unref(); |
| 76 | fRenderTarget = NULL; |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 77 | |
| 78 | fDesc.fFlags = fDesc.fFlags & |
| 79 | ~(kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit); |
| 80 | fDesc.fSampleCnt = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 84 | void GrTexture::onRelease() { |
| 85 | GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit)); |
| 86 | this->releaseRenderTarget(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 87 | |
| 88 | INHERITED::onRelease(); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 91 | void GrTexture::onAbandon() { |
| 92 | if (NULL != fRenderTarget) { |
| 93 | fRenderTarget->abandon(); |
| 94 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 95 | |
| 96 | INHERITED::onAbandon(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 97 | } |
| 98 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 99 | void GrTexture::validateDesc() const { |
| 100 | if (NULL != this->asRenderTarget()) { |
| 101 | // This texture has a render target |
| 102 | GrAssert(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 103 | |
| 104 | if (NULL != this->asRenderTarget()->getStencilBuffer()) { |
| 105 | GrAssert(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 106 | } else { |
| 107 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 108 | } |
| 109 | |
| 110 | GrAssert(fDesc.fSampleCnt == this->asRenderTarget()->numSamples()); |
| 111 | } else { |
| 112 | GrAssert(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 113 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 114 | GrAssert(0 == fDesc.fSampleCnt); |
| 115 | } |
| 116 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 117 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 118 | // 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] | 119 | // key |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 120 | enum TextureFlags { |
| 121 | /** |
| 122 | * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the |
| 123 | * hardware doesn't support that feature. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 124 | */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 125 | kStretchToPOT_TextureFlag = 0x1, |
| 126 | /** |
| 127 | * The kFilter bit can only be set when the kStretchToPOT flag is set and indicates whether the |
skia.committer@gmail.com | 2859eb7 | 2012-12-21 02:01:28 +0000 | [diff] [blame] | 128 | * stretched texture should be bilerp filtered or point sampled. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 129 | */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 130 | kFilter_TextureFlag = 0x2, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | namespace { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 134 | GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu, |
| 135 | const GrTextureParams* params, |
| 136 | const GrTextureDesc& desc) { |
| 137 | GrResourceKey::ResourceFlags flags = 0; |
| 138 | bool tiled = NULL != params && params->isTiled(); |
robertphillips@google.com | be61c05 | 2013-01-04 16:33:44 +0000 | [diff] [blame^] | 139 | if (tiled && !gpu->getCaps().npotTextureTileSupport()) { |
| 140 | if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 141 | flags |= kStretchToPOT_TextureFlag; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 142 | if (params->isBilerp()) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 143 | flags |= kFilter_TextureFlag; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 147 | return flags; |
| 148 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 149 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 150 | GrResourceKey::ResourceType texture_resource_type() { |
| 151 | static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType(); |
| 152 | return gType; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 157 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 158 | const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 159 | const GrCacheID& cacheID) { |
| 160 | GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); |
| 161 | return GrResourceKey(cacheID, texture_resource_type(), flags); |
| 162 | } |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 163 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 164 | GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) { |
| 165 | GrCacheID::Key idKey; |
| 166 | // Instead of a client-provided key of the texture contents we create a key from the |
| 167 | // descriptor. |
| 168 | GR_STATIC_ASSERT(sizeof(idKey) >= 12); |
| 169 | GrAssert(desc.fHeight < (1 << 16)); |
| 170 | GrAssert(desc.fWidth < (1 << 16)); |
| 171 | idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
| 172 | idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
| 173 | idKey.fData32[2] = desc.fFlags; |
| 174 | static const int kPadSize = sizeof(idKey) - 12; |
| 175 | memset(idKey.fData8 + 12, 0, kPadSize); |
| 176 | |
| 177 | GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); |
| 178 | return GrResourceKey(cacheID, texture_resource_type(), 0); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | bool GrTexture::NeedsResizing(const GrResourceKey& key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 182 | return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bool GrTexture::NeedsFiltering(const GrResourceKey& key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 186 | return SkToBool(key.getResourceFlags() & kFilter_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 187 | } |