[Impeller] avoid creating multiple concurrent message loops for Andorid Vulkan (flutter/engine#42146)

Fixes https://github.com/flutter/flutter/issues/127160

We should avoid creating multiple message loops that each have as many threads as processors on the machine.
This commit is contained in:
Dan Field
2023-05-19 20:41:18 -07:00
committed by GitHub
parent 8812268345
commit f9b687885e
13 changed files with 88 additions and 20 deletions

View File

@@ -228,6 +228,7 @@
../../../flutter/shell/platform/android/.gitignore
../../../flutter/shell/platform/android/android_context_gl_impeller_unittests.cc
../../../flutter/shell/platform/android/android_context_gl_unittests.cc
../../../flutter/shell/platform/android/android_context_vulkan_impeller_unittests.cc
../../../flutter/shell/platform/android/android_shell_holder_unittests.cc
../../../flutter/shell/platform/android/apk_asset_provider_unittests.cc
../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc

View File

@@ -120,6 +120,13 @@ void ContextVK::Setup(Settings settings) {
return;
}
if (!settings.worker_task_runner) {
VALIDATION_LOG
<< "Cannot set up a Vulkan context without a worker task runner.";
return;
}
worker_task_runner_ = settings.worker_task_runner;
auto& dispatcher = VULKAN_HPP_DEFAULT_DISPATCHER;
dispatcher.init(settings.proc_address_callback);
@@ -313,7 +320,7 @@ void ContextVK::Setup(Settings settings) {
new PipelineLibraryVK(shared_from_this(), //
caps, //
std::move(settings.cache_directory), //
settings.worker_task_runner //
worker_task_runner_ //
));
if (!pipeline_library->IsValid()) {
@@ -429,6 +436,11 @@ const vk::Device& ContextVK::GetDevice() const {
return device_.get();
}
const std::shared_ptr<fml::ConcurrentTaskRunner>
ContextVK::GetConcurrentWorkerTaskRunner() const {
return worker_task_runner_;
}
std::unique_ptr<Surface> ContextVK::AcquireNextSurface() {
TRACE_EVENT0("impeller", __FUNCTION__);
auto surface = swapchain_ ? swapchain_->AcquireNextDrawable() : nullptr;

View File

@@ -113,6 +113,9 @@ class ContextVK final : public Context,
// |DeviceHolder|
const vk::Device& GetDevice() const override;
const std::shared_ptr<fml::ConcurrentTaskRunner>
GetConcurrentWorkerTaskRunner() const;
[[nodiscard]] bool SetWindowSurface(vk::UniqueSurfaceKHR surface);
std::unique_ptr<Surface> AcquireNextSurface();
@@ -141,6 +144,7 @@ class ContextVK final : public Context,
std::shared_ptr<const Capabilities> device_capabilities_;
std::shared_ptr<FenceWaiterVK> fence_waiter_;
std::string device_name_;
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner_;
const uint64_t hash_;
bool is_valid_ = false;

View File

@@ -28,6 +28,7 @@ PipelineLibraryVK::PipelineLibraryVK(
device_holder,
std::move(cache_directory))),
worker_task_runner_(std::move(worker_task_runner)) {
FML_DCHECK(worker_task_runner_);
if (!pso_cache_->IsValid() || !worker_task_runner_) {
return;
}

View File

@@ -2106,4 +2106,13 @@ const std::weak_ptr<VsyncWaiter> Shell::GetVsyncWaiter() const {
return engine_->GetVsyncWaiter();
}
const std::shared_ptr<fml::ConcurrentTaskRunner>
Shell::GetConcurrentWorkerTaskRunner() const {
FML_DCHECK(vm_);
if (!vm_) {
return nullptr;
}
return vm_->GetConcurrentWorkerTaskRunner();
}
} // namespace flutter

View File

@@ -402,6 +402,9 @@ class Shell final : public PlatformView::Delegate,
const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const;
const std::shared_ptr<fml::ConcurrentTaskRunner>
GetConcurrentWorkerTaskRunner() const;
private:
using ServiceProtocolHandler =
std::function<bool(const ServiceProtocol::Handler::ServiceProtocolMap&,

View File

@@ -41,6 +41,7 @@ executable("flutter_shell_native_unittests") {
sources = [
"android_context_gl_impeller_unittests.cc",
"android_context_gl_unittests.cc",
"android_context_vulkan_impeller_unittests.cc",
"android_shell_holder_unittests.cc",
"apk_asset_provider_unittests.cc",
"flutter_shell_native_unittests.cc",

View File

@@ -14,7 +14,7 @@ namespace flutter {
static std::shared_ptr<impeller::Context> CreateImpellerContext(
const fml::RefPtr<vulkan::VulkanProcTable>& proc_table,
const std::shared_ptr<fml::ConcurrentMessageLoop>& concurrent_loop,
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
bool enable_vulkan_validation) {
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
@@ -32,18 +32,18 @@ static std::shared_ptr<impeller::Context> CreateImpellerContext(
settings.proc_address_callback = instance_proc_addr;
settings.shader_libraries_data = std::move(shader_mappings);
settings.cache_directory = fml::paths::GetCachesDirectory();
settings.worker_task_runner = concurrent_loop->GetTaskRunner();
settings.worker_task_runner = std::move(worker_task_runner);
settings.enable_validation = enable_vulkan_validation;
return impeller::ContextVK::Create(std::move(settings));
}
AndroidContextVulkanImpeller::AndroidContextVulkanImpeller(
bool enable_validation)
bool enable_validation,
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner)
: AndroidContext(AndroidRenderingAPI::kVulkan),
proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()),
workers_(fml::ConcurrentMessageLoop::Create()) {
auto impeller_context =
CreateImpellerContext(proc_table_, workers_, enable_validation);
proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()) {
auto impeller_context = CreateImpellerContext(
proc_table_, std::move(worker_task_runner), enable_validation);
SetImpellerContext(impeller_context);
is_valid_ =
proc_table_->HasAcquiredMandatoryProcAddresses() && impeller_context;

View File

@@ -7,7 +7,6 @@
#include "flutter/fml/concurrent_message_loop.h"
#include "flutter/fml/macros.h"
#include "flutter/impeller/toolkit/egl/display.h"
#include "flutter/shell/platform/android/context/android_context.h"
#include "flutter/vulkan/procs/vulkan_proc_table.h"
@@ -15,7 +14,9 @@ namespace flutter {
class AndroidContextVulkanImpeller : public AndroidContext {
public:
explicit AndroidContextVulkanImpeller(bool enable_validation);
AndroidContextVulkanImpeller(
bool enable_validation,
std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner);
~AndroidContextVulkanImpeller();
@@ -24,7 +25,6 @@ class AndroidContextVulkanImpeller : public AndroidContext {
private:
fml::RefPtr<vulkan::VulkanProcTable> proc_table_;
std::shared_ptr<fml::ConcurrentMessageLoop> workers_;
bool is_valid_ = false;
FML_DISALLOW_COPY_AND_ASSIGN(AndroidContextVulkanImpeller);

View File

@@ -0,0 +1,31 @@
#include <memory>
#include "flutter/fml/synchronization/waitable_event.h"
#include "flutter/impeller/renderer/backend/vulkan/context_vk.h"
#include "flutter/shell/platform/android/android_context_vulkan_impeller.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace flutter {
namespace testing {
TEST(AndroidContextVulkanImpeller, DoesNotCreateOwnMessageLoop) {
auto loop = fml::ConcurrentMessageLoop::Create();
auto context = std::make_unique<AndroidContextVulkanImpeller>(
false, loop->GetTaskRunner());
ASSERT_TRUE(context);
auto impeller_context = std::static_pointer_cast<impeller::ContextVK>(
context->GetImpellerContext());
ASSERT_TRUE(impeller_context);
fml::AutoResetWaitableEvent latch;
impeller_context->GetConcurrentWorkerTaskRunner()->PostTask([loop, &latch]() {
ASSERT_TRUE(loop->RunsTasksOnCurrentThread());
latch.Signal();
});
latch.Wait();
}
} // namespace testing
} // namespace flutter

View File

@@ -109,9 +109,10 @@ AndroidShellHolder::AndroidShellHolder(
[&jni_facade, &weak_platform_view](Shell& shell) {
std::unique_ptr<PlatformViewAndroid> platform_view_android;
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
jni_facade, // JNI interop
shell, // delegate
shell.GetTaskRunners(), // task runners
shell.GetConcurrentWorkerTaskRunner(), // worker task runner
jni_facade, // JNI interop
shell.GetSettings()
.enable_software_rendering, // use software rendering
shell.GetSettings().msaa_samples // msaa sample count

View File

@@ -70,6 +70,7 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
bool use_software_rendering,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
uint8_t msaa_samples,
bool enable_impeller,
bool enable_vulkan_validation) {
@@ -80,7 +81,7 @@ static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
// TODO(kaushikiska@): Enable this after wiring a preference for Vulkan
// backend.
#if false
return std::make_unique<AndroidContextVulkanImpeller>(enable_vulkan_validation);
return std::make_unique<AndroidContextVulkanImpeller>(enable_vulkan_validation, std::move(worker_task_runner));
#else
return std::make_unique<AndroidContextGLImpeller>(
std::make_unique<impeller::egl::Display>());
@@ -97,6 +98,7 @@ static std::shared_ptr<flutter::AndroidContext> CreateAndroidContext(
PlatformViewAndroid::PlatformViewAndroid(
PlatformView::Delegate& delegate,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
bool use_software_rendering,
uint8_t msaa_samples)
@@ -107,6 +109,7 @@ PlatformViewAndroid::PlatformViewAndroid(
CreateAndroidContext(
use_software_rendering,
task_runners,
worker_task_runner,
msaa_samples,
delegate.OnPlatformViewGetSettings().enable_impeller,
delegate.OnPlatformViewGetSettings().enable_vulkan_validation)) {}

View File

@@ -42,11 +42,13 @@ class PlatformViewAndroid final : public PlatformView {
public:
static bool Register(JNIEnv* env);
PlatformViewAndroid(PlatformView::Delegate& delegate,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
bool use_software_rendering,
uint8_t msaa_samples);
PlatformViewAndroid(
PlatformView::Delegate& delegate,
const flutter::TaskRunners& task_runners,
const std::shared_ptr<fml::ConcurrentTaskRunner>& worker_task_runner,
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade,
bool use_software_rendering,
uint8_t msaa_samples);
//----------------------------------------------------------------------------
/// @brief Creates a new PlatformViewAndroid but using an existing