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 | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrDrawTargetCaps.h" |
bsalomon@google.com | 05ef510 | 2011-05-02 21:14:59 +0000 | [diff] [blame] | 14 | #include "GrGpu.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 16 | #include "GrResourceCache.h" |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 17 | |
robertphillips@google.com | 4d73ac2 | 2012-06-13 18:54:08 +0000 | [diff] [blame] | 18 | SK_DEFINE_INST_COUNT(GrTexture) |
| 19 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | /** |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 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 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 34 | // Note: "this" texture might be freed inside addExistingTextureToCache |
robertphillips@google.com | f41f4d2 | 2012-06-25 17:26:29 +0000 | [diff] [blame] | 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(); |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 88 | |
| 89 | INHERITED::onRelease(); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 92 | void GrTexture::onAbandon() { |
| 93 | if (NULL != fRenderTarget) { |
| 94 | fRenderTarget->abandon(); |
| 95 | } |
robertphillips@google.com | d364554 | 2012-09-05 18:37:39 +0000 | [diff] [blame] | 96 | |
| 97 | INHERITED::onAbandon(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 98 | } |
| 99 | |
robertphillips@google.com | 3271628 | 2012-06-04 12:48:45 +0000 | [diff] [blame] | 100 | void GrTexture::validateDesc() const { |
| 101 | if (NULL != this->asRenderTarget()) { |
| 102 | // This texture has a render target |
| 103 | GrAssert(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 104 | |
| 105 | if (NULL != this->asRenderTarget()->getStencilBuffer()) { |
| 106 | GrAssert(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 107 | } else { |
| 108 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 109 | } |
| 110 | |
| 111 | GrAssert(fDesc.fSampleCnt == this->asRenderTarget()->numSamples()); |
| 112 | } else { |
| 113 | GrAssert(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit)); |
| 114 | GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit)); |
| 115 | GrAssert(0 == fDesc.fSampleCnt); |
| 116 | } |
| 117 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 118 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 119 | // 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] | 120 | // key |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 121 | enum TextureFlags { |
| 122 | /** |
| 123 | * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the |
| 124 | * hardware doesn't support that feature. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 125 | */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 126 | kStretchToPOT_TextureFlag = 0x1, |
| 127 | /** |
| 128 | * 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] | 129 | * stretched texture should be bilerp filtered or point sampled. |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 130 | */ |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 131 | kFilter_TextureFlag = 0x2, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | namespace { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 135 | GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu, |
| 136 | const GrTextureParams* params, |
| 137 | const GrTextureDesc& desc) { |
| 138 | GrResourceKey::ResourceFlags flags = 0; |
| 139 | bool tiled = NULL != params && params->isTiled(); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 140 | if (tiled && !gpu->caps()->npotTextureTileSupport()) { |
robertphillips@google.com | be61c05 | 2013-01-04 16:33:44 +0000 | [diff] [blame] | 141 | if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 142 | flags |= kStretchToPOT_TextureFlag; |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 143 | if (params->isBilerp()) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 144 | flags |= kFilter_TextureFlag; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 148 | return flags; |
| 149 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 150 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 151 | GrResourceKey::ResourceType texture_resource_type() { |
| 152 | static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType(); |
| 153 | return gType; |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 154 | } |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 155 | |
| 156 | // FIXME: This should be refactored with the code in gl/GrGpuGL.cpp. |
| 157 | GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) { |
| 158 | // By default, GrRenderTargets are GL's normal orientation so that they |
| 159 | // can be drawn to by the outside world without the client having |
| 160 | // to render upside down. |
| 161 | bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit); |
| 162 | if (kDefault_GrSurfaceOrigin == desc.fOrigin) { |
| 163 | return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin; |
| 164 | } else { |
| 165 | return desc.fOrigin; |
| 166 | } |
| 167 | } |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu, |
bsalomon@google.com | b867099 | 2012-07-25 21:27:09 +0000 | [diff] [blame] | 171 | const GrTextureParams* params, |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 172 | const GrTextureDesc& desc, |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 173 | const GrCacheID& cacheID) { |
| 174 | GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc); |
| 175 | return GrResourceKey(cacheID, texture_resource_type(), flags); |
| 176 | } |
robertphillips@google.com | 46a8600 | 2012-08-08 10:42:44 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 178 | GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) { |
| 179 | GrCacheID::Key idKey; |
| 180 | // Instead of a client-provided key of the texture contents we create a key from the |
| 181 | // descriptor. |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 182 | GR_STATIC_ASSERT(sizeof(idKey) >= 16); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 183 | GrAssert(desc.fHeight < (1 << 16)); |
| 184 | GrAssert(desc.fWidth < (1 << 16)); |
| 185 | idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
| 186 | idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
| 187 | idKey.fData32[2] = desc.fFlags; |
senorblanco@chromium.org | 709906b | 2013-02-07 00:35:25 +0000 | [diff] [blame] | 188 | idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually |
senorblanco@chromium.org | 3cb406b | 2013-02-05 19:50:46 +0000 | [diff] [blame] | 189 | static const int kPadSize = sizeof(idKey) - 16; |
| 190 | GR_STATIC_ASSERT(kPadSize >= 0); |
| 191 | memset(idKey.fData8 + 16, 0, kPadSize); |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 192 | |
| 193 | GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey); |
| 194 | return GrResourceKey(cacheID, texture_resource_type(), 0); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | bool GrTexture::NeedsResizing(const GrResourceKey& key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 198 | return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool GrTexture::NeedsFiltering(const GrResourceKey& key) { |
bsalomon@google.com | 0797c2c | 2012-12-20 15:13:01 +0000 | [diff] [blame] | 202 | return SkToBool(key.getResourceFlags() & kFilter_TextureFlag); |
robertphillips@google.com | a1e5795 | 2012-06-04 20:05:28 +0000 | [diff] [blame] | 203 | } |