Skip to content

Commit c586417

Browse files
Revert "Reduce app startup latency by initializing the engine on a separate thread (flutter#166918)" (flutter#167427)
This reverts commit c53fdbd. See flutter#167418
1 parent 00863d6 commit c586417

36 files changed

+91
-292
lines changed

engine/src/flutter/common/settings.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "flutter/fml/build_config.h"
1717
#include "flutter/fml/closure.h"
1818
#include "flutter/fml/mapping.h"
19-
#include "flutter/fml/task_queue_id.h"
2019
#include "flutter/fml/time/time_point.h"
2120
#include "flutter/fml/unique_fd.h"
2221

@@ -71,10 +70,8 @@ class FrameTiming {
7170
};
7271

7372
using TaskObserverAdd =
74-
std::function<fml::TaskQueueId(intptr_t /* key */,
75-
fml::closure /* callback */)>;
76-
using TaskObserverRemove =
77-
std::function<void(fml::TaskQueueId /* queue */, intptr_t /* key */)>;
73+
std::function<void(intptr_t /* key */, fml::closure /* callback */)>;
74+
using TaskObserverRemove = std::function<void(intptr_t /* key */)>;
7875
using UnhandledExceptionCallback =
7976
std::function<bool(const std::string& /* error */,
8077
const std::string& /* stack trace */)>;
@@ -362,20 +359,9 @@ struct Settings {
362359
/// This is used by the runOnPlatformThread API.
363360
bool enable_platform_isolates = false;
364361

365-
enum class MergedPlatformUIThread {
366-
// Use separate threads for the UI and platform task runners.
367-
kDisabled,
368-
// Use the platform thread for both the UI and platform task runners.
369-
kEnabled,
370-
// Start the engine on a separate UI thread and then move the UI task
371-
// runner to the platform thread after the engine is initialized.
372-
// This can improve app launch latency by allowing other work to run on
373-
// the platform thread during engine startup.
374-
kMergeAfterLaunch
375-
};
376-
377-
MergedPlatformUIThread merged_platform_ui_thread =
378-
MergedPlatformUIThread::kEnabled;
362+
// If true, the UI thread is the platform thread on supported
363+
// platforms.
364+
bool merged_platform_ui_thread = true;
379365
};
380366

381367
} // namespace flutter

engine/src/flutter/fml/platform/fuchsia/task_observers.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ void ExecuteAfterTaskObservers() {
1616
}
1717
}
1818

19-
fml::TaskQueueId CurrentMessageLoopAddAfterTaskObserver(intptr_t key,
20-
fit::closure observer) {
19+
void CurrentMessageLoopAddAfterTaskObserver(intptr_t key,
20+
fit::closure observer) {
2121
if (observer) {
2222
tTaskObservers[key] = std::move(observer);
2323
}
24-
return fml::TaskQueueId::Invalid();
2524
}
2625

27-
void CurrentMessageLoopRemoveAfterTaskObserver(fml::TaskQueueId queue_id,
28-
intptr_t key) {
26+
void CurrentMessageLoopRemoveAfterTaskObserver(intptr_t key) {
2927
tTaskObservers.erase(key);
3028
}
3129

engine/src/flutter/fml/platform/fuchsia/task_observers.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
#include <lib/fit/function.h>
99

10-
#include "flutter/fml/task_queue_id.h"
11-
1210
namespace fml {
1311

1412
// Executes all closures that were registered via
@@ -32,11 +30,10 @@ namespace fml {
3230
// somehow.
3331
void ExecuteAfterTaskObservers();
3432

35-
fml::TaskQueueId CurrentMessageLoopAddAfterTaskObserver(intptr_t key,
36-
fit::closure observer);
33+
void CurrentMessageLoopAddAfterTaskObserver(intptr_t key,
34+
fit::closure observer);
3735

38-
void CurrentMessageLoopRemoveAfterTaskObserver(fml::TaskQueueId queue_id,
39-
intptr_t key);
36+
void CurrentMessageLoopRemoveAfterTaskObserver(intptr_t key);
4037

4138
} // namespace fml
4239

engine/src/flutter/fml/task_queue_id.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class TaskQueueId {
2525
/// Intializes a task queue with the given value as it's ID.
2626
explicit TaskQueueId(size_t value) : value_(value) {}
2727

28-
static TaskQueueId Invalid() { return TaskQueueId(kInvalid); }
29-
3028
operator size_t() const { // NOLINT(google-explicit-constructor)
3129
return value_;
3230
}

engine/src/flutter/lib/ui/painting/image_decoder.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ImageDecoder::ImageDecoder(
5555

5656
ImageDecoder::~ImageDecoder() = default;
5757

58-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> ImageDecoder::GetWeakPtr() const {
58+
fml::WeakPtr<ImageDecoder> ImageDecoder::GetWeakPtr() const {
5959
return weak_factory_.GetWeakPtr();
6060
}
6161

engine/src/flutter/lib/ui/painting/image_decoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ImageDecoder {
4444
uint32_t target_height,
4545
const ImageResult& result) = 0;
4646

47-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> GetWeakPtr() const;
47+
fml::WeakPtr<ImageDecoder> GetWeakPtr() const;
4848

4949
protected:
5050
TaskRunners runners_;
@@ -57,7 +57,7 @@ class ImageDecoder {
5757
fml::WeakPtr<IOManager> io_manager);
5858

5959
private:
60-
fml::TaskRunnerAffineWeakPtrFactory<ImageDecoder> weak_factory_;
60+
fml::WeakPtrFactory<ImageDecoder> weak_factory_;
6161

6262
FML_DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
6363
};

engine/src/flutter/lib/ui/painting/image_generator_registry.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ ImageGeneratorRegistry::CreateCompatibleGenerator(const sk_sp<SkData>& buffer) {
7979
return nullptr;
8080
}
8181

82-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
83-
ImageGeneratorRegistry::GetWeakPtr() const {
82+
fml::WeakPtr<ImageGeneratorRegistry> ImageGeneratorRegistry::GetWeakPtr()
83+
const {
8484
return weak_factory_.GetWeakPtr();
8585
}
8686

engine/src/flutter/lib/ui/painting/image_generator_registry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ImageGeneratorRegistry {
5858
std::shared_ptr<ImageGenerator> CreateCompatibleGenerator(
5959
const sk_sp<SkData>& buffer);
6060

61-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry> GetWeakPtr() const;
61+
fml::WeakPtr<ImageGeneratorRegistry> GetWeakPtr() const;
6262

6363
private:
6464
struct PrioritizedFactory {
@@ -85,7 +85,7 @@ class ImageGeneratorRegistry {
8585
using FactorySet = std::set<PrioritizedFactory, Compare>;
8686
FactorySet image_generator_factories_;
8787
size_t nonce_;
88-
fml::TaskRunnerAffineWeakPtrFactory<ImageGeneratorRegistry> weak_factory_;
88+
fml::WeakPtrFactory<ImageGeneratorRegistry> weak_factory_;
8989
};
9090

9191
} // namespace flutter

engine/src/flutter/lib/ui/ui_dart_state.cc

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ UIDartState::Context::Context(
2424
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate,
2525
fml::WeakPtr<IOManager> io_manager,
2626
fml::RefPtr<SkiaUnrefQueue> unref_queue,
27-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
28-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
29-
image_generator_registry,
27+
fml::WeakPtr<ImageDecoder> image_decoder,
28+
fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry,
3029
std::string advisory_script_uri,
3130
std::string advisory_script_entrypoint,
3231
bool deterministic_rendering_enabled,
@@ -59,7 +58,6 @@ UIDartState::UIDartState(
5958
const UIDartState::Context& context)
6059
: add_callback_(std::move(add_callback)),
6160
remove_callback_(std::move(remove_callback)),
62-
callback_queue_id_(fml::TaskQueueId::kInvalid),
6361
logger_prefix_(std::move(logger_prefix)),
6462
is_root_isolate_(is_root_isolate),
6563
unhandled_exception_callback_(std::move(unhandled_exception_callback)),
@@ -180,12 +178,10 @@ void UIDartState::AddOrRemoveTaskObserver(bool add) {
180178
}
181179
FML_DCHECK(add_callback_ && remove_callback_);
182180
if (add) {
183-
callback_queue_id_ =
184-
add_callback_(reinterpret_cast<intptr_t>(this),
185-
[this]() { this->FlushMicrotasksNow(); });
181+
add_callback_(reinterpret_cast<intptr_t>(this),
182+
[this]() { this->FlushMicrotasksNow(); });
186183
} else {
187-
remove_callback_(callback_queue_id_, reinterpret_cast<intptr_t>(this));
188-
callback_queue_id_ = fml::TaskQueueId::Invalid();
184+
remove_callback_(reinterpret_cast<intptr_t>(this));
189185
}
190186
}
191187

@@ -194,13 +190,12 @@ UIDartState::GetSnapshotDelegate() const {
194190
return context_.snapshot_delegate;
195191
}
196192

197-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> UIDartState::GetImageDecoder()
198-
const {
193+
fml::WeakPtr<ImageDecoder> UIDartState::GetImageDecoder() const {
199194
return context_.image_decoder;
200195
}
201196

202-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
203-
UIDartState::GetImageGeneratorRegistry() const {
197+
fml::WeakPtr<ImageGeneratorRegistry> UIDartState::GetImageGeneratorRegistry()
198+
const {
204199
return context_.image_generator_registry;
205200
}
206201

engine/src/flutter/lib/ui/ui_dart_state.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ class UIDartState : public tonic::DartState {
4848
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate,
4949
fml::WeakPtr<IOManager> io_manager,
5050
fml::RefPtr<SkiaUnrefQueue> unref_queue,
51-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
52-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
53-
image_generator_registry,
51+
fml::WeakPtr<ImageDecoder> image_decoder,
52+
fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry,
5453
std::string advisory_script_uri,
5554
std::string advisory_script_entrypoint,
5655
bool deterministic_rendering_enabled,
@@ -77,13 +76,12 @@ class UIDartState : public tonic::DartState {
7776
fml::RefPtr<SkiaUnrefQueue> unref_queue;
7877

7978
/// The image decoder.
80-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder;
79+
fml::WeakPtr<ImageDecoder> image_decoder;
8180

8281
/// Cascading registry of image generator builders. Given compressed image
8382
/// bytes as input, this is used to find and create image generators, which
8483
/// can then be used for image decoding.
85-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
86-
image_generator_registry;
84+
fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry;
8785

8886
/// The advisory script URI (only used for debugging). This does not affect
8987
/// the code being run in the isolate in any way.
@@ -146,10 +144,9 @@ class UIDartState : public tonic::DartState {
146144

147145
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> GetSnapshotDelegate() const;
148146

149-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> GetImageDecoder() const;
147+
fml::WeakPtr<ImageDecoder> GetImageDecoder() const;
150148

151-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
152-
GetImageGeneratorRegistry() const;
149+
fml::WeakPtr<ImageGeneratorRegistry> GetImageGeneratorRegistry() const;
153150

154151
std::shared_ptr<IsolateNameServer> GetIsolateNameServer() const;
155152

@@ -208,7 +205,6 @@ class UIDartState : public tonic::DartState {
208205

209206
const TaskObserverAdd add_callback_;
210207
const TaskObserverRemove remove_callback_;
211-
fml::TaskQueueId callback_queue_id_;
212208
const std::string logger_prefix_;
213209
Dart_Port main_port_ = ILLEGAL_PORT;
214210
const bool is_root_isolate_;

engine/src/flutter/runtime/dart_isolate.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
155155

156156
{
157157
tonic::DartState::Scope scope(isolate.get());
158-
if (settings.merged_platform_ui_thread !=
159-
Settings::MergedPlatformUIThread::kMergeAfterLaunch) {
160-
Dart_SetCurrentThreadOwnsIsolate();
161-
}
158+
Dart_SetCurrentThreadOwnsIsolate();
162159

163160
if (settings.root_isolate_create_callback) {
164161
// Isolate callbacks always occur in isolate scope and before user code
@@ -352,7 +349,6 @@ Dart_Isolate DartIsolate::CreatePlatformIsolate(Dart_Handle entry_point,
352349
}
353350
old_task_observer_add(key, callback);
354351
});
355-
return platform_task_runner->GetTaskQueueId();
356352
};
357353

358354
UIDartState::Context context(task_runners);
@@ -1377,11 +1373,6 @@ std::weak_ptr<DartIsolate> DartIsolate::GetWeakIsolatePtr() {
13771373
return std::static_pointer_cast<DartIsolate>(shared_from_this());
13781374
}
13791375

1380-
void DartIsolate::SetOwnerToCurrentThread() {
1381-
tonic::DartIsolateScope isolate_scope(isolate());
1382-
Dart_SetCurrentThreadOwnsIsolate();
1383-
}
1384-
13851376
void DartIsolate::AddIsolateShutdownCallback(const fml::closure& closure) {
13861377
shutdown_callbacks_.emplace_back(std::make_unique<AutoFireClosure>(closure));
13871378
}

engine/src/flutter/runtime/dart_isolate.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,6 @@ class DartIsolate : public UIDartState {
411411
static Dart_Handle LoadLibraryFromKernel(
412412
const std::shared_ptr<const fml::Mapping>& mapping);
413413

414-
// Calls a Dart API that sets the isolate's owner thread to the current
415-
// thread.
416-
void SetOwnerToCurrentThread();
417-
418414
private:
419415
friend class IsolateConfiguration;
420416
class AutoFireClosure {

engine/src/flutter/runtime/runtime_controller.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ std::unique_ptr<RuntimeController> RuntimeController::Spawn(
5656
const fml::closure& p_isolate_shutdown_callback,
5757
const std::shared_ptr<const fml::Mapping>& p_persistent_isolate_data,
5858
fml::WeakPtr<IOManager> io_manager,
59-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
60-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
61-
image_generator_registry,
59+
fml::WeakPtr<ImageDecoder> image_decoder,
60+
fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry,
6261
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate) const {
6362
UIDartState::Context spawned_context{context_.task_runners,
6463
std::move(snapshot_delegate),
@@ -677,13 +676,6 @@ void RuntimeController::ShutdownPlatformIsolates() {
677676
platform_isolate_manager_->ShutdownPlatformIsolates();
678677
}
679678

680-
void RuntimeController::SetRootIsolateOwnerToCurrentThread() {
681-
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
682-
if (root_isolate) {
683-
root_isolate->SetOwnerToCurrentThread();
684-
}
685-
}
686-
687679
RuntimeController::Locale::Locale(std::string language_code_,
688680
std::string country_code_,
689681
std::string script_code_,

engine/src/flutter/runtime/runtime_controller.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ class RuntimeController : public PlatformConfigurationClient,
121121
const fml::closure& isolate_shutdown_callback,
122122
const std::shared_ptr<const fml::Mapping>& persistent_isolate_data,
123123
fml::WeakPtr<IOManager> io_manager,
124-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
125-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
126-
image_generator_registry,
124+
fml::WeakPtr<ImageDecoder> image_decoder,
125+
fml::WeakPtr<ImageGeneratorRegistry> image_generator_registry,
127126
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate) const;
128127

129128
// |PlatformConfigurationClient|
@@ -679,8 +678,6 @@ class RuntimeController : public PlatformConfigurationClient,
679678
return platform_isolate_manager_;
680679
}
681680

682-
void SetRootIsolateOwnerToCurrentThread();
683-
684681
//--------------------------------------------------------------------------
685682
/// @brief Shuts down all registered platform isolates. Must be called
686683
/// from the platform thread.

engine/src/flutter/shell/common/animator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Animator final {
148148
std::deque<uint64_t> trace_flow_ids_;
149149
bool has_rendered_ = false;
150150

151-
fml::TaskRunnerAffineWeakPtrFactory<Animator> weak_factory_;
151+
fml::WeakPtrFactory<Animator> weak_factory_;
152152

153153
friend class testing::ShellTest;
154154

0 commit comments

Comments
 (0)