blob: b4cefb0174de41fd14856a8b8c443edd0f31c53b [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"
Eric Karl914a36b2017-10-12 12:44:50 -070013#include "GrSurfacePriv.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
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040021void GrTexture::markMipMapsDirty() {
22 if (GrMipMapsStatus::kValid == fMipMapsStatus) {
23 fMipMapsStatus = GrMipMapsStatus::kDirty;
24 }
25}
26
27void GrTexture::markMipMapsClean() {
28 const bool sizeChanged = GrMipMapsStatus::kNotAllocated == fMipMapsStatus;
29 fMipMapsStatus = GrMipMapsStatus::kValid;
30 if (sizeChanged) {
31 // This must not be called until after changing fMipMapsStatus.
32 this->didChangeGpuMemorySize();
33 // TODO(http://skbug.com/4548) - The desc and scratch key should be
34 // updated to reflect the newly-allocated mipmaps.
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000035 }
36}
37
Robert Phillips29e52f12016-11-03 10:19:14 -040038size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040039 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Greg Daniele252f082017-10-23 16:05:23 -040040 this->texturePriv().mipMapped(), false);
Robert Phillips29e52f12016-11-03 10:19:14 -040041}
42
Brian Salomond34edf32017-05-19 15:45:48 -040043/////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -070044GrTexture::GrTexture(GrGpu* gpu, const GrSurfaceDesc& desc, GrSLType samplerType,
Greg Daniel834f1202017-10-09 15:06:20 -040045 GrSamplerState::Filter highestFilterMode,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040046 GrMipMapsStatus mipMapsStatus)
Brian Salomon2bbdcc42017-09-07 12:36:34 -040047 : INHERITED(gpu, desc)
48 , fSamplerType(samplerType)
49 , fHighestFilterMode(highestFilterMode)
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040050 , fMipMapsStatus(mipMapsStatus)
Brian Salomon2bbdcc42017-09-07 12:36:34 -040051 // Mip color mode is explicitly set after creation via GrTexturePriv
52 , fMipColorMode(SkDestinationSurfaceColorMode::kLegacy) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040053 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080054 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040055 } else {
56 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080057 }
bsalomon744998e2014-08-28 09:54:34 -070058}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000059
Eric Karl914a36b2017-10-12 12:44:50 -070060bool GrTexture::StealBackendTexture(sk_sp<GrTexture>&& texture,
61 GrBackendTexture* backendTexture,
62 SkImage::BackendTextureReleaseProc* releaseProc) {
63 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
64 return false;
65 }
66
67 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
68 return false;
69 }
70
71 // Release any not-stolen data being held by this class.
72 texture->onRelease();
73 // Abandon the GrTexture so it can't be re-used.
74 texture->abandon();
75
76 return true;
77}
78
kkinnunen2e6055b2016-04-22 01:48:29 -070079void GrTexture::computeScratchKey(GrScratchKey* key) const {
Robert Phillips92de6312017-05-23 07:43:48 -040080 const GrRenderTarget* rt = this->asRenderTarget();
81 int sampleCount = 0;
82 if (rt) {
83 sampleCount = rt->numStencilSamples();
kkinnunen2e6055b2016-04-22 01:48:29 -070084 }
Robert Phillips92de6312017-05-23 07:43:48 -040085 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(),
Robert Phillipsb0e93a22017-08-29 08:26:54 -040086 SkToBool(rt), sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -040087 this->texturePriv().mipMapped(), key);
kkinnunen2e6055b2016-04-22 01:48:29 -070088}
89
Brian Salomond34edf32017-05-19 15:45:48 -040090void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
Robert Phillipsb0e93a22017-08-29 08:26:54 -040091 bool isRenderTarget, int sampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -040092 GrMipMapped mipMapped, GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -080093 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomond34edf32017-05-19 15:45:48 -040094 uint32_t flags = isRenderTarget;
bsalomon@google.com0797c2c2012-12-20 15:13:01 +000095
Brian Salomond34edf32017-05-19 15:45:48 -040096 SkASSERT(0 == sampleCnt || isRenderTarget);
bsalomon7775c852014-12-30 12:50:52 -080097
cblume55f2d2d2016-02-26 13:20:48 -080098 // make sure desc.fConfig fits in 5 bits
99 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomond34edf32017-05-19 15:45:48 -0400100 SkASSERT(static_cast<int>(config) < (1 << 5));
101 SkASSERT(sampleCnt < (1 << 8));
bsalomon7775c852014-12-30 12:50:52 -0800102 SkASSERT(flags < (1 << 10));
Greg Daniele252f082017-10-23 16:05:23 -0400103 SkASSERT(static_cast<int>(mipMapped) <= 1);
bsalomon7775c852014-12-30 12:50:52 -0800104
joshualittb90de312015-10-01 11:54:34 -0700105 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400106 builder[0] = width;
107 builder[1] = height;
Greg Daniele252f082017-10-23 16:05:23 -0400108 builder[2] = config | (static_cast<uint8_t>(mipMapped) << 5) | (sampleCnt << 6) | (flags << 14);
Brian Salomond34edf32017-05-19 15:45:48 -0400109}
110
Greg Daniel21918232017-09-08 14:46:23 -0400111void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrScratchKey* key) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400112 // Note: the fOrigin field is not used in the scratch key
113 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight,
Brian Salomond34edf32017-05-19 15:45:48 -0400114 SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag), desc.fSampleCnt,
Greg Daniele252f082017-10-23 16:05:23 -0400115 GrMipMapped::kNo, key);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000116}