xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrTexture.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * 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.
6  */
7 #include "src/gpu/ganesh/GrTexture.h"
8 
9 #include "include/core/SkSize.h"
10 #include "include/core/SkTypes.h"
11 #include "include/gpu/GpuTypes.h"
12 #include "include/gpu/ganesh/GrDirectContext.h"
13 #include "include/gpu/ganesh/GrTypes.h"
14 #include "src/core/SkMipmap.h"
15 #include "src/gpu/ResourceKey.h"
16 #include "src/gpu/ganesh/GrCaps.h"
17 #include "src/gpu/ganesh/GrDirectContextPriv.h"
18 #include "src/gpu/ganesh/GrGpu.h"
19 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
20 #include "src/gpu/ganesh/GrRenderTarget.h"
21 #include "src/gpu/ganesh/GrResourceCache.h"
22 
23 #include <cstdint>
24 
markMipmapsDirty()25 void GrTexture::markMipmapsDirty() {
26     if (GrMipmapStatus::kValid == fMipmapStatus) {
27         fMipmapStatus = GrMipmapStatus::kDirty;
28     }
29 }
30 
markMipmapsClean()31 void GrTexture::markMipmapsClean() {
32     SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
33     fMipmapStatus = GrMipmapStatus::kValid;
34 }
35 
onGpuMemorySize() const36 size_t GrTexture::onGpuMemorySize() const {
37     return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
38                                   /*colorSamplesPerPixel=*/1, this->mipmapped());
39 }
40 
41 /////////////////////////////////////////////////////////////////////////////
GrTexture(GrGpu * gpu,const SkISize & dimensions,GrProtected isProtected,GrTextureType textureType,GrMipmapStatus mipmapStatus,std::string_view label)42 GrTexture::GrTexture(GrGpu* gpu,
43                      const SkISize& dimensions,
44                      GrProtected isProtected,
45                      GrTextureType textureType,
46                      GrMipmapStatus mipmapStatus,
47                      std::string_view label)
48         : GrSurface(gpu, dimensions, isProtected, label)
49         , fTextureType(textureType)
50         , fMipmapStatus(mipmapStatus) {
51     if (fMipmapStatus == GrMipmapStatus::kNotAllocated) {
52         fMaxMipmapLevel = 0;
53     } else {
54         fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height());
55     }
56     if (textureType == GrTextureType::kExternal) {
57         this->setReadOnly();
58     }
59 }
60 
StealBackendTexture(sk_sp<GrTexture> texture,GrBackendTexture * backendTexture,SkImages::BackendTextureReleaseProc * releaseProc)61 bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
62                                     GrBackendTexture* backendTexture,
63                                     SkImages::BackendTextureReleaseProc* releaseProc) {
64     if (!texture->unique()) {
65         return false;
66     }
67 
68     if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
69         return false;
70     }
71 #ifdef SK_DEBUG
72     GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
73     int preCount = cache->getResourceCount();
74 #endif
75     // Ensure that the texture will be released by the cache when we drop the last ref.
76     // A texture that has no refs and no keys should be immediately removed.
77     if (texture->getUniqueKey().isValid()) {
78         texture->resourcePriv().removeUniqueKey();
79     }
80     if (texture->resourcePriv().getScratchKey().isValid()) {
81         texture->resourcePriv().removeScratchKey();
82     }
83 #ifdef SK_DEBUG
84     texture.reset();
85     int postCount = cache->getResourceCount();
86     SkASSERT(postCount < preCount);
87 #endif
88     return true;
89 }
90 
computeScratchKey(skgpu::ScratchKey * key) const91 void GrTexture::computeScratchKey(skgpu::ScratchKey* key) const {
92     if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
93         int sampleCount = 1;
94         GrRenderable renderable = GrRenderable::kNo;
95         if (const auto* rt = this->asRenderTarget()) {
96             sampleCount = rt->numSamples();
97             renderable = GrRenderable::kYes;
98         }
99         auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
100         ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(),
101                           renderable, sampleCount, this->mipmapped(), isProtected, key);
102     }
103 }
104 
ComputeScratchKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,GrRenderable renderable,int sampleCnt,skgpu::Mipmapped mipmapped,GrProtected isProtected,skgpu::ScratchKey * key)105 void GrTexture::ComputeScratchKey(const GrCaps& caps,
106                                   const GrBackendFormat& format,
107                                   SkISize dimensions,
108                                   GrRenderable renderable,
109                                   int sampleCnt,
110                                   skgpu::Mipmapped mipmapped,
111                                   GrProtected isProtected,
112                                   skgpu::ScratchKey* key) {
113     static const skgpu::ScratchKey::ResourceType kType = skgpu::ScratchKey::GenerateResourceType();
114     SkASSERT(!dimensions.isEmpty());
115     SkASSERT(sampleCnt > 0);
116     SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
117 
118     SkASSERT(static_cast<uint32_t>(mipmapped) <= 1);
119     SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
120     SkASSERT(static_cast<uint32_t>(renderable) <= 1);
121     SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 3)));
122 
123     uint64_t formatKey = caps.computeFormatKey(format);
124 
125     skgpu::ScratchKey::Builder builder(key, kType, 5);
126     builder[0] = dimensions.width();
127     builder[1] = dimensions.height();
128     builder[2] = formatKey & 0xFFFFFFFF;
129     builder[3] = (formatKey >> 32) & 0xFFFFFFFF;
130     builder[4] = (static_cast<uint32_t>(mipmapped)   << 0)
131                | (static_cast<uint32_t>(isProtected) << 1)
132                | (static_cast<uint32_t>(renderable)  << 2)
133                | (static_cast<uint32_t>(sampleCnt)   << 3);
134 }
135