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