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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
// Copyright (c) 2016 The Chromium 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 "gpu/vulkan/vulkan_swap_chain.h"
#include "base/bind.h"
#include "gpu/vulkan/vulkan_command_buffer.h"
#include "gpu/vulkan/vulkan_command_pool.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "gpu/vulkan/vulkan_function_pointers.h"
namespace gpu {
namespace {
VkSemaphore CreateSemaphore(VkDevice vk_device) {
// Generic semaphore creation structure.
VkSemaphoreCreateInfo semaphore_create_info = {
VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
VkSemaphore vk_semaphore;
auto result = vkCreateSemaphore(vk_device, &semaphore_create_info, nullptr,
&vk_semaphore);
DLOG_IF(FATAL, VK_SUCCESS != result)
<< "vkCreateSemaphore() failed: " << result;
return vk_semaphore;
}
} // namespace
VulkanSwapChain::VulkanSwapChain() {}
VulkanSwapChain::~VulkanSwapChain() {
DCHECK(images_.empty());
DCHECK_EQ(static_cast<VkSwapchainKHR>(VK_NULL_HANDLE), swap_chain_);
}
bool VulkanSwapChain::Initialize(
VulkanDeviceQueue* device_queue,
VkSurfaceKHR surface,
const VkSurfaceFormatKHR& surface_format,
const gfx::Size& image_size,
uint32_t min_image_count,
VkSurfaceTransformFlagBitsKHR pre_transform,
bool use_protected_memory,
std::unique_ptr<VulkanSwapChain> old_swap_chain) {
DCHECK(device_queue);
DCHECK(!use_protected_memory || device_queue->allow_protected_memory());
use_protected_memory_ = use_protected_memory;
device_queue_ = device_queue;
is_incremental_present_supported_ =
gfx::HasExtension(device_queue_->enabled_extensions(),
VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME);
device_queue_->GetFenceHelper()->ProcessCleanupTasks();
return InitializeSwapChain(surface, surface_format, image_size,
min_image_count, pre_transform,
use_protected_memory, std::move(old_swap_chain)) &&
InitializeSwapImages(surface_format);
}
void VulkanSwapChain::Destroy() {
DCHECK(!is_writing_);
DestroySwapImages();
DestroySwapChain();
}
gfx::SwapResult VulkanSwapChain::PresentBuffer(const gfx::Rect& rect) {
DCHECK(acquired_image_);
DCHECK(end_write_semaphore_ != VK_NULL_HANDLE);
VkResult result = VK_SUCCESS;
VkDevice device = device_queue_->GetVulkanDevice();
VkQueue queue = device_queue_->GetVulkanQueue();
auto* fence_helper = device_queue_->GetFenceHelper();
auto& current_image_data = images_[*acquired_image_];
if (current_image_data.layout != VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
{
current_image_data.command_buffer->Clear();
ScopedSingleUseCommandBufferRecorder recorder(
*current_image_data.command_buffer);
current_image_data.command_buffer->TransitionImageLayout(
current_image_data.image, current_image_data.layout,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
}
current_image_data.layout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkSemaphore vk_semaphore = CreateSemaphore(device);
// Submit our command_buffer for the current buffer. It sets the image
// layout for presenting.
if (!current_image_data.command_buffer->Submit(1, &end_write_semaphore_, 1,
&vk_semaphore)) {
vkDestroySemaphore(device, vk_semaphore, nullptr /* pAllocator */);
return gfx::SwapResult::SWAP_FAILED;
}
current_image_data.layout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
fence_helper->EnqueueSemaphoreCleanupForSubmittedWork(end_write_semaphore_);
end_write_semaphore_ = vk_semaphore;
}
VkPresentInfoKHR present_info = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR};
present_info.waitSemaphoreCount = 1;
present_info.pWaitSemaphores = &end_write_semaphore_;
present_info.swapchainCount = 1;
present_info.pSwapchains = &swap_chain_;
present_info.pImageIndices = &acquired_image_.value();
VkRectLayerKHR rect_layer;
VkPresentRegionKHR present_region;
VkPresentRegionsKHR present_regions = {VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR};
if (is_incremental_present_supported_) {
rect_layer.offset = {rect.x(), rect.y()};
rect_layer.extent = {uint32_t(rect.width()), uint32_t(rect.height())};
rect_layer.layer = 0;
present_region.rectangleCount = 1;
present_region.pRectangles = &rect_layer;
present_regions.swapchainCount = 1;
present_regions.pRegions = &present_region;
present_info.pNext = &present_regions;
}
result = vkQueuePresentKHR(queue, &present_info);
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
DLOG(ERROR) << "vkQueuePresentKHR() failed: " << result;
return gfx::SwapResult::SWAP_FAILED;
}
DLOG_IF(ERROR, result == VK_SUBOPTIMAL_KHR) << "Swapchian is suboptimal.";
if (current_image_data.present_begin_semaphore != VK_NULL_HANDLE) {
// |present_begin_semaphore| for the previous present for this image can be
// safely destroyed after semaphore got from vkAcquireNextImageHKR() is
// passed. That acquired semaphore should be already waited on for a
// submitted GPU work. So we can safely eunqueue the
// |present_begin_semaphore| for cleanup here (the enqueued semaphore will
// be destroyed when all submitted GPU work is finished).
fence_helper->EnqueueSemaphoreCleanupForSubmittedWork(
current_image_data.present_begin_semaphore);
}
// We are not sure when the semaphore is not used by present engine, so don't
// destroy the semaphore until the image is returned from present engine.
current_image_data.present_begin_semaphore = end_write_semaphore_;
end_write_semaphore_ = VK_NULL_HANDLE;
in_present_images_.emplace_back(*acquired_image_);
acquired_image_.reset();
return gfx::SwapResult::SWAP_ACK;
}
bool VulkanSwapChain::InitializeSwapChain(
VkSurfaceKHR surface,
const VkSurfaceFormatKHR& surface_format,
const gfx::Size& image_size,
uint32_t min_image_count,
VkSurfaceTransformFlagBitsKHR pre_transform,
bool use_protected_memory,
std::unique_ptr<VulkanSwapChain> old_swap_chain) {
VkDevice device = device_queue_->GetVulkanDevice();
VkResult result = VK_SUCCESS;
VkSwapchainCreateInfoKHR swap_chain_create_info = {};
swap_chain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swap_chain_create_info.flags =
use_protected_memory ? VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR : 0;
swap_chain_create_info.surface = surface;
swap_chain_create_info.minImageCount = min_image_count,
swap_chain_create_info.imageFormat = surface_format.format;
swap_chain_create_info.imageColorSpace = surface_format.colorSpace;
swap_chain_create_info.imageExtent.width = image_size.width();
swap_chain_create_info.imageExtent.height = image_size.height();
swap_chain_create_info.imageArrayLayers = 1;
swap_chain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swap_chain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swap_chain_create_info.preTransform = pre_transform;
swap_chain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swap_chain_create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
swap_chain_create_info.clipped = true;
swap_chain_create_info.oldSwapchain =
old_swap_chain ? old_swap_chain->swap_chain_ : VK_NULL_HANDLE;
VkSwapchainKHR new_swap_chain = VK_NULL_HANDLE;
result = vkCreateSwapchainKHR(device, &swap_chain_create_info, nullptr,
&new_swap_chain);
if (old_swap_chain) {
auto* fence_helper = device_queue_->GetFenceHelper();
fence_helper->EnqueueVulkanObjectCleanupForSubmittedWork(
std::move(old_swap_chain));
}
if (VK_SUCCESS != result) {
DLOG(ERROR) << "vkCreateSwapchainKHR() failed: " << result;
return false;
}
swap_chain_ = new_swap_chain;
size_ = gfx::Size(swap_chain_create_info.imageExtent.width,
swap_chain_create_info.imageExtent.height);
return true;
}
void VulkanSwapChain::DestroySwapChain() {
if (swap_chain_ == VK_NULL_HANDLE)
return;
vkDestroySwapchainKHR(device_queue_->GetVulkanDevice(), swap_chain_,
nullptr /* pAllocator */);
swap_chain_ = VK_NULL_HANDLE;
}
bool VulkanSwapChain::InitializeSwapImages(
const VkSurfaceFormatKHR& surface_format) {
VkDevice device = device_queue_->GetVulkanDevice();
VkResult result = VK_SUCCESS;
uint32_t image_count = 0;
result = vkGetSwapchainImagesKHR(device, swap_chain_, &image_count, nullptr);
if (VK_SUCCESS != result) {
DLOG(ERROR) << "vkGetSwapchainImagesKHR(NULL) failed: " << result;
return false;
}
std::vector<VkImage> images(image_count);
result =
vkGetSwapchainImagesKHR(device, swap_chain_, &image_count, images.data());
if (VK_SUCCESS != result) {
DLOG(ERROR) << "vkGetSwapchainImagesKHR(images) failed: " << result;
return false;
}
command_pool_ = device_queue_->CreateCommandPool();
if (!command_pool_)
return false;
images_.resize(image_count);
for (uint32_t i = 0; i < image_count; ++i) {
auto& image_data = images_[i];
image_data.image = images[i];
// Initialize the command buffer for this buffer data.
image_data.command_buffer = command_pool_->CreatePrimaryCommandBuffer();
}
return true;
}
void VulkanSwapChain::DestroySwapImages() {
if (end_write_semaphore_)
vkDestroySemaphore(device_queue_->GetVulkanDevice(), end_write_semaphore_,
nullptr /* pAllocator */);
end_write_semaphore_ = VK_NULL_HANDLE;
for (auto& image_data : images_) {
if (image_data.command_buffer) {
image_data.command_buffer->Destroy();
image_data.command_buffer = nullptr;
}
if (image_data.present_begin_semaphore != VK_NULL_HANDLE) {
vkDestroySemaphore(device_queue_->GetVulkanDevice(),
image_data.present_begin_semaphore,
nullptr /* pAllocator */);
}
if (image_data.present_end_semaphore != VK_NULL_HANDLE) {
vkDestroySemaphore(device_queue_->GetVulkanDevice(),
image_data.present_end_semaphore,
nullptr /* pAllocator */);
}
}
images_.clear();
command_pool_->Destroy();
command_pool_ = nullptr;
}
bool VulkanSwapChain::BeginWriteCurrentImage(VkImage* image,
uint32_t* image_index,
VkImageLayout* image_layout,
VkSemaphore* semaphore) {
DCHECK(image);
DCHECK(image_index);
DCHECK(image_layout);
DCHECK(semaphore);
DCHECK(!is_writing_);
VkSemaphore vk_semaphore = VK_NULL_HANDLE;
if (!acquired_image_) {
DCHECK(end_write_semaphore_ == VK_NULL_HANDLE);
if (!AcquireNextImage())
return false;
DCHECK(acquired_image_);
std::swap(vk_semaphore, images_[*acquired_image_].present_end_semaphore);
} else {
// In this case, PresentBuffer() is not called after
// {Begin,End}WriteCurrentImage pairs, |end_write_semaphore_| should be
// waited on before writing the image again.
std::swap(vk_semaphore, end_write_semaphore_);
}
auto& current_image_data = images_[*acquired_image_];
*image = current_image_data.image;
*image_index = acquired_image_.value();
*image_layout = current_image_data.layout;
*semaphore = vk_semaphore;
is_writing_ = true;
return true;
}
void VulkanSwapChain::EndWriteCurrentImage(VkImageLayout image_layout,
VkSemaphore semaphore) {
DCHECK(is_writing_);
DCHECK(acquired_image_);
DCHECK(end_write_semaphore_ == VK_NULL_HANDLE);
auto& current_image_data = images_[*acquired_image_];
current_image_data.layout = image_layout;
end_write_semaphore_ = semaphore;
is_writing_ = false;
}
bool VulkanSwapChain::AcquireNextImage() {
DCHECK(!acquired_image_);
VkDevice device = device_queue_->GetVulkanDevice();
// The Vulkan spec doesn't require vkAcquireNextImageKHR() returns images in
// the present order for a vulkan swap chain. However for the best performnce,
// the driver should return images in order. To avoid buggy drivers, we will
// call vkAcquireNextImageKHR() continueslly until the expected image is
// returned.
do {
bool all_images_are_tracked = in_present_images_.size() == images_.size();
if (all_images_are_tracked) {
// Only check the expected_next_image, when all images are tracked.
uint32_t expected_next_image = in_present_images_.front();
// If the expected next image has been acquired, use it and return true.
if (images_[expected_next_image].present_end_semaphore !=
VK_NULL_HANDLE) {
in_present_images_.pop_front();
acquired_image_.emplace(expected_next_image);
break;
}
}
VkSemaphore vk_semaphore = CreateSemaphore(device);
DCHECK(vk_semaphore != VK_NULL_HANDLE);
// Acquire the next image.
uint32_t next_image;
auto result =
vkAcquireNextImageKHR(device, swap_chain_, UINT64_MAX, vk_semaphore,
VK_NULL_HANDLE, &next_image);
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
vkDestroySemaphore(device, vk_semaphore, nullptr /* pAllocator */);
DLOG(ERROR) << "vkAcquireNextImageKHR() failed: " << result;
return false;
}
DCHECK(images_[next_image].present_end_semaphore == VK_NULL_HANDLE);
images_[next_image].present_end_semaphore = vk_semaphore;
auto it = std::find(in_present_images_.begin(), in_present_images_.end(),
next_image);
if (it == in_present_images_.end()) {
DCHECK(!all_images_are_tracked);
// Got an image which is not in the present queue due to the new created
// swap chain. In this case, just use this image.
acquired_image_.emplace(next_image);
break;
}
DLOG_IF(ERROR, it != in_present_images_.begin())
<< "vkAcquireNextImageKHR() returned an unexpected image.";
} while (true);
return true;
}
VulkanSwapChain::ScopedWrite::ScopedWrite(VulkanSwapChain* swap_chain)
: swap_chain_(swap_chain) {
success_ = swap_chain_->BeginWriteCurrentImage(
&image_, &image_index_, &image_layout_, &begin_semaphore_);
}
VulkanSwapChain::ScopedWrite::~ScopedWrite() {
DCHECK(begin_semaphore_ == VK_NULL_HANDLE);
if (success_)
swap_chain_->EndWriteCurrentImage(image_layout_, end_semaphore_);
}
VkSemaphore VulkanSwapChain::ScopedWrite::TakeBeginSemaphore() {
VkSemaphore semaphore = begin_semaphore_;
begin_semaphore_ = VK_NULL_HANDLE;
return semaphore;
}
VkSemaphore VulkanSwapChain::ScopedWrite::GetEndSemaphore() {
DCHECK(end_semaphore_ == VK_NULL_HANDLE);
end_semaphore_ =
CreateSemaphore(swap_chain_->device_queue_->GetVulkanDevice());
return end_semaphore_;
}
VulkanSwapChain::ImageData::ImageData() = default;
VulkanSwapChain::ImageData::ImageData(ImageData&& other) = default;
VulkanSwapChain::ImageData::~ImageData() = default;
VulkanSwapChain::ImageData& VulkanSwapChain::ImageData::operator=(
ImageData&& other) = default;
} // namespace gpu
|