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

Commit bb4ec2d

Browse files
Add an engine switch that controls whether the platform isolate API is available. (#51784)
Platform isolates are currently supported only on Android and iOS. See flutter/flutter#136314
1 parent c1aa1d5 commit bb4ec2d

File tree

10 files changed

+34
-0
lines changed

10 files changed

+34
-0
lines changed

common/settings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ struct Settings {
356356
///
357357
/// This is currently only used by iOS.
358358
bool enable_embedder_api = false;
359+
360+
/// Enable support for isolates that run on the platform thread.
361+
///
362+
/// This is used by the runOnPlatformThread API.
363+
bool enable_platform_isolates = false;
359364
};
360365

361366
} // namespace flutter

lib/ui/dart_ui.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ void DartUI::InitForIsolate(const Settings& settings) {
386386
}
387387
}
388388

389+
if (settings.enable_platform_isolates) {
390+
result =
391+
Dart_SetField(dart_ui, ToDart("_platformIsolatesEnabled"), Dart_True());
392+
if (Dart_IsError(result)) {
393+
Dart_PropagateError(result);
394+
}
395+
}
396+
389397
result = Dart_SetField(dart_ui, ToDart("_implicitViewId"),
390398
Dart_NewInteger(kFlutterImplicitViewId));
391399
if (Dart_IsError(result)) {

lib/ui/natives.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ bool _impellerEnabled = false;
121121
// determine the current implicit view, if any.
122122
@pragma('vm:entry-point')
123123
int? _implicitViewId;
124+
125+
// Used internally to indicate whether isolates running on the platform thread
126+
// are enabled.
127+
@pragma('vm:entry-point')
128+
bool _platformIsolatesEnabled = false;

lib/ui/platform_isolate.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ part of dart.ui;
2727
///
2828
/// This API is currently experimental.
2929
Future<R> runOnPlatformThread<R>(FutureOr<R> Function() computation) {
30+
if (!_platformIsolatesEnabled) {
31+
throw UnsupportedError(
32+
'Platform thread isolates are not supported by this platform.');
33+
}
3034
if (isRunningOnPlatformThread) {
3135
return Future<R>(computation);
3236
}

shell/common/switches.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
542542
}
543543
}
544544

545+
settings.enable_platform_isolates =
546+
command_line.HasOption(FlagForSwitch(Switch::EnablePlatformIsolates));
547+
545548
return settings;
546549
}
547550

shell/common/switches.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ DEF_SWITCH(
298298
DEF_SWITCH(EnableEmbedderAPI,
299299
"enable-embedder-api",
300300
"Enable the embedder api. Defaults to false. iOS only.")
301+
DEF_SWITCH(EnablePlatformIsolates,
302+
"enable-platform-isolates",
303+
"Enable support for isolates that run on the platform thread.")
301304
DEF_SWITCHES_END
302305

303306
void PrintUsage(const std::string& executable_name);

shell/platform/android/flutter_main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ void FlutterMain::Init(JNIEnv* env,
150150
static_cast<int>(message.size()), message.c_str());
151151
};
152152

153+
settings.enable_platform_isolates = true;
154+
153155
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
154156
// There are no ownership concerns here as all mappings are owned by the
155157
// embedder and not the engine.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ static BOOL DoesHardwareSupportWideGamut() {
9191
syslog(LOG_ALERT, "%.*s", (int)log.size(), log.c_str());
9292
};
9393

94+
settings.enable_platform_isolates = true;
95+
9496
// The command line arguments may not always be complete. If they aren't, attempt to fill in
9597
// defaults.
9698

shell/platform/embedder/tests/embedder_unittests.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,7 @@ TEST_F(EmbedderTest, PlatformThreadIsolatesWithCustomPlatformTaskRunner) {
35633563
builder.SetSoftwareRendererConfig();
35643564
builder.SetPlatformTaskRunner(&task_runner_description);
35653565
builder.SetDartEntrypoint("invokePlatformThreadIsolate");
3566+
builder.AddCommandLineArgument("--enable-platform-isolates");
35663567
engine = builder.LaunchEngine();
35673568
ASSERT_TRUE(engine.is_valid());
35683569
});

shell/testing/tester_main.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ int main(int argc, char* argv[]) {
641641
}
642642

643643
settings.leak_vm = false;
644+
settings.enable_platform_isolates = true;
644645

645646
if (settings.icu_data_path.empty()) {
646647
settings.icu_data_path = "icudtl.dat";

0 commit comments

Comments
 (0)