32 auto acquire_res = device.createFenceUnique(
33 vk::FenceCreateInfo{vk::FenceCreateFlagBits::eSignaled});
34 if (acquire_res.result != vk::Result::eSuccess) {
38 acquire = std::move(acquire_res.value);
49 if (
auto result = device.waitForFences(
52 std::numeric_limits<uint64_t>::max()
54 result != vk::Result::eSuccess) {
58 if (
auto result = device.resetFences(*
acquire);
59 result != vk::Result::eSuccess) {
60 VALIDATION_LOG <<
"Could not reset fence: " << vk::to_string(result);
67 const std::weak_ptr<Context>& context,
68 std::weak_ptr<android::SurfaceControl> surface_control,
73 context, std::move(surface_control), cb, size, enable_msaa));
74 return impl->IsValid() ? impl :
nullptr;
78 const std::weak_ptr<Context>& context,
79 std::weak_ptr<android::SurfaceControl> surface_control,
83 : surface_control_(
std::move(surface_control)), cb_(cb) {
85 pool_ = std::make_shared<AHBTexturePoolVK>(context, desc_);
86 if (!pool_->IsValid()) {
89 transients_ = std::make_shared<SwapchainTransientsVK>(
93 auto sync = std::make_unique<AHBFrameSynchronizerVK>(
95 if (!sync->IsValid()) {
98 frame_data_.push_back(std::move(sync));
101 auto control = surface_control_.lock();
102 is_valid_ = control && control->IsValid();
121 auto context = transients_->GetContext().lock();
128 if (!frame_data_[frame_index_]->WaitForFence(
137 auto pool_entry = pool_->Pop();
139 if (!pool_entry.IsValid()) {
146 if (!ImportRenderReady(pool_entry.render_ready_fence, pool_entry.texture)) {
158 transients_, pool_entry.texture,
159 [weak = weak_from_this(), texture = pool_entry.texture]() {
160 auto thiz = weak.lock();
162 VALIDATION_LOG <<
"Swapchain died before image could be presented.";
165 return thiz->Present(texture);
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 "
185 auto context = transients_->GetContext().lock();
187 ContextVK::Cast(*context).GetGPUTracer()->MarkFrameEnd();
195 auto present_ready = SubmitSignalForPresentReady(texture);
197 if (!present_ready) {
202 android::SurfaceTransaction transaction =
204 if (!transaction.SetContents(control.get(),
205 texture->GetBackingStore(),
206 present_ready->CreateFD()
208 VALIDATION_LOG <<
"Could not set swapchain image contents on the surface "
212 return transaction.Apply(
213 [texture, weak = weak_from_this()](ASurfaceTransactionStats* stats) {
214 auto thiz = weak.lock();
218 thiz->OnTextureUpdatedOnSurfaceControl(texture, stats);
222 void AHBSwapchainImplVK::AddFinalCommandBuffer(
223 std::shared_ptr<CommandBuffer> cmd_buffer) {
224 frame_data_[frame_index_]->final_cmd_buffer = std::move(cmd_buffer);
227 std::shared_ptr<ExternalSemaphoreVK>
228 AHBSwapchainImplVK::SubmitSignalForPresentReady(
229 const std::shared_ptr<AHBTextureSourceVK>& texture)
const {
230 auto context = transients_->GetContext().lock();
235 auto present_ready = std::make_shared<ExternalSemaphoreVK>(context);
236 if (!present_ready || !present_ready->IsValid()) {
240 auto& sync = frame_data_[frame_index_];
241 auto command_buffer = sync->final_cmd_buffer;
242 if (!command_buffer) {
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()) {
250 sync->present_ready = present_ready;
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);
260 submit_info.setCommandBuffers(command_encoder_vk);
261 submit_info.setPSignalSemaphores(&sync->present_ready->GetHandle());
262 submit_info.setSignalSemaphoreCount(1);
264 auto result = ContextVK::Cast(*context).GetGraphicsQueue()->Submit(
265 submit_info, *sync->acquire);
266 if (result != vk::Result::eSuccess) {
269 return present_ready;
272 vk::UniqueSemaphore AHBSwapchainImplVK::CreateRenderReadySemaphore(
273 const std::shared_ptr<fml::UniqueFD>& fd)
const {
274 if (!fd->is_valid()) {
278 auto context = transients_->GetContext().lock();
283 const auto& context_vk = ContextVK::Cast(*context);
284 const auto& device = context_vk.GetDevice();
286 auto signal_wait = device.createSemaphoreUnique({});
287 if (signal_wait.result != vk::Result::eSuccess) {
291 context_vk.SetDebugName(*signal_wait.value,
"AHBRenderReadySemaphore");
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;
298 import_info.flags = vk::SemaphoreImportFlagBitsKHR::eTemporary;
300 const auto import_result = device.importSemaphoreFdKHR(import_info);
302 if (import_result != vk::Result::eSuccess) {
304 << vk::to_string(import_result);
312 [[maybe_unused]]
auto released = fd->release();
314 return std::move(signal_wait.value);
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();
327 if (!render_ready_fence || !render_ready_fence->is_valid()) {
328 frame_data_[frame_index_]->render_ready = {};
332 auto semaphore = CreateRenderReadySemaphore(render_ready_fence);
338 frame_data_[frame_index_]->render_ready = std::move(semaphore);
342 void AHBSwapchainImplVK::OnTextureUpdatedOnSurfaceControl(
343 std::shared_ptr<AHBTextureSourceVK> texture,
344 ASurfaceTransactionStats* stats) {
345 auto control = surface_control_.lock();
352 auto render_ready_fence =
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));
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)
const vk::Device & GetDevice() const
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() 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#...
fml::UniqueFD CreatePreviousReleaseFence(const SurfaceControl &control, ASurfaceTransactionStats *stats)
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)
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