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/GrContext.h"
11 #include "include/gpu/GrTexture.h"
12 #include "include/gpu/GrTypes.h"
13 #include "include/private/GrResourceKey.h"
14 #include "src/core/SkMipMap.h"
15 #include "src/gpu/GrCaps.h"
16 #include "src/gpu/GrContextPriv.h"
17 #include "src/gpu/GrGpu.h"
18 #include "src/gpu/GrRenderTarget.h"
19 #include "src/gpu/GrSurfacePriv.h"
20 #include "src/gpu/GrTexturePriv.h"
21
markMipMapsDirty()22 void GrTexture::markMipMapsDirty() {
23 if (GrMipMapsStatus::kValid == fMipMapsStatus) {
24 fMipMapsStatus = GrMipMapsStatus::kDirty;
25 }
26 }
27
markMipMapsClean()28 void GrTexture::markMipMapsClean() {
29 SkASSERT(GrMipMapsStatus::kNotAllocated != fMipMapsStatus);
30 fMipMapsStatus = GrMipMapsStatus::kValid;
31 }
32
onGpuMemorySize() const33 size_t GrTexture::onGpuMemorySize() const {
34 return GrSurface::ComputeSize(this->config(), this->width(), this->height(), 1,
35 this->texturePriv().mipMapped());
36 }
37
38 /////////////////////////////////////////////////////////////////////////////
GrTexture(GrGpu * gpu,const SkISize & size,GrPixelConfig config,GrProtected isProtected,GrTextureType textureType,GrMipMapsStatus mipMapsStatus)39 GrTexture::GrTexture(GrGpu* gpu, const SkISize& size, GrPixelConfig config, GrProtected isProtected,
40 GrTextureType textureType, GrMipMapsStatus mipMapsStatus)
41 : INHERITED(gpu, size, config, isProtected)
42 , fTextureType(textureType)
43 , fMipMapsStatus(mipMapsStatus) {
44 if (GrMipMapsStatus::kNotAllocated == fMipMapsStatus) {
45 fMaxMipMapLevel = 0;
46 } else {
47 fMaxMipMapLevel = SkMipMap::ComputeLevelCount(this->width(), this->height());
48 }
49 }
50
StealBackendTexture(sk_sp<GrTexture> texture,GrBackendTexture * backendTexture,SkImage::BackendTextureReleaseProc * releaseProc)51 bool GrTexture::StealBackendTexture(sk_sp<GrTexture> texture,
52 GrBackendTexture* backendTexture,
53 SkImage::BackendTextureReleaseProc* releaseProc) {
54 if (!texture->surfacePriv().hasUniqueRef() || texture->surfacePriv().hasPendingIO()) {
55 return false;
56 }
57
58 if (!texture->onStealBackendTexture(backendTexture, releaseProc)) {
59 return false;
60 }
61 #ifdef SK_DEBUG
62 GrResourceCache* cache = texture->getContext()->priv().getResourceCache();
63 int preCount = cache->getResourceCount();
64 #endif
65 // Ensure that the texture will be released by the cache when we drop the last ref.
66 // A texture that has no refs and no keys should be immediately removed.
67 if (texture->getUniqueKey().isValid()) {
68 texture->resourcePriv().removeUniqueKey();
69 }
70 if (texture->resourcePriv().getScratchKey().isValid()) {
71 texture->resourcePriv().removeScratchKey();
72 }
73 #ifdef SK_DEBUG
74 texture.reset();
75 int postCount = cache->getResourceCount();
76 SkASSERT(postCount < preCount);
77 #endif
78 return true;
79 }
80
computeScratchKey(GrScratchKey * key) const81 void GrTexture::computeScratchKey(GrScratchKey* key) const {
82 if (!this->getGpu()->caps()->isFormatCompressed(this->backendFormat())) {
83 int sampleCount = 1;
84 GrRenderable renderable = GrRenderable::kNo;
85 if (const auto* rt = this->asRenderTarget()) {
86 sampleCount = rt->numSamples();
87 renderable = GrRenderable::kYes;
88 }
89 GrTexturePriv::ComputeScratchKey(this->config(), this->width(), this->height(), renderable,
90 sampleCount, this->texturePriv().mipMapped(), key);
91 }
92 }
93
ComputeScratchKey(GrPixelConfig config,int width,int height,GrRenderable renderable,int sampleCnt,GrMipMapped mipMapped,GrScratchKey * key)94 void GrTexturePriv::ComputeScratchKey(GrPixelConfig config, int width, int height,
95 GrRenderable renderable, int sampleCnt, GrMipMapped mipMapped,
96 GrScratchKey* key) {
97 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
98 SkASSERT(width > 0);
99 SkASSERT(height > 0);
100 SkASSERT(sampleCnt > 0);
101 SkASSERT(1 == sampleCnt || renderable == GrRenderable::kYes);
102
103 // make sure desc.fConfig fits in 5 bits
104 SkASSERT(sk_float_log2(kLast_GrPixelConfig) <= 5);
105 SkASSERT(static_cast<int>(config) < (1 << 5));
106 SkASSERT(sampleCnt < (1 << 8));
107 SkASSERT(static_cast<int>(mipMapped) <= 1);
108
109 GrScratchKey::Builder builder(key, kType, 3);
110 builder[0] = width;
111 builder[1] = height;
112 builder[2] = config
113 | (static_cast<uint32_t>(mipMapped) << 5)
114 | (sampleCnt << 6)
115 | (static_cast<uint32_t>(renderable) << 14);
116 }
117
ComputeScratchKey(const GrSurfaceDesc & desc,GrRenderable renderable,int sampleCnt,GrScratchKey * key)118 void GrTexturePriv::ComputeScratchKey(const GrSurfaceDesc& desc, GrRenderable renderable,
119 int sampleCnt, GrScratchKey* key) {
120 // Note: the fOrigin field is not used in the scratch key
121 return ComputeScratchKey(desc.fConfig, desc.fWidth, desc.fHeight, renderable, sampleCnt,
122 GrMipMapped::kNo, key);
123 }
124