blob: bb1a6bb08111fb86a7cbdcaff9cdc87a5a62d708 [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
brianosmanfe199872016-06-13 07:59:48 -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));
robertphillipsf1c6cd72016-08-18 07:11:13 -070054 SkASSERT(textureSize <= WorstCaseSize(fDesc));
robertphillips6e83ac72015-08-13 05:19:14 -070055
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//////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070089GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
90 bool wasMipMapDataProvided)
91 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070092 , fSamplerType(samplerType)
93 // Gamma treatment is explicitly set after creation via GrTexturePriv
94 , fGammaTreatment(SkSourceGammaTreatment::kIgnore) {
cblume55f2d2d2016-02-26 13:20:48 -080095 if (wasMipMapDataProvided) {
96 fMipMapsStatus = kValid_MipMapsStatus;
97 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
98 } else {
99 fMipMapsStatus = kNotAllocated_MipMapsStatus;
100 fMaxMipMapLevel = 0;
101 }
bsalomon744998e2014-08-28 09:54:34 -0700102}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000103
kkinnunen2e6055b2016-04-22 01:48:29 -0700104void GrTexture::computeScratchKey(GrScratchKey* key) const {
105 if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
106 GrTexturePriv::ComputeScratchKey(fDesc, key);
107 }
108}
109
bsalomon7775c852014-12-30 12:50:52 -0800110void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
111 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000112
bsalomon7775c852014-12-30 12:50:52 -0800113 GrSurfaceOrigin origin = resolve_origin(desc);
114 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
115
cblume55f2d2d2016-02-26 13:20:48 -0800116 // make sure desc.fConfig fits in 5 bits
117 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
118 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
bsalomon7775c852014-12-30 12:50:52 -0800119 SkASSERT(desc.fSampleCnt < (1 << 8));
120 SkASSERT(flags < (1 << 10));
121 SkASSERT(static_cast<int>(origin) < (1 << 8));
122
joshualittb90de312015-10-01 11:54:34 -0700123 GrScratchKey::Builder builder(key, kType, 3);
124 builder[0] = desc.fWidth;
125 builder[1] = desc.fHeight;
cblume55f2d2d2016-02-26 13:20:48 -0800126 builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
127 | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000128}