blob: c31d774544480b7eef97d0cea90fd234a93567e3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com669fdc42011-04-05 17:08:27 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.com669fdc42011-04-05 17:08:27 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com669fdc42011-04-05 17:08:27 +000010#include "GrTexture.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000011
bsalomon@google.com669fdc42011-04-05 17:08:27 +000012#include "GrContext.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000013#include "GrGpu.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014#include "GrRenderTarget.h"
robertphillips@google.coma1e57952012-06-04 20:05:28 +000015#include "GrResourceCache.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000016
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000017SK_DEFINE_INST_COUNT(GrTexture)
bsalomon@google.com0b6ad222012-12-20 14:23:26 +000018GR_DEFINE_RESOURCE_CACHE_TYPE(GrTexture)
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000019
rmistry@google.comd6176b02012-08-23 18:14:13 +000020/**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000021 * 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 */
24void 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.comd6176b02012-08-23 18:14:13 +000034 // Note: "this" texture might be freed inside addExistingTextureToCache
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000035 // if it is purged.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000036 return;
37 }
38
39 this->INHERITED::internal_dispose();
40}
41
bsalomon@google.com669fdc42011-04-05 17:08:27 +000042bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000043 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000044 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000045 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000046 GrContext* context = this->getContext();
47 if (NULL == context) {
48 return false;
49 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000050 return context->readTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000051 left, top, width, height,
52 config, buffer, rowBytes,
53 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000054}
55
56void GrTexture::writePixels(int left, int top, int width, int height,
57 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000058 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000059 // 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.com0342a852012-08-20 19:22:38 +000065 left, top, width, height,
66 config, buffer, rowBytes,
67 pixelOpsFlags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000068}
bsalomon@google.comcee661a2011-07-26 12:32:36 +000069
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000070void GrTexture::releaseRenderTarget() {
71 if (NULL != fRenderTarget) {
72 GrAssert(fRenderTarget->asTexture() == this);
robertphillips@google.com32716282012-06-04 12:48:45 +000073 GrAssert(fDesc.fFlags & kRenderTarget_GrTextureFlagBit);
74
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000075 fRenderTarget->onTextureReleaseRenderTarget();
76 fRenderTarget->unref();
77 fRenderTarget = NULL;
robertphillips@google.com32716282012-06-04 12:48:45 +000078
79 fDesc.fFlags = fDesc.fFlags &
80 ~(kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit);
81 fDesc.fSampleCnt = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082 }
83}
84
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000085void GrTexture::onRelease() {
86 GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
87 this->releaseRenderTarget();
robertphillips@google.comd3645542012-09-05 18:37:39 +000088
89 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000090}
91
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000092void GrTexture::onAbandon() {
93 if (NULL != fRenderTarget) {
94 fRenderTarget->abandon();
95 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000096
97 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000098}
99
robertphillips@google.com32716282012-06-04 12:48:45 +0000100void 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.coma1e57952012-06-04 20:05:28 +0000118
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000119// These flags need to fit in <= 8 bits so they can be folded into the texture
robertphillips@google.com46a86002012-08-08 10:42:44 +0000120// key
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000121enum TextureBits {
122 /*
123 * The kNPOT bit is set when the texture is NPOT and is being repeated
124 * but the hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000125 */
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000126 kNPOT_TextureBit = 0x1,
127 /*
128 * The kFilter bit can only be set when the kNPOT flag is set and indicates
129 * whether the resizing of the texture should use filtering. This is
130 * to handle cases where the original texture is indexed to disable
131 * filtering.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132 */
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000133 kFilter_TextureBit = 0x2,
134 /*
135 * The kScratch bit is set if the texture is being used as a scratch
136 * texture.
137 */
138 kScratch_TextureBit = 0x4,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000139};
140
141namespace {
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000142void gen_texture_key_values(const GrGpu* gpu,
143 const GrTextureParams* params,
144 const GrTextureDesc& desc,
145 const GrCacheData& cacheData,
146 bool scratch,
147 GrCacheID* cacheID) {
148
149 uint64_t clientKey = cacheData.fClientCacheID;
150
151 if (scratch) {
152 // Instead of a client-provided key of the texture contents
153 // we create a key from the descriptor.
154 GrAssert(GrCacheData::kScratch_CacheID == clientKey);
155 clientKey = (desc.fFlags << 8) | ((uint64_t) desc.fConfig << 32);
156 }
157
158 cacheID->fPublicID = clientKey;
159 cacheID->fDomain = cacheData.fResourceDomain;
160
161 // we assume we only need 16 bits of width and height
162 // assert that texture creation will fail anyway if this assumption
163 // would cause key collisions.
164 GrAssert(gpu->getCaps().maxTextureSize() <= SK_MaxU16);
165 cacheID->fResourceSpecific32 = desc.fWidth | (desc.fHeight << 16);
166
167 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt < 256);
168 cacheID->fResourceSpecific16 = desc.fSampleCnt << 8;
169
170 if (!gpu->getCaps().npotTextureTileSupport()) {
171 bool isPow2 = GrIsPow2(desc.fWidth) && GrIsPow2(desc.fHeight);
172
173 bool tiled = NULL != params && params->isTiled();
174
175 if (tiled && !isPow2) {
176 cacheID->fResourceSpecific16 |= kNPOT_TextureBit;
bsalomon@google.comb8670992012-07-25 21:27:09 +0000177 if (params->isBilerp()) {
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000178 cacheID->fResourceSpecific16 |= kFilter_TextureBit;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000179 }
180 }
181 }
182
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000183 if (scratch) {
184 cacheID->fResourceSpecific16 |= kScratch_TextureBit;
185 }
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000186}
187}
188
189GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000190 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000191 const GrTextureDesc& desc,
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000192 const GrCacheData& cacheData,
193 bool scratch) {
194 GrCacheID id(GrTexture::GetResourceType());
195 gen_texture_key_values(gpu, params, desc, cacheData, scratch, &id);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000196
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000197 uint32_t v[4];
198 id.toRaw(v);
199 return GrResourceKey(v);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000200}
201
202bool GrTexture::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000203 return 0 != (key.getValue32(3) & kNPOT_TextureBit);
204}
205
206bool GrTexture::IsScratchTexture(const GrResourceKey& key) {
207 return 0 != (key.getValue32(3) & kScratch_TextureBit);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000208}
209
210bool GrTexture::NeedsFiltering(const GrResourceKey& key) {
bsalomon@google.com0b6ad222012-12-20 14:23:26 +0000211 return 0 != (key.getValue32(3) & kFilter_TextureBit);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000212}