blob: fb5a8d350e1551458f91768ed66a311aa23e8140 [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
bsalomon@google.com686bcb82013-04-09 15:04:12 +000018GrTexture::~GrTexture() {
19 if (NULL != fRenderTarget.get()) {
20 fRenderTarget.get()->owningTextureDestroyed();
21 }
22}
23
rmistry@google.comd6176b02012-08-23 18:14:13 +000024/**
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000025 * 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 {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000029 if (this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit) &&
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000030 NULL != this->INHERITED::getContext()) {
31 GrTexture* nonConstThis = const_cast<GrTexture *>(this);
32 this->fRefCnt = 1; // restore ref count to initial setting
33
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000034 nonConstThis->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000035 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
36
rmistry@google.comd6176b02012-08-23 18:14:13 +000037 // Note: "this" texture might be freed inside addExistingTextureToCache
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000038 // if it is purged.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000039 return;
40 }
41
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000042 SkASSERT(0 == this->getDeferredRefCount());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000043 this->INHERITED::internal_dispose();
44}
45
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000046void GrTextureImpl::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000047 if (mipMapsDirty) {
48 if (kValid_MipMapsStatus == fMipMapsStatus) {
49 fMipMapsStatus = kAllocated_MipMapsStatus;
50 }
51 } else {
52 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
53 fMipMapsStatus = kValid_MipMapsStatus;
54 if (sizeChanged) {
55 // This must not be called until after changing fMipMapsStatus.
56 this->didChangeGpuMemorySize();
57 }
58 }
59}
60
61size_t GrTexture::gpuMemorySize() const {
62 size_t textureSize = (size_t) fDesc.fWidth *
63 fDesc.fHeight *
64 GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000065
66 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070067 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000068 }
69
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000070 if (this->impl()->hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000071 // We don't have to worry about the mipmaps being a different size than
72 // we'd expect because we never change fDesc.fWidth/fHeight.
73 textureSize *= 2;
74 }
75 return textureSize;
76}
77
bsalomon@google.com669fdc42011-04-05 17:08:27 +000078bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000079 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000080 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000081 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000082 GrContext* context = this->getContext();
83 if (NULL == context) {
84 return false;
85 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000086 return context->readTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000087 left, top, width, height,
88 config, buffer, rowBytes,
89 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000090}
91
92void GrTexture::writePixels(int left, int top, int width, int height,
93 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000094 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000095 // go through context so that all necessary flushing occurs
96 GrContext* context = this->getContext();
97 if (NULL == context) {
98 return;
99 }
100 context->writeTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000101 left, top, width, height,
102 config, buffer, rowBytes,
103 pixelOpsFlags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000104}
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000105
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000106void GrTexture::onRelease() {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000107 SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
robertphillips@google.comd3645542012-09-05 18:37:39 +0000108 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000109}
110
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000111void GrTexture::onAbandon() {
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000112 if (NULL != fRenderTarget.get()) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000113 fRenderTarget->abandon();
114 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000115 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000116}
117
robertphillips@google.com32716282012-06-04 12:48:45 +0000118void GrTexture::validateDesc() const {
119 if (NULL != this->asRenderTarget()) {
120 // This texture has a render target
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000121 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000122
123 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000124 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000125 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000126 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000127 }
128
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000129 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +0000130 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000131 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
132 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
133 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +0000134 }
135}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000136
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000137//////////////////////////////////////////////////////////////////////////////
138
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000139// 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 +0000140// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000141enum TextureFlags {
142 /**
143 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
144 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000145 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000146 kStretchToPOT_TextureFlag = 0x1,
147 /**
humper@google.comb86add12013-07-25 18:49:07 +0000148 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
149 * stretched texture should be bilerped.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000150 */
humper@google.comb86add12013-07-25 18:49:07 +0000151 kBilerp_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000152};
153
154namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
156 const GrTextureParams* params,
157 const GrTextureDesc& desc) {
158 GrResourceKey::ResourceFlags flags = 0;
159 bool tiled = NULL != params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000160 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
tfarinaf9dae782014-06-06 06:35:28 -0700161 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000162 flags |= kStretchToPOT_TextureFlag;
humper@google.comb86add12013-07-25 18:49:07 +0000163 switch(params->filterMode()) {
164 case GrTextureParams::kNone_FilterMode:
165 break;
166 case GrTextureParams::kBilerp_FilterMode:
167 case GrTextureParams::kMipMap_FilterMode:
168 flags |= kBilerp_TextureFlag;
169 break;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000170 }
171 }
172 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000173 return flags;
174}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000175
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000176GrResourceKey::ResourceType texture_resource_type() {
177 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
178 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000179}
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000180
181// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
182GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
183 // By default, GrRenderTargets are GL's normal orientation so that they
184 // can be drawn to by the outside world without the client having
185 // to render upside down.
186 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
187 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
188 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
189 } else {
190 return desc.fOrigin;
191 }
192}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000193}
194
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000195//////////////////////////////////////////////////////////////////////////////
196
197GrResourceKey GrTextureImpl::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000198 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000199 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000200 const GrCacheID& cacheID) {
201 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
202 return GrResourceKey(cacheID, texture_resource_type(), flags);
203}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000204
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000205GrResourceKey GrTextureImpl::ComputeScratchKey(const GrTextureDesc& desc) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000206 GrCacheID::Key idKey;
207 // Instead of a client-provided key of the texture contents we create a key from the
208 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000209 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000210 SkASSERT(desc.fHeight < (1 << 16));
211 SkASSERT(desc.fWidth < (1 << 16));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000212 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
213 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
214 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000215 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000216 static const int kPadSize = sizeof(idKey) - 16;
217 GR_STATIC_ASSERT(kPadSize >= 0);
218 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000219
220 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
221 return GrResourceKey(cacheID, texture_resource_type(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000222}
223
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000224bool GrTextureImpl::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000225 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000226}
227
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000228bool GrTextureImpl::NeedsBilerp(const GrResourceKey& key) {
humper@google.comb86add12013-07-25 18:49:07 +0000229 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000230}