32 auto acquire_res =
device.createFenceUnique({});
33 if (acquire_res.result != vk::Result::eSuccess) {
37 acquire = std::move(acquire_res.value);
51 if (
auto result =
device.waitForFences(
54 std::numeric_limits<uint64_t>::max()
56 result != vk::Result::eSuccess) {
62 result != vk::Result::eSuccess) {
63 VALIDATION_LOG <<
"Could not reset fence: " << vk::to_string(result);
70 const std::weak_ptr<Context>& context,
71 std::weak_ptr<android::SurfaceControl> surface_control,
76 context, std::move(surface_control), cb, size, enable_msaa));
77 return impl->IsValid() ? impl :
nullptr;
81 const std::weak_ptr<Context>& context,
82 std::weak_ptr<android::SurfaceControl> surface_control,
86 : surface_control_(
std::move(surface_control)), cb_(cb) {
88 pool_ = std::make_shared<AHBTexturePoolVK>(context, desc_);
89 if (!pool_->IsValid()) {
92 transients_ = std::make_shared<SwapchainTransientsVK>(
96 auto sync = std::make_unique<AHBFrameSynchronizerVK>(
98 if (!sync->IsValid()) {
101 frame_data_.push_back(std::move(sync));
104 auto control = surface_control_.lock();
105 is_valid_ = control && control->IsValid();
124 auto context = transients_->GetContext().lock();
131 if (!frame_data_[frame_index_]->WaitForFence(
140 auto pool_entry = pool_->Pop();
142 if (!pool_entry.IsValid()) {
149 if (!ImportRenderReady(pool_entry.render_ready_fence, pool_entry.texture)) {
161 transients_, pool_entry.texture,
162 [weak = weak_from_this(),
texture = pool_entry.texture]() {
163 auto thiz = weak.lock();
165 VALIDATION_LOG <<
"Swapchain died before image could be presented.";
178bool AHBSwapchainImplVK::Present(
179 const std::shared_ptr<AHBTextureSourceVK>&
texture) {
180 auto control = surface_control_.lock();
181 if (!control || !control->IsValid()) {
182 VALIDATION_LOG <<
"Surface control died before swapchain image could be "
188 auto context = transients_->GetContext().lock();
190 ContextVK::Cast(*context).GetGPUTracer()->MarkFrameEnd();
198 auto present_ready = SubmitSignalForPresentReady(
texture);
200 if (!present_ready) {
205 android::SurfaceTransaction transaction =
207 if (!transaction.SetContents(control.get(),
209 present_ready->CreateFD()
211 VALIDATION_LOG <<
"Could not set swapchain image contents on the surface "
215 return transaction.Apply(
216 [
texture, weak = weak_from_this()](ASurfaceTransactionStats* stats) {
217 auto thiz = weak.lock();
221 thiz->OnTextureUpdatedOnSurfaceControl(
texture, stats);
225void AHBSwapchainImplVK::AddFinalCommandBuffer(
226 std::shared_ptr<CommandBuffer> cmd_buffer) {
227 frame_data_[frame_index_]->final_cmd_buffer = std::move(cmd_buffer);
230std::shared_ptr<ExternalSemaphoreVK>
231AHBSwapchainImplVK::SubmitSignalForPresentReady(
232 const std::shared_ptr<AHBTextureSourceVK>&
texture)
const {
233 auto context = transients_->GetContext().lock();
238 auto present_ready = std::make_shared<ExternalSemaphoreVK>(context);
239 if (!present_ready || !present_ready->IsValid()) {
243 auto& sync = frame_data_[frame_index_];
244 auto command_buffer = sync->final_cmd_buffer;
245 if (!command_buffer) {
248 CommandBufferVK& command_buffer_vk = CommandBufferVK::Cast(*command_buffer);
249 const auto command_encoder_vk = command_buffer_vk.GetCommandBuffer();
250 if (!command_buffer_vk.EndCommandBuffer()) {
253 sync->present_ready = present_ready;
255 vk::SubmitInfo submit_info;
256 vk::PipelineStageFlags wait_stage =
257 vk::PipelineStageFlagBits::eColorAttachmentOutput;
258 if (sync->render_ready) {
259 submit_info.setPWaitSemaphores(&sync->render_ready.get());
260 submit_info.setWaitSemaphoreCount(1);
261 submit_info.setWaitDstStageMask(wait_stage);
263 submit_info.setCommandBuffers(command_encoder_vk);
264 submit_info.setPSignalSemaphores(&sync->present_ready->GetHandle());
265 submit_info.setSignalSemaphoreCount(1);
267 auto result = ContextVK::Cast(*context).GetGraphicsQueue()->Submit(
268 submit_info, *sync->acquire);
269 if (result != vk::Result::eSuccess) {
272 sync->acquire_fence_pending =
true;
273 return present_ready;
276vk::UniqueSemaphore AHBSwapchainImplVK::CreateRenderReadySemaphore(
277 const std::shared_ptr<fml::UniqueFD>& fd)
const {
278 if (!fd->is_valid()) {
282 auto context = transients_->GetContext().lock();
287 const auto& context_vk = ContextVK::Cast(*context);
288 const auto&
device = context_vk.GetDevice();
290 auto signal_wait =
device.createSemaphoreUnique({});
291 if (signal_wait.result != vk::Result::eSuccess) {
295 context_vk.SetDebugName(*signal_wait.value,
"AHBRenderReadySemaphore");
297 vk::ImportSemaphoreFdInfoKHR import_info;
298 import_info.semaphore = *signal_wait.value;
299 import_info.fd = fd->get();
300 import_info.handleType = vk::ExternalSemaphoreHandleTypeFlagBits::eSyncFd;
302 import_info.flags = vk::SemaphoreImportFlagBitsKHR::eTemporary;
304 const auto import_result =
device.importSemaphoreFdKHR(import_info);
306 if (import_result != vk::Result::eSuccess) {
308 << vk::to_string(import_result);
316 [[maybe_unused]]
auto released = fd->release();
318 return std::move(signal_wait.value);
321bool AHBSwapchainImplVK::ImportRenderReady(
322 const std::shared_ptr<fml::UniqueFD>& render_ready_fence,
323 const std::shared_ptr<AHBTextureSourceVK>&
texture) {
324 auto context = transients_->GetContext().lock();
331 if (!render_ready_fence || !render_ready_fence->is_valid()) {
332 frame_data_[frame_index_]->render_ready = {};
336 auto semaphore = CreateRenderReadySemaphore(render_ready_fence);
342 frame_data_[frame_index_]->render_ready = std::move(semaphore);
346void AHBSwapchainImplVK::OnTextureUpdatedOnSurfaceControl(
347 std::shared_ptr<AHBTextureSourceVK>
texture,
348 ASurfaceTransactionStats* stats) {
349 auto control = surface_control_.lock();
356 auto render_ready_fence =
357 android::CreatePreviousReleaseFence(*control, stats);
362 Lock lock(currently_displayed_texture_mutex_);
363 auto old_texture = currently_displayed_texture_;
364 currently_displayed_texture_ = std::move(
texture);
365 pool_->Push(std::move(old_texture), std::move(render_ready_fence));
The implementation of a swapchain at a specific size. Resizes to the surface will cause the instance ...
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.
AHBSwapchainImplVK(const AHBSwapchainImplVK &)=delete
std::unique_ptr< Surface > AcquireNextDrawable()
Acquire the next surface that can be used to present to the swapchain.
const android::HardwareBufferDescriptor & GetDescriptor() const
Get the descriptor used to create the hardware buffers that will be displayed on the surface control.
const ISize & GetSize() const
static ContextVK & Cast(Context &base)
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
const vk::Device & GetDevice() const
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
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...
A wrapper for ASurfaceTransaction. https://developer.android.com/ndk/reference/group/native-activity#...
std::function< android::SurfaceTransaction()> CreateTransactionCB
constexpr PixelFormat ToPixelFormat(vk::Format format)
static constexpr const size_t kMaxPendingPresents
static TextureDescriptor ToSwapchainTextureDescriptor(const android::HardwareBufferDescriptor &ahb_desc)
~AHBFrameSynchronizerVK()
AHBFrameSynchronizerVK(const vk::Device &device)
bool WaitForFence(const vk::Device &device)
bool acquire_fence_pending
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
CompressionType compression_type
A descriptor use to specify hardware buffer allocations.
static HardwareBufferDescriptor MakeForSwapchainImage(const ISize &size)
Create a descriptor of the given size that is suitable for use as a swapchain image.
HardwareBufferFormat format