blob: 9051c851bd4db591afb38074cfb9c512d8859479 [file] [log] [blame]
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com669fdc42011-04-05 17:08:27 +00006 */
7
bsalomon@google.com669fdc42011-04-05 17:08:27 +00008#include "GrContext.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000010#include "GrGpu.h"
bsalomon7775c852014-12-30 12:50:52 -080011#include "GrResourceKey.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080012#include "GrRenderTarget.h"
13#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070014#include "GrTexture.h"
15#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080016#include "GrTypes.h"
17#include "SkMath.h"
18#include "SkMipMap.h"
19#include "SkTypes.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000020
bsalomonafbf2d62014-09-30 12:18:44 -070021void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000022 if (mipMapsDirty) {
23 if (kValid_MipMapsStatus == fMipMapsStatus) {
cblume61214052016-01-26 09:10:48 -080024 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000025 }
26 } else {
27 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
28 fMipMapsStatus = kValid_MipMapsStatus;
29 if (sizeChanged) {
30 // This must not be called until after changing fMipMapsStatus.
31 this->didChangeGpuMemorySize();
cblume55f2d2d2016-02-26 13:20:48 -080032 // TODO(http://skbug.com/4548) - The desc and scratch key should be
33 // updated to reflect the newly-allocated mipmaps.
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000034 }
35 }
36}
37
bsalomon69ed47f2014-11-12 11:13:39 -080038size_t GrTexture::onGpuMemorySize() const {
bsalomond4cb9222014-08-11 14:19:09 -070039 size_t textureSize;
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000040
41 if (GrPixelConfigIsCompressed(fDesc.fConfig)) {
krajcevski7ef21622014-07-16 15:21:13 -070042 textureSize = GrCompressedFormatDataSize(fDesc.fConfig, fDesc.fWidth, fDesc.fHeight);
bsalomond4cb9222014-08-11 14:19:09 -070043 } else {
44 textureSize = (size_t) fDesc.fWidth * fDesc.fHeight * GrBytesPerPixel(fDesc.fConfig);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +000045 }
46
bsalomonafbf2d62014-09-30 12:18:44 -070047 if (this->texturePriv().hasMipMaps()) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000048 // We don't have to worry about the mipmaps being a different size than
49 // we'd expect because we never change fDesc.fWidth/fHeight.
robertphillips6e83ac72015-08-13 05:19:14 -070050 textureSize += textureSize/3;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000051 }
robertphillips6e83ac72015-08-13 05:19:14 -070052
53 SkASSERT(!SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
54 SkASSERT(textureSize <= WorseCaseSize(fDesc));
55
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000056 return textureSize;
57}
58
robertphillips@google.com32716282012-06-04 12:48:45 +000059void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -070060 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +000061 // This texture has a render target
bsalomonf2703d82014-10-28 14:33:06 -070062 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
vbuzinovdded6962015-06-12 08:59:45 -070063 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +000064 } else {
bsalomonf2703d82014-10-28 14:33:06 -070065 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +000067 }
68}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000069
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000070//////////////////////////////////////////////////////////////////////////////
71
robertphillips@google.coma1e57952012-06-04 20:05:28 +000072namespace {
robertphillips@google.coma1e57952012-06-04 20:05:28 +000073
jvanverth39edf762014-12-22 11:44:19 -080074// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
bsalomonf2703d82014-10-28 14:33:06 -070075GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000076 // By default, GrRenderTargets are GL's normal orientation so that they
77 // can be drawn to by the outside world without the client having
78 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -070079 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000080 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
81 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
82 } else {
83 return desc.fOrigin;
84 }
85}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000086}
87
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000088//////////////////////////////////////////////////////////////////////////////
cblume55f2d2d2016-02-26 13:20:48 -080089GrTexture::GrTexture(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc,
cdalton9c3f1432016-03-11 10:07:37 -080090 GrSLType samplerType, bool wasMipMapDataProvided)
91 : INHERITED(gpu, lifeCycle, desc)
92 , fSamplerType(samplerType) {
erikchen9a1ed5d2016-02-10 16:32:34 -080093 if (!this->isExternal() && !GrPixelConfigIsCompressed(desc.fConfig) &&
94 !desc.fTextureStorageAllocator.fAllocateTextureStorage) {
bsalomon7775c852014-12-30 12:50:52 -080095 GrScratchKey key;
96 GrTexturePriv::ComputeScratchKey(desc, &key);
97 this->setScratchKey(key);
bsalomon8b79d232014-11-10 10:19:06 -080098 }
cblume55f2d2d2016-02-26 13:20:48 -080099
100 if (wasMipMapDataProvided) {
101 fMipMapsStatus = kValid_MipMapsStatus;
102 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
103 } else {
104 fMipMapsStatus = kNotAllocated_MipMapsStatus;
105 fMaxMipMapLevel = 0;
106 }
bsalomon744998e2014-08-28 09:54:34 -0700107}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000108
bsalomon7775c852014-12-30 12:50:52 -0800109void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
110 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000111
bsalomon7775c852014-12-30 12:50:52 -0800112 GrSurfaceOrigin origin = resolve_origin(desc);
113 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
114
cblume55f2d2d2016-02-26 13:20:48 -0800115 // make sure desc.fConfig fits in 5 bits
116 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
117 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
bsalomon7775c852014-12-30 12:50:52 -0800118 SkASSERT(desc.fSampleCnt < (1 << 8));
119 SkASSERT(flags < (1 << 10));
120 SkASSERT(static_cast<int>(origin) < (1 << 8));
121
joshualittb90de312015-10-01 11:54:34 -0700122 GrScratchKey::Builder builder(key, kType, 3);
123 builder[0] = desc.fWidth;
124 builder[1] = desc.fHeight;
cblume55f2d2d2016-02-26 13:20:48 -0800125 builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
126 | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000127}