blob: b5a0195df2149bf36c656a89a43a5a3be29161c6 [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
bsalomon@google.com686bcb82013-04-09 15:04:12 +000020GrTexture::~GrTexture() {
21 if (NULL != fRenderTarget.get()) {
22 fRenderTarget.get()->owningTextureDestroyed();
23 }
24}
25
rmistry@google.comd6176b02012-08-23 18:14:13 +000026/**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000027 * 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 */
30void 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.comd6176b02012-08-23 18:14:13 +000040 // Note: "this" texture might be freed inside addExistingTextureToCache
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000041 // if it is purged.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000042 return;
43 }
44
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000045 SkASSERT(0 == this->getDeferredRefCount());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000046 this->INHERITED::internal_dispose();
47}
48
bsalomon@google.com669fdc42011-04-05 17:08:27 +000049bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000050 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000051 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000052 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000053 GrContext* context = this->getContext();
54 if (NULL == context) {
55 return false;
56 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000057 return context->readTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000058 left, top, width, height,
59 config, buffer, rowBytes,
60 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000061}
62
63void GrTexture::writePixels(int left, int top, int width, int height,
64 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000065 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000066 // go through context so that all necessary flushing occurs
67 GrContext* context = this->getContext();
68 if (NULL == context) {
69 return;
70 }
71 context->writeTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000072 left, top, width, height,
73 config, buffer, rowBytes,
74 pixelOpsFlags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000075}
bsalomon@google.comcee661a2011-07-26 12:32:36 +000076
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000077void GrTexture::onRelease() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000078 SkASSERT(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
robertphillips@google.comd3645542012-09-05 18:37:39 +000079 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000080}
81
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000082void GrTexture::onAbandon() {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000083 if (NULL != fRenderTarget.get()) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000084 fRenderTarget->abandon();
85 }
robertphillips@google.comd3645542012-09-05 18:37:39 +000086 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000087}
88
robertphillips@google.com32716282012-06-04 12:48:45 +000089void GrTexture::validateDesc() const {
90 if (NULL != this->asRenderTarget()) {
91 // This texture has a render target
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000092 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +000093
94 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000095 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +000096 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000097 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +000098 }
99
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000100 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +0000101 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
103 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
104 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +0000105 }
106}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000107
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000108// 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 +0000109// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000110enum TextureFlags {
111 /**
112 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
113 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000114 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000115 kStretchToPOT_TextureFlag = 0x1,
116 /**
humper@google.comb86add12013-07-25 18:49:07 +0000117 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
118 * stretched texture should be bilerped.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000119 */
humper@google.comb86add12013-07-25 18:49:07 +0000120 kBilerp_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000121};
122
123namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000124GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
125 const GrTextureParams* params,
126 const GrTextureDesc& desc) {
127 GrResourceKey::ResourceFlags flags = 0;
128 bool tiled = NULL != params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000129 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
robertphillips@google.combe61c052013-01-04 16:33:44 +0000130 if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000131 flags |= kStretchToPOT_TextureFlag;
humper@google.comb86add12013-07-25 18:49:07 +0000132 switch(params->filterMode()) {
133 case GrTextureParams::kNone_FilterMode:
134 break;
135 case GrTextureParams::kBilerp_FilterMode:
136 case GrTextureParams::kMipMap_FilterMode:
137 flags |= kBilerp_TextureFlag;
138 break;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000139 }
140 }
141 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000142 return flags;
143}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000144
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000145GrResourceKey::ResourceType texture_resource_type() {
146 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
147 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000148}
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000149
150// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
151GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
152 // By default, GrRenderTargets are GL's normal orientation so that they
153 // can be drawn to by the outside world without the client having
154 // to render upside down.
155 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
156 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
157 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
158 } else {
159 return desc.fOrigin;
160 }
161}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000162}
163
164GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000165 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000166 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000167 const GrCacheID& cacheID) {
168 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
169 return GrResourceKey(cacheID, texture_resource_type(), flags);
170}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000171
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000172GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) {
173 GrCacheID::Key idKey;
174 // Instead of a client-provided key of the texture contents we create a key from the
175 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000176 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000177 SkASSERT(desc.fHeight < (1 << 16));
178 SkASSERT(desc.fWidth < (1 << 16));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000179 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
180 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
181 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000182 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000183 static const int kPadSize = sizeof(idKey) - 16;
184 GR_STATIC_ASSERT(kPadSize >= 0);
185 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000186
187 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
188 return GrResourceKey(cacheID, texture_resource_type(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000189}
190
191bool GrTexture::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000192 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000193}
194
humper@google.comb86add12013-07-25 18:49:07 +0000195bool GrTexture::NeedsBilerp(const GrResourceKey& key) {
196 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000197}