| Flutter Impeller |

ahb_swapchain_impl_vk.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 "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h"
6
7 #include "impeller/base/validation.h"
8 #include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
9 #include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h"
10 #include "impeller/renderer/backend/vulkan/swapchain/ahb/external_semaphore_vk.h"
11 #include "impeller/renderer/backend/vulkan/swapchain/surface_vk.h"
12 #include "impeller/toolkit/android/surface_transaction.h"
13 #include "impeller/toolkit/android/surface_transaction_stats.h"
14
15 namespace impeller {
16
17 static TextureDescriptor ToSwapchainTextureDescriptor(
18 const android::HardwareBufferDescriptor& ahb_desc) {
19 TextureDescriptor desc;
20 desc.storage_mode = StorageMode::kDevicePrivate;
21 desc.type = TextureType::kTexture2D;
22 desc.format = ToPixelFormat(ahb_desc.format);
23 desc.size = ahb_desc.size;
24 desc.mip_count = 1u;
25 desc.usage = TextureUsage::kRenderTarget;
26 desc.sample_count = SampleCount::kCount1;
27 desc.compression_type = CompressionType::kLossless;
28 return desc;
29 }
30
31 AHBFrameSynchronizerVK::AHBFrameSynchronizerVK(const vk::Device& device) {
32 auto acquire_res = device.createFenceUnique(
33 vk::FenceCreateInfo{vk::FenceCreateFlagBits::eSignaled});
34 if (acquire_res.result != vk::Result::eSuccess) {
35 VALIDATION_LOG << "Could not create synchronizer.";
36 return;
37 }
38 acquire = std::move(acquire_res.value);
39 is_valid = true;
40 }
41
42 AHBFrameSynchronizerVK::~AHBFrameSynchronizerVK() = default;
43
44 bool AHBFrameSynchronizerVK::IsValid() const {
45 return is_valid;
46 }
47
48 bool AHBFrameSynchronizerVK::WaitForFence(const vk::Device& device) {
49 if (auto result = device.waitForFences(
50 *acquire, // fence
51 true, // wait all
52 std::numeric_limits<uint64_t>::max() // timeout (ns)
53 );
54 result != vk::Result::eSuccess) {
55 VALIDATION_LOG << "Fence wait failed: " << vk::to_string(result);
56 return false;
57 }
58 if (auto result = device.resetFences(*acquire);
59 result != vk::Result::eSuccess) {
60 VALIDATION_LOG << "Could not reset fence: " << vk::to_string(result);
61 return false;
62 }
63 return true;
64 }
65
66 std::shared_ptr<AHBSwapchainImplVK> AHBSwapchainImplVK::Create(
67 const std::weak_ptr<Context>& context,
68 std::weak_ptr<android::SurfaceControl> surface_control,
69 const CreateTransactionCB& cb,
70 const ISize& size,
71 bool enable_msaa) {
72 auto impl = std::shared_ptr<AHBSwapchainImplVK>(new AHBSwapchainImplVK(
73 context, std::move(surface_control), cb, size, enable_msaa));
74 return impl->IsValid() ? impl : nullptr;
75 }
76
77 AHBSwapchainImplVK::AHBSwapchainImplVK(
78 const std::weak_ptr<Context>& context,
79 std::weak_ptr<android::SurfaceControl> surface_control,
80 const CreateTransactionCB& cb,
81 const ISize& size,
82 bool enable_msaa)
83 : surface_control_(std::move(surface_control)), cb_(cb) {
84 desc_ = android::HardwareBufferDescriptor::MakeForSwapchainImage(size);
85 pool_ = std::make_shared<AHBTexturePoolVK>(context, desc_);
86 if (!pool_->IsValid()) {
87 return;
88 }
89 transients_ = std::make_shared<SwapchainTransientsVK>(
90 context, ToSwapchainTextureDescriptor(desc_), enable_msaa);
91
92 for (auto i = 0u; i < kMaxPendingPresents; i++) {
93 auto sync = std::make_unique<AHBFrameSynchronizerVK>(
94 ContextVK::Cast(*context.lock()).GetDeviceHolder()->GetDevice());
95 if (!sync->IsValid()) {
96 return;
97 }
98 frame_data_.push_back(std::move(sync));
99 }
100
101 auto control = surface_control_.lock();
102 is_valid_ = control && control->IsValid();
103 }
104
105 AHBSwapchainImplVK::~AHBSwapchainImplVK() = default;
106
107 const ISize& AHBSwapchainImplVK::GetSize() const {
108 return desc_.size;
109 }
110
111 bool AHBSwapchainImplVK::IsValid() const {
112 return is_valid_;
113 }
114
115 const android::HardwareBufferDescriptor& AHBSwapchainImplVK::GetDescriptor()
116 const {
117 return desc_;
118 }
119
120 std::unique_ptr<Surface> AHBSwapchainImplVK::AcquireNextDrawable() {
121 auto context = transients_->GetContext().lock();
122 if (!context) {
123 return nullptr;
124 }
125
126 frame_index_ = (frame_index_ + 1) % kMaxPendingPresents;
127
128 if (!frame_data_[frame_index_]->WaitForFence(
129 ContextVK::Cast(*context).GetDevice())) {
130 return nullptr;
131 }
132
133 if (!is_valid_) {
134 return nullptr;
135 }
136
137 auto pool_entry = pool_->Pop();
138
139 if (!pool_entry.IsValid()) {
140 VALIDATION_LOG << "Could not create AHB texture source.";
141 return nullptr;
142 }
143
144 // Import the render ready semaphore that will block onscreen rendering until
145 // it is ready.
146 if (!ImportRenderReady(pool_entry.render_ready_fence, pool_entry.texture)) {
147 VALIDATION_LOG << "Could wait on render ready fence.";
148 return nullptr;
149 }
150
151 #if IMPELLER_DEBUG
152 if (context) {
153 ContextVK::Cast(*context).GetGPUTracer()->MarkFrameStart();
154 }
155 #endif // IMPELLER_DEBUG
156
157 auto surface = SurfaceVK::WrapSwapchainImage(
158 transients_, pool_entry.texture,
159 [weak = weak_from_this(), texture = pool_entry.texture]() {
160 auto thiz = weak.lock();
161 if (!thiz) {
162 VALIDATION_LOG << "Swapchain died before image could be presented.";
163 return false;
164 }
165 return thiz->Present(texture);
166 });
167
168 if (!surface) {
169 return nullptr;
170 }
171
172 return surface;
173 }
174
175 bool AHBSwapchainImplVK::Present(
176 const std::shared_ptr<AHBTextureSourceVK>& texture) {
177 auto control = surface_control_.lock();
178 if (!control || !control->IsValid()) {
179 VALIDATION_LOG << "Surface control died before swapchain image could be "
180 "presented.";
181 return false;
182 }
183
184 #if IMPELLER_DEBUG
185 auto context = transients_->GetContext().lock();
186 if (context) {
187 ContextVK::Cast(*context).GetGPUTracer()->MarkFrameEnd();
188 }
189 #endif // IMPELLER_DEBUG
190
191 if (!texture) {
192 return false;
193 }
194
195 auto present_ready = SubmitSignalForPresentReady(texture);
196
197 if (!present_ready) {
198 VALIDATION_LOG << "Could not submit completion signal.";
199 return false;
200 }
201
202 android::SurfaceTransaction transaction =
203 (cb_) ? cb_() : impeller::android::SurfaceTransaction();
204 if (!transaction.SetContents(control.get(), //
205 texture->GetBackingStore(), //
206 present_ready->CreateFD() //
207 )) {
208 VALIDATION_LOG << "Could not set swapchain image contents on the surface "
209 "control.";
210 return false;
211 }
212 return transaction.Apply(
213 [texture, weak = weak_from_this()](ASurfaceTransactionStats* stats) {
214 auto thiz = weak.lock();
215 if (!thiz) {
216 return;
217 }
218 thiz->OnTextureUpdatedOnSurfaceControl(texture, stats);
219 });
220 }
221
222 void AHBSwapchainImplVK::AddFinalCommandBuffer(
223 std::shared_ptr<CommandBuffer> cmd_buffer) {
224 frame_data_[frame_index_]->final_cmd_buffer = std::move(cmd_buffer);
225 }
226
227 std::shared_ptr<ExternalSemaphoreVK>
228 AHBSwapchainImplVK::SubmitSignalForPresentReady(
229 const std::shared_ptr<AHBTextureSourceVK>& texture) const {
230 auto context = transients_->GetContext().lock();
231 if (!context) {
232 return nullptr;
233 }
234
235 auto present_ready = std::make_shared<ExternalSemaphoreVK>(context);
236 if (!present_ready || !present_ready->IsValid()) {
237 return nullptr;
238 }
239
240 auto& sync = frame_data_[frame_index_];
241 auto command_buffer = sync->final_cmd_buffer;
242 if (!command_buffer) {
243 return nullptr;
244 }
245 CommandBufferVK& command_buffer_vk = CommandBufferVK::Cast(*command_buffer);
246 const auto command_encoder_vk = command_buffer_vk.GetCommandBuffer();
247 if (!command_buffer_vk.EndCommandBuffer()) {
248 return nullptr;
249 }
250 sync->present_ready = present_ready;
251
252 vk::SubmitInfo submit_info;
253 vk::PipelineStageFlags wait_stage =
254 vk::PipelineStageFlagBits::eColorAttachmentOutput;
255 if (sync->render_ready) {
256 submit_info.setPWaitSemaphores(&sync->render_ready.get());
257 submit_info.setWaitSemaphoreCount(1);
258 submit_info.setWaitDstStageMask(wait_stage);
259 }
260 submit_info.setCommandBuffers(command_encoder_vk);
261 submit_info.setPSignalSemaphores(&sync->present_ready->GetHandle());
262 submit_info.setSignalSemaphoreCount(1);
263
264 auto result = ContextVK::Cast(*context).GetGraphicsQueue()->Submit(
265 submit_info, *sync->acquire);
266 if (result != vk::Result::eSuccess) {
267 return nullptr;
268 }
269 return present_ready;
270 }
271
272 vk::UniqueSemaphore AHBSwapchainImplVK::CreateRenderReadySemaphore(
273 const std::shared_ptr<fml::UniqueFD>& fd) const {
274 if (!fd->is_valid()) {
275 return {};
276 }
277
278 auto context = transients_->GetContext().lock();
279 if (!context) {
280 return {};
281 }
282
283 const auto& context_vk = ContextVK::Cast(*context);
284 const auto& device = context_vk.GetDevice();
285
286 auto signal_wait = device.createSemaphoreUnique({});
287 if (signal_wait.result != vk::Result::eSuccess) {
288 return {};
289 }
290
291 context_vk.SetDebugName(*signal_wait.value, "AHBRenderReadySemaphore");
292
293 vk::ImportSemaphoreFdInfoKHR import_info;
294 import_info.semaphore = *signal_wait.value;
295 import_info.fd = fd->get();
296 import_info.handleType = vk::ExternalSemaphoreHandleTypeFlagBits::eSyncFd;
297 // From the spec: Sync FDs can only be imported temporarily.
298 import_info.flags = vk::SemaphoreImportFlagBitsKHR::eTemporary;
299
300 const auto import_result = device.importSemaphoreFdKHR(import_info);
301
302 if (import_result != vk::Result::eSuccess) {
303 VALIDATION_LOG << "Could not import semaphore FD: "
304 << vk::to_string(import_result);
305 return {};
306 }
307
308 // From the spec: Importing a semaphore payload from a file descriptor
309 // transfers ownership of the file descriptor from the application to the
310 // Vulkan implementation. The application must not perform any operations on
311 // the file descriptor after a successful import.
312 [[maybe_unused]] auto released = fd->release();
313
314 return std::move(signal_wait.value);
315 }
316
317 bool AHBSwapchainImplVK::ImportRenderReady(
318 const std::shared_ptr<fml::UniqueFD>& render_ready_fence,
319 const std::shared_ptr<AHBTextureSourceVK>& texture) {
320 auto context = transients_->GetContext().lock();
321 if (!context) {
322 return false;
323 }
324
325 // If there is no render ready fence, we are already ready to render into
326 // the texture. There is nothing more to do.
327 if (!render_ready_fence || !render_ready_fence->is_valid()) {
328 frame_data_[frame_index_]->render_ready = {};
329 return true;
330 }
331
332 auto semaphore = CreateRenderReadySemaphore(render_ready_fence);
333 if (!semaphore) {
334 return false;
335 }
336 // This semaphore will be later used to block the onscreen render pass
337 // from starting until the system is done reading the onscreen.
338 frame_data_[frame_index_]->render_ready = std::move(semaphore);
339 return true;
340 }
341
342 void AHBSwapchainImplVK::OnTextureUpdatedOnSurfaceControl(
343 std::shared_ptr<AHBTextureSourceVK> texture,
344 ASurfaceTransactionStats* stats) {
345 auto control = surface_control_.lock();
346 if (!control) {
347 return;
348 }
349
350 // Ask for an FD that gets signaled when the previous buffer is released. This
351 // can be invalid if there is no wait necessary.
352 auto render_ready_fence =
353 android::CreatePreviousReleaseFence(*control, stats);
354
355 // The transaction completion indicates that the surface control now
356 // references the hardware buffer. We can recycle the previous set buffer
357 // safely.
358 Lock lock(currently_displayed_texture_mutex_);
359 auto old_texture = currently_displayed_texture_;
360 currently_displayed_texture_ = std::move(texture);
361 pool_->Push(std::move(old_texture), std::move(render_ready_fence));
362 }
363
364 } // namespace impeller
ahb_formats.h
ahb_swapchain_impl_vk.h
impeller::AHBSwapchainImplVK::IsValid
bool IsValid() const
Definition: ahb_swapchain_impl_vk.cc:111
impeller::AHBSwapchainImplVK::Create
static std::shared_ptr< AHBSwapchainImplVK > Create(const std::weak_ptr< Context > &context, std::weak_ptr< android::SurfaceControl > surface_control, const CreateTransactionCB &cb, const ISize &size, bool enable_msaa)
Create a swapchain of a specific size whose images will be presented to the provided surface control.
Definition: ahb_swapchain_impl_vk.cc:66
impeller::AHBSwapchainImplVK::AHBSwapchainImplVK
AHBSwapchainImplVK(const AHBSwapchainImplVK &)=delete
impeller::AHBSwapchainImplVK::AcquireNextDrawable
std::unique_ptr< Surface > AcquireNextDrawable()
Acquire the next surface that can be used to present to the swapchain.
Definition: ahb_swapchain_impl_vk.cc:120
impeller::AHBSwapchainImplVK::GetDescriptor
const android::HardwareBufferDescriptor & GetDescriptor() const
Get the descriptor used to create the hardware buffers that will be displayed on the surface control.
Definition: ahb_swapchain_impl_vk.cc:115
impeller::AHBSwapchainImplVK::~AHBSwapchainImplVK
~AHBSwapchainImplVK()
impeller::AHBSwapchainImplVK::GetSize
const ISize & GetSize() const
Definition: ahb_swapchain_impl_vk.cc:107
impeller::BackendCast< ContextVK, Context >::Cast
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:589
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:639
impeller::ContextVK::GetDeviceHolder
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
Definition: context_vk.h:191
impeller::SurfaceVK::WrapSwapchainImage
static std::unique_ptr< SurfaceVK > WrapSwapchainImage(const std::shared_ptr< SwapchainTransientsVK > &transients, const std::shared_ptr< TextureSourceVK > &swapchain_image, SwapCallback swap_callback)
Wrap the swapchain image in a Surface, which provides the additional configuration required for usage...
Definition: surface_vk.cc:13
impeller::android::SurfaceTransaction
A wrapper for ASurfaceTransaction. https://developer.android.com/ndk/reference/group/native-activity#...
Definition: surface_transaction.h:54
command_buffer_vk.h
external_semaphore_vk.h
impeller::android::CreatePreviousReleaseFence
fml::UniqueFD CreatePreviousReleaseFence(const SurfaceControl &control, ASurfaceTransactionStats *stats)
Definition: surface_transaction_stats.cc:11
impeller
Definition: allocation.cc:12
impeller::CreateTransactionCB
std::function< android::SurfaceTransaction()> CreateTransactionCB
Definition: ahb_swapchain_impl_vk.h:24
impeller::ToPixelFormat
constexpr PixelFormat ToPixelFormat(vk::Format format)
Definition: formats_vk.h:183
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::kMaxPendingPresents
static constexpr const size_t kMaxPendingPresents
Definition: ahb_swapchain_impl_vk.h:26
impeller::TextureType::kTexture2D
@ kTexture2D
impeller::CompressionType::kLossless
@ kLossless
impeller::ToSwapchainTextureDescriptor
static TextureDescriptor ToSwapchainTextureDescriptor(const android::HardwareBufferDescriptor &ahb_desc)
Definition: ahb_swapchain_impl_vk.cc:17
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::SampleCount::kCount1
@ kCount1
std
Definition: comparable.h:95
surface_vk.h
impeller::AHBFrameSynchronizerVK::~AHBFrameSynchronizerVK
~AHBFrameSynchronizerVK()
impeller::AHBFrameSynchronizerVK::is_valid
bool is_valid
Definition: ahb_swapchain_impl_vk.h:33
impeller::AHBFrameSynchronizerVK::IsValid
bool IsValid() const
Definition: ahb_swapchain_impl_vk.cc:44
impeller::AHBFrameSynchronizerVK::AHBFrameSynchronizerVK
AHBFrameSynchronizerVK(const vk::Device &device)
Definition: ahb_swapchain_impl_vk.cc:31
impeller::AHBFrameSynchronizerVK::WaitForFence
bool WaitForFence(const vk::Device &device)
Definition: ahb_swapchain_impl_vk.cc:48
impeller::AHBFrameSynchronizerVK::acquire
vk::UniqueFence acquire
Definition: ahb_swapchain_impl_vk.h:29
impeller::TSize
Definition: size.h:25
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::TextureDescriptor::compression_type
CompressionType compression_type
Definition: texture_descriptor.h:46
impeller::TextureDescriptor::type
TextureType type
Definition: texture_descriptor.h:40
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
impeller::TextureDescriptor::mip_count
size_t mip_count
Definition: texture_descriptor.h:43
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::android::HardwareBufferDescriptor
A descriptor use to specify hardware buffer allocations.
Definition: hardware_buffer.h:47
impeller::android::HardwareBufferDescriptor::MakeForSwapchainImage
static HardwareBufferDescriptor MakeForSwapchainImage(const ISize &size)
Create a descriptor of the given size that is suitable for use as a swapchain image.
Definition: hardware_buffer.cc:87
impeller::android::HardwareBufferDescriptor::size
ISize size
Definition: hardware_buffer.h:49
impeller::android::HardwareBufferDescriptor::format
HardwareBufferFormat format
Definition: hardware_buffer.h:48
surface_transaction.h
surface_transaction_stats.h
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91