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