Skip to content

Commit c0917b1

Browse files
author
Jonah Williams
authored
[Impeller] Size AHB swapchain based on vk surface properties. (flutter#52692)
Pixel 4 uses 4 images, 7 Pro uses ... 6 actually. flutter#147721
1 parent 6e722ae commit c0917b1

6 files changed

Lines changed: 49 additions & 26 deletions

File tree

impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,25 @@ std::shared_ptr<AHBSwapchainImplVK> AHBSwapchainImplVK::Create(
4444
const std::weak_ptr<Context>& context,
4545
std::weak_ptr<android::SurfaceControl> surface_control,
4646
const ISize& size,
47-
bool enable_msaa) {
48-
auto impl = std::shared_ptr<AHBSwapchainImplVK>(new AHBSwapchainImplVK(
49-
context, std::move(surface_control), size, enable_msaa));
47+
bool enable_msaa,
48+
size_t swapchain_image_count) {
49+
auto impl = std::shared_ptr<AHBSwapchainImplVK>(
50+
new AHBSwapchainImplVK(context, std::move(surface_control), size,
51+
enable_msaa, swapchain_image_count));
5052
return impl->IsValid() ? impl : nullptr;
5153
}
5254

5355
AHBSwapchainImplVK::AHBSwapchainImplVK(
5456
const std::weak_ptr<Context>& context,
5557
std::weak_ptr<android::SurfaceControl> surface_control,
5658
const ISize& size,
57-
bool enable_msaa)
59+
bool enable_msaa,
60+
size_t swapchain_image_count)
5861
: surface_control_(std::move(surface_control)),
5962
pending_presents_(std::make_shared<fml::Semaphore>(kMaxPendingPresents)) {
6063
desc_ = android::HardwareBufferDescriptor::MakeForSwapchainImage(size);
61-
pool_ = std::make_shared<AHBTexturePoolVK>(context, desc_);
64+
pool_ =
65+
std::make_shared<AHBTexturePoolVK>(context, desc_, swapchain_image_count);
6266
if (!pool_->IsValid()) {
6367
return;
6468
}
@@ -245,7 +249,6 @@ vk::UniqueSemaphore AHBSwapchainImplVK::CreateRenderReadySemaphore(
245249
}
246250

247251
const auto& context_vk = ContextVK::Cast(*context);
248-
249252
const auto& device = context_vk.GetDevice();
250253

251254
auto signal_wait = device.createSemaphoreUnique({});

impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_impl_vk.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ class AHBSwapchainImplVK final
5151
const std::weak_ptr<Context>& context,
5252
std::weak_ptr<android::SurfaceControl> surface_control,
5353
const ISize& size,
54-
bool enable_msaa);
54+
bool enable_msaa,
55+
size_t swapchain_image_count);
5556

5657
~AHBSwapchainImplVK();
5758

@@ -107,7 +108,8 @@ class AHBSwapchainImplVK final
107108
const std::weak_ptr<Context>& context,
108109
std::weak_ptr<android::SurfaceControl> surface_control,
109110
const ISize& size,
110-
bool enable_msaa);
111+
bool enable_msaa,
112+
size_t swapchain_image_count);
111113

112114
bool Present(const AutoSemaSignaler& signaler,
113115
const std::shared_ptr<AHBTextureSourceVK>& texture);

impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.cc

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h"
66

77
#include "flutter/fml/trace_event.h"
8+
#include "impeller/renderer/backend/vulkan/context_vk.h"
89
#include "impeller/renderer/backend/vulkan/formats_vk.h"
910
#include "impeller/renderer/backend/vulkan/swapchain/ahb/ahb_formats.h"
11+
#include "third_party/vulkan-deps/vulkan-headers/src/include/vulkan/vulkan_enums.hpp"
1012

1113
namespace impeller {
1214

@@ -17,12 +19,26 @@ bool AHBSwapchainVK::IsAvailableOnPlatform() {
1719

1820
AHBSwapchainVK::AHBSwapchainVK(const std::shared_ptr<Context>& context,
1921
ANativeWindow* window,
22+
vk::UniqueSurfaceKHR surface,
2023
const ISize& size,
2124
bool enable_msaa)
2225
: context_(context),
2326
surface_control_(
2427
std::make_shared<android::SurfaceControl>(window, "ImpellerSurface")),
2528
enable_msaa_(enable_msaa) {
29+
const auto [caps_result, surface_caps] =
30+
ContextVK::Cast(*context).GetPhysicalDevice().getSurfaceCapabilitiesKHR(
31+
*surface);
32+
if (caps_result == vk::Result::eSuccess) {
33+
swapchain_image_count_ =
34+
std::clamp(surface_caps.minImageCount + 1u, // preferred image count
35+
surface_caps.minImageCount, // min count cannot be zero
36+
surface_caps.maxImageCount == 0u
37+
? surface_caps.minImageCount + 1u
38+
: surface_caps.maxImageCount // max zero means no limit
39+
);
40+
}
41+
2642
UpdateSurfaceSize(size);
2743
}
2844

@@ -56,10 +72,11 @@ void AHBSwapchainVK::UpdateSurfaceSize(const ISize& size) {
5672
return;
5773
}
5874
TRACE_EVENT0("impeller", __FUNCTION__);
59-
auto impl = AHBSwapchainImplVK::Create(context_, //
60-
surface_control_, //
61-
size, //
62-
enable_msaa_ //
75+
auto impl = AHBSwapchainImplVK::Create(context_, //
76+
surface_control_, //
77+
size, //
78+
enable_msaa_, //
79+
swapchain_image_count_ //
6380
);
6481
if (!impl || !impl->IsValid()) {
6582
VALIDATION_LOG << "Could not resize swapchain to size: " << size;

impeller/renderer/backend/vulkan/swapchain/ahb/ahb_swapchain_vk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ class AHBSwapchainVK final : public SwapchainVK {
5252
std::weak_ptr<Context> context_;
5353
std::shared_ptr<android::SurfaceControl> surface_control_;
5454
const bool enable_msaa_;
55+
size_t swapchain_image_count_ = 3u;
5556
std::shared_ptr<AHBSwapchainImplVK> impl_;
5657

5758
explicit AHBSwapchainVK(const std::shared_ptr<Context>& context,
5859
ANativeWindow* window,
60+
vk::UniqueSurfaceKHR surface,
5961
const ISize& size,
6062
bool enable_msaa);
6163
};

impeller/renderer/backend/vulkan/swapchain/ahb/ahb_texture_pool_vk.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "flutter/fml/unique_fd.h"
1111
#include "impeller/base/thread.h"
12-
#include "impeller/base/timing.h"
1312
#include "impeller/renderer/backend/vulkan/android/ahb_texture_source_vk.h"
1413

1514
namespace impeller {
@@ -30,14 +29,12 @@ namespace impeller {
3029
class AHBTexturePoolVK {
3130
public:
3231
struct PoolEntry {
33-
TimePoint last_access_time;
3432
std::shared_ptr<AHBTextureSourceVK> texture;
3533
std::shared_ptr<fml::UniqueFD> render_ready_fence;
3634

3735
explicit PoolEntry(std::shared_ptr<AHBTextureSourceVK> p_item,
3836
fml::UniqueFD p_render_ready_fence = {})
39-
: last_access_time(Clock::now()),
40-
texture(std::move(p_item)),
37+
: texture(std::move(p_item)),
4138
render_ready_fence(std::make_shared<fml::UniqueFD>(
4239
std::move(p_render_ready_fence))) {}
4340

impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ std::shared_ptr<SwapchainVK> SwapchainVK::Create(
4444
return nullptr;
4545
}
4646

47+
vk::AndroidSurfaceCreateInfoKHR surface_info;
48+
surface_info.setWindow(window.GetHandle());
49+
auto [result, surface] =
50+
ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique(
51+
surface_info);
52+
if (result != vk::Result::eSuccess) {
53+
VALIDATION_LOG << "Could not create KHR Android Surface: "
54+
<< vk::to_string(result);
55+
return nullptr;
56+
}
57+
4758
// TODO(147533): AHB swapchains on emulators are not functional.
4859
const auto emulator = ContextVK::Cast(*context).GetDriverInfo()->IsEmulator();
4960

@@ -52,6 +63,7 @@ std::shared_ptr<SwapchainVK> SwapchainVK::Create(
5263
auto ahb_swapchain = std::shared_ptr<AHBSwapchainVK>(new AHBSwapchainVK(
5364
context, //
5465
window.GetHandle(), //
66+
std::move(surface), //
5567
window.GetSize(), //
5668
enable_msaa //
5769
));
@@ -65,16 +77,6 @@ std::shared_ptr<SwapchainVK> SwapchainVK::Create(
6577
}
6678

6779
// Fallback to KHR swapchains if AHB swapchains aren't available.
68-
vk::AndroidSurfaceCreateInfoKHR surface_info;
69-
surface_info.setWindow(window.GetHandle());
70-
auto [result, surface] =
71-
ContextVK::Cast(*context).GetInstance().createAndroidSurfaceKHRUnique(
72-
surface_info);
73-
if (result != vk::Result::eSuccess) {
74-
VALIDATION_LOG << "Could not create KHR Android Surface: "
75-
<< vk::to_string(result);
76-
return nullptr;
77-
}
7880
return Create(context, std::move(surface), window.GetSize(), enable_msaa);
7981
}
8082
#endif // FML_OS_ANDROID

0 commit comments

Comments
 (0)