From baee6242e33936fdada74a616396551a77d81fe7 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Fri, 3 Mar 2023 15:17:19 -0800 Subject: [PATCH] [ios] add setting flag to enable embedder api. (flutter/engine#40025) [ios] add setting flag to enable embedder api. --- engine/src/flutter/common/settings.h | 5 +++ engine/src/flutter/shell/common/switches.cc | 4 +++ engine/src/flutter/shell/common/switches.h | 3 ++ .../shell/common/switches_unittests.cc | 17 ++++++++++ .../framework/Source/FlutterDartProject.mm | 8 +++++ .../ios/framework/Source/FlutterEngine.mm | 13 ++++++++ .../Source/FlutterEnginePlatformViewTest.mm | 3 +- .../ios/framework/Source/FlutterEngineTest.mm | 32 ++++++++++++++++--- .../framework/Source/FlutterEngine_Internal.h | 29 +++++++++-------- .../ios/framework/Source/FlutterEngine_Test.h | 5 +++ .../Source/FlutterTextureRegistryRelayTest.mm | 2 +- 11 files changed, 99 insertions(+), 22 deletions(-) diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index 687822c6e0..e5c3940876 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -316,6 +316,11 @@ struct Settings { /// If it is not 0 or 1, it must be one of 2, 4, 8, or 16. However, if the /// GPU does not support the requested sampling value, MSAA will be disabled. uint8_t msaa_samples = 0; + + /// Enable embedder api on the embedder. + /// + /// This is currently only used by iOS. + bool enable_embedder_api = false; }; } // namespace flutter diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index 6e8307ab4c..63c12cbe3b 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -446,6 +446,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { settings.enable_impeller = command_line.HasOption(FlagForSwitch(Switch::EnableImpeller)); + settings.enable_embedder_api = + command_line.HasOption(FlagForSwitch(Switch::EnableEmbedderAPI)); + settings.prefetched_default_font_manager = command_line.HasOption( FlagForSwitch(Switch::PrefetchedDefaultFontManager)); @@ -513,6 +516,7 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { << "' (expected 0, 1, 2, 4, 8, or 16)."; } } + return settings; } diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index c6cb7f4d89..f0cdd5e26c 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -273,6 +273,9 @@ DEF_SWITCH( "Setting this value to 0 or 1 disables MSAA. If it is not 0 or 1, it must " "be one of 2, 4, 8, or 16. However, if the GPU does not support the " "requested sampling value, MSAA will be disabled.") +DEF_SWITCH(EnableEmbedderAPI, + "enable-embedder-api", + "Enable the embedder api. Defaults to false. iOS only.") DEF_SWITCHES_END void PrintUsage(const std::string& executable_name); diff --git a/engine/src/flutter/shell/common/switches_unittests.cc b/engine/src/flutter/shell/common/switches_unittests.cc index c919e873df..f6a6d28886 100644 --- a/engine/src/flutter/shell/common/switches_unittests.cc +++ b/engine/src/flutter/shell/common/switches_unittests.cc @@ -76,5 +76,22 @@ TEST(SwitchesTest, MsaaSamples) { EXPECT_EQ(settings.msaa_samples, 0); } +TEST(SwitchesTest, EnableEmbedderAPI) { + { + // enable + fml::CommandLine command_line = fml::CommandLineFromInitializerList( + {"command", "--enable-embedder-api"}); + Settings settings = SettingsFromCommandLine(command_line); + EXPECT_EQ(settings.enable_embedder_api, true); + } + { + // default + fml::CommandLine command_line = + fml::CommandLineFromInitializerList({"command"}); + Settings settings = SettingsFromCommandLine(command_line); + EXPECT_EQ(settings.enable_embedder_api, false); + } +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm index f7d62e9c4c..ccf89a746b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm @@ -217,6 +217,14 @@ flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle) { CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale; settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4; + // Whether to enable ios embedder api. + NSNumber* enable_embedder_api = + [mainBundle objectForInfoDictionaryKey:@"FLTEnableIOSEmbedderAPI"]; + // Change the default only if the option is present. + if (enable_embedder_api) { + settings.enable_embedder_api = enable_embedder_api.boolValue; + } + return settings; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 9a94326c99..ff94acaa27 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -98,6 +98,12 @@ static constexpr int kNumProfilerSamplesPerSec = 5; @property(nonatomic, readwrite, copy) NSString* isolateId; @property(nonatomic, copy) NSString* initialRoute; @property(nonatomic, retain) id flutterViewControllerWillDeallocObserver; + +#pragma mark - Embedder API properties + +@property(nonatomic, assign) BOOL enableEmbedderAPI; +// Function pointers for interacting with the embedder.h API. +@property(nonatomic) FlutterEngineProcTable& embedderAPI; @end @implementation FlutterEngine { @@ -186,6 +192,13 @@ static constexpr int kNumProfilerSamplesPerSec = 5; _dartProject.reset([project retain]); } + _enableEmbedderAPI = _dartProject.get().settings.enable_embedder_api; + if (_enableEmbedderAPI) { + NSLog(@"============== iOS: enable_embedder_api is on =============="); + _embedderAPI.struct_size = sizeof(FlutterEngineProcTable); + FlutterEngineGetProcAddresses(&_embedderAPI); + } + if (!EnableTracingIfNecessary([_dartProject.get() settings])) { NSLog( @"Cannot create a FlutterEngine instance in debug mode without Flutter tooling or " diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm index 8245750b0f..e34a4ffc05 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEnginePlatformViewTest.mm @@ -112,8 +112,7 @@ flutter::FakeDelegate fake_delegate; } - (void)testCallsNotifyLowMemory { - id project = OCMClassMock([FlutterDartProject class]); - FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester" project:project]; + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"tester"]; XCTAssertNotNil(engine); id mockEngine = OCMPartialMock(engine); OCMStub([mockEngine notifyLowMemory]); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm index a10922f9da..7bd90a6a5a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm @@ -28,7 +28,7 @@ FLUTTER_ASSERT_ARC } - (void)testCreate { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; XCTAssertNotNil(engine); } @@ -101,7 +101,7 @@ FLUTTER_ASSERT_ARC } - (void)testSendMessageBeforeRun { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; XCTAssertNotNil(engine); XCTAssertThrows([engine.binaryMessenger @@ -111,7 +111,7 @@ FLUTTER_ASSERT_ARC } - (void)testSetMessageHandlerBeforeRun { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; XCTAssertNotNil(engine); XCTAssertThrows([engine.binaryMessenger @@ -122,7 +122,7 @@ FLUTTER_ASSERT_ARC } - (void)testNilSetMessageHandlerBeforeRun { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; XCTAssertNotNil(engine); XCTAssertNoThrow([engine.binaryMessenger setMessageHandlerOnChannel:@"foo" @@ -133,7 +133,7 @@ FLUTTER_ASSERT_ARC id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); OCMStub([plugin detachFromEngineForRegistrar:[OCMArg any]]); { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"engine" project:project]; NSObject* registrar = [engine registrarForPlugin:@"plugin"]; [registrar publish:plugin]; @@ -291,4 +291,26 @@ FLUTTER_ASSERT_ARC method_setImplementation(method, originalSetThreadPriority); } +- (void)testCanEnableDisableEmbedderAPIThroughInfoPlist { + { + // Not enable embedder API by default + auto settings = FLTDefaultSettingsForBundle(); + settings.enable_software_rendering = true; + FlutterDartProject* project = [[FlutterDartProject alloc] initWithSettings:settings]; + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; + XCTAssertFalse(engine.enableEmbedderAPI); + } + { + // Enable embedder api + id mockMainBundle = OCMPartialMock([NSBundle mainBundle]); + OCMStub([mockMainBundle objectForInfoDictionaryKey:@"FLTEnableIOSEmbedderAPI"]) + .andReturn(@"YES"); + auto settings = FLTDefaultSettingsForBundle(); + settings.enable_software_rendering = true; + FlutterDartProject* project = [[FlutterDartProject alloc] initWithSettings:settings]; + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; + XCTAssertTrue(engine.enableEmbedderAPI); + } +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h index 8a957ee709..6fe25e406a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h @@ -15,8 +15,6 @@ #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/shell.h" -// Embedder header included as an implementation detail (See BUILD.gn), iOS -// doesn't use the embedding API, just some structures from it. #include "flutter/shell/platform/embedder/embedder.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" @@ -29,7 +27,9 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" -extern NSString* _Nonnull const kFlutterEngineWillDealloc; +NS_ASSUME_NONNULL_BEGIN + +extern NSString* const kFlutterEngineWillDealloc; @interface FlutterEngine () @@ -46,10 +46,10 @@ extern NSString* _Nonnull const kFlutterEngineWillDealloc; - (flutter::Rasterizer::Screenshot)screenshot:(flutter::Rasterizer::ScreenshotType)type base64Encode:(bool)base64Encode; -- (nonnull FlutterPlatformPlugin*)platformPlugin; +- (FlutterPlatformPlugin*)platformPlugin; - (std::shared_ptr&)platformViewsController; -- (nonnull FlutterTextInputPlugin*)textInputPlugin; -- (nonnull FlutterRestorationPlugin*)restorationPlugin; +- (FlutterTextInputPlugin*)textInputPlugin; +- (FlutterRestorationPlugin*)restorationPlugin; - (void)launchEngine:(nullable NSString*)entrypoint libraryURI:(nullable NSString*)libraryOrNil entrypointArgs:(nullable NSArray*)entrypointArgs; @@ -58,10 +58,9 @@ extern NSString* _Nonnull const kFlutterEngineWillDealloc; initialRoute:(nullable NSString*)initialRoute; - (void)attachView; - (void)notifyLowMemory; -- (nonnull flutter::PlatformViewIOS*)iosPlatformView; +- (flutter::PlatformViewIOS*)iosPlatformView; -- (void)waitForFirstFrame:(NSTimeInterval)timeout - callback:(nonnull void (^)(BOOL didTimeout))callback; +- (void)waitForFirstFrame:(NSTimeInterval)timeout callback:(void (^)(BOOL didTimeout))callback; /** * Creates one running FlutterEngine from another, sharing components between them. @@ -69,10 +68,10 @@ extern NSString* _Nonnull const kFlutterEngineWillDealloc; * This results in a faster creation time and a smaller memory footprint engine. * This should only be called on a FlutterEngine that is running. */ -- (nonnull FlutterEngine*)spawnWithEntrypoint:(nullable NSString*)entrypoint - libraryURI:(nullable NSString*)libraryURI - initialRoute:(nullable NSString*)initialRoute - entrypointArgs:(nullable NSArray*)entrypointArgs; +- (FlutterEngine*)spawnWithEntrypoint:(nullable NSString*)entrypoint + libraryURI:(nullable NSString*)libraryURI + initialRoute:(nullable NSString*)initialRoute + entrypointArgs:(nullable NSArray*)entrypointArgs; /** * Dispatches the given key event data to the framework through the engine. @@ -82,7 +81,9 @@ extern NSString* _Nonnull const kFlutterEngineWillDealloc; callback:(nullable FlutterKeyEventCallback)callback userData:(nullable void*)userData; -@property(nonatomic, readonly, nonnull) FlutterDartProject* project; +@property(nonatomic, readonly) FlutterDartProject* project; @end +NS_ASSUME_NONNULL_END + #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTER_ENGINE_INTERNAL_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h index 70ffc7a5a6..1346c9aed6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h @@ -5,6 +5,7 @@ #import "flutter/shell/common/shell.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h" +#include "flutter/shell/platform/embedder/embedder.h" extern NSString* const kFlutterEngineWillDealloc; @@ -16,6 +17,10 @@ class ThreadHost; // Category to add test-only visibility. @interface FlutterEngine (Test) + +@property(readonly, nonatomic) FlutterEngineProcTable& embedderAPI; +@property(readonly, nonatomic) BOOL enableEmbedderAPI; + - (flutter::Shell&)shell; - (void)setBinaryMessenger:(FlutterBinaryMessengerRelay*)binaryMessenger; - (flutter::IOSRenderingAPI)platformViewsRenderingAPI; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm index c80cba66de..36b473dea8 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelayTest.mm @@ -55,7 +55,7 @@ FLUTTER_ASSERT_ARC __weak FlutterEngine* weakEngine; NSObject* strongRelay; @autoreleasepool { - id project = OCMClassMock([FlutterDartProject class]); + FlutterDartProject* project = [[FlutterDartProject alloc] init]; FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project]; strongRelay = [engine textureRegistry]; weakEngine = engine;