blob: e944846bfd698dd342a2cda4cb68fbcab4a373d4 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrTypes.h"
11#include "include/private/GrResourceKey.h"
Mike Reed13711eb2020-07-14 17:16:32 -040012#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrGpu.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040015#include "src/gpu/GrRenderTarget.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000016#include "src/gpu/GrTexture.h"
bsalomon@google.com8295dc12011-05-02 12:53:34 +000017
Robert Phillips4e105e22020-07-16 09:18:50 -040018#ifdef SK_DEBUG
19#include "include/gpu/GrDirectContext.h"
20#include "src/gpu/GrContextPriv.h"
21#endif
22
Brian Salomon8c82a872020-07-21 12:09:58 -040023void GrTexture::markMipmapsDirty() {
24 if (GrMipmapStatus::kValid == fMipmapStatus) {
25 fMipmapStatus = GrMipmapStatus::kDirty;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040026 }
27}
28
Brian Salomon8c82a872020-07-21 12:09:58 -040029void GrTexture::markMipmapsClean() {
30 SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
31 fMipmapStatus = GrMipmapStatus::kValid;
commit-bot@chromium.org11c6b392014-05-05 19:09:13 +000032}
33
Robert Phillips29e52f12016-11-03 10:19:14 -040034size_t GrTexture::onGpuMemorySize() const {
Greg Daniel0eca74c2020-10-01 13:46:00 -040035 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
36 /*colorSamplesPerPixel=*/1, this->mipmapped());
Robert Phillips29e52f12016-11-03 10:19:14 -040037}
38
Brian Salomond34edf32017-05-19 15:45:48 -040039/////////////////////////////////////////////////////////////////////////////
Brian Salomon9f2b86c2019-10-22 10:37:46 -040040GrTexture::GrTexture(GrGpu* gpu,
41 const SkISize& dimensions,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040042 GrProtected isProtected,
43 GrTextureType textureType,
Brian Salomona6db5102020-07-21 09:56:23 -040044 GrMipmapStatus mipmapStatus)
Greg Danield51fa2f2020-01-22 16:53:38 -050045 : INHERITED(gpu, dimensions, isProtected)
Brian Salomone8a766b2019-07-19 14:24:36 -040046 , fTextureType(textureType)
Brian Salomon8c82a872020-07-21 12:09:58 -040047 , fMipmapStatus(mipmapStatus) {
48 if (fMipmapStatus == GrMipmapStatus::kNotAllocated) {
49 fMaxMipmapLevel = 0;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040050 } else {
Brian Salomon8c82a872020-07-21 12:09:58 -040051 fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height());
cblume55f2d2d2016-02-26 13:20:48 -080052 }
bsalomon744998e2014-08-28 09:54:34 -070053}
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +000054
Brian Salomon35ba6142019-01-24 13:08:59 -050055bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
Eric Karl914a36b2017-10-12 12:44:50 -070056 GrBackendTexture* backendTexture,
57 SkImage::BackendTextureReleaseProc* releaseProc) {
Robert Phillipsaee18c92019-09-06 11:48:27 -040058 if (!texture->unique()) {
Eric Karl914a36b2017-10-12 12:44:50 -070059 return false;
60 }
61
62 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
63 return false;
64 }
Brian Salomon35ba6142019-01-24 13:08:59 -050065#ifdef SK_DEBUG
Robert Phillips9da87e02019-02-04 13:26:26 -050066 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
Brian Salomon35ba6142019-01-24 13:08:59 -050067 int preCount = cache->getResourceCount();
68#endif
69 // Ensure that the texture will be released by the cache when we drop the last ref.
70 // A texture that has no refs and no keys should be immediately removed.
71 if (texture->getUniqueKey().isValid()) {
72 texture->resourcePriv().removeUniqueKey();
73 }
74 if (texture->resourcePriv().getScratchKey().isValid()) {
75 texture->resourcePriv().removeScratchKey();
76 }
77#ifdef SK_DEBUG
78 texture.reset();
79 int postCount = cache->getResourceCount();
80 SkASSERT(postCount < preCount);
81#endif
Eric Karl914a36b2017-10-12 12:44:50 -070082 return true;
83}
84
kkinnunen2e6055b2016-04-22 01:48:29 -070085void GrTexture::computeScratchKey(GrScratchKey* key) const {
Brian Salomon1c53a9f2019-08-12 14:10:12 -040086 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050087 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -040088 GrRenderable renderable = GrRenderable::kNo;
89 if (const auto* rt = this->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -060090 sampleCount = rt->numSamples();
Brian Salomonf2c2ba92019-07-17 09:59:59 -040091 renderable = GrRenderable::kYes;
Jim Van Verth1676cb92019-01-15 13:24:45 -050092 }
Brian Salomon14cb4132019-09-16 13:14:47 -040093 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
Brian Salomon4cfae3b2020-07-23 10:33:24 -040094 ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(),
95 renderable, sampleCount, this->mipmapped(), isProtected, key);
kkinnunen2e6055b2016-04-22 01:48:29 -070096 }
97}
98
Brian Salomon4cfae3b2020-07-23 10:33:24 -040099void GrTexture::ComputeScratchKey(const GrCaps& caps,
100 const GrBackendFormat& format,
101 SkISize dimensions,
102 GrRenderable renderable,
103 int sampleCnt,
104 GrMipmapped mipMapped,
105 GrProtected isProtected,
106 GrScratchKey* key) {
bsalomon7775c852014-12-30 12:50:52 -0800107 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400108 SkASSERT(!dimensions.isEmpty());
Brian Salomonbdecacf2018-02-02 20:32:49 -0500109 SkASSERT(sampleCnt > 0);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400110 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
bsalomon7775c852014-12-30 12:50:52 -0800111
Brian Salomon14cb4132019-09-16 13:14:47 -0400112 SkASSERT(static_cast<uint32_t>(mipMapped) <= 1);
113 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
114 SkASSERT(static_cast<uint32_t>(renderable) <= 1);
Greg Danield51fa2f2020-01-22 16:53:38 -0500115 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 3)));
bsalomon7775c852014-12-30 12:50:52 -0800116
Greg Danield51fa2f2020-01-22 16:53:38 -0500117 uint64_t formatKey = caps.computeFormatKey(format);
118
119 GrScratchKey::Builder builder(key, kType, 5);
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400120 builder[0] = dimensions.width();
121 builder[1] = dimensions.height();
Greg Danield51fa2f2020-01-22 16:53:38 -0500122 builder[2] = formatKey & 0xFFFFFFFF;
123 builder[3] = (formatKey >> 32) & 0xFFFFFFFF;
124 builder[4] = (static_cast<uint32_t>(mipMapped) << 0)
125 | (static_cast<uint32_t>(isProtected) << 1)
126 | (static_cast<uint32_t>(renderable) << 2)
127 | (static_cast<uint32_t>(sampleCnt) << 3);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000128}