blob: 3186d89d77535cf734c2cc70319a2d0a21e5e892 [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 {
29
30 if (this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit) &&
31 NULL != this->INHERITED::getContext()) {
32 GrTexture* nonConstThis = const_cast<GrTexture *>(this);
33 this->fRefCnt = 1; // restore ref count to initial setting
34
35 nonConstThis->resetFlag((GrTextureFlags) kReturnToCache_FlagBit);
36 nonConstThis->INHERITED::getContext()->addExistingTextureToCache(nonConstThis);
37
rmistry@google.comd6176b02012-08-23 18:14:13 +000038 // Note: "this" texture might be freed inside addExistingTextureToCache
robertphillips@google.comf41f4d22012-06-25 17:26:29 +000039 // if it is purged.
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000040 return;
41 }
42
robertphillips@google.com0255a5d2013-10-24 14:03:01 +000043 SkASSERT(0 == this->getDeferredRefCount());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +000044 this->INHERITED::internal_dispose();
45}
46
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000047void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
48 if (mipMapsDirty) {
49 if (kValid_MipMapsStatus == fMipMapsStatus) {
50 fMipMapsStatus = kAllocated_MipMapsStatus;
51 }
52 } else {
53 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
54 fMipMapsStatus = kValid_MipMapsStatus;
55 if (sizeChanged) {
56 // This must not be called until after changing fMipMapsStatus.
57 this->didChangeGpuMemorySize();
58 }
59 }
60}
61
62size_t GrTexture::gpuMemorySize() const {
63 size_t textureSize = (size_t) fDesc.fWidth *
64 fDesc.fHeight *
65 GrBytesPerPixel(fDesc.fConfig);
66 if (kNotAllocated_MipMapsStatus != fMipMapsStatus) {
67 // We don't have to worry about the mipmaps being a different size than
68 // we'd expect because we never change fDesc.fWidth/fHeight.
69 textureSize *= 2;
70 }
71 return textureSize;
72}
73
bsalomon@google.com669fdc42011-04-05 17:08:27 +000074bool GrTexture::readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000075 GrPixelConfig config, void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000076 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000077 // go through context so that all necessary flushing occurs
bsalomon@google.com6f379512011-11-16 20:36:03 +000078 GrContext* context = this->getContext();
79 if (NULL == context) {
80 return false;
81 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +000082 return context->readTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000083 left, top, width, height,
84 config, buffer, rowBytes,
85 pixelOpsFlags);
bsalomon@google.com6f379512011-11-16 20:36:03 +000086}
87
88void GrTexture::writePixels(int left, int top, int width, int height,
89 GrPixelConfig config, const void* buffer,
bsalomon@google.com0342a852012-08-20 19:22:38 +000090 size_t rowBytes, uint32_t pixelOpsFlags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +000091 // go through context so that all necessary flushing occurs
92 GrContext* context = this->getContext();
93 if (NULL == context) {
94 return;
95 }
96 context->writeTexturePixels(this,
bsalomon@google.com0342a852012-08-20 19:22:38 +000097 left, top, width, height,
98 config, buffer, rowBytes,
99 pixelOpsFlags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000100}
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000101
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000102void GrTexture::onRelease() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000103 SkASSERT(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
robertphillips@google.comd3645542012-09-05 18:37:39 +0000104 INHERITED::onRelease();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000105}
106
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000107void GrTexture::onAbandon() {
bsalomon@google.com686bcb82013-04-09 15:04:12 +0000108 if (NULL != fRenderTarget.get()) {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000109 fRenderTarget->abandon();
110 }
robertphillips@google.comd3645542012-09-05 18:37:39 +0000111 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000112}
113
robertphillips@google.com32716282012-06-04 12:48:45 +0000114void GrTexture::validateDesc() const {
115 if (NULL != this->asRenderTarget()) {
116 // This texture has a render target
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000117 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000118
119 if (NULL != this->asRenderTarget()->getStencilBuffer()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000120 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000121 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000122 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
robertphillips@google.com32716282012-06-04 12:48:45 +0000123 }
124
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000125 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +0000126 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrTextureFlagBit));
128 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrTextureFlagBit));
129 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +0000130 }
131}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000132
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000133// 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 +0000134// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000135enum TextureFlags {
136 /**
137 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
138 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000139 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000140 kStretchToPOT_TextureFlag = 0x1,
141 /**
humper@google.comb86add12013-07-25 18:49:07 +0000142 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
143 * stretched texture should be bilerped.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000144 */
humper@google.comb86add12013-07-25 18:49:07 +0000145 kBilerp_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000146};
147
148namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000149GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
150 const GrTextureParams* params,
151 const GrTextureDesc& desc) {
152 GrResourceKey::ResourceFlags flags = 0;
153 bool tiled = NULL != params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000154 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
robertphillips@google.combe61c052013-01-04 16:33:44 +0000155 if (!GrIsPow2(desc.fWidth) || !GrIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000156 flags |= kStretchToPOT_TextureFlag;
humper@google.comb86add12013-07-25 18:49:07 +0000157 switch(params->filterMode()) {
158 case GrTextureParams::kNone_FilterMode:
159 break;
160 case GrTextureParams::kBilerp_FilterMode:
161 case GrTextureParams::kMipMap_FilterMode:
162 flags |= kBilerp_TextureFlag;
163 break;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000164 }
165 }
166 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000167 return flags;
168}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000169
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000170GrResourceKey::ResourceType texture_resource_type() {
171 static const GrResourceKey::ResourceType gType = GrResourceKey::GenerateResourceType();
172 return gType;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000173}
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000174
175// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
176GrSurfaceOrigin resolve_origin(const GrTextureDesc& desc) {
177 // By default, GrRenderTargets are GL's normal orientation so that they
178 // can be drawn to by the outside world without the client having
179 // to render upside down.
180 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrTextureFlagBit);
181 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
182 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
183 } else {
184 return desc.fOrigin;
185 }
186}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000187}
188
189GrResourceKey GrTexture::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000190 const GrTextureParams* params,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000191 const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000192 const GrCacheID& cacheID) {
193 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
194 return GrResourceKey(cacheID, texture_resource_type(), flags);
195}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000196
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000197GrResourceKey GrTexture::ComputeScratchKey(const GrTextureDesc& desc) {
198 GrCacheID::Key idKey;
199 // Instead of a client-provided key of the texture contents we create a key from the
200 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000201 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000202 SkASSERT(desc.fHeight < (1 << 16));
203 SkASSERT(desc.fWidth < (1 << 16));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000204 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
205 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
206 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000207 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000208 static const int kPadSize = sizeof(idKey) - 16;
209 GR_STATIC_ASSERT(kPadSize >= 0);
210 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000211
212 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
213 return GrResourceKey(cacheID, texture_resource_type(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000214}
215
216bool GrTexture::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000217 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000218}
219
humper@google.comb86add12013-07-25 18:49:07 +0000220bool GrTexture::NeedsBilerp(const GrResourceKey& key) {
221 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000222}