[Impeller] Treat SubOptimalKHR as rotated. (flutter/engine#43214)

If we don't follow the guidelines at https://developer.android.com/games/optimize/vulkan-prerotation , then any presentations while the screen is rotated will result in suboptimalKHR. Use a change in state across presentations from sucess -> suboptimal or suboptimal -> sucess to detect whether the swapchain needs to be recreated.

Fixes https://github.com/flutter/flutter/issues/129459 (mostly)
This commit is contained in:
Jonah Williams
2023-06-26 14:34:54 -07:00
committed by GitHub
parent 3bef1ee147
commit 8b6c071526
3 changed files with 32 additions and 6 deletions

View File

@@ -124,13 +124,15 @@ static std::optional<vk::Queue> ChoosePresentQueue(
std::shared_ptr<SwapchainImplVK> SwapchainImplVK::Create(
const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain) {
return std::shared_ptr<SwapchainImplVK>(
new SwapchainImplVK(context, std::move(surface), old_swapchain));
return std::shared_ptr<SwapchainImplVK>(new SwapchainImplVK(
context, std::move(surface), was_rotated, old_swapchain));
}
SwapchainImplVK::SwapchainImplVK(const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain) {
if (!context) {
return;
@@ -197,7 +199,7 @@ SwapchainImplVK::SwapchainImplVK(const std::shared_ptr<Context>& context,
);
swapchain_info.imageArrayLayers = 1u;
swapchain_info.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
swapchain_info.preTransform = caps.currentTransform;
swapchain_info.preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity;
swapchain_info.compositeAlpha = composite.value();
// If we set the clipped value to true, Vulkan expects we will never read back
// from the buffer. This is analogous to [CAMetalLayer framebufferOnly] in
@@ -273,6 +275,8 @@ SwapchainImplVK::SwapchainImplVK(const std::shared_ptr<Context>& context,
synchronizers_ = std::move(synchronizers);
current_frame_ = synchronizers_.size() - 1u;
is_valid_ = true;
was_rotated_ = was_rotated;
is_rotated_ = was_rotated;
}
SwapchainImplVK::~SwapchainImplVK() {
@@ -314,6 +318,10 @@ SwapchainImplVK::AcquireResult SwapchainImplVK::AcquireNextDrawable() {
return {};
}
if (was_rotated_ != is_rotated_) {
return AcquireResult{true /* out of date */};
}
const auto& context = ContextVK::Cast(*context_strong);
current_frame_ = (current_frame_ + 1u) % synchronizers_.size();
@@ -338,8 +346,12 @@ SwapchainImplVK::AcquireResult SwapchainImplVK::AcquireNextDrawable() {
nullptr // fence
);
if (acq_result == vk::Result::eSuboptimalKHR ||
acq_result == vk::Result::eErrorOutOfDateKHR) {
if (acq_result == vk::Result::eSuboptimalKHR) {
is_rotated_ = true;
return AcquireResult{true /* out of date */};
}
if (acq_result == vk::Result::eErrorOutOfDateKHR) {
return AcquireResult{true /* out of date */};
}
@@ -448,6 +460,10 @@ bool SwapchainImplVK::Present(const std::shared_ptr<SwapchainImageVK>& image,
// successfully.
[[fallthrough]];
case vk::Result::eSuccess:
is_rotated_ = false;
return true;
case vk::Result::eSuboptimalKHR:
is_rotated_ = true;
return true;
default:
VALIDATION_LOG << "Could not present queue: " << vk::to_string(result);

View File

@@ -31,6 +31,7 @@ class SwapchainImplVK final
static std::shared_ptr<SwapchainImplVK> Create(
const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain = VK_NULL_HANDLE);
~SwapchainImplVK();
@@ -47,6 +48,8 @@ class SwapchainImplVK final
: surface(std::move(p_surface)) {}
};
bool GetIsRotated() const { return is_rotated_; }
AcquireResult AcquireNextDrawable();
vk::Format GetSurfaceFormat() const;
@@ -66,8 +69,12 @@ class SwapchainImplVK final
size_t current_frame_ = 0u;
bool is_valid_ = false;
bool was_rotated_ = false;
bool is_rotated_ = false;
SwapchainImplVK(const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface,
bool was_rotated,
vk::SwapchainKHR old_swapchain);
bool Present(const std::shared_ptr<SwapchainImageVK>& image, uint32_t index);

View File

@@ -12,7 +12,8 @@ namespace impeller {
std::shared_ptr<SwapchainVK> SwapchainVK::Create(
const std::shared_ptr<Context>& context,
vk::UniqueSurfaceKHR surface) {
auto impl = SwapchainImplVK::Create(context, std::move(surface));
auto impl = SwapchainImplVK::Create(context, std::move(surface),
/*was_rotated=*/false);
if (!impl || !impl->IsValid()) {
return nullptr;
}
@@ -45,10 +46,12 @@ std::unique_ptr<Surface> SwapchainVK::AcquireNextDrawable() {
// This swapchain implementation indicates that it is out of date. Tear it
// down and make a new one.
auto context = impl_->GetContext();
auto was_rotated = impl_->GetIsRotated();
auto [surface, old_swapchain] = impl_->DestroySwapchain();
auto new_impl = SwapchainImplVK::Create(context, //
std::move(surface), //
was_rotated, //
*old_swapchain //
);
if (!new_impl || !new_impl->IsValid()) {