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