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

Commit 150b4a9

Browse files
committed
Refactored the FlutterEngine to make it easier to implement [FlutterEngine spawn*] methods.
1 parent 4494a83 commit 150b4a9

File tree

3 files changed

+63
-42
lines changed

3 files changed

+63
-42
lines changed

shell/common/thread_host.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ ThreadHost::ThreadHost() = default;
1010

1111
ThreadHost::ThreadHost(ThreadHost&&) = default;
1212

13-
ThreadHost::ThreadHost(std::string name_prefix, uint64_t mask) {
13+
ThreadHost::ThreadHost(std::string name_prefix_arg, uint64_t mask)
14+
: name_prefix(name_prefix_arg) {
1415
if (mask & ThreadHost::Type::Platform) {
1516
platform_thread = std::make_unique<fml::Thread>(name_prefix + ".platform");
1617
}

shell/common/thread_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct ThreadHost {
2222
Profiler = 1 << 4,
2323
};
2424

25+
std::string name_prefix;
2526
std::unique_ptr<fml::Thread> platform_thread;
2627
std::unique_ptr<fml::Thread> ui_thread;
2728
std::unique_ptr<fml::Thread> raster_thread;

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

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
NSString* const FlutterDefaultDartEntrypoint = nil;
3737
NSString* const FlutterDefaultInitialRoute = nil;
3838
static constexpr int kNumProfilerSamplesPerSec = 5;
39+
static size_t g_shellCount = 0;
3940

4041
@interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
4142
@property(nonatomic, assign) FlutterEngine* flutterEngine;
@@ -353,10 +354,11 @@ - (void)resetChannels {
353354
_settingsChannel.reset();
354355
}
355356

356-
- (void)startProfiler:(NSString*)threadLabel {
357+
- (void)startProfiler {
358+
FML_DCHECK(!_threadHost.name_prefix.empty());
357359
_profiler_metrics = std::make_unique<flutter::ProfilerMetricsIOS>();
358360
_profiler = std::make_unique<flutter::SamplingProfiler>(
359-
threadLabel.UTF8String, _threadHost.profiler_thread->GetTaskRunner(),
361+
_threadHost.name_prefix.c_str(), _threadHost.profiler_thread->GetTaskRunner(),
360362
[self]() { return self->_profiler_metrics->GenerateSample(); }, kNumProfilerSamplesPerSec);
361363
_profiler->Start();
362364
}
@@ -462,6 +464,47 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil {
462464
libraryOrNil:libraryOrNil]);
463465
}
464466

467+
- (void)setupShell:(std::unique_ptr<flutter::Shell>)shell
468+
withObservatoryPublication:(BOOL)doesObservatoryPublication {
469+
[self setupChannels];
470+
[self onLocaleUpdated:nil];
471+
[self initializeDisplays];
472+
if (!_platformViewsController) {
473+
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
474+
}
475+
_publisher.reset([[FlutterObservatoryPublisher alloc]
476+
initWithEnableObservatoryPublication:doesObservatoryPublication]);
477+
[self maybeSetupPlatformViewChannels];
478+
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
479+
}
480+
481+
+ (BOOL)isProfilerEnabled {
482+
bool profilerEnabled = false;
483+
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
484+
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
485+
profilerEnabled = true;
486+
#endif
487+
return profilerEnabled;
488+
}
489+
490+
+ (NSString*)generateThreadLabel:(NSString*)labelPrefix {
491+
return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++g_shellCount];
492+
}
493+
494+
+ (flutter::ThreadHost)makeThreadHost:(NSString*)threadLabel {
495+
// The current thread will be used as the platform thread. Ensure that the message loop is
496+
// initialized.
497+
fml::MessageLoop::EnsureInitializedForCurrentThread();
498+
499+
uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
500+
flutter::ThreadHost::Type::IO;
501+
if ([FlutterEngine isProfilerEnabled]) {
502+
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
503+
}
504+
return {threadLabel.UTF8String, // label
505+
threadHostType};
506+
}
507+
465508
- (BOOL)createShell:(NSString*)entrypoint
466509
libraryURI:(NSString*)libraryURI
467510
initialRoute:(NSString*)initialRoute {
@@ -470,7 +513,6 @@ - (BOOL)createShell:(NSString*)entrypoint
470513
return NO;
471514
}
472515

473-
static size_t shellCount = 1;
474516
self.initialRoute = initialRoute;
475517

476518
auto settings = [_dartProject.get() settings];
@@ -490,24 +532,8 @@ - (BOOL)createShell:(NSString*)entrypoint
490532
settings.advisory_script_uri = std::string("main.dart");
491533
}
492534

493-
const auto threadLabel = [NSString stringWithFormat:@"%@.%zu", _labelPrefix, shellCount++];
494-
495-
// The current thread will be used as the platform thread. Ensure that the message loop is
496-
// initialized.
497-
fml::MessageLoop::EnsureInitializedForCurrentThread();
498-
499-
uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
500-
flutter::ThreadHost::Type::IO;
501-
bool profilerEnabled = false;
502-
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
503-
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
504-
profilerEnabled = true;
505-
#endif
506-
if (profilerEnabled) {
507-
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
508-
}
509-
_threadHost = {threadLabel.UTF8String, // label
510-
threadHostType};
535+
NSString* threadLabel = [FlutterEngine generateThreadLabel:_labelPrefix];
536+
_threadHost = [FlutterEngine makeThreadHost:threadLabel];
511537

512538
// Lambda captures by pointers to ObjC objects are fine here because the
513539
// create call is
@@ -530,29 +556,22 @@ - (BOOL)createShell:(NSString*)entrypoint
530556
);
531557

532558
// Create the shell. This is a blocking operation.
533-
_shell = flutter::Shell::Create(std::move(task_runners), // task runners
534-
std::move(platformData), // window data
535-
std::move(settings), // settings
536-
on_create_platform_view, // platform view creation
537-
on_create_rasterizer // rasterzier creation
538-
);
539-
540-
if (_shell == nullptr) {
559+
std::unique_ptr<flutter::Shell> shell =
560+
flutter::Shell::Create(std::move(task_runners), // task runners
561+
std::move(platformData), // window data
562+
std::move(settings), // settings
563+
on_create_platform_view, // platform view creation
564+
on_create_rasterizer // rasterzier creation
565+
);
566+
567+
if (shell == nullptr) {
541568
FML_LOG(ERROR) << "Could not start a shell FlutterEngine with entrypoint: "
542569
<< entrypoint.UTF8String;
543570
} else {
544-
[self setupChannels];
545-
[self onLocaleUpdated:nil];
546-
[self initializeDisplays];
547-
if (!_platformViewsController) {
548-
_platformViewsController.reset(new flutter::FlutterPlatformViewsController());
549-
}
550-
_publisher.reset([[FlutterObservatoryPublisher alloc]
551-
initWithEnableObservatoryPublication:settings.enable_observatory_publication]);
552-
[self maybeSetupPlatformViewChannels];
553-
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
554-
if (profilerEnabled) {
555-
[self startProfiler:threadLabel];
571+
[self setupShell:std::move(shell)
572+
withObservatoryPublication:settings.enable_observatory_publication];
573+
if ([FlutterEngine isProfilerEnabled]) {
574+
[self startProfiler];
556575
}
557576
}
558577

0 commit comments

Comments
 (0)