Skip to content

Commit 4e9459e

Browse files
authored
Refactored the FlutterEngine to make it easier to implement spawn functionality (flutter#21890)
1 parent 9b34207 commit 4e9459e

File tree

3 files changed

+61
-39
lines changed

3 files changed

+61
-39
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: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,11 @@ - (void)resetChannels {
373373
_keyEventChannel.reset();
374374
}
375375

376-
- (void)startProfiler:(NSString*)threadLabel {
376+
- (void)startProfiler {
377+
FML_DCHECK(!_threadHost.name_prefix.empty());
377378
_profiler_metrics = std::make_unique<flutter::ProfilerMetricsIOS>();
378379
_profiler = std::make_unique<flutter::SamplingProfiler>(
379-
threadLabel.UTF8String, _threadHost.profiler_thread->GetTaskRunner(),
380+
_threadHost.name_prefix.c_str(), _threadHost.profiler_thread->GetTaskRunner(),
380381
[self]() { return self->_profiler_metrics->GenerateSample(); }, kNumProfilerSamplesPerSec);
381382
_profiler->Start();
382383
}
@@ -487,6 +488,46 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil {
487488
libraryOrNil:libraryOrNil]);
488489
}
489490

491+
- (void)setupShell:(std::unique_ptr<flutter::Shell>)shell
492+
withObservatoryPublication:(BOOL)doesObservatoryPublication {
493+
_shell = std::move(shell);
494+
[self setupChannels];
495+
[self onLocaleUpdated:nil];
496+
[self initializeDisplays];
497+
_publisher.reset([[FlutterObservatoryPublisher alloc]
498+
initWithEnableObservatoryPublication:doesObservatoryPublication]);
499+
[self maybeSetupPlatformViewChannels];
500+
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
501+
}
502+
503+
+ (BOOL)isProfilerEnabled {
504+
bool profilerEnabled = false;
505+
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
506+
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
507+
profilerEnabled = true;
508+
#endif
509+
return profilerEnabled;
510+
}
511+
512+
+ (NSString*)generateThreadLabel:(NSString*)labelPrefix {
513+
static size_t s_shellCount = 0;
514+
return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++s_shellCount];
515+
}
516+
517+
+ (flutter::ThreadHost)makeThreadHost:(NSString*)threadLabel {
518+
// The current thread will be used as the platform thread. Ensure that the message loop is
519+
// initialized.
520+
fml::MessageLoop::EnsureInitializedForCurrentThread();
521+
522+
uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
523+
flutter::ThreadHost::Type::IO;
524+
if ([FlutterEngine isProfilerEnabled]) {
525+
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
526+
}
527+
return {threadLabel.UTF8String, // label
528+
threadHostType};
529+
}
530+
490531
- (BOOL)createShell:(NSString*)entrypoint
491532
libraryURI:(NSString*)libraryURI
492533
initialRoute:(NSString*)initialRoute {
@@ -495,7 +536,6 @@ - (BOOL)createShell:(NSString*)entrypoint
495536
return NO;
496537
}
497538

498-
static size_t shellCount = 1;
499539
self.initialRoute = initialRoute;
500540

501541
auto settings = [_dartProject.get() settings];
@@ -515,24 +555,8 @@ - (BOOL)createShell:(NSString*)entrypoint
515555
settings.advisory_script_uri = std::string("main.dart");
516556
}
517557

518-
const auto threadLabel = [NSString stringWithFormat:@"%@.%zu", _labelPrefix, shellCount++];
519-
520-
// The current thread will be used as the platform thread. Ensure that the message loop is
521-
// initialized.
522-
fml::MessageLoop::EnsureInitializedForCurrentThread();
523-
524-
uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
525-
flutter::ThreadHost::Type::IO;
526-
bool profilerEnabled = false;
527-
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
528-
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
529-
profilerEnabled = true;
530-
#endif
531-
if (profilerEnabled) {
532-
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
533-
}
534-
_threadHost = {threadLabel.UTF8String, // label
535-
threadHostType};
558+
NSString* threadLabel = [FlutterEngine generateThreadLabel:_labelPrefix];
559+
_threadHost = [FlutterEngine makeThreadHost:threadLabel];
536560

537561
// Lambda captures by pointers to ObjC objects are fine here because the
538562
// create call is synchronous.
@@ -554,26 +578,22 @@ - (BOOL)createShell:(NSString*)entrypoint
554578
);
555579

556580
// Create the shell. This is a blocking operation.
557-
_shell = flutter::Shell::Create(std::move(task_runners), // task runners
558-
std::move(platformData), // window data
559-
std::move(settings), // settings
560-
on_create_platform_view, // platform view creation
561-
on_create_rasterizer // rasterzier creation
562-
);
563-
564-
if (_shell == nullptr) {
581+
std::unique_ptr<flutter::Shell> shell =
582+
flutter::Shell::Create(std::move(task_runners), // task runners
583+
std::move(platformData), // window data
584+
std::move(settings), // settings
585+
on_create_platform_view, // platform view creation
586+
on_create_rasterizer // rasterzier creation
587+
);
588+
589+
if (shell == nullptr) {
565590
FML_LOG(ERROR) << "Could not start a shell FlutterEngine with entrypoint: "
566591
<< entrypoint.UTF8String;
567592
} else {
568-
[self setupChannels];
569-
[self onLocaleUpdated:nil];
570-
[self initializeDisplays];
571-
_publisher.reset([[FlutterObservatoryPublisher alloc]
572-
initWithEnableObservatoryPublication:settings.enable_observatory_publication]);
573-
[self maybeSetupPlatformViewChannels];
574-
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
575-
if (profilerEnabled) {
576-
[self startProfiler:threadLabel];
593+
[self setupShell:std::move(shell)
594+
withObservatoryPublication:settings.enable_observatory_publication];
595+
if ([FlutterEngine isProfilerEnabled]) {
596+
[self startProfiler];
577597
}
578598
}
579599

0 commit comments

Comments
 (0)