Flutter Engine
 
Loading...
Searching...
No Matches
embedder_test_backingstore_producer_vulkan.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8#include "third_party/skia/include/core/SkColorSpace.h"
9#include "third_party/skia/include/gpu/ganesh/GrBackendSurface.h"
10#include "third_party/skia/include/gpu/ganesh/SkSurfaceGanesh.h"
11#include "third_party/skia/include/gpu/ganesh/vk/GrVkBackendSurface.h"
12#include "third_party/skia/include/gpu/ganesh/vk/GrVkTypes.h"
13
14namespace flutter::testing {
15
16namespace {
17struct UserData {
18 sk_sp<SkSurface> surface;
20};
21} // namespace
22
24 sk_sp<GrDirectContext> context,
26 : EmbedderTestBackingStoreProducer(std::move(context), type),
27 test_vulkan_context_(nullptr) {}
28
31
33 const FlutterBackingStoreConfig* config,
34 FlutterBackingStore* backing_store_out) {
35 if (!test_vulkan_context_) {
36 test_vulkan_context_ = fml::MakeRefCounted<TestVulkanContext>();
37 }
38
39 auto surface_size = DlISize(config->size.width, config->size.height);
40 auto optional_image = test_vulkan_context_->CreateImage(surface_size);
41 if (!optional_image.has_value()) {
42 FML_LOG(ERROR) << "Could not create Vulkan image.";
43 return false;
44 }
45 TestVulkanImage* test_image = new TestVulkanImage(std::move(*optional_image));
46
47 GrVkImageInfo image_info = {
48 .fImage = test_image->GetImage(),
49 .fImageTiling = VK_IMAGE_TILING_OPTIMAL,
50 .fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED,
51 .fFormat = VK_FORMAT_R8G8B8A8_UNORM,
52 .fImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
53 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
54 VK_IMAGE_USAGE_TRANSFER_DST_BIT |
55 VK_IMAGE_USAGE_SAMPLED_BIT,
56 .fSampleCount = 1,
57 .fLevelCount = 1,
58 };
59 auto backend_texture = GrBackendTextures::MakeVk(
60 surface_size.width, surface_size.height, image_info);
61
62 SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);
63
64 SkSurfaces::TextureReleaseProc release_vktexture = [](void* user_data) {
65 delete reinterpret_cast<TestVulkanImage*>(user_data);
66 };
67
68 sk_sp<SkSurface> surface = SkSurfaces::WrapBackendTexture(
69 context_.get(), // context
70 backend_texture, // back-end texture
71 kTopLeft_GrSurfaceOrigin, // surface origin
72 1, // sample count
73 kRGBA_8888_SkColorType, // color type
74 SkColorSpace::MakeSRGB(), // color space
75 &surface_properties, // surface properties
76 release_vktexture, // texture release proc
77 test_image // release context
78 );
79
80 if (!surface) {
81 FML_LOG(ERROR) << "Could not create Skia surface from Vulkan image.";
82 return false;
83 }
84 backing_store_out->type = kFlutterBackingStoreTypeVulkan;
85
87 image->image = reinterpret_cast<uint64_t>(image_info.fImage);
88 image->format = VK_FORMAT_R8G8B8A8_UNORM;
89 backing_store_out->vulkan.image = image;
90
91 // Collect all allocated resources in the destruction_callback.
92 {
93 auto user_data = new UserData{.surface = surface, .image = image};
94 backing_store_out->user_data = user_data;
95 backing_store_out->vulkan.user_data = user_data;
96 backing_store_out->vulkan.destruction_callback = [](void* user_data) {
97 UserData* d = reinterpret_cast<UserData*>(user_data);
98 delete d->image;
99 delete d;
100 };
101 }
102
103 return true;
104}
105
107 const FlutterBackingStore* backing_store) const {
108 UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
109 return user_data->surface;
110}
111
113 const FlutterBackingStore* backing_store) const {
114 UserData* user_data = reinterpret_cast<UserData*>(backing_store->user_data);
115 return user_data->surface->makeImageSnapshot();
116}
117
118} // namespace flutter::testing
GLenum type
EmbedderTestBackingStoreProducerVulkan(sk_sp< GrDirectContext > context, RenderTargetType type)
sk_sp< SkSurface > GetSurface(const FlutterBackingStore *backing_store) const override
sk_sp< SkImage > MakeImageSnapshot(const FlutterBackingStore *backing_store) const override
bool Create(const FlutterBackingStoreConfig *config, FlutterBackingStore *backing_store_out) override
Captures the lifetime of a test VkImage along with its bound memory.
@ kFlutterBackingStoreTypeVulkan
Specifies a Vulkan backing store. This is backed by a Vulkan VkImage.
Definition embedder.h:2057
FlutterVulkanImage * image
auto & d
Definition main.cc:28
VkSurfaceKHR surface
Definition main.cc:65
#define FML_LOG(severity)
Definition logging.h:101
impeller::ISize32 DlISize
Definition ref_ptr.h:261
FlutterSize size
The size of the render target the engine expects to render into.
Definition embedder.h:2093
FlutterVulkanBackingStore vulkan
Definition embedder.h:2085
FlutterBackingStoreType type
Specifies the type of backing store.
Definition embedder.h:2071
double height
Definition embedder.h:629
double width
Definition embedder.h:628
VoidCallback destruction_callback
Definition embedder.h:1994
const FlutterVulkanImage * image
Definition embedder.h:1987
FlutterVulkanImageHandle image
Definition embedder.h:931
uint32_t format
The VkFormat of the image (for example: VK_FORMAT_R8G8B8A8_UNORM).
Definition embedder.h:933