forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulkan_window.cc
267 lines (209 loc) · 7.81 KB
/
vulkan_window.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// 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.
// FLUTTER_NOLINT: https://github.com/flutter/flutter/issues/68331
#include "vulkan_window.h"
#include <memory>
#include <string>
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "vulkan_application.h"
#include "vulkan_device.h"
#include "vulkan_native_surface.h"
#include "vulkan_surface.h"
#include "vulkan_swapchain.h"
namespace vulkan {
VulkanWindow::VulkanWindow(fml::RefPtr<VulkanProcTable> proc_table,
std::unique_ptr<VulkanNativeSurface> native_surface)
: VulkanWindow(/*context/*/ nullptr,
proc_table,
std::move(native_surface)) {}
VulkanWindow::VulkanWindow(const sk_sp<GrDirectContext>& context,
fml::RefPtr<VulkanProcTable> proc_table,
std::unique_ptr<VulkanNativeSurface> native_surface)
: valid_(false), vk(std::move(proc_table)), skia_gr_context_(context) {
if (!vk || !vk->HasAcquiredMandatoryProcAddresses()) {
FML_DLOG(INFO) << "Proc table has not acquired mandatory proc addresses.";
return;
}
if (native_surface && !native_surface->IsValid()) {
FML_DLOG(INFO) << "Native surface is invalid.";
return;
}
// Create the application instance.
std::vector<std::string> extensions = {
VK_KHR_SURFACE_EXTENSION_NAME, // parent extension
native_surface->GetExtensionName() // child extension
};
application_ = std::make_unique<VulkanApplication>(*vk, "Flutter",
std::move(extensions));
if (!application_->IsValid() || !vk->AreInstanceProcsSetup()) {
// Make certain the application instance was created and it set up the
// instance proc table entries.
FML_DLOG(INFO) << "Instance proc addresses have not been set up.";
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 set up the device proc table
// entries.
FML_DLOG(INFO) << "Device proc addresses have not been set up.";
return;
}
if (!native_surface) {
return;
}
// Create the logical surface from the native platform surface.
surface_ = std::make_unique<VulkanSurface>(*vk, *application_,
std::move(native_surface));
if (!surface_->IsValid()) {
FML_DLOG(INFO) << "Vulkan surface is invalid.";
return;
}
// Create the Skia GrDirectContext.
if (!skia_gr_context_ && !CreateSkiaGrContext()) {
FML_DLOG(INFO) << "Could not create Skia context.";
return;
}
// Create the swapchain.
if (!RecreateSwapchain()) {
FML_DLOG(INFO) << "Could not set up the swapchain initially.";
return;
}
valid_ = true;
}
VulkanWindow::~VulkanWindow() = default;
bool VulkanWindow::IsValid() const {
return valid_;
}
GrDirectContext* VulkanWindow::GetSkiaGrContext() {
return skia_gr_context_.get();
}
bool VulkanWindow::CreateSkiaGrContext() {
GrVkBackendContext backend_context;
if (!CreateSkiaBackendContext(&backend_context)) {
return false;
}
GrContextOptions options;
options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
sk_sp<GrDirectContext> context =
GrDirectContext::MakeVulkan(backend_context, options);
if (context == nullptr) {
return false;
}
context->setResourceCacheLimits(kGrCacheMaxCount, kGrCacheMaxByteSize);
skia_gr_context_ = context;
return true;
}
bool VulkanWindow::CreateSkiaBackendContext(GrVkBackendContext* context) {
auto getProc = vk->CreateSkiaGetProc();
if (getProc == nullptr) {
return false;
}
uint32_t skia_features = 0;
if (!logical_device_->GetPhysicalDeviceFeaturesSkia(&skia_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->fExtensions = kKHR_surface_GrVkExtensionFlag |
kKHR_swapchain_GrVkExtensionFlag |
surface_->GetNativeSurface().GetSkiaExtensionName();
context->fFeatures = skia_features;
context->fGetProc = std::move(getProc);
context->fOwnsInstanceAndDevice = false;
return true;
}
sk_sp<SkSurface> VulkanWindow::AcquireSurface() {
if (!IsValid()) {
FML_DLOG(INFO) << "Surface is invalid.";
return nullptr;
}
auto surface_size = surface_->GetSize();
// This check is theoretically unnecessary as the swapchain should report that
// the surface is out-of-date and perform swapchain recreation at the new
// configuration. However, on Android, the swapchain never reports that it is
// of date. Hence this extra check. Platforms that don't have this issue, or,
// cant report this information (which is optional anyway), report a zero
// size.
if (surface_size != SkISize::Make(0, 0) &&
surface_size != swapchain_->GetSize()) {
FML_DLOG(INFO) << "Swapchain and surface sizes are out of sync. Recreating "
"swapchain.";
if (!RecreateSwapchain()) {
FML_DLOG(INFO) << "Could not recreate swapchain.";
valid_ = false;
return nullptr;
}
}
while (true) {
sk_sp<SkSurface> surface;
auto acquire_result = VulkanSwapchain::AcquireStatus::ErrorSurfaceLost;
std::tie(acquire_result, surface) = swapchain_->AcquireSurface();
if (acquire_result == VulkanSwapchain::AcquireStatus::Success) {
// Successfully acquired a surface from the swapchain. Nothing more to do.
return surface;
}
if (acquire_result == VulkanSwapchain::AcquireStatus::ErrorSurfaceLost) {
// Surface is lost. This is an unrecoverable error.
FML_DLOG(INFO) << "Swapchain reported surface was lost.";
return nullptr;
}
if (acquire_result ==
VulkanSwapchain::AcquireStatus::ErrorSurfaceOutOfDate) {
// Surface out of date. Recreate the swapchain at the new configuration.
if (RecreateSwapchain()) {
// Swapchain was recreated, try surface acquisition again.
continue;
} else {
// Could not recreate the swapchain at the new configuration.
FML_DLOG(INFO) << "Swapchain reported surface was out of date but "
"could not recreate the swapchain at the new "
"configuration.";
valid_ = false;
return nullptr;
}
}
break;
}
FML_DCHECK(false) << "Unhandled VulkanSwapchain::AcquireResult";
return nullptr;
}
bool VulkanWindow::SwapBuffers() {
if (!IsValid()) {
FML_DLOG(INFO) << "Window was invalid.";
return false;
}
return swapchain_->Submit();
}
bool VulkanWindow::RecreateSwapchain() {
// This way, we always lose our reference to the old swapchain. Even if we
// cannot create a new one to replace it.
auto old_swapchain = std::move(swapchain_);
if (!vk->IsValid()) {
return false;
}
if (logical_device_ == nullptr || !logical_device_->IsValid()) {
return false;
}
if (surface_ == nullptr || !surface_->IsValid()) {
return false;
}
if (skia_gr_context_ == nullptr) {
return false;
}
auto swapchain = std::make_unique<VulkanSwapchain>(
*vk, *logical_device_, *surface_, skia_gr_context_.get(),
std::move(old_swapchain), logical_device_->GetGraphicsQueueIndex());
if (!swapchain->IsValid()) {
return false;
}
swapchain_ = std::move(swapchain);
return true;
}
} // namespace vulkan