Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
shell_test_platform_view_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
5#include "flutter/shell/common/shell_test_platform_view_vulkan.h"
6
7#include <utility>
8
9#include "flutter/common/graphics/persistent_cache.h"
10#include "flutter/flutter_vma/flutter_skia_vma.h"
11#include "flutter/shell/common/context_options.h"
12#include "flutter/vulkan/vulkan_skia_proc_table.h"
13#include "flutter/vulkan/vulkan_utilities.h"
14
18
19#if OS_FUCHSIA
20#define VULKAN_SO_PATH "libvulkan.so"
21#else
22#include "flutter/vulkan/swiftshader_path.h"
23#endif
24
25namespace flutter {
26namespace testing {
27
29 PlatformView::Delegate& delegate,
30 const TaskRunners& task_runners,
31 std::shared_ptr<ShellTestVsyncClock> vsync_clock,
32 CreateVsyncWaiter create_vsync_waiter,
33 std::shared_ptr<ShellTestExternalViewEmbedder>
34 shell_test_external_view_embedder)
35 : ShellTestPlatformView(delegate, task_runners),
36 create_vsync_waiter_(std::move(create_vsync_waiter)),
37 vsync_clock_(std::move(vsync_clock)),
38 proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>(VULKAN_SO_PATH)),
39 shell_test_external_view_embedder_(
40 std::move(shell_test_external_view_embedder)) {}
41
43
44std::unique_ptr<VsyncWaiter> ShellTestPlatformViewVulkan::CreateVSyncWaiter() {
45 return create_vsync_waiter_();
46}
47
49 vsync_clock_->SimulateVSync();
50}
51
52// |PlatformView|
54 return std::make_unique<OffScreenSurface>(proc_table_,
55 shell_test_external_view_embedder_);
56}
57
58// |PlatformView|
59std::shared_ptr<ExternalViewEmbedder>
61 return shell_test_external_view_embedder_;
62}
63
64// |PlatformView|
66 return [](DefaultPointerDataDispatcher::Delegate& delegate) {
67 return std::make_unique<SmoothPointerDataDispatcher>(delegate);
68 };
69}
70
71// TODO(gw280): This code was forked from vulkan_window.cc specifically for
72// shell_test.
73// We need to merge this functionality back into //vulkan.
74// https://github.com/flutter/flutter/issues/51132
75ShellTestPlatformViewVulkan::OffScreenSurface::OffScreenSurface(
77 std::shared_ptr<ShellTestExternalViewEmbedder>
78 shell_test_external_view_embedder)
79 : vk_(std::move(vk)),
80 shell_test_external_view_embedder_(
81 std::move(shell_test_external_view_embedder)) {
82 if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) {
83 FML_DLOG(ERROR) << "Proc table has not acquired mandatory proc addresses.";
84 return;
85 }
86
87 // Create the application instance.
88 std::vector<std::string> extensions = {
90 };
91
92 application_ = std::make_unique<vulkan::VulkanApplication>(
93 *vk_, "FlutterTest", std::move(extensions), VK_MAKE_VERSION(1, 0, 0),
94 VK_MAKE_VERSION(1, 1, 0), true);
95
96 if (!application_->IsValid() || !vk_->AreInstanceProcsSetup()) {
97 // Make certain the application instance was created and it set up the
98 // instance proc table entries.
99 FML_DLOG(ERROR) << "Instance proc addresses have not been set up.";
100 return;
101 }
102
103 // Create the device.
104
105 logical_device_ = application_->AcquireFirstCompatibleLogicalDevice();
106
107 if (logical_device_ == nullptr || !logical_device_->IsValid() ||
108 !vk_->AreDeviceProcsSetup()) {
109 // Make certain the device was created and it set up the device proc table
110 // entries.
111 FML_DLOG(ERROR) << "Device proc addresses have not been set up.";
112 return;
113 }
114
115 memory_allocator_ = FlutterSkiaVulkanMemoryAllocator::Make(
116 application_->GetAPIVersion(), application_->GetInstance(),
117 logical_device_->GetPhysicalDeviceHandle(), logical_device_->GetHandle(),
118 vk_, true);
119
120 // Create the Skia GrContext.
121 if (!CreateSkiaGrContext()) {
122 FML_DLOG(ERROR) << "Could not create Skia context.";
123 return;
124 }
125
126 valid_ = true;
127}
128
129bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaGrContext() {
130 GrVkBackendContext backend_context;
131
132 if (!CreateSkiaBackendContext(&backend_context)) {
133 FML_DLOG(ERROR) << "Could not create Skia backend context.";
134 return false;
135 }
136
137 const auto options =
139
140 sk_sp<GrDirectContext> context =
141 GrDirectContexts::MakeVulkan(backend_context, options);
142
143 if (context == nullptr) {
144 FML_DLOG(ERROR) << "Failed to create GrDirectContext";
145 return false;
146 }
147
148 context->setResourceCacheLimit(vulkan::kGrCacheMaxByteSize);
149
150 context_ = context;
151
152 return true;
153}
154
155bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaBackendContext(
156 GrVkBackendContext* context) {
157 auto getProc = CreateSkiaGetProc(vk_);
158
159 if (getProc == nullptr) {
160 FML_DLOG(ERROR) << "GetProcAddress is null";
161 return false;
162 }
163
164 uint32_t skia_features = 0;
165 if (!logical_device_->GetPhysicalDeviceFeaturesSkia(&skia_features)) {
166 FML_DLOG(ERROR) << "Failed to get Physical Device features";
167 return false;
168 }
169
170 context->fInstance = application_->GetInstance();
171 context->fPhysicalDevice = logical_device_->GetPhysicalDeviceHandle();
172 context->fDevice = logical_device_->GetHandle();
173 context->fQueue = logical_device_->GetQueueHandle();
174 context->fGraphicsQueueIndex = logical_device_->GetGraphicsQueueIndex();
175 context->fMinAPIVersion = application_->GetAPIVersion();
176 context->fMaxAPIVersion = application_->GetAPIVersion();
177 context->fFeatures = skia_features;
178 context->fGetProc = std::move(getProc);
179 context->fOwnsInstanceAndDevice = false;
180 context->fMemoryAllocator = memory_allocator_;
181
182 return true;
183}
184
185ShellTestPlatformViewVulkan::OffScreenSurface::~OffScreenSurface() {}
186
187bool ShellTestPlatformViewVulkan::OffScreenSurface::IsValid() {
188 return valid_;
189}
190
191std::unique_ptr<SurfaceFrame>
192ShellTestPlatformViewVulkan::OffScreenSurface::AcquireFrame(
193 const SkISize& size) {
197 image_info, 0, nullptr);
198 SurfaceFrame::SubmitCallback callback = [](const SurfaceFrame&,
199 DlCanvas* canvas) -> bool {
200 canvas->Flush();
201 return true;
202 };
203
204 SurfaceFrame::FramebufferInfo framebuffer_info;
205 framebuffer_info.supports_readback = true;
206
207 return std::make_unique<SurfaceFrame>(std::move(surface), framebuffer_info,
208 std::move(callback),
209 /*frame_size=*/SkISize::Make(800, 600));
210}
211
212GrDirectContext* ShellTestPlatformViewVulkan::OffScreenSurface::GetContext() {
213 return context_.get();
214}
215
216SkMatrix ShellTestPlatformViewVulkan::OffScreenSurface::GetRootTransformation()
217 const {
219 matrix.reset();
220 return matrix;
221}
222
223} // namespace testing
224} // namespace flutter
const char * options
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
static sk_sp< VulkanMemoryAllocator > Make(uint32_t vulkan_api_version, VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device, const fml::RefPtr< vulkan::VulkanProcTable > &vk, bool mustUseCoherentHostVisibleMemory)
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
The interface for Engine to implement.
std::function< bool(SurfaceFrame &surface_frame, DlCanvas *canvas)> SubmitCallback
PointerDataDispatcherMaker GetDispatcherMaker() override
Returns a platform-specific PointerDataDispatcherMaker so the Engine can construct the PointerDataPac...
ShellTestPlatformViewVulkan(PlatformView::Delegate &delegate, const TaskRunners &task_runners, std::shared_ptr< ShellTestVsyncClock > vsync_clock, CreateVsyncWaiter create_vsync_waiter, std::shared_ptr< ShellTestExternalViewEmbedder > shell_test_external_view_embedder)
std::unique_ptr< VsyncWaiter > CreateVSyncWaiter() override
Invoked by the shell to obtain a platform specific vsync waiter. It is optional for platforms to over...
std::shared_ptr< ExternalViewEmbedder > CreateExternalViewEmbedder() override
std::unique_ptr< Surface > CreateRenderingSurface() override
VkSurfaceKHR surface
Definition main.cc:49
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DLOG(severity)
Definition logging.h:102
SK_API sk_sp< GrDirectContext > MakeVulkan(const GrVkBackendContext &, const GrContextOptions &)
unsigned useCenter Optional< SkMatrix > matrix
Definition SkRecords.h:258
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
std::function< std::unique_ptr< VsyncWaiter >()> CreateVsyncWaiter
GrContextOptions MakeDefaultContextOptions(ContextType type, std::optional< GrBackendApi > api)
Initializes GrContextOptions with values suitable for Flutter. The options can be further tweaked bef...
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
Definition ref_ptr.h:256
static const size_t kGrCacheMaxByteSize
GrVkGetProc CreateSkiaGetProc(const fml::RefPtr< vulkan::VulkanProcTable > &vk)
VkPhysicalDevice fPhysicalDevice
skgpu::VulkanGetProc fGetProc
sk_sp< skgpu::VulkanMemoryAllocator > fMemoryAllocator
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
#define VULKAN_SO_PATH
#define ERROR(message)
#define VK_MAKE_VERSION(major, minor, patch)
Definition vulkan_core.h:78
#define VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME