Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / gpu / ganesh / vk / GrVkTexture.cpp
1 /*
2  * Copyright 2015 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
9 #include "src/gpu/ganesh/vk/GrVkTexture.h"
10
11 #include "src/gpu/ganesh/GrTexture.h"
12 #include "src/gpu/ganesh/vk/GrVkDescriptorSet.h"
13 #include "src/gpu/ganesh/vk/GrVkGpu.h"
14 #include "src/gpu/ganesh/vk/GrVkImageView.h"
15 #include "src/gpu/ganesh/vk/GrVkTextureRenderTarget.h"
16 #include "src/gpu/ganesh/vk/GrVkUtil.h"
17
18 #include "include/gpu/vk/GrVkTypes.h"
19
20 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
21
22 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
23 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
24                          SkBudgeted budgeted,
25                          SkISize dimensions,
26                          sk_sp<GrVkImage> texture,
27                          GrMipmapStatus mipmapStatus,
28                          std::string_view label)
29         : GrSurface(gpu,
30                     dimensions,
31                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
32                     label)
33         , GrTexture(gpu,
34                     dimensions,
35                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
36                     GrTextureType::k2D,
37                     mipmapStatus,
38                     label)
39         , fTexture(std::move(texture))
40         , fDescSetCache(kMaxCachedDescSets) {
41     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == fTexture->mipLevels()));
42     // We don't support creating external GrVkTextures
43     SkASSERT(!fTexture->ycbcrConversionInfo().isValid() ||
44              !fTexture->ycbcrConversionInfo().fExternalFormat);
45     SkASSERT(SkToBool(fTexture->vkUsageFlags() & VK_IMAGE_USAGE_SAMPLED_BIT));
46     this->registerWithCache(budgeted);
47     if (GrVkFormatIsCompressed(fTexture->imageFormat())) {
48         this->setReadOnly();
49     }
50 }
51
52 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
53                          SkISize dimensions,
54                          sk_sp<GrVkImage> texture,
55                          GrMipmapStatus mipmapStatus,
56                          GrWrapCacheable cacheable,
57                          GrIOType ioType,
58                          bool isExternal,
59                          std::string_view label)
60         : GrSurface(gpu,
61                     dimensions,
62                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
63                     label)
64         , GrTexture(gpu,
65                     dimensions,
66                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
67                     isExternal ? GrTextureType::kExternal : GrTextureType::k2D,
68                     mipmapStatus,
69                     label)
70         , fTexture(std::move(texture))
71         , fDescSetCache(kMaxCachedDescSets) {
72     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == fTexture->mipLevels()));
73     SkASSERT(SkToBool(fTexture->vkUsageFlags() & VK_IMAGE_USAGE_SAMPLED_BIT));
74     if (ioType == kRead_GrIOType) {
75         this->setReadOnly();
76     }
77     this->registerWithCacheWrapped(cacheable);
78 }
79
80 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
81 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
82                          SkISize dimensions,
83                          sk_sp<GrVkImage> texture,
84                          GrMipmapStatus mipmapStatus,
85                          std::string_view label)
86         : GrSurface(gpu,
87                     dimensions,
88                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
89                     label)
90         , GrTexture(gpu,
91                     dimensions,
92                     texture->isProtected() ? GrProtected::kYes : GrProtected::kNo,
93                     GrTextureType::k2D,
94                     mipmapStatus,
95                     label)
96         , fTexture(std::move(texture))
97         , fDescSetCache(kMaxCachedDescSets) {
98     SkASSERT((GrMipmapStatus::kNotAllocated == mipmapStatus) == (1 == fTexture->mipLevels()));
99     // Since this ctor is only called from GrVkTextureRenderTarget, we can't have a ycbcr conversion
100     // since we don't support that on render targets.
101     SkASSERT(!fTexture->ycbcrConversionInfo().isValid());
102     SkASSERT(SkToBool(fTexture->vkUsageFlags() & VK_IMAGE_USAGE_SAMPLED_BIT));
103 }
104
105 sk_sp<GrVkTexture> GrVkTexture::MakeNewTexture(GrVkGpu* gpu, SkBudgeted budgeted,
106                                                SkISize dimensions,
107                                                VkFormat format, uint32_t mipLevels,
108                                                GrProtected isProtected,
109                                                GrMipmapStatus mipmapStatus,
110                                                std::string_view label) {
111     sk_sp<GrVkImage> texture = GrVkImage::MakeTexture(
112             gpu, dimensions, format, mipLevels, GrRenderable::kNo, /*numSamples=*/1, budgeted,
113             isProtected);
114
115     if (!texture) {
116         return nullptr;
117     }
118     return sk_sp<GrVkTexture>(new GrVkTexture(
119             gpu, budgeted, dimensions, std::move(texture), mipmapStatus, label));
120 }
121
122 sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(
123         GrVkGpu* gpu, SkISize dimensions, GrWrapOwnership wrapOwnership, GrWrapCacheable cacheable,
124         GrIOType ioType, const GrVkImageInfo& info,
125         sk_sp<GrBackendSurfaceMutableStateImpl> mutableState) {
126     // Adopted textures require both image and allocation because we're responsible for freeing
127     SkASSERT(VK_NULL_HANDLE != info.fImage &&
128              (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
129
130     sk_sp<GrVkImage> texture = GrVkImage::MakeWrapped(gpu,
131                                                       dimensions,
132                                                       info,
133                                                       std::move(mutableState),
134                                                       GrAttachment::UsageFlags::kTexture,
135                                                       wrapOwnership,
136                                                       cacheable);
137     if (!texture) {
138         return nullptr;
139     }
140
141     GrMipmapStatus mipmapStatus = info.fLevelCount > 1 ? GrMipmapStatus::kValid
142                                                        : GrMipmapStatus::kNotAllocated;
143
144     bool isExternal = info.fYcbcrConversionInfo.isValid() &&
145                       (info.fYcbcrConversionInfo.fExternalFormat != 0);
146     isExternal |= (info.fImageTiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT);
147     return sk_sp<GrVkTexture>(new GrVkTexture(gpu,
148                                               dimensions,
149                                               std::move(texture),
150                                               mipmapStatus,
151                                               cacheable,
152                                               ioType,
153                                               isExternal,
154                                               /*label=*/{}));
155 }
156
157 GrVkTexture::~GrVkTexture() {
158     // either release or abandon should have been called by the owner of this object.
159     SkASSERT(!fTexture);
160 }
161
162 void GrVkTexture::onRelease() {
163     fTexture.reset();
164
165     fDescSetCache.reset();
166
167     GrTexture::onRelease();
168 }
169
170 struct GrVkTexture::DescriptorCacheEntry {
171     DescriptorCacheEntry(const GrVkDescriptorSet* fDescSet, GrVkGpu* gpu)
172             : fDescriptorSet(fDescSet), fGpu(gpu) {}
173     ~DescriptorCacheEntry() {
174         if (fDescriptorSet) {
175             fDescriptorSet->recycle();
176         }
177     }
178
179     const GrVkDescriptorSet* fDescriptorSet;
180     GrVkGpu* fGpu;
181 };
182
183 void GrVkTexture::onAbandon() {
184     fTexture.reset();
185
186     fDescSetCache.reset();
187
188     GrTexture::onAbandon();
189 }
190
191 GrBackendTexture GrVkTexture::getBackendTexture() const {
192     return GrBackendTexture(fTexture->width(), fTexture->height(), fTexture->vkImageInfo(),
193                             fTexture->getMutableState());
194 }
195
196 GrVkGpu* GrVkTexture::getVkGpu() const {
197     SkASSERT(!this->wasDestroyed());
198     return static_cast<GrVkGpu*>(this->getGpu());
199 }
200
201 const GrVkImageView* GrVkTexture::textureView() { return fTexture->textureView(); }
202
203 const GrVkDescriptorSet* GrVkTexture::cachedSingleDescSet(GrSamplerState state) {
204     if (std::unique_ptr<DescriptorCacheEntry>* e = fDescSetCache.find(state)) {
205         return (*e)->fDescriptorSet;
206     }
207     return nullptr;
208 }
209
210 void GrVkTexture::addDescriptorSetToCache(const GrVkDescriptorSet* descSet, GrSamplerState state) {
211     SkASSERT(!fDescSetCache.find(state));
212     descSet->ref();
213     fDescSetCache.insert(state, std::make_unique<DescriptorCacheEntry>(descSet, this->getVkGpu()));
214 }