blob: 667ddd1e513670113a579dbc7f16bee47da5bced [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
robertphillipsb06e5a22014-09-30 06:58:20 -070010#include "GrTexture.h"
11
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
bsalomon@google.com686bcb82013-04-09 15:04:12 +000018GrTexture::~GrTexture() {
bsalomon49f085d2014-09-05 13:34:00 -070019 if (fRenderTarget.get()) {
bsalomon@google.com686bcb82013-04-09 15:04:12 +000020 fRenderTarget.get()->owningTextureDestroyed();
21 }
22}
23
robertphillipsdbe60742014-09-30 06:54:17 -070024/**
25 * This method allows us to interrupt the normal deletion process and place
26 * textures back in the texture cache when their ref count goes to zero.
27 */
28void GrTexture::internal_dispose() const {
robertphillipsb06e5a22014-09-30 06:58:20 -070029 if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
robertphillipsdbe60742014-09-30 06:54:17 -070030 this->INHERITED::getContext()) {
31 GrTexture* nonConstThis = const_cast<GrTexture *>(this);
32 this->ref(); // restore ref count to initial setting
33
robertphillipsb06e5a22014-09-30 06:58:20 -070034 nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
robertphillipsdbe60742014-09-30 06:54:17 -070035 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
36
37 // Note: "this" texture might be freed inside addExistingTextureToCache
38 // if it is purged.
39 return;
40 }
41
42 this->INHERITED::internal_dispose();
43}
44
robertphillipsb06e5a22014-09-30 06:58:20 -070045void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000046 if (mipMapsDirty) {
47 if (kValid_MipMapsStatus == fMipMapsStatus) {
robertphillipsb06e5a22014-09-30 06:58:20 -070048 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000049 }
50 } else {
51 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
52 fMipMapsStatus = kValid_MipMapsStatus;
53 if (sizeChanged) {
54 // This must not be called until after changing fMipMapsStatus.
55 this->didChangeGpuMemorySize();
56 }
57 }
58}
59
60size_t GrTexture::gpuMemorySize() const {
bsalomond4cb9222014-08-11 14:19:09 -070061 size_t textureSize;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000062
63 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070064 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
bsalomond4cb9222014-08-11 14:19:09 -070065 } else {
66 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000067 }
68
robertphillipsb06e5a22014-09-30 06:58:20 -070069 if (this->impl()->hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000070 // We don't have to worry about the mipmaps being a different size than
71 // we'd expect because we never change fDesc.fWidth/fHeight.
72 textureSize *= 2;
73 }
74 return textureSize;
75}
76
bsalomon@google.com669fdc42011-04-05 17:08:27 +000077bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000078 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000079 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000080 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000081 GrContext* context = this->getContext();
82 if (NULL == context) {
83 return false;
84 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000085 return context->readTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000086 left, top, width, height,
87 config, buffer, rowBytes,
88 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000089}
90
91void GrTexture::writePixels(int left, int top, int width, int height,
92 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000093 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000094 // go through context so that all necessary flushing occurs
95 GrContext* context = this->getContext();
96 if (NULL == context) {
97 return;
98 }
99 context->writeTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000100 left, top, width, height,
101 config, buffer, rowBytes,
102 pixelOpsFlags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000103}
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000104
robertphillipsdbe60742014-09-30 06:54:17 -0700105void GrTexture::abandonReleaseCommon() {
106 // In debug builds the resource cache tracks removed/exclusive textures and has an unref'ed ptr.
107 // After abandon() or release() the resource cache will be unreachable (getContext() == NULL).
108 // So we readd the texture to the cache here so that it is removed from the exclusive list and
109 // there is no longer an unref'ed ptr to the texture in the cache.
robertphillipsb06e5a22014-09-30 06:58:20 -0700110 if (this->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_FlagBit)) {
robertphillipsdbe60742014-09-30 06:54:17 -0700111 SkASSERT(!this->wasDestroyed());
112 this->ref(); // restores the ref the resource cache gave up when it marked this exclusive.
robertphillipsb06e5a22014-09-30 06:58:20 -0700113 this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
robertphillipsdbe60742014-09-30 06:54:17 -0700114 this->getContext()->addExistingTextureToCache(this);
115 }
116}
117
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000118void GrTexture::onRelease() {
robertphillipsdbe60742014-09-30 06:54:17 -0700119 this->abandonReleaseCommon();
robertphillipsb06e5a22014-09-30 06:58:20 -0700120 SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
robertphillips@google.comd3645542012-09-05 18:37:39 +0000121 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000122}
123
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000124void GrTexture::onAbandon() {
robertphillipsdbe60742014-09-30 06:54:17 -0700125 this->abandonReleaseCommon();
bsalomon49f085d2014-09-05 13:34:00 -0700126 if (fRenderTarget.get()) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000127 fRenderTarget->abandon();
128 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000129 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000130}
131
robertphillips@google.com32716282012-06-04 12:48:45 +0000132void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -0700133 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +0000134 // This texture has a render target
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000135 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000136
bsalomon49f085d2014-09-05 13:34:00 -0700137 if (this->asRenderTarget()->getStencilBuffer()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000138 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000139 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000140 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000141 }
142
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000143 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +0000144 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
146 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
147 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +0000148 }
149}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000150
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000151//////////////////////////////////////////////////////////////////////////////
152
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000153// 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 +0000154// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155enum TextureFlags {
156 /**
157 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
158 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000159 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000160 kStretchToPOT_TextureFlag = 0x1,
161 /**
humper@google.comb86add12013-07-25 18:49:07 +0000162 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
163 * stretched texture should be bilerped.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000164 */
humper@google.comb86add12013-07-25 18:49:07 +0000165 kBilerp_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000166};
167
168namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000169GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
170 const GrTextureParams* params,
171 const GrTextureDesc& desc) {
172 GrResourceKey::ResourceFlags flags = 0;
bsalomon49f085d2014-09-05 13:34:00 -0700173 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000174 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
tfarinaf9dae782014-06-06 06:35:28 -0700175 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000176 flags |= kStretchToPOT_TextureFlag;
humper@google.comb86add12013-07-25 18:49:07 +0000177 switch(params->filterMode()) {
178 case GrTextureParams::kNone_FilterMode:
179 break;
180 case GrTextureParams::kBilerp_FilterMode:
181 case GrTextureParams::kMipMap_FilterMode:
182 flags |= kBilerp_TextureFlag;
183 break;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000184 }
185 }
186 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000187 return flags;
188}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000189
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000190GrResourceKey::ResourceType texture_resource_type() {
191 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
192 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000193}
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000194
195// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
196GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
197 // By default, GrRenderTargets are GL's normal orientation so that they
198 // can be drawn to by the outside world without the client having
199 // to render upside down.
200 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
201 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
202 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
203 } else {
204 return desc.fOrigin;
205 }
206}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000207}
208
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000209//////////////////////////////////////////////////////////////////////////////
robertphillipsb06e5a22014-09-30 06:58:20 -0700210GrTextureImpl::GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
bsalomon744998e2014-08-28 09:54:34 -0700211 : INHERITED(gpu, isWrapped, desc)
212 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
robertphillipsb06e5a22014-09-30 06:58:20 -0700213 this->setScratchKey(ComputeScratchKey(desc));
bsalomon744998e2014-08-28 09:54:34 -0700214}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000215
robertphillipsb06e5a22014-09-30 06:58:20 -0700216GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000217 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000218 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000219 const GrCacheID& cacheID) {
220 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
221 return GrResourceKey(cacheID, texture_resource_type(), flags);
222}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000223
robertphillipsb06e5a22014-09-30 06:58:20 -0700224GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000225 GrCacheID::Key idKey;
226 // Instead of a client-provided key of the texture contents we create a key from the
227 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000228 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000229 SkASSERT(desc.fHeight < (1 << 16));
230 SkASSERT(desc.fWidth < (1 << 16));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000231 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
232 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
233 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000234 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000235 static const int kPadSize = sizeof(idKey) - 16;
236 GR_STATIC_ASSERT(kPadSize >= 0);
237 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000238
239 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
240 return GrResourceKey(cacheID, texture_resource_type(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000241}
242
robertphillipsb06e5a22014-09-30 06:58:20 -0700243bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000244 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000245}
246
robertphillipsb06e5a22014-09-30 06:58:20 -0700247bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
humper@google.comb86add12013-07-25 18:49:07 +0000248 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000249}