2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkMath.h"
9 #include "include/core/SkTypes.h"
10 #include "include/gpu/GrTypes.h"
11 #include "src/core/SkMipmap.h"
12 #include "src/gpu/ganesh/GrCaps.h"
13 #include "src/gpu/ganesh/GrGpu.h"
14 #include "src/gpu/ganesh/GrRenderTarget.h"
15 #include "src/gpu/ganesh/GrResourceCache.h"
16 #include "src/gpu/ganesh/GrTexture.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
23 void GrTexture::markMipmapsDirty() {
24 if (GrMipmapStatus::kValid == fMipmapStatus) {
25 fMipmapStatus = GrMipmapStatus::kDirty;
29 void GrTexture::markMipmapsClean() {
30 SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
31 fMipmapStatus = GrMipmapStatus::kValid;
34 size_t GrTexture::onGpuMemorySize() const {
35 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
36 /*colorSamplesPerPixel=*/1, this->mipmapped());
39 /////////////////////////////////////////////////////////////////////////////
40 GrTexture::GrTexture(GrGpu* gpu,
41 const SkISize& dimensions,
42 GrProtected isProtected,
43 GrTextureType textureType,
44 GrMipmapStatus mipmapStatus,
45 std::string_view label)
46 : INHERITED(gpu, dimensions, isProtected, label)
47 , fTextureType(textureType)
48 , fMipmapStatus(mipmapStatus) {
49 if (fMipmapStatus == GrMipmapStatus::kNotAllocated) {
52 fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height());
54 if (textureType == GrTextureType::kExternal) {
59 bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
60 GrBackendTexture* backendTexture,
61 SkImage::BackendTextureReleaseProc* releaseProc) {
62 if (!texture->unique()) {
66 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
70 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
71 int preCount = cache->getResourceCount();
73 // Ensure that the texture will be released by the cache when we drop the last ref.
74 // A texture that has no refs and no keys should be immediately removed.
75 if (texture->getUniqueKey().isValid()) {
76 texture->resourcePriv().removeUniqueKey();
78 if (texture->resourcePriv().getScratchKey().isValid()) {
79 texture->resourcePriv().removeScratchKey();
83 int postCount = cache->getResourceCount();
84 SkASSERT(postCount < preCount);
89 void GrTexture::computeScratchKey(skgpu::ScratchKey* key) const {
90 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
92 GrRenderable renderable = GrRenderable::kNo;
93 if (const auto* rt = this->asRenderTarget()) {
94 sampleCount = rt->numSamples();
95 renderable = GrRenderable::kYes;
97 auto isProtected = this->isProtected() ? GrProtected::kYes : GrProtected::kNo;
98 ComputeScratchKey(*this->getGpu()->caps(), this->backendFormat(), this->dimensions(),
99 renderable, sampleCount, this->mipmapped(), isProtected, key);
103 void GrTexture::ComputeScratchKey(const GrCaps& caps,
104 const GrBackendFormat& format,
106 GrRenderable renderable,
108 GrMipmapped mipmapped,
109 GrProtected isProtected,
110 skgpu::ScratchKey* key) {
111 static const skgpu::ScratchKey::ResourceType kType = skgpu::ScratchKey::GenerateResourceType();
112 SkASSERT(!dimensions.isEmpty());
113 SkASSERT(sampleCnt > 0);
114 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
116 SkASSERT(static_cast<uint32_t>(mipmapped) <= 1);
117 SkASSERT(static_cast<uint32_t>(isProtected) <= 1);
118 SkASSERT(static_cast<uint32_t>(renderable) <= 1);
119 SkASSERT(static_cast<uint32_t>(sampleCnt) < (1 << (32 - 3)));
121 uint64_t formatKey = caps.computeFormatKey(format);
123 skgpu::ScratchKey::Builder builder(key, kType, 5);
124 builder[0] = dimensions.width();
125 builder[1] = dimensions.height();
126 builder[2] = formatKey & 0xFFFFFFFF;
127 builder[3] = (formatKey >> 32) & 0xFFFFFFFF;
128 builder[4] = (static_cast<uint32_t>(mipmapped) << 0)
129 | (static_cast<uint32_t>(isProtected) << 1)
130 | (static_cast<uint32_t>(renderable) << 2)
131 | (static_cast<uint32_t>(sampleCnt) << 3);