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 "include/private/GrResourceKey.h"
12 #include "src/core/SkMipmap.h"
13 #include "src/gpu/GrCaps.h"
14 #include "src/gpu/GrGpu.h"
15 #include "src/gpu/GrRenderTarget.h"
16 #include "src/gpu/GrResourceCache.h"
17 #include "src/gpu/GrTexture.h"
18
19 #ifdef SK_DEBUG
20 #include "include/gpu/GrDirectContext.h"
21 #include "src/gpu/GrDirectContextPriv.h"
22 #endif
23
markMipmapsDirty()24 void GrTexture::markMipmapsDirty() {
25 if (GrMipmapStatus::kValid == fMipmapStatus) {
26 fMipmapStatus = GrMipmapStatus::kDirty;
27 }
28 }
29
markMipmapsClean()30 void GrTexture::markMipmapsClean() {
31 SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
32 fMipmapStatus = GrMipmapStatus::kValid;
33 }
34
onGpuMemorySize() const35 size_t GrTexture::onGpuMemorySize() const {
36 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
37 /*colorSamplesPerPixel=*/1, this->mipmapped());
38 }
39
40 /////////////////////////////////////////////////////////////////////////////
GrTexture(GrGpu * gpu,const SkISize & dimensions,GrProtected isProtected,GrTextureType textureType,GrMipmapStatus mipmapStatus)41 GrTexture::GrTexture(GrGpu* gpu,
42 const SkISize& dimensions,
43 GrProtected isProtected,
44 GrTextureType textureType,
45 GrMipmapStatus mipmapStatus)
46 : INHERITED(gpu, dimensions, isProtected)
47 , fTextureType(textureType)
48 , fMipmapStatus(mipmapStatus) {
49 if (fMipmapStatus == GrMipmapStatus::kNotAllocated) {
50 fMaxMipmapLevel = 0;
51 } else {
52 fMaxMipmapLevel = SkMipmap::ComputeLevelCount(this->width(), this->height());
53 }
54 if (textureType == GrTextureType::kExternal) {
55 this->setReadOnly();
56 }
57 }
58
StealBackendTexture(sk_sp<GrTexture> texture,GrBackendTexture * backendTexture,SkImage::BackendTextureReleaseProc * releaseProc)59 bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
60 GrBackendTexture* backendTexture,
61 SkImage::BackendTextureReleaseProc* releaseProc) {
62 if (!texture->unique()) {
63 return false;
64 }
65
66 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
67 return false;
68 }
69 #ifdef SK_DEBUG
70 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
71 int preCount = cache->getResourceCount();
72 #endif
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();
77 }
78 if (texture->resourcePriv().getScratchKey().isValid()) {
79 texture->resourcePriv().removeScratchKey();
80 }
81 #ifdef SK_DEBUG
82 texture.reset();
83 int postCount = cache->getResourceCount();
84 SkASSERT(postCount < preCount);
85 #endif
86 return true;
87 }
88
computeScratchKey(GrScratchKey * key) const89 void GrTexture::computeScratchKey(GrScratchKey* key) const {
90 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
91 int sampleCount = 1;
92 GrRenderable renderable = GrRenderable::kNo;
93 if (const auto* rt = this->asRenderTarget()) {
94 sampleCount = rt->numSamples();
95 renderable = GrRenderable::kYes;
96 }
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);
100 }
101 }
102
ComputeScratchKey(const GrCaps & caps,const GrBackendFormat & format,SkISize dimensions,GrRenderable renderable,int sampleCnt,GrMipmapped mipMapped,GrProtected isProtected,GrScratchKey * key)103 void GrTexture::ComputeScratchKey(const GrCaps& caps,
104 const GrBackendFormat& format,
105 SkISize dimensions,
106 GrRenderable renderable,
107 int sampleCnt,
108 GrMipmapped mipMapped,
109 GrProtected isProtected,
110 GrScratchKey* key) {
111 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
112 SkASSERT(!dimensions.isEmpty());
113 SkASSERT(sampleCnt > 0);
114 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
115
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)));
120
121 uint64_t formatKey = caps.computeFormatKey(format);
122
123 GrScratchKey::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);
132 }
133