blob: 18c4ec244d4af4d830aed19999cc8995bf46649e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkMath.h"
9#include "include/core/SkTypes.h"
10#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTexture.h"
12#include "include/gpu/GrTypes.h"
13#include "include/private/GrResourceKey.h"
14#include "src/core/SkMipMap.h"
15#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGpu.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040018#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrSurfacePriv.h"
20#include "src/gpu/GrTexturePriv.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000021
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040022void GrTexture::markMipMapsDirty() {
23 if (GrMipMapsStatus::kValid == fMipMapsStatus) {
24 fMipMapsStatus = GrMipMapsStatus::kDirty;
25 }
26}
27
28void GrTexture::markMipMapsClean() {
Greg Daniel09c94002018-06-08 22:11:51 +000029 SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus);
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040030 fMipMapsStatus = GrMipMapsStatus::kValid;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000031}
32
Robert Phillips29e52f12016-11-03 10:19:14 -040033size_t GrTexture::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040034 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
Robert Phillipsf9fcf7f2019-07-11 09:03:27 -040035 this->texturePriv().mipMapped());
Robert Phillips29e52f12016-11-03 10:19:14 -040036}
37
Brian Salomond34edf32017-05-19 15:45:48 -040038/////////////////////////////////////////////////////////////////////////////
Brian Salomona9c22572019-08-05 12:57:09 -040039GrTexture::GrTexture(GrGpu* gpu, const SkISize& size, GrPixelConfig config, GrProtected isProtected,
Brian Salomone8a766b2019-07-19 14:24:36 -040040 GrTextureType textureType, GrMipMapsStatus mipMapsStatus)
Brian Salomona9c22572019-08-05 12:57:09 -040041 : INHERITED(gpu, size, config, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -040042 , fTextureType(textureType)
43 , fMipMapsStatus(mipMapsStatus) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040044 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
cblume55f2d2d2016-02-26 13:20:48 -080045 fMaxMipMapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040046 } else {
47 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080048 }
bsalomon744998e2014-08-28 09:54:34 -070049}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000050
Brian Salomon35ba6142019-01-24 13:08:59 -050051bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070052 GrBackendTexture* backendTexture,
53 SkImage::BackendTextureReleaseProc* releaseProc) {
Robert Phillipsaee18c92019-09-06 11:48:27 -040054 if (!texture->unique()) {
Eric Karl914a36b2017-10-12 12:44:50 -070055 return false;
56 }
57
58 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
59 return false;
60 }
Brian Salomon35ba6142019-01-24 13:08:59 -050061#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050062 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050063 int preCount = cache->getResourceCount();
64#endif
65 // Ensure that the texture will be released by the cache when we drop the last ref.
66 // A texture that has no refs and no keys should be immediately removed.
67 if (texture->getUniqueKey().isValid()) {
68 texture->resourcePriv().removeUniqueKey();
69 }
70 if (texture->resourcePriv().getScratchKey().isValid()) {
71 texture->resourcePriv().removeScratchKey();
72 }
73#ifdef SK_DEBUG
74 texture.reset();
75 int postCount = cache->getResourceCount();
76 SkASSERT(postCount < preCount);
77#endif
Eric Karl914a36b2017-10-12 12:44:50 -070078 return true;
79}
80
kkinnunen2e6055b2016-04-22 01:48:29 -070081void GrTexture::computeScratchKey(GrScratchKey* key) const {
Brian Salomon1c53a9f2019-08-12 14:10:12 -040082 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050083 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040084 GrRenderable renderable = GrRenderable::kNo;
85 if (const auto* rt = this->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060086 sampleCount = rt->numSamples();
Brian Salomonf2c2ba92019-07-17 09:59:59 -040087 renderable = GrRenderable::kYes;
Jim Van Verth1676cb92019-01-15 13:24:45 -050088 }
Brian Salomon14cb4132019-09-16 13:14:47 -040089 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040090 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), renderable,
Brian Salomon14cb4132019-09-16 13:14:47 -040091 sampleCount, this->texturePriv().mipMapped(), isProtected,
92 key);
kkinnunen2e6055b2016-04-22 01:48:29 -070093 }
94}
95
Brian Salomon14cb4132019-09-16 13:14:47 -040096void GrTexturePriv::ComputeScratchKey(GrPixelConfig config,
97 int width,
98 int height,
99 GrRenderable renderable,
100 int sampleCnt,
101 GrMipMapped mipMapped,
102 GrProtected isProtected,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400103 GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -0800104 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500105 SkASSERT(width > 0);
106 SkASSERT(height > 0);
107 SkASSERT(sampleCnt > 0);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400108 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
bsalomon7775c852014-12-30 12:50:52 -0800109
cblume55f2d2d2016-02-26 13:20:48 -0800110 // make sure desc.fConfig fits in 5 bits
111 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
Brian Salomon14cb4132019-09-16 13:14:47 -0400112 SkASSERT(static_cast<uint32_t>(config) < (1 << 5));
113 SkASSERT(static_cast<uint32_t>(mipMapped) <= 1);
114 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
115 SkASSERT(static_cast<uint32_t>(renderable) <= 1);
116 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 8)));
bsalomon7775c852014-12-30 12:50:52 -0800117
joshualittb90de312015-10-01 11:54:34 -0700118 GrScratchKey::Builder builder(key, kType, 3);
Brian Salomond34edf32017-05-19 15:45:48 -0400119 builder[0] = width;
120 builder[1] = height;
Brian Salomon14cb4132019-09-16 13:14:47 -0400121 builder[2] = (static_cast<uint32_t>(config) << 0)
122 | (static_cast<uint32_t>(mipMapped) << 5)
123 | (static_cast<uint32_t>(isProtected) << 6)
124 | (static_cast<uint32_t>(renderable) << 7)
125 | (static_cast<uint32_t>(sampleCnt) << 8);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000126}