From 9235fbb69508c4ca689b4c6c7459d580e809575c Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 13 Sep 2024 14:39:20 -0700 Subject: [PATCH] [impeller] add Android flag for disabling surface control for debugging. (flutter/engine#55185) I've found a few instances where Vulkan worked correctly but surface control did not. lets add a debugging flag we can ask folks to try to narrow down the issue. --- engine/src/flutter/common/settings.h | 3 +++ .../renderer/backend/vulkan/context_vk.cc | 5 +++++ .../renderer/backend/vulkan/context_vk.h | 6 +++++ .../backend/vulkan/swapchain/swapchain_vk.cc | 7 ++++-- engine/src/flutter/shell/common/switches.cc | 3 +++ engine/src/flutter/shell/common/switches.h | 3 +++ .../android/android_context_vk_impeller.cc | 20 ++++++++--------- .../android/android_context_vk_impeller.h | 5 ++--- .../android/context/android_context.h | 7 ++++++ .../shell/platform/android/flutter_main.cc | 7 +++--- .../engine/loader/FlutterLoader.java | 6 +++++ .../platform/android/platform_view_android.cc | 21 ++++++++++++------ .../engine/loader/FlutterLoaderTest.java | 22 +++++++++++++++++++ 13 files changed, 89 insertions(+), 26 deletions(-) diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 7eb09b4fe0..345cf3d977 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -229,6 +229,9 @@ struct Settings { bool enable_impeller = false; #endif + // Force disable the android surface control even where supported. + bool disable_surface_control = false; + // If true, the UI thread is the platform thread on supported // platforms. bool merged_platform_ui_thread = true; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc index fa2711c1e6..2dfcb236fb 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.cc @@ -445,6 +445,7 @@ void ContextVK::Setup(Settings settings) { descriptor_pool_recycler_ = std::move(descriptor_pool_recycler); device_name_ = std::string(physical_device_properties.deviceName); command_queue_vk_ = std::make_shared(weak_from_this()); + should_disable_surface_control_ = settings.disable_surface_control; is_valid_ = true; // Create the GPU Tracer later because it depends on state from @@ -616,4 +617,8 @@ const std::unique_ptr& ContextVK::GetDriverInfo() const { return driver_info_; } +bool ContextVK::GetShouldDisableSurfaceControlSwapchain() const { + return should_disable_surface_control_; +} + } // namespace impeller diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h index 22909582ee..3d9b03e702 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/context_vk.h @@ -49,6 +49,7 @@ class ContextVK final : public Context, fml::UniqueFD cache_directory; bool enable_validation = false; bool enable_gpu_tracing = false; + bool disable_surface_control = false; /// If validations are requested but cannot be enabled, log a fatal error. bool fatal_missing_validations = false; @@ -168,6 +169,10 @@ class ContextVK final : public Context, void InitializeCommonlyUsedShadersIfNeeded() const override; + /// @brief Whether the Android Surface control based swapchain should be + /// disabled, even if the device is capable of supporting it. + bool GetShouldDisableSurfaceControlSwapchain() const; + private: struct DeviceHolderImpl : public DeviceHolderVK { // |DeviceHolder| @@ -200,6 +205,7 @@ class ContextVK final : public Context, std::shared_ptr gpu_tracer_; std::shared_ptr descriptor_pool_recycler_; std::shared_ptr command_queue_vk_; + bool should_disable_surface_control_ = false; const uint64_t hash_; diff --git a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc index f6c6d440e4..1b872778db 100644 --- a/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc +++ b/engine/src/flutter/impeller/renderer/backend/vulkan/swapchain/swapchain_vk.cc @@ -57,11 +57,14 @@ std::shared_ptr SwapchainVK::Create( } // TODO(147533): AHB swapchains on emulators are not functional. - const auto emulator = ContextVK::Cast(*context).GetDriverInfo()->IsEmulator(); + auto& context_vk = ContextVK::Cast(*context); + const auto emulator = context_vk.GetDriverInfo()->IsEmulator(); + const auto should_disable_sc = + context_vk.GetShouldDisableSurfaceControlSwapchain(); // Try AHB swapchains first. if (!emulator && AHBSwapchainVK::IsAvailableOnPlatform() && - !android::ShadowRealm::ShouldDisableAHB()) { + !android::ShadowRealm::ShouldDisableAHB() && !should_disable_sc) { auto ahb_swapchain = std::shared_ptr(new AHBSwapchainVK( context, // window.GetHandle(), // diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index eae5b0c056..de131b00fd 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -524,6 +524,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { settings.enable_platform_isolates = command_line.HasOption(FlagForSwitch(Switch::EnablePlatformIsolates)); + settings.disable_surface_control = command_line.HasOption( + FlagForSwitch(Switch::DisableAndroidSurfaceControl)); + return settings; } diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index be0499543b..25ecd83ae7 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -297,6 +297,9 @@ DEF_SWITCH(EnablePlatformIsolates, DEF_SWITCH(EnableMergedPlatformUIThread, "enable-merged-platform-ui-thread", "Merge the ui thread and platform thread.") +DEF_SWITCH(DisableAndroidSurfaceControl, + "disable-surface-control", + "Disable the SurfaceControl backed swapchain even when supported.") DEF_SWITCHES_END void PrintUsage(const std::string& executable_name); diff --git a/engine/src/flutter/shell/platform/android/android_context_vk_impeller.cc b/engine/src/flutter/shell/platform/android/android_context_vk_impeller.cc index 7d8dc26ba8..35a69f431d 100644 --- a/engine/src/flutter/shell/platform/android/android_context_vk_impeller.cc +++ b/engine/src/flutter/shell/platform/android/android_context_vk_impeller.cc @@ -10,14 +10,13 @@ #include "flutter/impeller/entity/vk/framebuffer_blend_shaders_vk.h" #include "flutter/impeller/entity/vk/modern_shaders_vk.h" #include "flutter/impeller/renderer/backend/vulkan/context_vk.h" +#include "shell/platform/android/context/android_context.h" namespace flutter { static std::shared_ptr CreateImpellerContext( const fml::RefPtr& vulkan_dylib, - bool enable_vulkan_validation, - bool enable_gpu_tracing, - bool quiet) { + const AndroidContext::ContextSettings& p_settings) { if (!vulkan_dylib) { VALIDATION_LOG << "Could not open the Vulkan dylib."; return nullptr; @@ -46,12 +45,13 @@ static std::shared_ptr CreateImpellerContext( settings.proc_address_callback = instance_proc_addr.value(); settings.shader_libraries_data = std::move(shader_mappings); settings.cache_directory = fml::paths::GetCachesDirectory(); - settings.enable_validation = enable_vulkan_validation; - settings.enable_gpu_tracing = enable_gpu_tracing; + settings.enable_validation = p_settings.enable_validation; + settings.enable_gpu_tracing = p_settings.enable_gpu_tracing; + settings.disable_surface_control = p_settings.disable_surface_control; auto context = impeller::ContextVK::Create(std::move(settings)); - if (!quiet) { + if (!p_settings.quiet) { if (context && impeller::CapabilitiesVK::Cast(*context->GetCapabilities()) .AreValidationsEnabled()) { FML_LOG(IMPORTANT) << "Using the Impeller rendering backend (Vulkan with " @@ -69,13 +69,11 @@ static std::shared_ptr CreateImpellerContext( return context; } -AndroidContextVKImpeller::AndroidContextVKImpeller(bool enable_validation, - bool enable_gpu_tracing, - bool quiet) +AndroidContextVKImpeller::AndroidContextVKImpeller( + const AndroidContext::ContextSettings& settings) : AndroidContext(AndroidRenderingAPI::kImpellerVulkan), vulkan_dylib_(fml::NativeLibrary::Create("libvulkan.so")) { - auto impeller_context = CreateImpellerContext( - vulkan_dylib_, enable_validation, enable_gpu_tracing, quiet); + auto impeller_context = CreateImpellerContext(vulkan_dylib_, settings); SetImpellerContext(impeller_context); is_valid_ = !!impeller_context; } diff --git a/engine/src/flutter/shell/platform/android/android_context_vk_impeller.h b/engine/src/flutter/shell/platform/android/android_context_vk_impeller.h index afad4001bb..65b13d7056 100644 --- a/engine/src/flutter/shell/platform/android/android_context_vk_impeller.h +++ b/engine/src/flutter/shell/platform/android/android_context_vk_impeller.h @@ -14,9 +14,8 @@ namespace flutter { class AndroidContextVKImpeller : public AndroidContext { public: - AndroidContextVKImpeller(bool enable_validation, - bool enable_gpu_tracing, - bool quiet = false); + explicit AndroidContextVKImpeller( + const AndroidContext::ContextSettings& settings); ~AndroidContextVKImpeller(); diff --git a/engine/src/flutter/shell/platform/android/context/android_context.h b/engine/src/flutter/shell/platform/android/context/android_context.h index 8f4fc005fc..18ed3315c9 100644 --- a/engine/src/flutter/shell/platform/android/context/android_context.h +++ b/engine/src/flutter/shell/platform/android/context/android_context.h @@ -22,6 +22,13 @@ class AndroidContext { virtual ~AndroidContext(); + struct ContextSettings { + bool enable_validation = false; + bool enable_gpu_tracing = false; + bool disable_surface_control = false; + bool quiet = false; + }; + AndroidRenderingAPI RenderingApi() const; virtual bool IsValid() const; diff --git a/engine/src/flutter/shell/platform/android/flutter_main.cc b/engine/src/flutter/shell/platform/android/flutter_main.cc index 6c3e21835e..184a2e98d4 100644 --- a/engine/src/flutter/shell/platform/android/flutter_main.cc +++ b/engine/src/flutter/shell/platform/android/flutter_main.cc @@ -23,6 +23,7 @@ #include "flutter/shell/common/shell.h" #include "flutter/shell/common/switches.h" #include "flutter/shell/platform/android/android_context_vk_impeller.h" +#include "flutter/shell/platform/android/context/android_context.h" #include "flutter/shell/platform/android/flutter_main.h" #include "impeller/base/validation.h" #include "impeller/toolkit/android/proc_table.h" @@ -269,9 +270,9 @@ AndroidRenderingAPI FlutterMain::SelectedRenderingAPI( // checking if it is valid. impeller::ScopedValidationDisable disable_validation; auto vulkan_backend = std::make_unique( - /*enable_vulkan_validation=*/false, - /*enable_vulkan_gpu_tracing=*/false, - /*quiet=*/true); + AndroidContext::ContextSettings{.enable_validation = false, + .enable_gpu_tracing = false, + .quiet = true}); if (!vulkan_backend->IsValid()) { return kVulkanUnsupportedFallback; } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java index 92a6e1e5dd..c9c55656e7 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java @@ -49,6 +49,8 @@ public class FlutterLoader { "io.flutter.embedding.android.EnableVulkanGPUTracing"; private static final String ENABLED_MERGED_PLATFORM_UI_THREAD_KEY = "io.flutter.embedding.android.EnableMergedPlatformUIThread"; + private static final String DISABLE_SURFACE_CONTROL = + "io.flutter.embedding.android.DisableSurfaceControl"; /** * Set whether leave or clean up the VM after the last shell shuts down. It can be set from app's @@ -361,6 +363,9 @@ public class FlutterLoader { if (metaData.getBoolean(IMPELLER_VULKAN_GPU_TRACING_DATA_KEY, false)) { shellArgs.add("--enable-vulkan-gpu-tracing"); } + if (metaData.getBoolean(DISABLE_SURFACE_CONTROL, false)) { + shellArgs.add("--disable-surface-control"); + } if (metaData.containsKey(ENABLED_MERGED_PLATFORM_UI_THREAD_KEY)) { if (metaData.getBoolean(ENABLED_MERGED_PLATFORM_UI_THREAD_KEY)) { shellArgs.add("--enable-merged-platform-ui-thread"); @@ -368,6 +373,7 @@ public class FlutterLoader { shellArgs.add("--no-enable-merged-platform-ui-thread"); } } + String backend = metaData.getString(IMPELLER_BACKEND_META_DATA_KEY); if (backend != null) { shellArgs.add("--impeller-backend=" + backend); diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 926c99928b..0e658e1f13 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -38,6 +38,17 @@ namespace flutter { +namespace { +AndroidContext::ContextSettings CreateContextSettings( + const Settings& p_settings) { + AndroidContext::ContextSettings settings; + settings.enable_gpu_tracing = p_settings.enable_vulkan_gpu_tracing; + settings.enable_validation = p_settings.enable_vulkan_validation; + settings.disable_surface_control = p_settings.disable_surface_control; + return settings; +} +} // namespace + AndroidSurfaceFactoryImpl::AndroidSurfaceFactoryImpl( const std::shared_ptr& context, bool enable_impeller) @@ -66,9 +77,8 @@ static std::shared_ptr CreateAndroidContext( bool use_software_rendering, const flutter::TaskRunners& task_runners, AndroidRenderingAPI android_rendering_api, - bool enable_vulkan_validation, bool enable_opengl_gpu_tracing, - bool enable_vulkan_gpu_tracing) { + const AndroidContext::ContextSettings& settings) { switch (android_rendering_api) { case AndroidRenderingAPI::kSoftware: return std::make_shared(AndroidRenderingAPI::kSoftware); @@ -77,8 +87,7 @@ static std::shared_ptr CreateAndroidContext( std::make_unique(), enable_opengl_gpu_tracing); case AndroidRenderingAPI::kImpellerVulkan: - return std::make_unique( - enable_vulkan_validation, enable_vulkan_gpu_tracing); + return std::make_unique(settings); case AndroidRenderingAPI::kSkiaOpenGLES: return std::make_unique( fml::MakeRefCounted(), // @@ -101,10 +110,8 @@ PlatformViewAndroid::PlatformViewAndroid( use_software_rendering, task_runners, delegate.OnPlatformViewGetSettings().android_rendering_api, - delegate.OnPlatformViewGetSettings().enable_vulkan_validation, delegate.OnPlatformViewGetSettings().enable_opengl_gpu_tracing, - delegate.OnPlatformViewGetSettings().enable_vulkan_gpu_tracing)) { -} + CreateContextSettings(delegate.OnPlatformViewGetSettings()))) {} PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java index e403668887..2c197d3c58 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java @@ -236,6 +236,28 @@ public class FlutterLoaderTest { assertTrue(arguments.contains(enableImpellerArg)); } + @Test + public void itSetsDisableSurfaceControlFromMetaData() { + FlutterJNI mockFlutterJNI = mock(FlutterJNI.class); + FlutterLoader flutterLoader = new FlutterLoader(mockFlutterJNI); + Bundle metaData = new Bundle(); + metaData.putBoolean("io.flutter.embedding.android.DisableSurfaceControl", true); + ctx.getApplicationInfo().metaData = metaData; + + FlutterLoader.Settings settings = new FlutterLoader.Settings(); + assertFalse(flutterLoader.initialized()); + flutterLoader.startInitialization(ctx, settings); + flutterLoader.ensureInitializationComplete(ctx, null); + shadowOf(getMainLooper()).idle(); + + final String disabledControlArg = "--disable-surface-control"; + ArgumentCaptor shellArgsCaptor = ArgumentCaptor.forClass(String[].class); + verify(mockFlutterJNI, times(1)) + .init(eq(ctx), shellArgsCaptor.capture(), anyString(), anyString(), anyString(), anyLong()); + List arguments = Arrays.asList(shellArgsCaptor.getValue()); + assertTrue(arguments.contains(disabledControlArg)); + } + @Test @TargetApi(API_LEVELS.API_23) @Config(sdk = API_LEVELS.API_23)