blob: 8c12bd2d5e87dc3f5416a700fc3e37bde4e96f16 [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)
18
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000019/**
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 */
23void 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
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000033 // Note: "this" texture might be freed inside addExistingTextureToCache
34 // if it is purged.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000035 return;
36 }
37
38 this->INHERITED::internal_dispose();
39}
40
bsalomon@google.com669fdc42011-04-05 17:08:27 +000041bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000042 GrPixelConfig config, void* buffer,
43 size_t rowBytes) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000044 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000045 GrContext* context = this->getContext();
46 if (NULL == context) {
47 return false;
48 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000049 return context->readTexturePixels(this,
bsalomon@google.com6f379512011-11-16 20:36:03 +000050 left, top,
51 width, height,
52 config, buffer, rowBytes);
53}
54
55void GrTexture::writePixels(int left, int top, int width, int height,
56 GrPixelConfig config, const void* buffer,
57 size_t rowBytes) {
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,
64 left, top,
65 width, height,
66 config, buffer, rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000067}
bsalomon@google.comcee661a2011-07-26 12:32:36 +000068
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000069void GrTexture::releaseRenderTarget() {
70 if (NULL != fRenderTarget) {
71 GrAssert(fRenderTarget->asTexture() == this);
robertphillips@google.com32716282012-06-04 12:48:45 +000072 GrAssert(fDesc.fFlags & kRenderTarget_GrTextureFlagBit);
73
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000074 fRenderTarget->onTextureReleaseRenderTarget();
75 fRenderTarget->unref();
76 fRenderTarget = NULL;
robertphillips@google.com32716282012-06-04 12:48:45 +000077
78 fDesc.fFlags = fDesc.fFlags &
79 ~(kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit);
80 fDesc.fSampleCnt = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000081 }
82}
83
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000084void GrTexture::onRelease() {
85 GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
86 this->releaseRenderTarget();
87}
88
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000089void GrTexture::onAbandon() {
90 if (NULL != fRenderTarget) {
91 fRenderTarget->abandon();
92 }
93}
94
robertphillips@google.com32716282012-06-04 12:48:45 +000095void GrTexture::validateDesc() const {
96 if (NULL != this->asRenderTarget()) {
97 // This texture has a render target
98 GrAssert(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
99
100 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
101 GrAssert(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
102 } else {
103 GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
104 }
105
106 GrAssert(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
107 } else {
108 GrAssert(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
109 GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
110 GrAssert(0 == fDesc.fSampleCnt);
111 }
112}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000113
114enum TextureBits {
115 kFirst_TextureBit = (GrResourceKey::kLastPublic_TypeBit << 1),
116
117 /*
118 * The kNPOT bit is set when the texture is NPOT and is being repeated
119 * but the hardware doesn't support that feature.
120 */
121 kNPOT_TextureBit = kFirst_TextureBit,
122 /*
123 * The kFilter bit can only be set when the kNPOT flag is set and indicates
124 * whether the resizing of the texture should use filtering. This is
125 * to handle cases where the original texture is indexed to disable
126 * filtering.
127 */
128 kFilter_TextureBit = kNPOT_TextureBit << 1,
129 /*
130 * The kScratch bit is set if the texture is being used as a scratch
131 * texture.
132 */
133 kScratch_TextureBit = kFilter_TextureBit << 1,
134};
135
136namespace {
137void gen_texture_key_values(const GrGpu* gpu,
138 const GrSamplerState* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000139 const GrTextureDesc& desc,
140 bool scratch,
141 uint32_t v[4]) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000142
143 uint64_t clientKey = desc.fClientCacheID;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000144
145 if (scratch) {
146 // Instead of a client-provided key of the texture contents
147 // we create a key of from the descriptor.
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000148 GrAssert(kScratch_CacheID == clientKey);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000149 clientKey = (desc.fFlags << 8) | ((uint64_t) desc.fConfig << 32);
150 }
151
152 // we assume we only need 16 bits of width and height
153 // assert that texture creation will fail anyway if this assumption
154 // would cause key collisions.
155 GrAssert(gpu->getCaps().fMaxTextureSize <= SK_MaxU16);
caryclark@google.comcf6285b2012-06-06 12:09:01 +0000156 v[0] = (uint32_t) (clientKey & 0xffffffffUL);
157 v[1] = (uint32_t) ((clientKey >> 32) & 0xffffffffUL);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000158 v[2] = desc.fWidth | (desc.fHeight << 16);
159
160 v[3] = (desc.fSampleCnt << 24);
161 GrAssert(desc.fSampleCnt >= 0 && desc.fSampleCnt < 256);
162
163 if (!gpu->getCaps().fNPOTTextureTileSupport) {
164 bool isPow2 = GrIsPow2(desc.fWidth) && GrIsPow2(desc.fHeight);
165
166 bool tiled = NULL != sampler &&
167 ((sampler->getWrapX() != GrSamplerState::kClamp_WrapMode) ||
168 (sampler->getWrapY() != GrSamplerState::kClamp_WrapMode));
169
170 if (tiled && !isPow2) {
171 v[3] |= kNPOT_TextureBit;
172 if (GrSamplerState::kNearest_Filter != sampler->getFilter()) {
173 v[3] |= kFilter_TextureBit;
174 }
175 }
176 }
177
178 if (scratch) {
179 v[3] |= kScratch_TextureBit;
180 }
181
182 v[3] |= GrResourceKey::kTexture_TypeBit;
183}
184}
185
186GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
187 const GrSamplerState* sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000188 const GrTextureDesc& desc,
189 bool scratch) {
190 uint32_t v[4];
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000191 gen_texture_key_values(gpu, sampler, desc, scratch, v);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000192 return GrResourceKey(v);
193}
194
195bool GrTexture::NeedsResizing(const GrResourceKey& key) {
196 return 0 != (key.getValue32(3) & kNPOT_TextureBit);
197}
198
199bool GrTexture::IsScratchTexture(const GrResourceKey& key) {
200 return 0 != (key.getValue32(3) & kScratch_TextureBit);
201}
202
203bool GrTexture::NeedsFiltering(const GrResourceKey& key) {
204 return 0 != (key.getValue32(3) & kFilter_TextureBit);
205}