From fcb244895894cf34cc292fab02363d4cfa019f87 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Thu, 4 Apr 2024 12:22:08 -0700 Subject: [PATCH 1/2] Remove #if SHELL_ENABLE_METAL checks in iOS code (#51636) All physical iOS devices Flutter supports (iOS 12+) can run Metal. The only time Flutter doesn't use Metal afaik is for iOS simulators running < iOS 13, which is covered by a few `if (@available(iOS METAL_IOS_VERSION_BASELINE, *))` checks. https://github.com/flutter/engine/blob/8a51e124fbf168295a1b05f8d66459bfb29ae8a9/shell/platform/darwin/ios/rendering_api_selection.h#L37-L41 Remove hardware checks for physical devices. Remove `shell_enable_metal` from the gn files and the `#if SHELL_ENABLE_METAL` checks in the iOS embedder. I limited this PR to just iOS, but I imagine it's safe to remove `shell_enable_metal` everywhere? https://github.com/flutter/engine/blob/41da00ac46bc38a97a63ed0635450271d71afd9f/shell/platform/darwin/macos/BUILD.gn#L18 https://github.com/flutter/engine/blob/41da00ac46bc38a97a63ed0635450271d71afd9f/tools/gn#L673-L679 --- shell/platform/darwin/ios/BUILD.gn | 30 ++++++++----------- shell/platform/darwin/ios/ios_context.mm | 7 +---- shell/platform/darwin/ios/ios_surface.mm | 10 ++----- .../darwin/ios/rendering_api_selection.mm | 21 ++----------- 4 files changed, 17 insertions(+), 51 deletions(-) diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index 2c8d038e9ccfe..60f98dd61d5d6 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -19,7 +19,7 @@ shell_gpu_configuration("ios_gpu_configuration") { enable_software = true enable_gl = false enable_vulkan = false - enable_metal = shell_enable_metal + enable_metal = true } # The headers that will be copied to the Flutter.framework and be accessed @@ -147,12 +147,22 @@ source_set("flutter_framework_source") { "framework/Source/vsync_waiter_ios.mm", "ios_context.h", "ios_context.mm", + "ios_context_metal_impeller.h", + "ios_context_metal_impeller.mm", + "ios_context_metal_skia.h", + "ios_context_metal_skia.mm", "ios_context_software.h", "ios_context_software.mm", + "ios_external_texture_metal.h", + "ios_external_texture_metal.mm", "ios_external_view_embedder.h", "ios_external_view_embedder.mm", "ios_surface.h", "ios_surface.mm", + "ios_surface_metal_impeller.h", + "ios_surface_metal_impeller.mm", + "ios_surface_metal_skia.h", + "ios_surface_metal_skia.mm", "ios_surface_software.h", "ios_surface_software.mm", "platform_message_handler_ios.h", @@ -170,23 +180,6 @@ source_set("flutter_framework_source") { defines += [ "APPLICATION_EXTENSION_API_ONLY=1" ] } - if (shell_enable_metal) { - sources += [ - "ios_context_metal_impeller.h", - "ios_context_metal_impeller.mm", - "ios_context_metal_skia.h", - "ios_context_metal_skia.mm", - "ios_external_texture_metal.h", - "ios_external_texture_metal.mm", - "ios_surface_metal_impeller.h", - "ios_surface_metal_impeller.mm", - "ios_surface_metal_skia.h", - "ios_surface_metal_skia.mm", - ] - - deps += [ "//flutter/shell/platform/darwin/graphics" ] - } - deps += [ ":ios_gpu_configuration", "//flutter/common", @@ -200,6 +193,7 @@ source_set("flutter_framework_source") { "//flutter/shell/platform/common:common_cpp_input", "//flutter/shell/platform/darwin/common", "//flutter/shell/platform/darwin/common:framework_common", + "//flutter/shell/platform/darwin/graphics", "//flutter/shell/platform/embedder:embedder_as_internal_library", "//flutter/shell/profiling:profiling", "//flutter/skia", diff --git a/shell/platform/darwin/ios/ios_context.mm b/shell/platform/darwin/ios/ios_context.mm index 0f74ded917fe4..2a5fd7bc5de15 100644 --- a/shell/platform/darwin/ios/ios_context.mm +++ b/shell/platform/darwin/ios/ios_context.mm @@ -6,12 +6,9 @@ #include "flutter/shell/platform/darwin/ios/rendering_api_selection.h" #include "flutter/fml/logging.h" -#include "flutter/shell/platform/darwin/ios/ios_context_software.h" - -#if SHELL_ENABLE_METAL #include "flutter/shell/platform/darwin/ios/ios_context_metal_impeller.h" #include "flutter/shell/platform/darwin/ios/ios_context_metal_skia.h" -#endif // SHELL_ENABLE_METAL +#include "flutter/shell/platform/darwin/ios/ios_context_software.h" namespace flutter { @@ -32,7 +29,6 @@ "in an environment that does not support Metal. Enabling GPU pass through in your " "environment may fix this. If that is not possible, then disable Impeller."; return std::make_unique(); -#if SHELL_ENABLE_METAL case IOSRenderingAPI::kMetal: switch (backend) { case IOSRenderingBackend::kSkia: @@ -40,7 +36,6 @@ case IOSRenderingBackend::kImpeller: return std::make_unique(is_gpu_disabled_sync_switch); } -#endif // SHELL_ENABLE_METAL default: break; } diff --git a/shell/platform/darwin/ios/ios_surface.mm b/shell/platform/darwin/ios/ios_surface.mm index 63745aaebefa0..d09778c6001c3 100644 --- a/shell/platform/darwin/ios/ios_surface.mm +++ b/shell/platform/darwin/ios/ios_surface.mm @@ -4,14 +4,10 @@ #import "flutter/shell/platform/darwin/ios/ios_surface.h" -#import "flutter/shell/platform/darwin/ios/ios_surface_software.h" - -#include "flutter/shell/platform/darwin/ios/rendering_api_selection.h" - -#if SHELL_ENABLE_METAL #import "flutter/shell/platform/darwin/ios/ios_surface_metal_impeller.h" #import "flutter/shell/platform/darwin/ios/ios_surface_metal_skia.h" -#endif // SHELL_ENABLE_METAL +#import "flutter/shell/platform/darwin/ios/ios_surface_software.h" +#include "flutter/shell/platform/darwin/ios/rendering_api_selection.h" namespace flutter { @@ -20,7 +16,6 @@ FML_DCHECK(layer); FML_DCHECK(context); -#if SHELL_ENABLE_METAL if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { if ([layer.get() isKindOfClass:[CAMetalLayer class]]) { switch (context->GetBackend()) { @@ -40,7 +35,6 @@ } } } -#endif // SHELL_ENABLE_METAL return std::make_unique(layer, // layer std::move(context) // context diff --git a/shell/platform/darwin/ios/rendering_api_selection.mm b/shell/platform/darwin/ios/rendering_api_selection.mm index f7b969066ff0d..3cf47c8145338 100644 --- a/shell/platform/darwin/ios/rendering_api_selection.mm +++ b/shell/platform/darwin/ios/rendering_api_selection.mm @@ -5,11 +5,9 @@ #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h" #include +#include #include #import -#if SHELL_ENABLE_METAL -#include -#endif // SHELL_ENABLE_METAL #import #include "flutter/fml/logging.h" @@ -18,18 +16,6 @@ namespace flutter { -#if SHELL_ENABLE_METAL -bool ShouldUseMetalRenderer() { - bool ios_version_supports_metal = false; - if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { - auto device = MTLCreateSystemDefaultDevice(); - ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3]; - [device release]; - } - return ios_version_supports_metal; -} -#endif // SHELL_ENABLE_METAL - IOSRenderingAPI GetRenderingAPIForProcess(bool force_software) { #if TARGET_OS_SIMULATOR if (force_software) { @@ -42,12 +28,9 @@ IOSRenderingAPI GetRenderingAPIForProcess(bool force_software) { } #endif // TARGET_OS_SIMULATOR -#if SHELL_ENABLE_METAL - static bool should_use_metal = ShouldUseMetalRenderer(); - if (should_use_metal) { + if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { return IOSRenderingAPI::kMetal; } -#endif // SHELL_ENABLE_METAL // When Metal isn't available we use Skia software rendering since it performs // a little better than emulated OpenGL. Also, omitting an OpenGL backend From fed113282b8552e9eee598830e4c72cd140bab8b Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Fri, 5 Apr 2024 14:17:02 -0700 Subject: [PATCH 2/2] Re-add ShouldUseMetalRenderer, remove shell_enable_metal from macOS BUILD gn --- .../platform/darwin/ios/rendering_api_selection.mm | 13 ++++++++++++- shell/platform/darwin/macos/BUILD.gn | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/rendering_api_selection.mm b/shell/platform/darwin/ios/rendering_api_selection.mm index 3cf47c8145338..b9659796bb9b5 100644 --- a/shell/platform/darwin/ios/rendering_api_selection.mm +++ b/shell/platform/darwin/ios/rendering_api_selection.mm @@ -16,6 +16,16 @@ namespace flutter { +bool ShouldUseMetalRenderer() { + bool ios_version_supports_metal = false; + if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { + auto device = MTLCreateSystemDefaultDevice(); + ios_version_supports_metal = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3]; + [device release]; + } + return ios_version_supports_metal; +} + IOSRenderingAPI GetRenderingAPIForProcess(bool force_software) { #if TARGET_OS_SIMULATOR if (force_software) { @@ -28,7 +38,8 @@ IOSRenderingAPI GetRenderingAPIForProcess(bool force_software) { } #endif // TARGET_OS_SIMULATOR - if (@available(iOS METAL_IOS_VERSION_BASELINE, *)) { + static bool should_use_metal = ShouldUseMetalRenderer(); + if (should_use_metal) { return IOSRenderingAPI::kMetal; } diff --git a/shell/platform/darwin/macos/BUILD.gn b/shell/platform/darwin/macos/BUILD.gn index b56bb449bd3ff..401b63bc0b8b6 100644 --- a/shell/platform/darwin/macos/BUILD.gn +++ b/shell/platform/darwin/macos/BUILD.gn @@ -15,7 +15,7 @@ shell_gpu_configuration("macos_gpu_configuration") { enable_software = true enable_gl = true enable_vulkan = false - enable_metal = shell_enable_metal + enable_metal = true } group("macos") {