blob: ea0ed765172fe91989c3002674e98dc2edec4a8c [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 Phillipsd6214d42016-11-07 08:23:48 -050039 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,
Brian Salomon514baff2016-11-17 15:17:07 -050073 GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070074 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070075 , fSamplerType(samplerType)
Brian Salomon739c5bf2016-11-07 09:53:44 -050076 , fHighestFilterMode(highestFilterMode)
Brian Osman7b8400d2016-11-08 17:08:54 -050077 // Mip color mode is explicitly set after creation via GrTexturePriv
78 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
cblume55f2d2d2016-02-26 13:20:48 -080079 if (wasMipMapDataProvided) {
80 fMipMapsStatus = kValid_MipMapsStatus;
81 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(fDesc.fWidth, fDesc.fHeight);
82 } else {
83 fMipMapsStatus = kNotAllocated_MipMapsStatus;
84 fMaxMipMapLevel = 0;
85 }
bsalomon744998e2014-08-28 09:54:34 -070086}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000087
kkinnunen2e6055b2016-04-22 01:48:29 -070088void GrTexture::computeScratchKey(GrScratchKey* key) const {
89 if (!GrPixelConfigIsCompressed(fDesc.fConfig)) {
90 GrTexturePriv::ComputeScratchKey(fDesc, key);
91 }
92}
93
bsalomon7775c852014-12-30 12:50:52 -080094void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
95 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000096
bsalomon7775c852014-12-30 12:50:52 -080097 GrSurfaceOrigin origin = resolve_origin(desc);
98 uint32_t flags = desc.fFlags & ~kCheckAllocation_GrSurfaceFlag;
99
cblume55f2d2d2016-02-26 13:20:48 -0800100 // make sure desc.fConfig fits in 5 bits
101 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
102 SkASSERT(static_cast<int>(desc.fConfig) < (1 << 5));
bsalomon7775c852014-12-30 12:50:52 -0800103 SkASSERT(desc.fSampleCnt < (1 << 8));
104 SkASSERT(flags < (1 << 10));
105 SkASSERT(static_cast<int>(origin) < (1 << 8));
106
joshualittb90de312015-10-01 11:54:34 -0700107 GrScratchKey::Builder builder(key, kType, 3);
108 builder[0] = desc.fWidth;
109 builder[1] = desc.fHeight;
cblume55f2d2d2016-02-26 13:20:48 -0800110 builder[2] = desc.fConfig | (desc.fIsMipMapped << 5) | (desc.fSampleCnt << 6) | (flags << 14)
111 | (origin << 24);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000112}