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

Commit 6a3b5fe

Browse files
authored
[ios] Refactor IOSSurface factory and unify surface creation (#21877)
1 parent 62459b3 commit 6a3b5fe

17 files changed

+167
-46
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,8 @@ FILE: ../../../flutter/shell/platform/darwin/ios/ios_render_target_gl.h
10101010
FILE: ../../../flutter/shell/platform/darwin/ios/ios_render_target_gl.mm
10111011
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.h
10121012
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface.mm
1013+
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_factory.h
1014+
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_factory.mm
10131015
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_gl.h
10141016
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_gl.mm
10151017
FILE: ../../../flutter/shell/platform/darwin/ios/ios_surface_metal.h

shell/platform/darwin/ios/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ source_set("flutter_framework_source") {
9999
"ios_render_target_gl.mm",
100100
"ios_surface.h",
101101
"ios_surface.mm",
102+
"ios_surface_factory.h",
103+
"ios_surface_factory.mm",
102104
"ios_surface_gl.h",
103105
"ios_surface_gl.mm",
104106
"ios_surface_software.h",

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h"
2929
#import "flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.h"
3030
#import "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h"
31+
#import "flutter/shell/platform/darwin/ios/ios_context.h"
3132
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
33+
#import "flutter/shell/platform/darwin/ios/ios_surface_factory.h"
3234
#import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
3335
#import "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
3436
#include "flutter/shell/profiling/sampling_profiler.h"
@@ -63,6 +65,7 @@ @implementation FlutterEngine {
6365
fml::WeakPtr<FlutterViewController> _viewController;
6466
fml::scoped_nsobject<FlutterObservatoryPublisher> _publisher;
6567

68+
std::shared_ptr<flutter::IOSSurfaceFactory> _surfaceFactory;
6669
std::unique_ptr<flutter::FlutterPlatformViewsController> _platformViewsController;
6770
std::unique_ptr<flutter::ProfilerMetricsIOS> _profiler_metrics;
6871
std::unique_ptr<flutter::SamplingProfiler> _profiler;
@@ -127,7 +130,7 @@ - (instancetype)initWithName:(NSString*)labelPrefix
127130

128131
_pluginPublications = [NSMutableDictionary new];
129132
_registrars = [[NSMutableDictionary alloc] init];
130-
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
133+
[self ensurePlatformViewController];
131134

132135
_binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
133136
_connections.reset(new flutter::ConnectionCollection());
@@ -161,6 +164,16 @@ - (instancetype)initWithName:(NSString*)labelPrefix
161164
return self;
162165
}
163166

167+
- (void)ensurePlatformViewController {
168+
if (!_platformViewsController) {
169+
auto renderingApi = flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering);
170+
_surfaceFactory = flutter::IOSSurfaceFactory::Create(renderingApi);
171+
auto pvc = new flutter::FlutterPlatformViewsController(_surfaceFactory);
172+
_surfaceFactory->SetPlatformViewsController(pvc);
173+
_platformViewsController.reset(pvc);
174+
}
175+
}
176+
164177
- (void)dealloc {
165178
/// Notify plugins of dealloc. This should happen first in dealloc since the
166179
/// plugins may be talking to things like the binaryMessenger.
@@ -510,13 +523,13 @@ - (BOOL)createShell:(NSString*)entrypoint
510523
threadHostType};
511524

512525
// Lambda captures by pointers to ObjC objects are fine here because the
513-
// create call is
514-
// synchronous.
526+
// create call is synchronous.
515527
flutter::Shell::CreateCallback<flutter::PlatformView> on_create_platform_view =
516-
[](flutter::Shell& shell) {
528+
[self](flutter::Shell& shell) {
529+
[self ensurePlatformViewController];
517530
return std::make_unique<flutter::PlatformViewIOS>(
518531
shell, flutter::GetRenderingAPIForProcess(FlutterView.forceSoftwareRendering),
519-
shell.GetTaskRunners());
532+
self->_surfaceFactory, shell.GetTaskRunners());
520533
};
521534

522535
flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
@@ -544,9 +557,6 @@ - (BOOL)createShell:(NSString*)entrypoint
544557
[self setupChannels];
545558
[self onLocaleUpdated:nil];
546559
[self initializeDisplays];
547-
if (!_platformViewsController) {
548-
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
549-
}
550560
_publisher.reset([[FlutterObservatoryPublisher alloc]
551561
initWithEnableObservatoryPublication:settings.enable_observatory_publication]);
552562
[self maybeSetupPlatformViewChannels];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ - (void)testCallsNotifyLowMemory {
6767
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
6868
/*delegate=*/mock_delegate,
6969
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
70+
flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware),
7071
/*task_runners=*/runners);
7172

7273
id project = OCMClassMock([FlutterDartProject class]);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
- (instancetype)init NS_DESIGNATED_INITIALIZER;
3737
- (instancetype)initWithContentsScale:(CGFloat)contentsScale;
38-
- (std::unique_ptr<flutter::IOSSurface>)createSurface:
39-
(std::shared_ptr<flutter::IOSContext>)ios_context;
4038

4139
@end
4240

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ + (Class)layerClass {
6262
return [FlutterView layerClass];
6363
}
6464

65-
- (std::unique_ptr<flutter::IOSSurface>)createSurface:
66-
(std::shared_ptr<flutter::IOSContext>)ios_context {
67-
return flutter::IOSSurface::Create(std::move(ios_context), // context
68-
fml::scoped_nsobject<CALayer>{[self.layer retain]}, // layer
69-
nullptr // platform views controller
70-
);
71-
}
72-
7365
// TODO(amirh): implement drawLayer to support snapshotting.
7466

7567
@end

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h"
1717
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"
1818
#import "flutter/shell/platform/darwin/ios/ios_surface.h"
19+
#import "flutter/shell/platform/darwin/ios/ios_surface_factory.h"
1920
#import "flutter/shell/platform/darwin/ios/ios_surface_gl.h"
2021

2122
namespace flutter {
@@ -32,8 +33,8 @@
3233
overlay_view.reset([[FlutterOverlayView alloc] init]);
3334
overlay_view_wrapper.reset([[FlutterOverlayView alloc] init]);
3435

35-
std::unique_ptr<IOSSurface> ios_surface =
36-
[overlay_view.get() createSurface:std::move(ios_context)];
36+
auto ca_layer = fml::scoped_nsobject<CALayer>{[[overlay_view.get() layer] retain]};
37+
std::unique_ptr<IOSSurface> ios_surface = ios_surface_factory_->CreateSurface(ca_layer);
3738
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface();
3839

3940
layer = std::make_shared<FlutterPlatformViewLayer>(
@@ -44,8 +45,8 @@
4445
overlay_view.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale]);
4546
overlay_view_wrapper.reset([[FlutterOverlayView alloc] initWithContentsScale:screenScale]);
4647

47-
std::unique_ptr<IOSSurface> ios_surface =
48-
[overlay_view.get() createSurface:std::move(ios_context)];
48+
auto ca_layer = fml::scoped_nsobject<CALayer>{[[overlay_view.get() layer] retain]};
49+
std::unique_ptr<IOSSurface> ios_surface = ios_surface_factory_->CreateSurface(ca_layer);
4950
std::unique_ptr<Surface> surface = ios_surface->CreateGPUSurface(gr_context);
5051

5152
layer = std::make_shared<FlutterPlatformViewLayer>(

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,16 @@ - (void)testCanCreatePlatformViewWithoutFlutterView {
121121
/*raster=*/thread_task_runner,
122122
/*ui=*/thread_task_runner,
123123
/*io=*/thread_task_runner);
124+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
124125
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
125126
/*delegate=*/mock_delegate,
126127
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
128+
/*ios_surface_factory=*/surface_factory,
127129
/*task_runners=*/runners);
128130

129-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
131+
auto flutterPlatformViewsController =
132+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
133+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
130134

131135
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
132136
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -175,12 +179,16 @@ - (void)testCompositePlatformView {
175179
/*raster=*/thread_task_runner,
176180
/*ui=*/thread_task_runner,
177181
/*io=*/thread_task_runner);
182+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
178183
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
179184
/*delegate=*/mock_delegate,
180185
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
186+
/*ios_surface_factory=*/surface_factory,
181187
/*task_runners=*/runners);
182188

183-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
189+
auto flutterPlatformViewsController =
190+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
191+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
184192

185193
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
186194
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -230,12 +238,16 @@ - (void)testChildClippingViewShouldBeTheBoundingRectOfPlatformView {
230238
/*raster=*/thread_task_runner,
231239
/*ui=*/thread_task_runner,
232240
/*io=*/thread_task_runner);
241+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
233242
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
234243
/*delegate=*/mock_delegate,
235244
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
245+
/*ios_surface_factory=*/surface_factory,
236246
/*task_runners=*/runners);
237247

238-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
248+
auto flutterPlatformViewsController =
249+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
250+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
239251

240252
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
241253
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -301,12 +313,16 @@ - (void)testClipRect {
301313
/*raster=*/thread_task_runner,
302314
/*ui=*/thread_task_runner,
303315
/*io=*/thread_task_runner);
316+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
304317
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
305318
/*delegate=*/mock_delegate,
306319
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
320+
/*ios_surface_factory=*/surface_factory,
307321
/*task_runners=*/runners);
308322

309-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
323+
auto flutterPlatformViewsController =
324+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
325+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
310326

311327
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
312328
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -373,12 +389,16 @@ - (void)testClipRRect {
373389
/*raster=*/thread_task_runner,
374390
/*ui=*/thread_task_runner,
375391
/*io=*/thread_task_runner);
392+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
376393
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
377394
/*delegate=*/mock_delegate,
378395
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
396+
/*ios_surface_factory=*/surface_factory,
379397
/*task_runners=*/runners);
380398

381-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
399+
auto flutterPlatformViewsController =
400+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
401+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
382402

383403
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
384404
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -445,12 +465,16 @@ - (void)testClipPath {
445465
/*raster=*/thread_task_runner,
446466
/*ui=*/thread_task_runner,
447467
/*io=*/thread_task_runner);
468+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
448469
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
449470
/*delegate=*/mock_delegate,
450471
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
472+
/*ios_surface_factory=*/surface_factory,
451473
/*task_runners=*/runners);
452474

453-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
475+
auto flutterPlatformViewsController =
476+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
477+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
454478

455479
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
456480
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];
@@ -518,12 +542,16 @@ - (void)testSetFlutterViewControllerAfterCreateCanStillDispatchTouchEvents {
518542
/*raster=*/thread_task_runner,
519543
/*ui=*/thread_task_runner,
520544
/*io=*/thread_task_runner);
545+
auto surface_factory = flutter::IOSSurfaceFactory::Create(flutter::IOSRenderingAPI::kSoftware);
521546
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
522547
/*delegate=*/mock_delegate,
523548
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
549+
/*ios_surface_factory=*/surface_factory,
524550
/*task_runners=*/runners);
525551

526-
auto flutterPlatformViewsController = std::make_unique<flutter::FlutterPlatformViewsController>();
552+
auto flutterPlatformViewsController =
553+
std::make_unique<flutter::FlutterPlatformViewsController>(surface_factory);
554+
surface_factory->SetPlatformViewsController(flutterPlatformViewsController.get());
527555

528556
FlutterPlatformViewsTestMockFlutterPlatformFactory* factory =
529557
[[FlutterPlatformViewsTestMockFlutterPlatformFactory new] autorelease];

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void ResetAnchor(CALayer* layer);
6161

6262
class IOSContextGL;
6363
class IOSSurface;
64+
class IOSSurfaceFactory;
6465

6566
struct FlutterPlatformViewLayer {
6667
FlutterPlatformViewLayer(fml::scoped_nsobject<UIView> overlay_view,
@@ -87,7 +88,9 @@ struct FlutterPlatformViewLayer {
8788
// This class isn't thread safe.
8889
class FlutterPlatformViewLayerPool {
8990
public:
90-
FlutterPlatformViewLayerPool() = default;
91+
FlutterPlatformViewLayerPool(std::shared_ptr<IOSSurfaceFactory> ios_surface_factory)
92+
: ios_surface_factory_(ios_surface_factory) {}
93+
9194
~FlutterPlatformViewLayerPool() = default;
9295

9396
// Gets a layer from the pool if available, or allocates a new one.
@@ -118,12 +121,14 @@ class FlutterPlatformViewLayerPool {
118121
size_t available_layer_index_ = 0;
119122
std::vector<std::shared_ptr<FlutterPlatformViewLayer>> layers_;
120123

124+
const std::shared_ptr<IOSSurfaceFactory> ios_surface_factory_;
125+
121126
FML_DISALLOW_COPY_AND_ASSIGN(FlutterPlatformViewLayerPool);
122127
};
123128

124129
class FlutterPlatformViewsController {
125130
public:
126-
FlutterPlatformViewsController();
131+
FlutterPlatformViewsController(std::shared_ptr<IOSSurfaceFactory> surface_factory);
127132

128133
~FlutterPlatformViewsController();
129134

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323

2424
FlutterPlatformViewLayer::~FlutterPlatformViewLayer() = default;
2525

26-
FlutterPlatformViewsController::FlutterPlatformViewsController()
27-
: layer_pool_(std::make_unique<FlutterPlatformViewLayerPool>()),
26+
FlutterPlatformViewsController::FlutterPlatformViewsController(
27+
std::shared_ptr<IOSSurfaceFactory> surface_factory)
28+
: layer_pool_(std::make_unique<FlutterPlatformViewLayerPool>(surface_factory)),
2829
weak_factory_(std::make_unique<fml::WeakPtrFactory<FlutterPlatformViewsController>>(this)){};
2930

3031
FlutterPlatformViewsController::~FlutterPlatformViewsController() = default;

0 commit comments

Comments
 (0)