Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shell/common/thread_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ ThreadHost::ThreadHost() = default;

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

ThreadHost::ThreadHost(std::string name_prefix, uint64_t mask) {
ThreadHost::ThreadHost(std::string name_prefix_arg, uint64_t mask)
: name_prefix(name_prefix_arg) {
if (mask & ThreadHost::Type::Platform) {
platform_thread = std::make_unique<fml::Thread>(name_prefix + ".platform");
}
Expand Down
1 change: 1 addition & 0 deletions shell/common/thread_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct ThreadHost {
Profiler = 1 << 4,
};

std::string name_prefix;
std::unique_ptr<fml::Thread> platform_thread;
std::unique_ptr<fml::Thread> ui_thread;
std::unique_ptr<fml::Thread> raster_thread;
Expand Down
96 changes: 58 additions & 38 deletions shell/platform/darwin/ios/framework/Source/FlutterEngine.mm
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,11 @@ - (void)resetChannels {
_keyEventChannel.reset();
}

- (void)startProfiler:(NSString*)threadLabel {
- (void)startProfiler {
FML_DCHECK(!_threadHost.name_prefix.empty());
_profiler_metrics = std::make_unique<flutter::ProfilerMetricsIOS>();
_profiler = std::make_unique<flutter::SamplingProfiler>(
threadLabel.UTF8String, _threadHost.profiler_thread->GetTaskRunner(),
_threadHost.name_prefix.c_str(), _threadHost.profiler_thread->GetTaskRunner(),
[self]() { return self->_profiler_metrics->GenerateSample(); }, kNumProfilerSamplesPerSec);
_profiler->Start();
}
Expand Down Expand Up @@ -487,6 +488,46 @@ - (void)launchEngine:(NSString*)entrypoint libraryURI:(NSString*)libraryOrNil {
libraryOrNil:libraryOrNil]);
}

- (void)setupShell:(std::unique_ptr<flutter::Shell>)shell
withObservatoryPublication:(BOOL)doesObservatoryPublication {
_shell = std::move(shell);
[self setupChannels];
[self onLocaleUpdated:nil];
[self initializeDisplays];
_publisher.reset([[FlutterObservatoryPublisher alloc]
initWithEnableObservatoryPublication:doesObservatoryPublication]);
[self maybeSetupPlatformViewChannels];
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
}

+ (BOOL)isProfilerEnabled {
bool profilerEnabled = false;
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
profilerEnabled = true;
#endif
return profilerEnabled;
}

+ (NSString*)generateThreadLabel:(NSString*)labelPrefix {
static size_t s_shellCount = 0;
return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++s_shellCount];
}

+ (flutter::ThreadHost)makeThreadHost:(NSString*)threadLabel {
// The current thread will be used as the platform thread. Ensure that the message loop is
// initialized.
fml::MessageLoop::EnsureInitializedForCurrentThread();

uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
flutter::ThreadHost::Type::IO;
if ([FlutterEngine isProfilerEnabled]) {
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
}
return {threadLabel.UTF8String, // label
threadHostType};
}

- (BOOL)createShell:(NSString*)entrypoint
libraryURI:(NSString*)libraryURI
initialRoute:(NSString*)initialRoute {
Expand All @@ -495,7 +536,6 @@ - (BOOL)createShell:(NSString*)entrypoint
return NO;
}

static size_t shellCount = 1;
self.initialRoute = initialRoute;

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

const auto threadLabel = [NSString stringWithFormat:@"%@.%zu", _labelPrefix, shellCount++];

// The current thread will be used as the platform thread. Ensure that the message loop is
// initialized.
fml::MessageLoop::EnsureInitializedForCurrentThread();

uint32_t threadHostType = flutter::ThreadHost::Type::UI | flutter::ThreadHost::Type::GPU |
flutter::ThreadHost::Type::IO;
bool profilerEnabled = false;
#if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
(FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
profilerEnabled = true;
#endif
if (profilerEnabled) {
threadHostType = threadHostType | flutter::ThreadHost::Type::Profiler;
}
_threadHost = {threadLabel.UTF8String, // label
threadHostType};
NSString* threadLabel = [FlutterEngine generateThreadLabel:_labelPrefix];
_threadHost = [FlutterEngine makeThreadHost:threadLabel];

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

// Create the shell. This is a blocking operation.
_shell = flutter::Shell::Create(std::move(task_runners), // task runners
std::move(platformData), // window data
std::move(settings), // settings
on_create_platform_view, // platform view creation
on_create_rasterizer // rasterzier creation
);

if (_shell == nullptr) {
std::unique_ptr<flutter::Shell> shell =
flutter::Shell::Create(std::move(task_runners), // task runners
std::move(platformData), // window data
std::move(settings), // settings
on_create_platform_view, // platform view creation
on_create_rasterizer // rasterzier creation
);

if (shell == nullptr) {
FML_LOG(ERROR) << "Could not start a shell FlutterEngine with entrypoint: "
<< entrypoint.UTF8String;
} else {
[self setupChannels];
[self onLocaleUpdated:nil];
[self initializeDisplays];
_publisher.reset([[FlutterObservatoryPublisher alloc]
initWithEnableObservatoryPublication:settings.enable_observatory_publication]);
[self maybeSetupPlatformViewChannels];
_shell->GetIsGpuDisabledSyncSwitch()->SetSwitch(_isGpuDisabled ? true : false);
if (profilerEnabled) {
[self startProfiler:threadLabel];
[self setupShell:std::move(shell)
withObservatoryPublication:settings.enable_observatory_publication];
if ([FlutterEngine isProfilerEnabled]) {
[self startProfiler];
}
}

Expand Down