blob: 87df1470e90f007e2e4b44997bd6cbf4c025c14a [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.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000014#include "GrGpu.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015#include "GrRenderTarget.h"
robertphillips@google.coma1e57952012-06-04 20:05:28 +000016#include "GrResourceCache.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000017
robertphillips@google.com4d73ac22012-06-13 18:54:08 +000018SK_DEFINE_INST_COUNT(GrTexture)
19
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
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000070void GrTexture::onRelease() {
71 GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
robertphillips@google.comd3645542012-09-05 18:37:39 +000072 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000073}
74
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000075void GrTexture::onAbandon() {
bsalomon@google.comd02e8802013-04-03 19:49:13 +000076 if (NULL != fRenderTarget.get()) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000077 fRenderTarget->abandon();
78 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000079 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000080}
81
robertphillips@google.com32716282012-06-04 12:48:45 +000082void GrTexture::validateDesc() const {
83 if (NULL != this->asRenderTarget()) {
84 // This texture has a render target
85 GrAssert(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
86
87 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
88 GrAssert(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
89 } else {
90 GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
91 }
92
93 GrAssert(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
94 } else {
95 GrAssert(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
96 GrAssert(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
97 GrAssert(0 == fDesc.fSampleCnt);
98 }
99}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000100
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000101// 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 +0000102// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000103enum TextureFlags {
104 /**
105 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
106 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000107 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000108 kStretchToPOT_TextureFlag = 0x1,
109 /**
110 * 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 +0000111 * stretched texture should be bilerp filtered or point sampled.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000112 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000113 kFilter_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000114};
115
116namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000117GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
118 const GrTextureParams* params,
119 const GrTextureDesc& desc) {
120 GrResourceKey::ResourceFlags flags = 0;
121 bool tiled = NULL != params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000122 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
robertphillips@google.combe61c052013-01-04 16:33:44 +0000123 if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000124 flags |= kStretchToPOT_TextureFlag;
bsalomon@google.comb8670992012-07-25 21:27:09 +0000125 if (params->isBilerp()) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000126 flags |= kFilter_TextureFlag;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000127 }
128 }
129 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000130 return flags;
131}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133GrResourceKey::ResourceType texture_resource_type() {
134 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
135 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000136}
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000137
138// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
139GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
140 // By default, GrRenderTargets are GL's normal orientation so that they
141 // can be drawn to by the outside world without the client having
142 // to render upside down.
143 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
144 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
145 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
146 } else {
147 return desc.fOrigin;
148 }
149}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000150}
151
152GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000153 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000154 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155 const GrCacheID& cacheID) {
156 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
157 return GrResourceKey(cacheID, texture_resource_type(), flags);
158}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000159
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000160GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) {
161 GrCacheID::Key idKey;
162 // Instead of a client-provided key of the texture contents we create a key from the
163 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000164 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000165 GrAssert(desc.fHeight < (1 << 16));
166 GrAssert(desc.fWidth < (1 << 16));
167 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
168 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
169 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000170 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000171 static const int kPadSize = sizeof(idKey) - 16;
172 GR_STATIC_ASSERT(kPadSize >= 0);
173 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000174
175 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
176 return GrResourceKey(cacheID, texture_resource_type(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000177}
178
179bool GrTexture::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000180 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000181}
182
183bool GrTexture::NeedsFiltering(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000184 return SkToBool(key.getResourceFlags() & kFilter_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000185}