Flutter Engine
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 "flutter/shell/common/persistent_cache.h"
8 #include "flutter/vulkan/vulkan_utilities.h"
9 
10 namespace flutter {
11 namespace testing {
12 
14  PlatformView::Delegate& delegate,
15  TaskRunners task_runners,
16  std::shared_ptr<ShellTestVsyncClock> vsync_clock,
17  CreateVsyncWaiter create_vsync_waiter,
18  std::shared_ptr<ShellTestExternalViewEmbedder>
19  shell_test_external_view_embedder)
20  : ShellTestPlatformView(delegate, std::move(task_runners)),
21  create_vsync_waiter_(std::move(create_vsync_waiter)),
22  vsync_clock_(vsync_clock),
23  proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()),
24  shell_test_external_view_embedder_(shell_test_external_view_embedder) {}
25 
27 
28 std::unique_ptr<VsyncWaiter> ShellTestPlatformViewVulkan::CreateVSyncWaiter() {
29  return create_vsync_waiter_();
30 }
31 
33  vsync_clock_->SimulateVSync();
34 }
35 
36 // |PlatformView|
37 std::unique_ptr<Surface> ShellTestPlatformViewVulkan::CreateRenderingSurface() {
38  return std::make_unique<OffScreenSurface>(proc_table_,
39  shell_test_external_view_embedder_);
40 }
41 
42 // |PlatformView|
43 PointerDataDispatcherMaker ShellTestPlatformViewVulkan::GetDispatcherMaker() {
44  return [](DefaultPointerDataDispatcher::Delegate& delegate) {
45  return std::make_unique<SmoothPointerDataDispatcher>(delegate);
46  };
47 }
48 
49 // TODO(gw280): This code was forked from vulkan_window.cc specifically for
50 // shell_test.
51 // We need to merge this functionality back into //vulkan.
52 // https://github.com/flutter/flutter/issues/51132
53 ShellTestPlatformViewVulkan::OffScreenSurface::OffScreenSurface(
55  std::shared_ptr<ShellTestExternalViewEmbedder>
56  shell_test_external_view_embedder)
57  : valid_(false),
58  vk_(std::move(vk)),
59  shell_test_external_view_embedder_(shell_test_external_view_embedder) {
60  if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) {
61  FML_DLOG(ERROR) << "Proc table has not acquired mandatory proc addresses.";
62  return;
63  }
64 
65  // Create the application instance.
66  std::vector<std::string> extensions = {
67  VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME,
68  };
69 
70  application_ = std::make_unique<vulkan::VulkanApplication>(
71  *vk_, "FlutterTest", std::move(extensions), VK_MAKE_VERSION(1, 0, 0),
72  VK_MAKE_VERSION(1, 1, 0), true);
73 
74  if (!application_->IsValid() || !vk_->AreInstanceProcsSetup()) {
75  // Make certain the application instance was created and it setup the
76  // instance proc table entries.
77  FML_DLOG(ERROR) << "Instance proc addresses have not been setup.";
78  return;
79  }
80 
81  // Create the device.
82 
83  logical_device_ = application_->AcquireFirstCompatibleLogicalDevice();
84 
85  if (logical_device_ == nullptr || !logical_device_->IsValid() ||
86  !vk_->AreDeviceProcsSetup()) {
87  // Make certain the device was created and it setup the device proc table
88  // entries.
89  FML_DLOG(ERROR) << "Device proc addresses have not been setup.";
90  return;
91  }
92 
93  // Create the Skia GrContext.
94  if (!CreateSkiaGrContext()) {
95  FML_DLOG(ERROR) << "Could not create Skia context.";
96  return;
97  }
98 
99  valid_ = true;
100 }
101 
102 bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaGrContext() {
103  GrVkBackendContext backend_context;
104 
105  if (!CreateSkiaBackendContext(&backend_context)) {
106  FML_DLOG(ERROR) << "Could not create Skia backend context.";
107  return false;
108  }
109 
110  GrContextOptions options;
112  options.fShaderCacheStrategy = GrContextOptions::ShaderCacheStrategy::kSkSL;
113  }
115  options.fPersistentCache = PersistentCache::GetCacheForProcess();
116 
117  sk_sp<GrDirectContext> context =
118  GrDirectContext::MakeVulkan(backend_context, options);
119 
120  if (context == nullptr) {
121  FML_DLOG(ERROR) << "Failed to create GrDirectContext";
122  return false;
123  }
124 
125  context->setResourceCacheLimits(vulkan::kGrCacheMaxCount,
127 
128  context_ = context;
129 
130  return true;
131 }
132 
133 bool ShellTestPlatformViewVulkan::OffScreenSurface::CreateSkiaBackendContext(
134  GrVkBackendContext* context) {
135  auto getProc = vk_->CreateSkiaGetProc();
136 
137  if (getProc == nullptr) {
138  FML_DLOG(ERROR) << "GetProcAddress is null";
139  return false;
140  }
141 
142  uint32_t skia_features = 0;
143  if (!logical_device_->GetPhysicalDeviceFeaturesSkia(&skia_features)) {
144  FML_DLOG(ERROR) << "Failed to get Physical Device features";
145  return false;
146  }
147 
148  context->fInstance = application_->GetInstance();
149  context->fPhysicalDevice = logical_device_->GetPhysicalDeviceHandle();
150  context->fDevice = logical_device_->GetHandle();
151  context->fQueue = logical_device_->GetQueueHandle();
152  context->fGraphicsQueueIndex = logical_device_->GetGraphicsQueueIndex();
153  context->fMinAPIVersion = application_->GetAPIVersion();
154  context->fMaxAPIVersion = application_->GetAPIVersion();
155  context->fFeatures = skia_features;
156  context->fGetProc = std::move(getProc);
157  context->fOwnsInstanceAndDevice = false;
158  return true;
159 }
160 
161 ShellTestPlatformViewVulkan::OffScreenSurface::~OffScreenSurface() {}
162 
163 bool ShellTestPlatformViewVulkan::OffScreenSurface::IsValid() {
164  return valid_;
165 }
166 
167 std::unique_ptr<SurfaceFrame>
168 ShellTestPlatformViewVulkan::OffScreenSurface::AcquireFrame(
169  const SkISize& size) {
170  auto image_info = SkImageInfo::Make(size, SkColorType::kRGBA_8888_SkColorType,
171  SkAlphaType::kOpaque_SkAlphaType);
172  auto surface = SkSurface::MakeRenderTarget(context_.get(), SkBudgeted::kNo,
173  image_info, 0, nullptr);
174  SurfaceFrame::SubmitCallback callback = [](const SurfaceFrame&,
175  SkCanvas* canvas) -> bool {
176  canvas->flush();
177  return true;
178  };
179 
180  return std::make_unique<SurfaceFrame>(std::move(surface), true,
181  std::move(callback));
182 }
183 
184 GrDirectContext* ShellTestPlatformViewVulkan::OffScreenSurface::GetContext() {
185  return context_.get();
186 }
187 
188 SkMatrix ShellTestPlatformViewVulkan::OffScreenSurface::GetRootTransformation()
189  const {
190  SkMatrix matrix;
191  matrix.reset();
192  return matrix;
193 }
194 
196 ShellTestPlatformViewVulkan::OffScreenSurface::GetExternalViewEmbedder() {
197  return shell_test_external_view_embedder_.get();
198 }
199 
200 } // namespace testing
201 } // namespace flutter
std::function< bool(const SurfaceFrame &surface_frame, SkCanvas *canvas)> SubmitCallback
Definition: surface_frame.h:22
static const size_t kGrCacheMaxByteSize
ShellTestPlatformViewVulkan(PlatformView::Delegate &delegate, TaskRunners task_runners, std::shared_ptr< ShellTestVsyncClock > vsync_clock, CreateVsyncWaiter create_vsync_waiter, std::shared_ptr< ShellTestExternalViewEmbedder > shell_test_external_view_embedder)
Definition: ref_ptr.h:252
The interface for Engine to implement.
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
std::function< std::unique_ptr< PointerDataDispatcher >(PointerDataDispatcher::Delegate &)> PointerDataDispatcherMaker
Signature for constructing PointerDataDispatcher.
Definition: ascii_trie.cc:9
std::function< std::unique_ptr< VsyncWaiter >()> CreateVsyncWaiter
RefPtr< T > MakeRefCounted(Args &&... args)
Definition: ref_ptr.h:244
static PersistentCache * GetCacheForProcess()
static const int kGrCacheMaxCount
#define FML_DLOG(severity)
Definition: logging.h:85
Used to forward events from the platform view to interested subsystems. This forwarding is done by th...
Definition: platform_view.h:51