blob: 58bbeaae425c6ef43dbd52b05526b4f5099b9a56 [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
bsalomon@google.com669fdc42011-04-05 17:08:27 +00009#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000010#include "GrDrawTargetCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000011#include "GrGpu.h"
robertphillips@google.coma1e57952012-06-04 20:05:28 +000012#include "GrResourceCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070013#include "GrTexture.h"
14#include "GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000015
bsalomonafbf2d62014-09-30 12:18:44 -070016void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000017 if (mipMapsDirty) {
18 if (kValid_MipMapsStatus == fMipMapsStatus) {
bsalomonafbf2d62014-09-30 12:18:44 -070019 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000020 }
21 } else {
22 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
23 fMipMapsStatus = kValid_MipMapsStatus;
24 if (sizeChanged) {
25 // This must not be called until after changing fMipMapsStatus.
26 this->didChangeGpuMemorySize();
27 }
28 }
29}
30
bsalomon69ed47f2014-11-12 11:13:39 -080031size_t GrTexture::onGpuMemorySize() const {
bsalomond4cb9222014-08-11 14:19:09 -070032 size_t textureSize;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000033
34 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070035 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
bsalomond4cb9222014-08-11 14:19:09 -070036 } else {
37 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000038 }
39
bsalomonafbf2d62014-09-30 12:18:44 -070040 if (this->texturePriv().hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000041 // We don't have to worry about the mipmaps being a different size than
42 // we'd expect because we never change fDesc.fWidth/fHeight.
43 textureSize *= 2;
44 }
45 return textureSize;
46}
47
robertphillips@google.com32716282012-06-04 12:48:45 +000048void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -070049 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +000050 // This texture has a render target
bsalomonf2703d82014-10-28 14:33:06 -070051 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000052
bsalomon49f085d2014-09-05 13:34:00 -070053 if (this->asRenderTarget()->getStencilBuffer()) {
bsalomonf2703d82014-10-28 14:33:06 -070054 SkASSERT(0 != (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000055 } else {
bsalomonf2703d82014-10-28 14:33:06 -070056 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
robertphillips@google.com32716282012-06-04 12:48:45 +000057 }
58
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +000060 } else {
bsalomonf2703d82014-10-28 14:33:06 -070061 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
62 SkASSERT(0 == (fDesc.fFlags & kNoStencil_GrSurfaceFlag));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000063 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +000064 }
65}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000066
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000067//////////////////////////////////////////////////////////////////////////////
68
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000069// 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 +000070// key
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000071enum TextureFlags {
72 /**
73 * The kStretchToPOT bit is set when the texture is NPOT and is being repeated but the
74 * hardware doesn't support that feature.
robertphillips@google.coma1e57952012-06-04 20:05:28 +000075 */
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000076 kStretchToPOT_TextureFlag = 0x1,
77 /**
humper@google.comb86add12013-07-25 18:49:07 +000078 * The kBilerp bit can only be set when the kStretchToPOT flag is set and indicates whether the
79 * stretched texture should be bilerped.
robertphillips@google.coma1e57952012-06-04 20:05:28 +000080 */
humper@google.comb86add12013-07-25 18:49:07 +000081 kBilerp_TextureFlag = 0x2,
robertphillips@google.coma1e57952012-06-04 20:05:28 +000082};
83
84namespace {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000085GrResourceKey::ResourceFlags get_texture_flags(const GrGpu* gpu,
86 const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -070087 const GrSurfaceDesc& desc) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000088 GrResourceKey::ResourceFlags flags = 0;
bsalomon49f085d2014-09-05 13:34:00 -070089 bool tiled = params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +000090 if (tiled && !gpu->caps()->npotTextureTileSupport()) {
tfarinaf9dae782014-06-06 06:35:28 -070091 if (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight)) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000092 flags |= kStretchToPOT_TextureFlag;
humper@google.comb86add12013-07-25 18:49:07 +000093 switch(params->filterMode()) {
94 case GrTextureParams::kNone_FilterMode:
95 break;
96 case GrTextureParams::kBilerp_FilterMode:
97 case GrTextureParams::kMipMap_FilterMode:
98 flags |= kBilerp_TextureFlag;
99 break;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000100 }
101 }
102 }
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000103 return flags;
104}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000105
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000106// FIXME: This should be refactored with the code in gl/GrGpuGL.cpp.
bsalomonf2703d82014-10-28 14:33:06 -0700107GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000108 // By default, GrRenderTargets are GL's normal orientation so that they
109 // can be drawn to by the outside world without the client having
110 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -0700111 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000112 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
113 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
114 } else {
115 return desc.fOrigin;
116 }
117}
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000118}
119
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000120//////////////////////////////////////////////////////////////////////////////
bsalomonf2703d82014-10-28 14:33:06 -0700121GrTexture::GrTexture(GrGpu* gpu, bool isWrapped, const GrSurfaceDesc& desc)
bsalomon744998e2014-08-28 09:54:34 -0700122 : INHERITED(gpu, isWrapped, desc)
123 , fMipMapsStatus(kNotAllocated_MipMapsStatus) {
bsalomon8b79d232014-11-10 10:19:06 -0800124
125 if (!isWrapped) {
126 this->setScratchKey(GrTexturePriv::ComputeScratchKey(desc));
127 }
bsalomonafbf2d62014-09-30 12:18:44 -0700128 // only make sense if alloc size is pow2
129 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
130 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
bsalomon744998e2014-08-28 09:54:34 -0700131}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000132
bsalomonafbf2d62014-09-30 12:18:44 -0700133GrResourceKey GrTexturePriv::ComputeKey(const GrGpu* gpu,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000134 const GrTextureParams* params,
bsalomonf2703d82014-10-28 14:33:06 -0700135 const GrSurfaceDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000136 const GrCacheID& cacheID) {
137 GrResourceKey::ResourceFlags flags = get_texture_flags(gpu, params, desc);
bsalomonbcf0a522014-10-08 08:40:09 -0700138 return GrResourceKey(cacheID, ResourceType(), flags);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000139}
robertphillips@google.com46a86002012-08-08 10:42:44 +0000140
bsalomonf2703d82014-10-28 14:33:06 -0700141GrResourceKey GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000142 GrCacheID::Key idKey;
143 // Instead of a client-provided key of the texture contents we create a key from the
144 // descriptor.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000145 GR_STATIC_ASSERT(sizeof(idKey) >= 16);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000146 SkASSERT(desc.fHeight < (1 << 16));
147 SkASSERT(desc.fWidth < (1 << 16));
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000148 idKey.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
149 idKey.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
150 idKey.fData32[2] = desc.fFlags;
senorblanco@chromium.org709906b2013-02-07 00:35:25 +0000151 idKey.fData32[3] = resolve_origin(desc); // Only needs 2 bits actually
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000152 static const int kPadSize = sizeof(idKey) - 16;
153 GR_STATIC_ASSERT(kPadSize >= 0);
154 memset(idKey.fData8 + 16, 0, kPadSize);
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000155
156 GrCacheID cacheID(GrResourceKey::ScratchDomain(), idKey);
bsalomonbcf0a522014-10-08 08:40:09 -0700157 return GrResourceKey(cacheID, ResourceType(), 0);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000158}
159
bsalomonafbf2d62014-09-30 12:18:44 -0700160bool GrTexturePriv::NeedsResizing(const GrResourceKey& key) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000161 return SkToBool(key.getResourceFlags() & kStretchToPOT_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000162}
163
bsalomonafbf2d62014-09-30 12:18:44 -0700164bool GrTexturePriv::NeedsBilerp(const GrResourceKey& key) {
humper@google.comb86add12013-07-25 18:49:07 +0000165 return SkToBool(key.getResourceFlags() & kBilerp_TextureFlag);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000166}