Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions impeller/renderer/backend/vulkan/swapchain_impl_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -448,6 +456,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);
Expand Down
7 changes: 7 additions & 0 deletions impeller/renderer/backend/vulkan/swapchain_impl_vk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -47,6 +48,8 @@ class SwapchainImplVK final
: surface(std::move(p_surface)) {}
};

bool GetIsRotated() const { return is_rotated_; }

AcquireResult AcquireNextDrawable();

vk::Format GetSurfaceFormat() const;
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion impeller/renderer/backend/vulkan/swapchain_vk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down