Flutter Impeller
ahb_swapchain_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 
6 
7 #include "flutter/fml/trace_event.h"
11 #include "third_party/vulkan-deps/vulkan-headers/src/include/vulkan/vulkan_enums.hpp"
12 
13 namespace impeller {
14 
18 }
19 
20 AHBSwapchainVK::AHBSwapchainVK(const std::shared_ptr<Context>& context,
21  ANativeWindow* window,
22  const CreateTransactionCB& cb,
23  const vk::UniqueSurfaceKHR& surface,
24  const ISize& size,
25  bool enable_msaa)
26  : context_(context),
27  surface_control_(
28  std::make_shared<android::SurfaceControl>(window, "ImpellerSurface")),
29  enable_msaa_(enable_msaa),
30  cb_(cb) {
31  const auto [caps_result, surface_caps] =
32  ContextVK::Cast(*context).GetPhysicalDevice().getSurfaceCapabilitiesKHR(
33  *surface);
34  if (caps_result == vk::Result::eSuccess) {
35  swapchain_image_count_ =
36  std::clamp(surface_caps.minImageCount + 1u, // preferred image count
37  surface_caps.minImageCount, // min count cannot be zero
38  surface_caps.maxImageCount == 0u
39  ? surface_caps.minImageCount + 1u
40  : surface_caps.maxImageCount // max zero means no limit
41  );
42  }
43 
44  UpdateSurfaceSize(size);
45 }
46 
48 
49 // |SwapchainVK|
51  return impl_ ? impl_->IsValid() : false;
52 }
53 
54 // |SwapchainVK|
55 std::unique_ptr<Surface> AHBSwapchainVK::AcquireNextDrawable() {
56  if (!IsValid()) {
57  return nullptr;
58  }
59 
60  TRACE_EVENT0("impeller", __FUNCTION__);
61  return impl_->AcquireNextDrawable();
62 }
63 
64 // |SwapchainVK|
66  return IsValid()
67  ? ToVKImageFormat(ToPixelFormat(impl_->GetDescriptor().format))
68  : vk::Format::eUndefined;
69 }
70 
71 // |SwapchainVK|
73  std::shared_ptr<CommandBuffer> cmd_buffer) const {
74  return impl_->AddFinalCommandBuffer(cmd_buffer);
75 }
76 
77 // |SwapchainVK|
79  if (impl_ && impl_->GetSize() == size) {
80  return;
81  }
82  TRACE_EVENT0("impeller", __FUNCTION__);
83  auto impl = AHBSwapchainImplVK::Create(context_, //
84  surface_control_, //
85  cb_, //
86  size, //
87  enable_msaa_, //
88  swapchain_image_count_ //
89  );
90  if (!impl || !impl->IsValid()) {
91  VALIDATION_LOG << "Could not resize swapchain to size: " << size;
92  return;
93  }
94  impl_ = std::move(impl);
95 }
96 
97 } // namespace impeller
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, size_t swapchain_image_count)
Create a swapchain of a specific size whose images will be presented to the provided surface control.
static bool IsAvailableOnPlatform()
void UpdateSurfaceSize(const ISize &size) override
Mark the current swapchain configuration as dirty, forcing it to be recreated on the next frame.
bool IsValid() const override
void AddFinalCommandBuffer(std::shared_ptr< CommandBuffer > cmd_buffer) const override
vk::Format GetSurfaceFormat() const override
std::unique_ptr< Surface > AcquireNextDrawable() override
AHBSwapchainVK(const AHBSwapchainVK &)=delete
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:624
std::function< android::SurfaceTransaction()> CreateTransactionCB
constexpr PixelFormat ToPixelFormat(vk::Format format)
Definition: formats_vk.h:183
constexpr vk::Format ToVKImageFormat(PixelFormat format)
Definition: formats_vk.h:146
Definition: comparable.h:95
#define VALIDATION_LOG
Definition: validation.h:91