blob: 614d7711c2399c438f6021acde1907f0245904ec [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
rmistry@google.comd6176b02012-08-23 18:14:13 +000019/**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000020 * 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
rmistry@google.comd6176b02012-08-23 18:14:13 +000033 // Note: "this" texture might be freed inside addExistingTextureToCache
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000034 // 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,
bsalomon@google.com0342a852012-08-20 19:22:38 +000043 size_t rowBytes, uint32_t pixelOpsFlags) {
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.com0342a852012-08-20 19:22:38 +000050 left, top, width, height,
51 config, buffer, rowBytes,
52 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000053}
54
55void GrTexture::writePixels(int left, int top, int width, int height,
56 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000057 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000058 // 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.com0342a852012-08-20 19:22:38 +000064 left, top, width, height,
65 config, buffer, rowBytes,
66 pixelOpsFlags);
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();
robertphillips@google.comd3645542012-09-05 18:37:39 +000087
88 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000089}
90
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000091void GrTexture::onAbandon() {
92 if (NULL != fRenderTarget) {
93 fRenderTarget->abandon();
94 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000095
96 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000097}
98
robertphillips@google.com32716282012-06-04 12:48:45 +000099void 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.coma1e57952012-06-04 20:05:28 +0000117
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000118// These flags need to fit in a GrResourceKey::ResourceFlags so they can be folded into the texture
robertphillips@google.com46a86002012-08-08 10:42:44 +0000119// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000120enum 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.coma1e57952012-06-04 20:05:28 +0000124 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000125 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.com2859eb72012-12-21 02:01:28 +0000128 * stretched texture should be bilerp filtered or point sampled.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000129 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000130 kFilter_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000131};
132
133namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000134GrResourceKey::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.combe61c052013-01-04 16:33:44 +0000139 if (tiled && !gpu->getCaps().npotTextureTileSupport()) {
140 if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000141 flags |= kStretchToPOT_TextureFlag;
bsalomon@google.comb8670992012-07-25 21:27:09 +0000142 if (params->isBilerp()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000143 flags |= kFilter_TextureFlag;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000144 }
145 }
146 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000147 return flags;
148}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000149
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000150GrResourceKey::ResourceType texture_resource_type() {
151 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
152 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000153}
154}
155
156GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000157 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000158 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000159 const GrCacheID& cacheID) {
160 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
161 return GrResourceKey(cacheID, texture_resource_type(), flags);
162}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000163
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000164GrResourceKey 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.coma1e57952012-06-04 20:05:28 +0000179}
180
181bool GrTexture::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000182 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000183}
184
185bool GrTexture::NeedsFiltering(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000186 return SkToBool(key.getResourceFlags() & kFilter_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000187}