blob: 8e79a699fca82b100911fd1d9252a8a933234217 [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"
bsalomonafbf2d62014-09-30 12:18:44 -070013#include "GrTexture.h"
14#include "GrTexturePriv.h"
cblume55f2d2d2016-02-26 13:20:48 -080015#include "GrTypes.h"
16#include "SkMath.h"
17#include "SkMipMap.h"
18#include "SkTypes.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000019
brianosmanfe199872016-06-13 07:59:48 -070020void GrTexture::dirtyMipMaps(bool mipMapsDirty) {
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000021 if (mipMapsDirty) {
22 if (kValid_MipMapsStatus == fMipMapsStatus) {
cblume61214052016-01-26 09:10:48 -080023 fMipMapsStatus = kAllocated_MipMapsStatus;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000024 }
25 } else {
26 const bool sizeChanged = kNotAllocated_MipMapsStatus == fMipMapsStatus;
27 fMipMapsStatus = kValid_MipMapsStatus;
28 if (sizeChanged) {
29 // This must not be called until after changing fMipMapsStatus.
30 this->didChangeGpuMemorySize();
cblume55f2d2d2016-02-26 13:20:48 -080031 // TODO(http://skbug.com/4548) - The desc and scratch key should be
32 // updated to reflect the newly-allocated mipmaps.
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000033 }
34 }
35}
36
Robert Phillips29e52f12016-11-03 10:19:14 -040037size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040038 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
39 this->texturePriv().hasMipMaps(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040040}
41
Brian Salomond34edf32017-05-19 15:45:48 -040042/////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070043GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Brian Salomon514baff2016-11-17 15:17:07 -050044 GrSamplerParams::FilterMode highestFilterMode, bool wasMipMapDataProvided)
kkinnunen2e6055b2016-04-22 01:48:29 -070045 : INHERITED(gpu, desc)
brianosmanfe199872016-06-13 07:59:48 -070046 , fSamplerType(samplerType)
Brian Salomon739c5bf2016-11-07 09:53:44 -050047 , fHighestFilterMode(highestFilterMode)
Brian Osman7b8400d2016-11-08 17:08:54 -050048 // Mip color mode is explicitly set after creation via GrTexturePriv
49 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
cblume55f2d2d2016-02-26 13:20:48 -080050 if (wasMipMapDataProvided) {
51 fMipMapsStatus = kValid_MipMapsStatus;
Brian Salomond34edf32017-05-19 15:45:48 -040052 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080053 } else {
54 fMipMapsStatus = kNotAllocated_MipMapsStatus;
55 fMaxMipMapLevel = 0;
56 }
bsalomon744998e2014-08-28 09:54:34 -070057}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000058
kkinnunen2e6055b2016-04-22 01:48:29 -070059void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillips92de6312017-05-23 07:43:48 -040060 const GrRenderTarget* rt = this->asRenderTarget();
61 int sampleCount = 0;
62 if (rt) {
63 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070064 }
Robert Phillips92de6312017-05-23 07:43:48 -040065 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsdf0e09f2017-07-28 11:56:47 -040066 SkToBool(rt), sampleCount,
Robert Phillips92de6312017-05-23 07:43:48 -040067 this->texturePriv().hasMipMaps(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070068}
69
Brian Salomond34edf32017-05-19 15:45:48 -040070void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillipsdf0e09f2017-07-28 11:56:47 -040071 bool isRenderTarget, int sampleCnt,
Brian Salomond34edf32017-05-19 15:45:48 -040072 bool isMipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080073 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040074 uint32_t flags = isRenderTarget;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000075
Brian Salomond34edf32017-05-19 15:45:48 -040076 SkASSERT(0 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080077
cblume55f2d2d2016-02-26 13:20:48 -080078 // make sure desc.fConfig fits in 5 bits
79 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -040080 SkASSERT(static_cast<int>(config) < (1 << 5));
81 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -080082 SkASSERT(flags < (1 << 10));
bsalomon7775c852014-12-30 12:50:52 -080083
joshualittb90de312015-10-01 11:54:34 -070084 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -040085 builder[0] = width;
86 builder[1] = height;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -040087 builder[2] = config | (isMipMapped << 5) | (sampleCnt << 6) | (flags << 14);
Brian Salomond34edf32017-05-19 15:45:48 -040088}
89
90void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -040091 SkASSERT(kDefault_GrSurfaceOrigin != desc.fOrigin);
92
93 // Note: the fOrigin field is not used in the scratch key
94 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight,
Brian Salomond34edf32017-05-19 15:45:48 -040095 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
96 desc.fIsMipMapped, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +000097}