@@ -40,21 +40,169 @@ constexpr char kSystemChannel[] = "flutter/system";
4040constexpr char kTypeKey [] = " type" ;
4141constexpr char kFontChange [] = " fontsChange" ;
4242
43+ <<<<<<< HEAD
44+ =======
45+ namespace {
46+
47+ std::unique_ptr<Engine> CreateEngine (
48+ Engine::Delegate& delegate,
49+ const PointerDataDispatcherMaker& dispatcher_maker,
50+ DartVM& vm,
51+ fml::RefPtr<const DartSnapshot> isolate_snapshot,
52+ TaskRunners task_runners,
53+ const PlatformData& platform_data,
54+ Settings settings,
55+ std::unique_ptr<Animator> animator,
56+ fml::WeakPtr<IOManager> io_manager,
57+ fml::RefPtr<SkiaUnrefQueue> unref_queue,
58+ fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
59+ std::shared_ptr<VolatilePathTracker> volatile_path_tracker) {
60+ return std::make_unique<Engine>(delegate, //
61+ dispatcher_maker, //
62+ vm, //
63+ isolate_snapshot, //
64+ task_runners, //
65+ platform_data, //
66+ settings, //
67+ std::move (animator), //
68+ io_manager, //
69+ unref_queue, //
70+ snapshot_delegate, //
71+ volatile_path_tracker);
72+ }
73+
74+ void Tokenize (const std::string& input,
75+ std::vector<std::string>* results,
76+ char delimiter) {
77+ std::istringstream ss (input);
78+ std::string token;
79+ while (std::getline (ss, token, delimiter)) {
80+ results->push_back (token);
81+ }
82+ }
83+
84+ // Though there can be multiple shells, some settings apply to all components in
85+ // the process. These have to be setup before the shell or any of its
86+ // sub-components can be initialized. In a perfect world, this would be empty.
87+ // TODO(chinmaygarde): The unfortunate side effect of this call is that settings
88+ // that cause shell initialization failures will still lead to some of their
89+ // settings being applied.
90+ void PerformInitializationTasks (Settings& settings) {
91+ {
92+ fml::LogSettings log_settings;
93+ log_settings.min_log_level =
94+ settings.verbose_logging ? fml::LOG_INFO : fml::LOG_ERROR;
95+ fml::SetLogSettings (log_settings);
96+ }
97+
98+ static std::once_flag gShellSettingsInitialization = {};
99+ std::call_once (gShellSettingsInitialization , [&settings] {
100+ if (settings.engine_start_timestamp .count () == 0 ) {
101+ settings.engine_start_timestamp =
102+ std::chrono::microseconds (Dart_TimelineGetMicros ());
103+ }
104+
105+ tonic::SetLogHandler (
106+ [](const char * message) { FML_LOG (ERROR) << message; });
107+
108+ if (settings.trace_skia ) {
109+ InitSkiaEventTracer (settings.trace_skia );
110+ }
111+
112+ if (!settings.trace_allowlist .empty ()) {
113+ std::vector<std::string> prefixes;
114+ Tokenize (settings.trace_allowlist , &prefixes, ' ,' );
115+ fml::tracing::TraceSetAllowlist (prefixes);
116+ }
117+
118+ if (!settings.skia_deterministic_rendering_on_cpu ) {
119+ SkGraphics::Init ();
120+ } else {
121+ FML_DLOG (INFO) << " Skia deterministic rendering is enabled." ;
122+ }
123+
124+ if (settings.icu_initialization_required ) {
125+ if (settings.icu_data_path .size () != 0 ) {
126+ fml::icu::InitializeICU (settings.icu_data_path );
127+ } else if (settings.icu_mapper ) {
128+ fml::icu::InitializeICUFromMapping (settings.icu_mapper ());
129+ } else {
130+ FML_DLOG (WARNING) << " Skipping ICU initialization in the shell." ;
131+ }
132+ }
133+ });
134+
135+ PersistentCache::SetCacheSkSL (settings.cache_sksl );
136+ }
137+
138+ } // namespace
139+
140+ std::unique_ptr<Shell> Shell::Create (
141+ const PlatformData& platform_data,
142+ TaskRunners task_runners,
143+ Settings settings,
144+ const Shell::CreateCallback<PlatformView>& on_create_platform_view,
145+ const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
146+ bool is_gpu_disabled) {
147+ // This must come first as it initializes tracing.
148+ PerformInitializationTasks (settings);
149+
150+ TRACE_EVENT0 (" flutter" , " Shell::Create" );
151+
152+ // Always use the `vm_snapshot` and `isolate_snapshot` provided by the
153+ // settings to launch the VM. If the VM is already running, the snapshot
154+ // arguments are ignored.
155+ auto vm_snapshot = DartSnapshot::VMSnapshotFromSettings (settings);
156+ auto isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings (settings);
157+ auto vm = DartVMRef::Create (settings, vm_snapshot, isolate_snapshot);
158+ FML_CHECK (vm) << " Must be able to initialize the VM." ;
159+
160+ // If the settings did not specify an `isolate_snapshot`, fall back to the
161+ // one the VM was launched with.
162+ if (!isolate_snapshot) {
163+ isolate_snapshot = vm->GetVMData ()->GetIsolateSnapshot ();
164+ }
165+ return CreateWithSnapshot (std::move (platform_data), //
166+ std::move (task_runners), //
167+ std::move (settings), //
168+ std::move (vm), //
169+ std::move (isolate_snapshot), //
170+ std::move (on_create_platform_view), //
171+ std::move (on_create_rasterizer), //
172+ CreateEngine, is_gpu_disabled);
173+ }
174+
175+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
43176std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread (
44177 DartVMRef vm,
45178 TaskRunners task_runners,
46179 const PlatformData platform_data,
47180 Settings settings,
48181 fml::RefPtr<const DartSnapshot> isolate_snapshot,
49182 const Shell::CreateCallback<PlatformView>& on_create_platform_view,
183+ <<<<<<< HEAD
50184 const Shell::CreateCallback<Rasterizer>& on_create_rasterizer) {
185+ =======
186+ const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
187+ const Shell::EngineCreateCallback& on_create_engine,
188+ bool is_gpu_disabled) {
189+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
51190 if (!task_runners.IsValid ()) {
52191 FML_LOG (ERROR) << " Task runners to run the shell were invalid." ;
53192 return nullptr ;
54193 }
55194
195+ <<<<<<< HEAD
56196 auto shell =
57197 std::unique_ptr<Shell>(new Shell (std::move (vm), task_runners, settings));
198+ =======
199+ auto shell = std::unique_ptr<Shell>(
200+ new Shell (std::move (vm), task_runners, settings,
201+ std::make_shared<VolatilePathTracker>(
202+ task_runners.GetUITaskRunner (),
203+ !settings.skia_deterministic_rendering_on_cpu ),
204+ is_gpu_disabled));
205+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
58206
59207 // Create the rasterizer on the raster thread.
60208 std::promise<std::unique_ptr<Rasterizer>> rasterizer_promise;
@@ -283,7 +431,13 @@ std::unique_ptr<Shell> Shell::Create(
283431 fml::RefPtr<const DartSnapshot> isolate_snapshot,
284432 const Shell::CreateCallback<PlatformView>& on_create_platform_view,
285433 const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
434+ <<<<<<< HEAD
286435 DartVMRef vm) {
436+ =======
437+ const Shell::EngineCreateCallback& on_create_engine,
438+ bool is_gpu_disabled) {
439+ // This must come first as it initializes tracing.
440+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
287441 PerformInitializationTasks (settings);
288442 PersistentCache::SetCacheSkSL (settings.cache_sksl );
289443
@@ -298,6 +452,7 @@ std::unique_ptr<Shell> Shell::Create(
298452 std::unique_ptr<Shell> shell;
299453 fml::TaskRunner::RunNowOrPostTask (
300454 task_runners.GetPlatformTaskRunner (),
455+ <<<<<<< HEAD
301456 fml::MakeCopyable ([&latch, //
302457 vm = std::move (vm), //
303458 &shell, //
@@ -318,15 +473,53 @@ std::unique_ptr<Shell> Shell::Create(
318473 );
319474 latch.Signal ();
320475 }));
476+ =======
477+ fml::MakeCopyable (
478+ [&latch, //
479+ &shell, //
480+ task_runners = std::move (task_runners), //
481+ platform_data = std::move (platform_data), //
482+ settings = std::move (settings), //
483+ vm = std::move (vm), //
484+ isolate_snapshot = std::move (isolate_snapshot), //
485+ on_create_platform_view = std::move (on_create_platform_view), //
486+ on_create_rasterizer = std::move (on_create_rasterizer), //
487+ on_create_engine = std::move (on_create_engine),
488+ is_gpu_disabled]() mutable {
489+ shell = CreateShellOnPlatformThread (
490+ std::move (vm), //
491+ std::move (task_runners), //
492+ std::move (platform_data), //
493+ std::move (settings), //
494+ std::move (isolate_snapshot), //
495+ std::move (on_create_platform_view), //
496+ std::move (on_create_rasterizer), //
497+ std::move (on_create_engine), is_gpu_disabled);
498+ latch.Signal ();
499+ }));
500+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
321501 latch.Wait ();
322502 return shell;
323503}
324504
505+ <<<<<<< HEAD
325506Shell::Shell (DartVMRef vm, TaskRunners task_runners, Settings settings)
326507 : task_runners_ (std::move (task_runners)),
327508 settings_ (std::move (settings)),
328509 vm_ (std::move (vm)),
329510 is_gpu_disabled_sync_switch_ (new fml::SyncSwitch ()),
511+ =======
512+ Shell::Shell (DartVMRef vm,
513+ TaskRunners task_runners,
514+ Settings settings,
515+ std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
516+ bool is_gpu_disabled)
517+ : task_runners_ (std::move (task_runners)),
518+ settings_ (std::move (settings)),
519+ vm_ (std::move (vm)),
520+ is_gpu_disabled_sync_switch_ (new fml::SyncSwitch (is_gpu_disabled)),
521+ volatile_path_tracker_ (std::move (volatile_path_tracker)),
522+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
330523 weak_factory_gpu_ (nullptr ),
331524 weak_factory_ (this ) {
332525 FML_CHECK (vm_) << " Must have access to VM to create a shell." ;
@@ -435,6 +628,59 @@ Shell::~Shell() {
435628 platform_latch.Wait ();
436629}
437630
631+ <<<<<<< HEAD
632+ =======
633+ std::unique_ptr<Shell> Shell::Spawn (
634+ RunConfiguration run_configuration,
635+ const CreateCallback<PlatformView>& on_create_platform_view,
636+ const CreateCallback<Rasterizer>& on_create_rasterizer) const {
637+ FML_DCHECK (task_runners_.IsValid ());
638+ auto shell_maker = [&](bool is_gpu_disabled) {
639+ std::unique_ptr<Shell> result (CreateWithSnapshot (
640+ PlatformData{}, task_runners_, GetSettings (), vm_,
641+ vm_->GetVMData ()->GetIsolateSnapshot (), on_create_platform_view,
642+ on_create_rasterizer,
643+ [engine = this ->engine_ .get ()](
644+ Engine::Delegate& delegate,
645+ const PointerDataDispatcherMaker& dispatcher_maker, DartVM& vm,
646+ fml::RefPtr<const DartSnapshot> isolate_snapshot,
647+ TaskRunners task_runners, const PlatformData& platform_data,
648+ Settings settings, std::unique_ptr<Animator> animator,
649+ fml::WeakPtr<IOManager> io_manager,
650+ fml::RefPtr<SkiaUnrefQueue> unref_queue,
651+ fml::WeakPtr<SnapshotDelegate> snapshot_delegate,
652+ std::shared_ptr<VolatilePathTracker> volatile_path_tracker) {
653+ return engine->Spawn (/* delegate=*/ delegate,
654+ /* dispatcher_maker=*/ dispatcher_maker,
655+ /* settings=*/ settings,
656+ /* animator=*/ std::move (animator));
657+ },
658+ is_gpu_disabled));
659+ return result;
660+ };
661+ std::unique_ptr<Shell> result;
662+ GetIsGpuDisabledSyncSwitch ()->Execute (
663+ fml::SyncSwitch::Handlers ()
664+ .SetIfFalse ([&] { result = shell_maker (false ); })
665+ .SetIfTrue ([&] { result = shell_maker (true ); }));
666+ result->shared_resource_context_ = io_manager_->GetSharedResourceContext ();
667+ result->RunEngine (std::move (run_configuration));
668+
669+ task_runners_.GetRasterTaskRunner ()->PostTask (
670+ [rasterizer = rasterizer_->GetWeakPtr (),
671+ spawn_rasterizer = result->rasterizer_ ->GetWeakPtr ()]() {
672+ if (rasterizer) {
673+ rasterizer->BlockThreadMerging ();
674+ }
675+ if (spawn_rasterizer) {
676+ spawn_rasterizer->BlockThreadMerging ();
677+ }
678+ });
679+
680+ return result;
681+ }
682+
683+ >>>>>>> 183b6b9dc... Started initializing the gpu disable syncswitch based on the app state. (#24503 )
438684void Shell::NotifyLowMemoryWarning () const {
439685 auto trace_id = fml::tracing::TraceNonce ();
440686 TRACE_EVENT_ASYNC_BEGIN0 (" flutter" , " Shell::NotifyLowMemoryWarning" ,
0 commit comments