Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2f24554

Browse files
committed
Recreate platform view controller even if it exists
On simulator for example we instantiate the app with `initWithName` after this is done, we might still not have set the `FlutterView.forceSoftwareRendering` which is done in `createShell`. This will ensure that we create the platform view controller with the right rendering api.
1 parent c08c281 commit 2f24554

11 files changed

+78
-34
lines changed

shell/platform/darwin/ios/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ shared_library("ios_test_flutter") {
221221
deps = [
222222
":flutter_framework_source",
223223
":ios_test_flutter_mrc",
224+
"//flutter/common:common",
224225
"//flutter/shell/platform/darwin/common:framework_shared",
225226
"//flutter/third_party/tonic",
226227
"//flutter/third_party/txt",

shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin";
2929

30-
static flutter::Settings DefaultSettingsForProcess(NSBundle* bundle = nil) {
30+
flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle) {
3131
auto command_line = flutter::CommandLineFromNSProcessInfo();
3232

3333
// Precedence:
@@ -181,7 +181,17 @@ - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
181181
self = [super init];
182182

183183
if (self) {
184-
_settings = DefaultSettingsForProcess(bundle);
184+
_settings = FLTDefaultSettingsForBundle(bundle);
185+
}
186+
187+
return self;
188+
}
189+
190+
- (instancetype)initWithSettings:(const flutter::Settings&)settings {
191+
self = [self initWithPrecompiledDartBundle:nil];
192+
193+
if (self) {
194+
_settings = settings;
185195
}
186196

187197
return self;

shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@
1212

1313
NS_ASSUME_NONNULL_BEGIN
1414

15+
flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle = nil);
16+
1517
@interface FlutterDartProject ()
1618

19+
/**
20+
* This is currently used for *only for tests* to override settings.
21+
*/
22+
- (instancetype)initWithSettings:(const flutter::Settings&)settings;
1723
- (const flutter::Settings&)settings;
1824
- (const flutter::PlatformData)defaultPlatformData;
1925

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ @implementation FlutterEngine {
6666
fml::scoped_nsobject<FlutterObservatoryPublisher> _publisher;
6767

6868
std::shared_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
69+
flutter::IOSRenderingAPI _renderingApi;
6970
std::shared_ptr<flutter::IOSSurfaceFactory> _surfaceFactory;
7071
std::unique_ptr<flutter::ProfilerMetricsIOS> _profiler_metrics;
7172
std::unique_ptr<flutter::SamplingProfiler> _profiler;
@@ -131,7 +132,7 @@ - (instancetype)initWithName:(NSString*)labelPrefix
131132

132133
_pluginPublications = [NSMutableDictionary new];
133134
_registrars = [[NSMutableDictionary alloc] init];
134-
[self ensurePlatformViewController];
135+
[self recreatePlatformViewController];
135136

136137
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
137138
_connections.reset(new flutter::ConnectionCollection());
@@ -165,14 +166,15 @@ - (instancetype)initWithName:(NSString*)labelPrefix
165166
return self;
166167
}
167168

168-
- (void)ensurePlatformViewController {
169-
if (!_platformViewsController) {
170-
auto renderingApi = flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering);
171-
_surfaceFactory = flutter::IOSSurfaceFactory::Create(renderingApi);
172-
auto pvc = new flutter::FlutterPlatformViewsController(_surfaceFactory);
173-
_surfaceFactory->SetPlatformViewsController(pvc);
174-
_platformViewsController.reset(pvc);
175-
}
169+
- (void)recreatePlatformViewController {
170+
_renderingApi = flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering);
171+
_surfaceFactory = flutter::IOSSurfaceFactory::Create(_renderingApi);
172+
_platformViewsController.reset(new flutter::FlutterPlatformViewsController(_surfaceFactory));
173+
_surfaceFactory->SetPlatformViewsController(_platformViewsController);
174+
}
175+
176+
- (flutter::IOSRenderingAPI)platformViewsRenderingAPI {
177+
return _renderingApi;
176178
}
177179

178180
- (void)dealloc {
@@ -536,10 +538,9 @@ - (BOOL)createShell:(NSString*)entrypoint
536538
// create call is synchronous.
537539
flutter::Shell::CreateCallback<flutter::PlatformView> on_create_platform_view =
538540
[self](flutter::Shell& shell) {
539-
[self ensurePlatformViewController];
541+
[self recreatePlatformViewController];
540542
return std::make_unique<flutter::PlatformViewIOS>(
541-
shell, flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering),
542-
self->_surfaceFactory, shell.GetTaskRunners());
543+
shell, self->_renderingApi, self->_surfaceFactory, shell.GetTaskRunners());
543544
};
544545

545546
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =

shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#import <OCMock/OCMock.h>
66
#import <XCTest/XCTest.h>
77

8+
#import "flutter/common/settings.h"
89
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
910
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h"
11+
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
1012
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h"
1113

1214
FLUTTER_ASSERT_ARC
@@ -113,4 +115,23 @@ - (void)testWaitForFirstFrameTimeout {
113115
[self waitForExpectationsWithTimeout:1 handler:nil];
114116
}
115117

118+
- (void)testPlatformViewsControllerRenderingMetalBackend {
119+
FlutterEngine* engine = [[FlutterEngine alloc] init];
120+
[engine run];
121+
flutter::IOSRenderingAPI renderingApi = [engine platformViewsRenderingAPI];
122+
123+
XCTAssertEqual(renderingApi, flutter::IOSRenderingAPI::kMetal);
124+
}
125+
126+
- (void)testPlatformViewsControllerRenderingSoftware {
127+
auto settings = FLTDefaultSettingsForBundle();
128+
settings.enable_software_rendering = true;
129+
FlutterDartProject* project = [[FlutterDartProject alloc] initWithSettings:settings];
130+
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project];
131+
[engine run];
132+
flutter::IOSRenderingAPI renderingApi = [engine platformViewsRenderingAPI];
133+
134+
XCTAssertEqual(renderingApi, flutter::IOSRenderingAPI::kSoftware);
135+
}
136+
116137
@end

shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
// found in the LICENSE file.
44

55
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h"
6+
#include "shell/platform/darwin/ios/rendering_api_selection.h"
67

78
@class FlutterBinaryMessengerRelay;
89

910
// Category to add test-only visibility.
1011
@interface FlutterEngine (Test) <FlutterBinaryMessenger>
1112
- (void)setBinaryMessenger:(FlutterBinaryMessengerRelay*)binaryMessenger;
1213
- (void)waitForFirstFrame:(NSTimeInterval)timeout callback:(void (^)(BOOL didTimeout))callback;
14+
- (flutter::IOSRenderingAPI)platformViewsRenderingAPI;
1315
@end

shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ - (void)testCanCreatePlatformViewWithoutFlutterView {
129129
/*task_runners=*/runners);
130130

131131
auto flutterPlatformViewsController =
132-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
133-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
132+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
133+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
134134

135135
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
136136
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -187,8 +187,8 @@ - (void)testCompositePlatformView {
187187
/*task_runners=*/runners);
188188

189189
auto flutterPlatformViewsController =
190-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
191-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
190+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
191+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
192192

193193
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
194194
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -246,8 +246,8 @@ - (void)testChildClippingViewShouldBeTheBoundingRectOfPlatformView {
246246
/*task_runners=*/runners);
247247

248248
auto flutterPlatformViewsController =
249-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
250-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
249+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
250+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
251251

252252
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
253253
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -321,8 +321,8 @@ - (void)testClipRect {
321321
/*task_runners=*/runners);
322322

323323
auto flutterPlatformViewsController =
324-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
325-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
324+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
325+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
326326

327327
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
328328
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -397,8 +397,8 @@ - (void)testClipRRect {
397397
/*task_runners=*/runners);
398398

399399
auto flutterPlatformViewsController =
400-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
401-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
400+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
401+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
402402

403403
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
404404
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -473,8 +473,8 @@ - (void)testClipPath {
473473
/*task_runners=*/runners);
474474

475475
auto flutterPlatformViewsController =
476-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
477-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
476+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
477+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
478478

479479
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
480480
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -550,8 +550,8 @@ - (void)testSetFlutterViewControllerAfterCreateCanStillDispatchTouchEvents {
550550
/*task_runners=*/runners);
551551

552552
auto flutterPlatformViewsController =
553-
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
554-
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
553+
std::make_shared<flutter::FlutterPlatformViewsController>(surface_factory);
554+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController);
555555

556556
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
557557
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];

shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ - (void)testSemanticsDeallocated {
242242
auto flutterPlatformViewsController =
243243
std::make_shared<flutter::FlutterPlatformViewsController>(surfaceFactory);
244244
flutterPlatformViewsController->SetFlutterView(mockFlutterView);
245-
surfaceFactory->SetPlatformViewsController(flutterPlatformViewsController.get());
245+
surfaceFactory->SetPlatformViewsController(flutterPlatformViewsController);
246246

247247
MockFlutterPlatformFactory* factory = [[MockFlutterPlatformFactory new] autorelease];
248248
flutterPlatformViewsController->RegisterViewFactory(
@@ -352,6 +352,7 @@ - (void)testAnnouncesRouteChangesWhenAddAdditionalRoute {
352352
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
353353
/*delegate=*/mock_delegate,
354354
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
355+
flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware),
355356
/*task_runners=*/runners);
356357
id mockFlutterView = OCMClassMock([FlutterView class]);
357358
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
@@ -435,6 +436,7 @@ - (void)testAnnouncesRouteChangesRemoveRouteInMiddle {
435436
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
436437
/*delegate=*/mock_delegate,
437438
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
439+
flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware),
438440
/*task_runners=*/runners);
439441
id mockFlutterView = OCMClassMock([FlutterView class]);
440442
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);

shell/platform/darwin/ios/ios_surface_factory.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class IOSSurfaceFactory {
2222
~IOSSurfaceFactory();
2323

2424
void SetPlatformViewsController(
25-
FlutterPlatformViewsController* platform_views_controller);
25+
const std::shared_ptr<FlutterPlatformViewsController>&
26+
platform_views_controller);
2627

2728
std::unique_ptr<IOSSurface> CreateSurface(
2829
fml::scoped_nsobject<CALayer> ca_layer);
2930

3031
private:
31-
FlutterPlatformViewsController* platform_views_controller_;
32+
std::shared_ptr<FlutterPlatformViewsController> platform_views_controller_;
3233
std::shared_ptr<IOSContext> ios_context_;
3334

3435
FML_DISALLOW_COPY_AND_ASSIGN(IOSSurfaceFactory);

shell/platform/darwin/ios/ios_surface_factory.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
IOSSurfaceFactory::~IOSSurfaceFactory() = default;
1919

2020
void IOSSurfaceFactory::SetPlatformViewsController(
21-
FlutterPlatformViewsController* platform_views_controller) {
21+
const std::shared_ptr<FlutterPlatformViewsController>& platform_views_controller) {
2222
platform_views_controller_ = platform_views_controller;
2323
}
2424

testing/ios/IosUnitTests/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ also run in LUCI builds.
99
testing/run_tests.py [--type=objc]
1010
```
1111

12-
After the `ios_flutter_test` target is built you can also run the tests inside
12+
After the `ios_test_flutter` target is built you can also run the tests inside
1313
of Xcode with `testing/ios/IosUnitTests/IosUnitTests.xcodeproj`. If you
1414
modify the test or under-test files, you'll have to run `run_tests.py` again.
1515

1616
## Adding Tests
1717

1818
When you add a new unit test file, also add a reference to that file in
1919
shell/platform/darwin/ios/BUILD.gn, under the `sources` list of the
20-
`ios_flutter_test` target. Once it's there, it will execute with the other
20+
`ios_test_flutter` target. Once it's there, it will execute with the other
2121
tests.

0 commit comments

Comments
 (0)