Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / ganesh / GrTexture.cpp
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/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"
17
18 #ifdef SK_DEBUG
19 #include "include/gpu/GrDirectContext.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
21 #endif
22
23 void GrTexture::markMipmapsDirty() {
24     if (GrMipmapStatus::kValid == fMipmapStatus) {
25         fMipmapStatus = GrMipmapStatus::kDirty;
26     }
27 }
28
29 void GrTexture::markMipmapsClean() {
30     SkASSERT(GrMipmapStatus::kNotAllocated != fMipmapStatus);
31     fMipmapStatus = GrMipmapStatus::kValid;
32 }
33
34 size_t GrTexture::onGpuMemorySize() const {
35     return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
36                                   /*colorSamplesPerPixel=*/1, this->mipmapped());
37 }
38
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) {
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
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
89 void GrTexture::computeScratchKey(skgpu::ScratchKey* 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
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                                   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);
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     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);
132 }