[Impeller] Remove the Vulkan queue submit thread and always present Vulkan images on the raster thread (flutter/engine#50484)
The queue submit thread could sometimes call vkQueuePresentKHR while the raster thread is calling vkAcquireNextImageKHR. The simultaneous use of the swapchain on multiple threads violates Vulkan threading rules.
This commit is contained in:
@@ -106,15 +106,6 @@ PlaygroundImplVK::PlaygroundImplVK(PlaygroundSwitches switches)
|
||||
return;
|
||||
}
|
||||
|
||||
// Without this, the playground will timeout waiting for the presentation.
|
||||
// It's better to have some Vulkan validation tests running on CI to catch
|
||||
// regressions, but for now this is a workaround.
|
||||
//
|
||||
// TODO(matanlurey): https://github.com/flutter/flutter/issues/134852.
|
||||
//
|
||||
// (Note, if you're using MoltenVK, or Linux, you can comment out this line).
|
||||
context_vk->SetSyncPresentation(true);
|
||||
|
||||
VkSurfaceKHR vk_surface;
|
||||
auto res = vk::Result{::glfwCreateWindowSurface(
|
||||
context_vk->GetInstance(), // instance
|
||||
|
||||
@@ -91,11 +91,6 @@ class ContextVK final : public Context,
|
||||
// |Context|
|
||||
void Shutdown() override;
|
||||
|
||||
// |Context|
|
||||
void SetSyncPresentation(bool value) override { sync_presentation_ = value; }
|
||||
|
||||
bool GetSyncPresentation() const { return sync_presentation_; }
|
||||
|
||||
void SetOffscreenFormat(PixelFormat pixel_format);
|
||||
|
||||
template <typename T>
|
||||
@@ -202,7 +197,6 @@ class ContextVK final : public Context,
|
||||
std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
|
||||
std::shared_ptr<CommandQueue> command_queue_vk_;
|
||||
|
||||
bool sync_presentation_ = false;
|
||||
const uint64_t hash_;
|
||||
|
||||
bool is_valid_ = false;
|
||||
|
||||
@@ -91,10 +91,6 @@ std::unique_ptr<Surface> SurfaceContextVK::AcquireNextSurface() {
|
||||
return surface;
|
||||
}
|
||||
|
||||
void SurfaceContextVK::SetSyncPresentation(bool value) {
|
||||
parent_->SetSyncPresentation(value);
|
||||
}
|
||||
|
||||
void SurfaceContextVK::UpdateSurfaceSize(const ISize& size) const {
|
||||
swapchain_->UpdateSurfaceSize(size);
|
||||
}
|
||||
|
||||
@@ -69,9 +69,6 @@ class SurfaceContextVK : public Context,
|
||||
// |Context|
|
||||
void Shutdown() override;
|
||||
|
||||
// |Context|
|
||||
void SetSyncPresentation(bool value) override;
|
||||
|
||||
[[nodiscard]] bool SetWindowSurface(vk::UniqueSurfaceKHR surface,
|
||||
const ISize& size);
|
||||
|
||||
|
||||
@@ -30,13 +30,11 @@ struct FrameSynchronizer {
|
||||
vk::UniqueSemaphore render_ready;
|
||||
vk::UniqueSemaphore present_ready;
|
||||
std::shared_ptr<CommandBuffer> final_cmd_buffer;
|
||||
/// @brief A latch that is signaled _after_ a given swapchain image is
|
||||
/// presented.
|
||||
std::shared_ptr<fml::Semaphore> present_semaphore;
|
||||
bool is_valid = false;
|
||||
|
||||
explicit FrameSynchronizer(const vk::Device& device) {
|
||||
auto acquire_res = device.createFenceUnique({});
|
||||
auto acquire_res = device.createFenceUnique(
|
||||
vk::FenceCreateInfo{vk::FenceCreateFlagBits::eSignaled});
|
||||
auto render_res = device.createSemaphoreUnique({});
|
||||
auto present_res = device.createSemaphoreUnique({});
|
||||
if (acquire_res.result != vk::Result::eSuccess ||
|
||||
@@ -54,15 +52,6 @@ struct FrameSynchronizer {
|
||||
~FrameSynchronizer() = default;
|
||||
|
||||
bool WaitForFence(const vk::Device& device) {
|
||||
if (!present_semaphore) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!present_semaphore->Wait()) {
|
||||
VALIDATION_LOG << "Present semaphore wait failed";
|
||||
return false;
|
||||
}
|
||||
present_semaphore.reset();
|
||||
if (auto result = device.waitForFences(
|
||||
*acquire, // fence
|
||||
true, // wait all
|
||||
@@ -477,56 +466,39 @@ bool SwapchainImplVK::Present(const std::shared_ptr<SwapchainImageVK>& image,
|
||||
}
|
||||
}
|
||||
|
||||
auto task = [&, index, current_frame = current_frame_] {
|
||||
auto context_strong = context_.lock();
|
||||
if (!context_strong) {
|
||||
return;
|
||||
}
|
||||
//----------------------------------------------------------------------------
|
||||
/// Present the image.
|
||||
///
|
||||
uint32_t indices[] = {static_cast<uint32_t>(index)};
|
||||
|
||||
const auto& sync = synchronizers_[current_frame];
|
||||
vk::PresentInfoKHR present_info;
|
||||
present_info.setSwapchains(*swapchain_);
|
||||
present_info.setImageIndices(indices);
|
||||
present_info.setWaitSemaphores(*sync->present_ready);
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// Present the image.
|
||||
///
|
||||
uint32_t indices[] = {static_cast<uint32_t>(index)};
|
||||
auto result = present_queue_.presentKHR(present_info);
|
||||
|
||||
vk::PresentInfoKHR present_info;
|
||||
present_info.setSwapchains(*swapchain_);
|
||||
present_info.setImageIndices(indices);
|
||||
present_info.setWaitSemaphores(*sync->present_ready);
|
||||
|
||||
auto result = present_queue_.presentKHR(present_info);
|
||||
sync->present_semaphore->Signal();
|
||||
|
||||
switch (result) {
|
||||
case vk::Result::eErrorOutOfDateKHR:
|
||||
// Caller will recreate the impl on acquisition, not submission.
|
||||
[[fallthrough]];
|
||||
case vk::Result::eErrorSurfaceLostKHR:
|
||||
// Vulkan guarantees that the set of queue operations will still
|
||||
// complete successfully.
|
||||
[[fallthrough]];
|
||||
case vk::Result::eSuboptimalKHR:
|
||||
// Even though we're handling rotation changes via polling, we
|
||||
// still need to handle the case where the swapchain signals that
|
||||
// it's suboptimal (i.e. every frame when we are rotated given we
|
||||
// aren't doing Vulkan pre-rotation).
|
||||
[[fallthrough]];
|
||||
case vk::Result::eSuccess:
|
||||
return;
|
||||
default:
|
||||
VALIDATION_LOG << "Could not present queue: " << vk::to_string(result);
|
||||
return;
|
||||
}
|
||||
FML_UNREACHABLE();
|
||||
};
|
||||
|
||||
sync->present_semaphore = std::make_shared<fml::Semaphore>(0u);
|
||||
if (context.GetSyncPresentation()) {
|
||||
task();
|
||||
} else {
|
||||
context.GetQueueSubmitRunner()->PostTask(task);
|
||||
switch (result) {
|
||||
case vk::Result::eErrorOutOfDateKHR:
|
||||
// Caller will recreate the impl on acquisition, not submission.
|
||||
[[fallthrough]];
|
||||
case vk::Result::eErrorSurfaceLostKHR:
|
||||
// Vulkan guarantees that the set of queue operations will still
|
||||
// complete successfully.
|
||||
[[fallthrough]];
|
||||
case vk::Result::eSuboptimalKHR:
|
||||
// Even though we're handling rotation changes via polling, we
|
||||
// still need to handle the case where the swapchain signals that
|
||||
// it's suboptimal (i.e. every frame when we are rotated given we
|
||||
// aren't doing Vulkan pre-rotation).
|
||||
[[fallthrough]];
|
||||
case vk::Result::eSuccess:
|
||||
break;
|
||||
default:
|
||||
VALIDATION_LOG << "Could not present queue: " << vk::to_string(result);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -506,6 +506,12 @@ VkResult vkGetFenceStatus(VkDevice device, VkFence fence) {
|
||||
return mock_fence->GetStatus();
|
||||
}
|
||||
|
||||
VkResult vkResetFences(VkDevice device,
|
||||
uint32_t fenceCount,
|
||||
const VkFence* fences) {
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VkResult vkCreateDebugUtilsMessengerEXT(
|
||||
VkInstance instance,
|
||||
const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo,
|
||||
@@ -773,6 +779,8 @@ PFN_vkVoidFunction GetMockVulkanProcAddress(VkInstance instance,
|
||||
return (PFN_vkVoidFunction)vkWaitForFences;
|
||||
} else if (strcmp("vkGetFenceStatus", pName) == 0) {
|
||||
return (PFN_vkVoidFunction)vkGetFenceStatus;
|
||||
} else if (strcmp("vkResetFences", pName) == 0) {
|
||||
return (PFN_vkVoidFunction)vkResetFences;
|
||||
} else if (strcmp("vkCreateDebugUtilsMessengerEXT", pName) == 0) {
|
||||
return (PFN_vkVoidFunction)vkCreateDebugUtilsMessengerEXT;
|
||||
} else if (strcmp("vkSetDebugUtilsObjectNameEXT", pName) == 0) {
|
||||
|
||||
@@ -171,17 +171,6 @@ class Context {
|
||||
///
|
||||
virtual void Shutdown() = 0;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Force the Vulkan presentation (submitKHR) to be performed on
|
||||
/// the raster task runner.
|
||||
///
|
||||
/// This is required for correct rendering on Android when using
|
||||
/// the hybrid composition mode. This has no effect on other
|
||||
/// backends. This is analogous to the check for isMainThread in
|
||||
/// surface_mtl.mm to block presentation on scheduling of all
|
||||
/// pending work.
|
||||
virtual void SetSyncPresentation(bool value) {}
|
||||
|
||||
CaptureContext capture;
|
||||
|
||||
/// Stores a task on the `ContextMTL` that is awaiting access for the GPU.
|
||||
|
||||
@@ -353,8 +353,4 @@ void AndroidShellHolder::UpdateDisplayMetrics() {
|
||||
shell_->OnDisplayUpdates(std::move(displays));
|
||||
}
|
||||
|
||||
void AndroidShellHolder::SetIsRenderingToImageView(bool value) {
|
||||
platform_view_->SetIsRenderingToImageView(value);
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -105,8 +105,6 @@ class AndroidShellHolder {
|
||||
|
||||
void UpdateDisplayMetrics();
|
||||
|
||||
void SetIsRenderingToImageView(bool value);
|
||||
|
||||
private:
|
||||
const flutter::Settings settings_;
|
||||
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
|
||||
|
||||
@@ -143,7 +143,6 @@ public class FlutterImageView extends View implements RenderSurface {
|
||||
switch (kind) {
|
||||
case background:
|
||||
flutterRenderer.swapSurface(imageReader.getSurface());
|
||||
flutterRenderer.SetRenderingToImageView(true);
|
||||
break;
|
||||
case overlay:
|
||||
// Do nothing since the attachment is done by the handler of
|
||||
@@ -174,12 +173,6 @@ public class FlutterImageView extends View implements RenderSurface {
|
||||
closeCurrentImage();
|
||||
invalidate();
|
||||
isAttachedToFlutterRenderer = false;
|
||||
if (kind == SurfaceKind.background) {
|
||||
// The overlay FlutterImageViews seem to be constructed per frame and not
|
||||
// always used; An overlay FlutterImageView always seems to imply
|
||||
// a background FlutterImageView.
|
||||
flutterRenderer.SetRenderingToImageView(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
|
||||
@@ -755,13 +755,6 @@ public class FlutterJNI {
|
||||
int[] displayFeaturesType,
|
||||
int[] displayFeaturesState);
|
||||
|
||||
@UiThread
|
||||
public void SetIsRenderingToImageView(boolean value) {
|
||||
nativeSetIsRenderingToImageView(nativeShellHolderId, value);
|
||||
}
|
||||
|
||||
private native void nativeSetIsRenderingToImageView(long nativeShellHolderId, boolean value);
|
||||
|
||||
// ----- End Render Surface Support -----
|
||||
|
||||
// ------ Start Touch Interaction Support ---
|
||||
|
||||
@@ -65,7 +65,6 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
@NonNull private final AtomicLong nextTextureId = new AtomicLong(0L);
|
||||
@Nullable private Surface surface;
|
||||
private boolean isDisplayingFlutterUi = false;
|
||||
private int isRenderingToImageViewCount = 0;
|
||||
private final Handler handler = new Handler();
|
||||
|
||||
@NonNull
|
||||
@@ -99,19 +98,6 @@ public class FlutterRenderer implements TextureRegistry {
|
||||
return isDisplayingFlutterUi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Informs the renderer whether the surface it is rendering to is backend by a {@code
|
||||
* FlutterImageView}, which requires additional synchonization in the Vulkan backend.
|
||||
*/
|
||||
public void SetRenderingToImageView(boolean value) {
|
||||
if (value) {
|
||||
isRenderingToImageViewCount++;
|
||||
} else {
|
||||
isRenderingToImageViewCount--;
|
||||
}
|
||||
flutterJNI.SetIsRenderingToImageView(isRenderingToImageViewCount > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a listener that is invoked whenever this {@code FlutterRenderer} starts and stops painting
|
||||
* pixels to an Android {@code View} hierarchy.
|
||||
|
||||
@@ -120,12 +120,6 @@ class PlatformViewAndroid final : public PlatformView {
|
||||
return platform_message_handler_;
|
||||
}
|
||||
|
||||
void SetIsRenderingToImageView(bool value) {
|
||||
if (GetImpellerContext()) {
|
||||
GetImpellerContext()->SetSyncPresentation(value);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
|
||||
std::shared_ptr<AndroidContext> android_context_;
|
||||
|
||||
@@ -556,13 +556,6 @@ static void NotifyLowMemoryWarning(JNIEnv* env,
|
||||
ANDROID_SHELL_HOLDER->NotifyLowMemoryWarning();
|
||||
}
|
||||
|
||||
static void SetIsRenderingToImageView(JNIEnv* env,
|
||||
jobject jcaller,
|
||||
jlong shell_holder,
|
||||
bool value) {
|
||||
ANDROID_SHELL_HOLDER->SetIsRenderingToImageView(value);
|
||||
}
|
||||
|
||||
static jboolean FlutterTextUtilsIsEmoji(JNIEnv* env,
|
||||
jobject obj,
|
||||
jint codePoint) {
|
||||
@@ -722,11 +715,6 @@ bool RegisterApi(JNIEnv* env) {
|
||||
.signature = "(J)V",
|
||||
.fnPtr = reinterpret_cast<void*>(&NotifyLowMemoryWarning),
|
||||
},
|
||||
{
|
||||
.name = "nativeSetIsRenderingToImageView",
|
||||
.signature = "(JZ)V",
|
||||
.fnPtr = reinterpret_cast<void*>(&SetIsRenderingToImageView),
|
||||
},
|
||||
|
||||
// Start of methods from FlutterView
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user