Flutter Engine
The Flutter Engine
GrVkBackendSurface.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
8
13#include "include/gpu/GrTypes.h"
26
27#include <algorithm>
28#include <cstddef>
29#include <cstdint>
30#include <string>
31
33public:
35 : fFormat(format), fYcbcrConversionInfo(ycbcrInfo) {}
36
37 VkFormat asVkFormat() const { return fFormat; }
39 return &fYcbcrConversionInfo;
40 }
41
42private:
43 SkTextureCompressionType compressionType() const override {
44 switch (fFormat) {
51 default:
53 }
54 }
55
56 size_t bytesPerBlock() const override {
57 return skgpu::VkFormatBytesPerBlock(fFormat);
58 }
59
60 int stencilBits() const override {
61 return skgpu::VkFormatStencilBits(fFormat);
62 }
63
64 uint32_t channelMask() const override {
65 return skgpu::VkFormatChannels(fFormat);
66 }
67
68 GrColorFormatDesc desc() const override {
69 return GrVkFormatDesc(fFormat);
70 }
71
72 bool equal(const GrBackendFormatData* that) const override {
73 SkASSERT(!that || that->type() == GrBackendApi::kVulkan);
74 if (auto otherVk = static_cast<const GrVkBackendFormatData*>(that)) {
75 return fFormat == otherVk->fFormat &&
76 fYcbcrConversionInfo == otherVk->fYcbcrConversionInfo;
77 }
78 return false;
79 }
80
81 std::string toString() const override {
82#if defined(SK_DEBUG) || GR_TEST_UTILS
83 return skgpu::VkFormatToStr(fFormat);
84#else
85 return "";
86#endif
87 }
88
89 void copyTo(AnyFormatData& formatData) const override {
90 formatData.emplace<GrVkBackendFormatData>(fFormat, fYcbcrConversionInfo);
91 }
92
93 void makeTexture2D() override {
94 // If we have a ycbcr we remove it from the backend format and set the VkFormat to
95 // R8G8B8A8_UNORM
96 if (fYcbcrConversionInfo.isValid()) {
97 fYcbcrConversionInfo = skgpu::VulkanYcbcrConversionInfo();
99 }
100 }
101
102#if defined(SK_DEBUG)
103 GrBackendApi type() const override { return GrBackendApi::kVulkan; }
104#endif
105
106 VkFormat fFormat;
107 skgpu::VulkanYcbcrConversionInfo fYcbcrConversionInfo;
108};
109
112 SkASSERT(!data || data->type() == GrBackendApi::kVulkan);
113 return static_cast<const GrVkBackendFormatData*>(data);
114}
115
116namespace GrBackendFormats {
117
118GrBackendFormat MakeVk(VkFormat format, bool willUseDRMFormatModifiers) {
123}
124
126 bool willUseDRMFormatModifiers) {
127 SkASSERT(ycbcrInfo.isValid());
128 GrTextureType textureType =
129 ((ycbcrInfo.isValid() && ycbcrInfo.fExternalFormat) || willUseDRMFormatModifiers)
133 textureType,
135 GrVkBackendFormatData(ycbcrInfo.fFormat, ycbcrInfo));
136}
137
138bool AsVkFormat(const GrBackendFormat& format, VkFormat* vkFormat) {
139 SkASSERT(vkFormat);
140 if (format.isValid() && format.backend() == GrBackendApi::kVulkan) {
142 SkASSERT(data);
143 *vkFormat = data->asVkFormat();
144 return true;
145 }
146 return false;
147}
148
150 if (format.isValid() && format.backend() == GrBackendApi::kVulkan) {
152 SkASSERT(data);
153 return data->getYcbcrConversionInfo();
154 }
155 return nullptr;
156}
157
158} // namespace GrBackendFormats
159
160
162public:
165 : fVkInfo(info) {
166 if (mutableState) {
167 fMutableState = std::move(mutableState);
168 } else {
169 fMutableState =
170 sk_make_sp<skgpu::MutableTextureState>(skgpu::MutableTextureStates::MakeVulkan(
172 }
173 }
174
175 const GrVkImageInfo& info() const { return fVkInfo; }
176
178 return fMutableState;
179 }
181 fMutableState->set(state);
182 }
183
184 skgpu::MutableTextureState* mutableState() { return fMutableState.get(); }
185 const skgpu::MutableTextureState* mutableState() const { return fMutableState.get(); }
186
187private:
188 void copyTo(AnyTextureData& textureData) const override {
189 textureData.emplace<GrVkBackendTextureData>(fVkInfo, fMutableState);
190 }
191
192 bool isProtected() const override { return fVkInfo.fProtected == skgpu::Protected::kYes; }
193
194 bool equal(const GrBackendTextureData* that) const override {
195 SkASSERT(!that || that->type() == GrBackendApi::kVulkan);
196 if (auto otherVk = static_cast<const GrVkBackendTextureData*>(that)) {
197 // For our tests when checking equality we are assuming both backendTexture objects will
198 // be using the same mutable state object.
199 if (fMutableState != otherVk->fMutableState) {
200 return false;
201 }
202 return GrVkImageInfoWithMutableState(fVkInfo, fMutableState.get()) ==
203 GrVkImageInfoWithMutableState(otherVk->fVkInfo, fMutableState.get());
204 }
205 return false;
206 }
207
208 bool isSameTexture(const GrBackendTextureData* that) const override {
209 SkASSERT(!that || that->type() == GrBackendApi::kVulkan);
210 if (auto otherVk = static_cast<const GrVkBackendTextureData*>(that)) {
211 return fVkInfo.fImage == otherVk->fVkInfo.fImage;
212 }
213 return false;
214
215 }
216
217 GrBackendFormat getBackendFormat() const override {
218 auto info = GrVkImageInfoWithMutableState(fVkInfo, fMutableState.get());
222 return GrBackendFormats::MakeVk(info.fYcbcrConversionInfo, usesDRMModifier);
223 }
224 return GrBackendFormats::MakeVk(info.fFormat, usesDRMModifier);
225 }
226
227#if defined(SK_DEBUG)
228 GrBackendApi type() const override { return GrBackendApi::kVulkan; }
229#endif
230
231 GrVkImageInfo fVkInfo;
233};
234
237 SkASSERT(!data || data->type() == GrBackendApi::kVulkan);
238 return static_cast<const GrVkBackendTextureData*>(data);
239}
240
243 SkASSERT(!data || data->type() == GrBackendApi::kVulkan);
244 return static_cast<GrVkBackendTextureData*>(data);
245}
246
248 if ((info.fYcbcrConversionInfo.isValid() && info.fYcbcrConversionInfo.fExternalFormat != 0) ||
251 }
252 return GrTextureType::k2D;
253}
254
257
260
261// We don't know if the backend texture is made renderable or not, so we default the usage flags
262// to include color attachment as well.
265
267 VkImageUsageFlags defaultFlags) {
268 if (info.fImageUsageFlags == 0) {
269 GrVkImageInfo newInfo = info;
270 newInfo.fImageUsageFlags = defaultFlags;
271 return newInfo;
272 }
273 return info;
274}
275
276namespace GrBackendTextures {
277
279 int height,
280 const GrVkImageInfo& vkInfo,
281 std::string_view label) {
283 width,
284 height,
285 label,
286 skgpu::Mipmapped(vkInfo.fLevelCount > 1),
290}
291
293 int height,
294 const GrVkImageInfo& vkInfo,
297 width,
298 height,
299 /*label=*/{},
300 skgpu::Mipmapped(vkInfo.fLevelCount > 1),
304 std::move(mutableState)));
305}
306
308 if (!tex.isValid() || tex.backend() != GrBackendApi::kVulkan) {
309 return false;
310 }
312 SkASSERT(data);
313 *outInfo = GrVkImageInfoWithMutableState(data->info(), data->mutableState());
314 return true;
315}
316
318 if (tex && tex->isValid() && tex->backend() == GrBackendApi::kVulkan) {
320 SkASSERT(data);
322 }
323}
324
325} // namespace GrBackendTextures
326
327
329public:
332 : fVkInfo(info) {
333 if (mutableState) {
334 fMutableState = std::move(mutableState);
335 } else {
336 fMutableState =
337 sk_make_sp<skgpu::MutableTextureState>(skgpu::MutableTextureStates::MakeVulkan(
339 }
340 }
341
342 const GrVkImageInfo& info() const { return fVkInfo; }
343
345 return fMutableState;
346 }
348 fMutableState->set(state);
349 }
350
351 skgpu::MutableTextureState* mutableState() { return fMutableState.get(); }
352 const skgpu::MutableTextureState* mutableState() const { return fMutableState.get(); }
353
354private:
355 GrBackendFormat getBackendFormat() const override {
356 auto info = GrVkImageInfoWithMutableState(fVkInfo, fMutableState.get());
360 }
362 }
363
364 bool isProtected() const override { return fVkInfo.fProtected == skgpu::Protected::kYes; }
365
366 bool equal(const GrBackendRenderTargetData* that) const override {
367 SkASSERT(!that || that->type() == GrBackendApi::kVulkan);
368 if (auto otherVk = static_cast<const GrVkBackendRenderTargetData*>(that)) {
369 // For our tests when checking equality we are assuming both objects will be using the
370 // same mutable state object.
371 if (fMutableState != otherVk->fMutableState) {
372 return false;
373 }
374 return GrVkImageInfoWithMutableState(fVkInfo, fMutableState.get()) ==
375 GrVkImageInfoWithMutableState(otherVk->fVkInfo, fMutableState.get());
376 }
377 return false;
378 }
379
380 void copyTo(AnyRenderTargetData& rtData) const override {
381 rtData.emplace<GrVkBackendRenderTargetData>(fVkInfo, fMutableState);
382 }
383
384#if defined(SK_DEBUG)
385 GrBackendApi type() const override { return GrBackendApi::kVulkan; }
386#endif
387
388 GrVkImageInfo fVkInfo;
390};
391
394 SkASSERT(!data || data->type() == GrBackendApi::kVulkan);
395 return static_cast<const GrVkBackendRenderTargetData*>(data);
396}
397
400 SkASSERT(!data || data->type() == GrBackendApi::kVulkan);
401 return static_cast<GrVkBackendRenderTargetData*>(data);
402}
403
404namespace GrBackendRenderTargets {
405
408 width,
409 height,
410 std::max(1U, vkInfo.fSampleCount),
411 /*stencilBits=*/0,
413 /*framebufferOnly=*/false,
415}
416
418 int height,
419 const GrVkImageInfo& vkInfo,
422 width,
423 height,
424 std::max(1U, vkInfo.fSampleCount),
425 /*stencilBits=*/0,
427 /*framebufferOnly=*/false,
429 std::move(mutableState)));
430}
431
433 if (!rt.isValid() || rt.backend() != GrBackendApi::kVulkan) {
434 return false;
435 }
437 SkASSERT(data);
438 *outInfo = GrVkImageInfoWithMutableState(data->info(), data->mutableState());
439 return true;
440}
441
443 if (rt && rt->isValid() && rt->backend() == GrBackendApi::kVulkan) {
446 }
447}
448
449} // namespace GrBackendRenderTargets
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
GrTextureType
Definition: GrTypesPriv.h:268
GrBackendApi
Definition: GrTypes.h:95
static const VkImageUsageFlags kDefaultTexRTUsageFlags
static GrVkImageInfo apply_default_usage_flags(const GrVkImageInfo &info, VkImageUsageFlags defaultFlags)
static const VkImageUsageFlags kDefaultRTUsageFlags
static const GrVkBackendFormatData * get_and_cast_data(const GrBackendFormat &format)
static GrTextureType vk_image_info_to_texture_type(const GrVkImageInfo &info)
static const VkImageUsageFlags kDefaultUsageFlags
GrVkImageInfo GrVkImageInfoWithMutableState(const GrVkImageInfo &info, const skgpu::MutableTextureState *mutableState)
static constexpr GrColorFormatDesc GrVkFormatDesc(VkFormat vkFormat)
Definition: GrVkUtil.h:57
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkTextureCompressionType
GLenum type
GrBackendFormat::AnyFormatData AnyFormatData
GrBackendRenderTarget::AnyRenderTargetData AnyRenderTargetData
GrBackendApi backend() const
static GrBackendRenderTarget MakeGrBackendRenderTarget(int width, int height, int sampleCnt, int stencilBits, GrBackendApi backend, bool framebufferOnly, const RenderTargetData &data)
static GrBackendTexture MakeGrBackendTexture(int width, int height, std::string_view label, skgpu::Mipmapped mipped, GrBackendApi backend, GrTextureType texture, const TextureData &data)
static GrBackendFormat MakeGrBackendFormat(GrTextureType textureType, GrBackendApi api, const FormatData &data)
static const GrBackendFormatData * GetBackendData(const GrBackendFormat &format)
GrBackendTexture::AnyTextureData AnyTextureData
bool isValid() const
GrBackendApi backend() const
const skgpu::VulkanYcbcrConversionInfo * getYcbcrConversionInfo() const
GrVkBackendFormatData(VkFormat format, const skgpu::VulkanYcbcrConversionInfo &ycbcrInfo)
VkFormat asVkFormat() const
sk_sp< skgpu::MutableTextureState > getMutableState() const override
void setMutableState(const skgpu::MutableTextureState &state) override
const GrVkImageInfo & info() const
GrVkBackendRenderTargetData(const GrVkImageInfo &info, sk_sp< skgpu::MutableTextureState > mutableState=nullptr)
const skgpu::MutableTextureState * mutableState() const
skgpu::MutableTextureState * mutableState()
GrVkBackendTextureData(const GrVkImageInfo &info, sk_sp< skgpu::MutableTextureState > mutableState=nullptr)
sk_sp< skgpu::MutableTextureState > getMutableState() const override
void setMutableState(const skgpu::MutableTextureState &state) override
const skgpu::MutableTextureState * mutableState() const
const GrVkImageInfo & info() const
skgpu::MutableTextureState * mutableState()
T * get() const
Definition: SkRefCnt.h:303
void set(const MutableTextureState &that)
AtkStateType state
uint32_t uint32_t * format
static float max(float r, float g, float b)
Definition: hsl.cpp:49
FlTexture * texture
SK_API const skgpu::VulkanYcbcrConversionInfo * GetVkYcbcrConversionInfo(const GrBackendFormat &)
SK_API bool AsVkFormat(const GrBackendFormat &, VkFormat *)
SK_API GrBackendFormat MakeVk(VkFormat format, bool willUseDRMFormatModifiers=false)
SK_API bool GetVkImageInfo(const GrBackendRenderTarget &, GrVkImageInfo *)
SK_API void SetVkImageLayout(GrBackendRenderTarget *, VkImageLayout)
SK_API GrBackendRenderTarget MakeVk(int width, int height, const GrVkImageInfo &)
SK_API void SetVkImageLayout(GrBackendTexture *, VkImageLayout)
SK_API GrBackendTexture MakeVk(int width, int height, const GrVkImageInfo &, std::string_view label={})
SK_API bool GetVkImageInfo(const GrBackendTexture &, GrVkImageInfo *)
void SetVkImageLayout(MutableTextureState *state, VkImageLayout layout)
SK_API MutableTextureState MakeVulkan(VkImageLayout layout, uint32_t queueFamilyIndex)
static constexpr const char * VkFormatToStr(VkFormat vkFormat)
static constexpr int VkFormatStencilBits(VkFormat format)
static constexpr size_t VkFormatBytesPerBlock(VkFormat vkFormat)
static constexpr uint32_t VkFormatChannels(VkFormat vkFormat)
Mipmapped
Definition: GpuTypes.h:53
int32_t height
int32_t width
VkImage fImage
Definition: GrVkTypes.h:26
uint32_t fCurrentQueueFamily
Definition: GrVkTypes.h:34
VkFormat fFormat
Definition: GrVkTypes.h:30
uint32_t fSampleCount
Definition: GrVkTypes.h:32
skgpu::Protected fProtected
Definition: GrVkTypes.h:35
uint32_t fLevelCount
Definition: GrVkTypes.h:33
skgpu::VulkanYcbcrConversionInfo fYcbcrConversionInfo
Definition: GrVkTypes.h:36
VkImageLayout fImageLayout
Definition: GrVkTypes.h:29
VkImageUsageFlags fImageUsageFlags
Definition: GrVkTypes.h:31
VkImageTiling fImageTiling
Definition: GrVkTypes.h:28
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
VkImageLayout
Definition: vulkan_core.h:1330
VkFlags VkImageUsageFlags
Definition: vulkan_core.h:2382
@ VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT
Definition: vulkan_core.h:1769
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
Definition: vulkan_core.h:2353
@ VK_IMAGE_USAGE_SAMPLED_BIT
Definition: vulkan_core.h:2354
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
Definition: vulkan_core.h:2356
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
Definition: vulkan_core.h:2352
VkFormat
Definition: vulkan_core.h:1458
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan_core.h:1496
@ VK_FORMAT_BC1_RGB_UNORM_BLOCK
Definition: vulkan_core.h:1590
@ VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK
Definition: vulkan_core.h:1606
@ VK_FORMAT_BC1_RGBA_UNORM_BLOCK
Definition: vulkan_core.h:1592