blob: de1135a1eb7bece759e7881f48f974213f2770d1 [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
Robert Phillips29e52f12016-11-03 10:19:14 -040038size_t GrTexture::onGpuMemorySize() const {
Robert Phillipsccd3c892016-11-04 10:23:43 -040039 return GrSurface::ComputeSize(fDesc, 1, this->texturePriv().hasMipMaps());
Robert Phillips29e52f12016-11-03 10:19:14 -040040}
41
robertphillips@google.com32716282012-06-04 12:48:45 +000042void GrTexture::validateDesc() const {
bsalomon49f085d2014-09-05 13:34:00 -070043 if (this->asRenderTarget()) {
robertphillips@google.com32716282012-06-04 12:48:45 +000044 // This texture has a render target
bsalomonf2703d82014-10-28 14:33:06 -070045 SkASSERT(0 != (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
vbuzinovdded6962015-06-12 08:59:45 -070046 SkASSERT(fDesc.fSampleCnt == this->asRenderTarget()->numColorSamples());
robertphillips@google.com32716282012-06-04 12:48:45 +000047 } else {
bsalomonf2703d82014-10-28 14:33:06 -070048 SkASSERT(0 == (fDesc.fFlags & kRenderTarget_GrSurfaceFlag));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049 SkASSERT(0 == fDesc.fSampleCnt);
robertphillips@google.com32716282012-06-04 12:48:45 +000050 }
51}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000052
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000053//////////////////////////////////////////////////////////////////////////////
54
robertphillips@google.coma1e57952012-06-04 20:05:28 +000055namespace {
robertphillips@google.coma1e57952012-06-04 20:05:28 +000056
jvanverth39edf762014-12-22 11:44:19 -080057// FIXME: This should be refactored with the code in gl/GrGLGpu.cpp.
bsalomonf2703d82014-10-28 14:33:06 -070058GrSurfaceOrigin resolve_origin(const GrSurfaceDesc& desc) {
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000059 // By default, GrRenderTargets are GL's normal orientation so that they
60 // can be drawn to by the outside world without the client having
61 // to render upside down.
bsalomonf2703d82014-10-28 14:33:06 -070062 bool renderTarget = 0 != (desc.fFlags & kRenderTarget_GrSurfaceFlag);
senorblanco@chromium.org709906b2013-02-07 00:35:25 +000063 if (kDefault_GrSurfaceOrigin == desc.fOrigin) {
64 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
65 } else {
66 return desc.fOrigin;
67 }
68}
robertphillips@google.coma1e57952012-06-04 20:05:28 +000069}
70
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000071//////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070072GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Ben Wagnerce4d04a2016-11-06 12:46:37 +000073 bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070074 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070075 , fSamplerType(samplerType)
76 // Gamma treatment is explicitly set after creation via GrTexturePriv
77 , fGammaTreatment(SkSourceGammaTreatment::kIgnore) {
cblume55f2d2d2016-02-26 13:20:48 -080078 if (wasMipMapDataProvided) {
79 fMipMapsStatus = kValid_MipMapsStatus;
80 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
81 } else {
82 fMipMapsStatus = kNotAllocated_MipMapsStatus;
83 fMaxMipMapLevel = 0;
84 }
bsalomon744998e2014-08-28 09:54:34 -070085}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000086
kkinnunen2e6055b2016-04-22 01:48:29 -070087void GrTexture::computeScratchKey(GrScratchKey* key) const {
88 if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
89 GrTexturePriv::ComputeScratchKey(fDesc, key);
90 }
91}
92
bsalomon7775c852014-12-30 12:50:52 -080093void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
94 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000095
bsalomon7775c852014-12-30 12:50:52 -080096 GrSurfaceOrigin origin = resolve_origin(desc);
97 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
98
cblume55f2d2d2016-02-26 13:20:48 -080099 // make sure desc.fConfig fits in 5 bits
100 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
101 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
bsalomon7775c852014-12-30 12:50:52 -0800102 SkASSERT(desc.fSampleCnt < (1 << 8));
103 SkASSERT(flags < (1 << 10));
104 SkASSERT(static_cast<int>(origin) < (1 << 8));
105
joshualittb90de312015-10-01 11:54:34 -0700106 GrScratchKey::Builder builder(key, kType, 3);
107 builder[0] = desc.fWidth;
108 builder[1] = desc.fHeight;
cblume55f2d2d2016-02-26 13:20:48 -0800109 builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
110 | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000111}