Skip to content

Commit c0c12a7

Browse files
author
Jonah Williams
authored
[Impeller] reland: defer impeller context initialization onto the raster thread. (#167765)
Attempt to speed up vulkan startup by ensuring vulkan driver initialization happens on raster thread. Prev. we immediately initialized the impeller::Context, unfortunately setting up the vulkan context can take upwards of 100ms. This time is running on the platform thread and blocking startup. Instead, I attempt to hide/defer as much as possible what backend is being used - this requires us to access the impeller context via a shared_future in some cases instead of immediately knowing it is valid.
1 parent 5da19cc commit c0c12a7

38 files changed

Lines changed: 975 additions & 349 deletions

engine/src/flutter/ci/licenses_golden/licenses_flutter

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51891,7 +51891,6 @@ ORIGIN: ../../../flutter/lib/ui/dart_wrapper.h + ../../../flutter/LICENSE
5189151891
ORIGIN: ../../../flutter/lib/ui/floating_point.h + ../../../flutter/LICENSE
5189251892
ORIGIN: ../../../flutter/lib/ui/geometry.dart + ../../../flutter/LICENSE
5189351893
ORIGIN: ../../../flutter/lib/ui/hooks.dart + ../../../flutter/LICENSE
51894-
ORIGIN: ../../../flutter/lib/ui/io_manager.cc + ../../../flutter/LICENSE
5189551894
ORIGIN: ../../../flutter/lib/ui/io_manager.h + ../../../flutter/LICENSE
5189651895
ORIGIN: ../../../flutter/lib/ui/isolate_name_server.dart + ../../../flutter/LICENSE
5189751896
ORIGIN: ../../../flutter/lib/ui/isolate_name_server/isolate_name_server.cc + ../../../flutter/LICENSE
@@ -52425,6 +52424,8 @@ ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h + ../../../flut
5242552424
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc + ../../../flutter/LICENSE
5242652425
ORIGIN: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h + ../../../flutter/LICENSE
5242752426
ORIGIN: ../../../flutter/shell/platform/android/AndroidManifest.xml + ../../../flutter/LICENSE
52427+
ORIGIN: ../../../flutter/shell/platform/android/android_context_dynamic_impeller.cc + ../../../flutter/LICENSE
52428+
ORIGIN: ../../../flutter/shell/platform/android/android_context_dynamic_impeller.h + ../../../flutter/LICENSE
5242852429
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc + ../../../flutter/LICENSE
5242952430
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_impeller.h + ../../../flutter/LICENSE
5243052431
ORIGIN: ../../../flutter/shell/platform/android/android_context_gl_skia.cc + ../../../flutter/LICENSE
@@ -52443,6 +52444,8 @@ ORIGIN: ../../../flutter/shell/platform/android/android_image_generator.h + ../.
5244352444
ORIGIN: ../../../flutter/shell/platform/android/android_rendering_selector.h + ../../../flutter/LICENSE
5244452445
ORIGIN: ../../../flutter/shell/platform/android/android_shell_holder.cc + ../../../flutter/LICENSE
5244552446
ORIGIN: ../../../flutter/shell/platform/android/android_shell_holder.h + ../../../flutter/LICENSE
52447+
ORIGIN: ../../../flutter/shell/platform/android/android_surface_dynamic_impeller.cc + ../../../flutter/LICENSE
52448+
ORIGIN: ../../../flutter/shell/platform/android/android_surface_dynamic_impeller.h + ../../../flutter/LICENSE
5244652449
ORIGIN: ../../../flutter/shell/platform/android/android_surface_gl_impeller.cc + ../../../flutter/LICENSE
5244752450
ORIGIN: ../../../flutter/shell/platform/android/android_surface_gl_impeller.h + ../../../flutter/LICENSE
5244852451
ORIGIN: ../../../flutter/shell/platform/android/android_surface_gl_skia.cc + ../../../flutter/LICENSE
@@ -52459,6 +52462,8 @@ ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_
5245952462
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.h + ../../../flutter/LICENSE
5246052463
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc + ../../../flutter/LICENSE
5246152464
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.h + ../../../flutter/LICENSE
52465+
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_wrapper.cc + ../../../flutter/LICENSE
52466+
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_wrapper.h + ../../../flutter/LICENSE
5246252467
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.cc + ../../../flutter/LICENSE
5246352468
ORIGIN: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.h + ../../../flutter/LICENSE
5246452469
ORIGIN: ../../../flutter/shell/platform/android/flutter_main.cc + ../../../flutter/LICENSE
@@ -54883,7 +54888,6 @@ FILE: ../../../flutter/lib/ui/dart_wrapper.h
5488354888
FILE: ../../../flutter/lib/ui/floating_point.h
5488454889
FILE: ../../../flutter/lib/ui/geometry.dart
5488554890
FILE: ../../../flutter/lib/ui/hooks.dart
54886-
FILE: ../../../flutter/lib/ui/io_manager.cc
5488754891
FILE: ../../../flutter/lib/ui/io_manager.h
5488854892
FILE: ../../../flutter/lib/ui/isolate_name_server.dart
5488954893
FILE: ../../../flutter/lib/ui/isolate_name_server/isolate_name_server.cc
@@ -55418,6 +55422,8 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
5541855422
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.cc
5541955423
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_impeller.h
5542055424
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
55425+
FILE: ../../../flutter/shell/platform/android/android_context_dynamic_impeller.cc
55426+
FILE: ../../../flutter/shell/platform/android/android_context_dynamic_impeller.h
5542155427
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.cc
5542255428
FILE: ../../../flutter/shell/platform/android/android_context_gl_impeller.h
5542355429
FILE: ../../../flutter/shell/platform/android/android_context_gl_skia.cc
@@ -55436,6 +55442,8 @@ FILE: ../../../flutter/shell/platform/android/android_image_generator.h
5543655442
FILE: ../../../flutter/shell/platform/android/android_rendering_selector.h
5543755443
FILE: ../../../flutter/shell/platform/android/android_shell_holder.cc
5543855444
FILE: ../../../flutter/shell/platform/android/android_shell_holder.h
55445+
FILE: ../../../flutter/shell/platform/android/android_surface_dynamic_impeller.cc
55446+
FILE: ../../../flutter/shell/platform/android/android_surface_dynamic_impeller.h
5543955447
FILE: ../../../flutter/shell/platform/android/android_surface_gl_impeller.cc
5544055448
FILE: ../../../flutter/shell/platform/android/android_surface_gl_impeller.h
5544155449
FILE: ../../../flutter/shell/platform/android/android_surface_gl_skia.cc
@@ -55452,6 +55460,8 @@ FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_vi
5545255460
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder.h
5545355461
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.cc
5545455462
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_2.h
55463+
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_wrapper.cc
55464+
FILE: ../../../flutter/shell/platform/android/external_view_embedder/external_view_embedder_wrapper.h
5545555465
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.cc
5545655466
FILE: ../../../flutter/shell/platform/android/external_view_embedder/surface_pool.h
5545755467
FILE: ../../../flutter/shell/platform/android/flutter_main.cc

engine/src/flutter/impeller/renderer/context.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@
44

55
#include "impeller/renderer/context.h"
66

7+
#include <mutex>
78
#include <utility>
89

910
namespace impeller {
1011

12+
ImpellerContextFuture::ImpellerContextFuture(
13+
std::future<std::shared_ptr<impeller::Context>> context)
14+
: future_(std::move(context)) {}
15+
16+
std::shared_ptr<impeller::Context> ImpellerContextFuture::GetContext() {
17+
std::scoped_lock<std::mutex> lock(mutex_);
18+
if (!did_wait_ && future_.valid()) {
19+
context_ = future_.get();
20+
did_wait_ = true;
21+
}
22+
return context_;
23+
}
24+
1125
Context::~Context() = default;
1226

1327
Context::Context(const Flags& flags) : flags_(flags) {}

engine/src/flutter/impeller/renderer/context.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#ifndef FLUTTER_IMPELLER_RENDERER_CONTEXT_H_
66
#define FLUTTER_IMPELLER_RENDERER_CONTEXT_H_
77

8+
#include <future>
89
#include <memory>
910
#include <string>
1011

1112
#include "fml/closure.h"
1213
#include "impeller/base/flags.h"
14+
#include "impeller/base/thread_safety.h"
1315
#include "impeller/core/allocator.h"
1416
#include "impeller/core/formats.h"
1517
#include "impeller/renderer/capabilities.h"
@@ -22,6 +24,22 @@ class ShaderLibrary;
2224
class CommandBuffer;
2325
class PipelineLibrary;
2426

27+
/// A wrapper for provided a deferred initialization of impeller to various
28+
/// engine subsystems.
29+
class ImpellerContextFuture {
30+
public:
31+
explicit ImpellerContextFuture(
32+
std::future<std::shared_ptr<impeller::Context>> context);
33+
34+
std::shared_ptr<impeller::Context> GetContext();
35+
36+
private:
37+
std::mutex mutex_;
38+
std::future<std::shared_ptr<impeller::Context>> future_;
39+
std::shared_ptr<impeller::Context> context_;
40+
bool did_wait_ = false;
41+
};
42+
2543
//------------------------------------------------------------------------------
2644
/// @brief To do anything rendering related with Impeller, you need a
2745
/// context.

engine/src/flutter/lib/ui/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ source_set("ui") {
3838
"dart_ui.h",
3939
"dart_wrapper.h",
4040
"floating_point.h",
41-
"io_manager.cc",
4241
"io_manager.h",
4342
"isolate_name_server/isolate_name_server.cc",
4443
"isolate_name_server/isolate_name_server.h",

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class IOManager {
3131
virtual std::shared_ptr<const fml::SyncSwitch>
3232
GetIsGpuDisabledSyncSwitch() = 0;
3333

34-
virtual std::shared_ptr<impeller::Context> GetImpellerContext() const;
34+
/// @brief Retrieve the impeller::Context.
35+
///
36+
/// Call may block until context is availabe.
37+
virtual std::shared_ptr<impeller::Context> GetImpellerContext() const = 0;
3538
};
3639

3740
} // namespace flutter

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ ImageDecoderImpeller::ImageDecoderImpeller(
105105
context_ = context_promise.get_future();
106106
runners_.GetIOTaskRunner()->PostTask(fml::MakeCopyable(
107107
[promise = std::move(context_promise), io_manager]() mutable {
108-
promise.set_value(io_manager ? io_manager->GetImpellerContext()
109-
: nullptr);
108+
if (io_manager) {
109+
promise.set_value(io_manager->GetImpellerContext());
110+
} else {
111+
promise.set_value(nullptr);
112+
}
110113
}));
111114
}
112115

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ UIDartState::Context::Context(
3131
std::string advisory_script_entrypoint,
3232
bool deterministic_rendering_enabled,
3333
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
34+
std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend,
3435
bool enable_impeller,
35-
bool enable_flutter_gpu,
36-
impeller::RuntimeStageBackend runtime_stage_backend)
36+
bool enable_flutter_gpu)
3737
: task_runners(task_runners),
3838
snapshot_delegate(std::move(snapshot_delegate)),
3939
io_manager(std::move(io_manager)),
@@ -44,9 +44,9 @@ UIDartState::Context::Context(
4444
advisory_script_entrypoint(std::move(advisory_script_entrypoint)),
4545
deterministic_rendering_enabled(deterministic_rendering_enabled),
4646
concurrent_task_runner(std::move(concurrent_task_runner)),
47+
runtime_stage_backend(std::move(runtime_stage_backend)),
4748
enable_impeller(enable_impeller),
48-
enable_flutter_gpu(enable_flutter_gpu),
49-
runtime_stage_backend(runtime_stage_backend) {}
49+
enable_flutter_gpu(enable_flutter_gpu) {}
5050

5151
UIDartState::UIDartState(
5252
TaskObserverAdd add_callback,
@@ -88,10 +88,6 @@ bool UIDartState::IsFlutterGPUEnabled() const {
8888
return context_.enable_impeller && context_.enable_flutter_gpu;
8989
}
9090

91-
impeller::RuntimeStageBackend UIDartState::GetRuntimeStageBackend() const {
92-
return context_.runtime_stage_backend;
93-
}
94-
9591
void UIDartState::DidSetIsolate() {
9692
main_port_ = Dart_GetMainPortId();
9793
std::ostringstream debug_name;
@@ -253,4 +249,9 @@ Dart_Isolate UIDartState::CreatePlatformIsolate(Dart_Handle entry_point,
253249
return nullptr;
254250
}
255251

252+
/// The runtime stage to use for fragment shaders.
253+
impeller::RuntimeStageBackend UIDartState::GetRuntimeStageBackend() const {
254+
return context_.runtime_stage_backend.get();
255+
}
256+
256257
} // namespace flutter

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_LIB_UI_UI_DART_STATE_H_
66
#define FLUTTER_LIB_UI_UI_DART_STATE_H_
77

8+
#include <future>
89
#include <memory>
910
#include <string>
1011
#include <utility>
@@ -19,6 +20,7 @@
1920
#include "flutter/lib/ui/painting/image_decoder.h"
2021
#include "flutter/lib/ui/snapshot_delegate.h"
2122
#include "flutter/shell/common/platform_message_handler.h"
23+
#include "impeller/core/runtime_types.h"
2224
#include "impeller/runtime_stage/runtime_stage.h"
2325
#include "third_party/dart/runtime/include/dart_api.h"
2426
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
@@ -44,20 +46,21 @@ class UIDartState : public tonic::DartState {
4446
struct Context {
4547
explicit Context(const TaskRunners& task_runners);
4648

47-
Context(const TaskRunners& task_runners,
48-
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate,
49-
fml::WeakPtr<IOManager> io_manager,
50-
fml::RefPtr<SkiaUnrefQueue> unref_queue,
51-
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
52-
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
53-
image_generator_registry,
54-
std::string advisory_script_uri,
55-
std::string advisory_script_entrypoint,
56-
bool deterministic_rendering_enabled,
57-
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
58-
bool enable_impeller,
59-
bool enable_flutter_gpu,
60-
impeller::RuntimeStageBackend runtime_stage_backend);
49+
Context(
50+
const TaskRunners& task_runners,
51+
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate,
52+
fml::WeakPtr<IOManager> io_manager,
53+
fml::RefPtr<SkiaUnrefQueue> unref_queue,
54+
fml::TaskRunnerAffineWeakPtr<ImageDecoder> image_decoder,
55+
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
56+
image_generator_registry,
57+
std::string advisory_script_uri,
58+
std::string advisory_script_entrypoint,
59+
bool deterministic_rendering_enabled,
60+
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner,
61+
std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend,
62+
bool enable_impeller,
63+
bool enable_flutter_gpu);
6164

6265
/// The task runners used by the shell hosting this runtime controller. This
6366
/// may be used by the isolate to scheduled asynchronous texture uploads or
@@ -101,14 +104,14 @@ class UIDartState : public tonic::DartState {
101104
/// of shared worker threads.
102105
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner;
103106

107+
/// The runtime stage backend for fragment shaders.
108+
std::shared_future<impeller::RuntimeStageBackend> runtime_stage_backend;
109+
104110
/// Whether Impeller is enabled or not.
105111
bool enable_impeller = false;
106112

107113
/// Whether flutter_gpu is enabled or not.
108114
bool enable_flutter_gpu = false;
109-
110-
/// The expected backend for runtime stage shaders.
111-
impeller::RuntimeStageBackend runtime_stage_backend;
112115
};
113116

114117
Dart_Port main_port() const { return main_port_; }
@@ -180,7 +183,7 @@ class UIDartState : public tonic::DartState {
180183
/// Whether Flutter GPU is enabled for this application.
181184
bool IsFlutterGPUEnabled() const;
182185

183-
/// The expected type for runtime stage shaders.
186+
/// The runtime stage to use for fragment shaders.
184187
impeller::RuntimeStageBackend GetRuntimeStageBackend() const;
185188

186189
virtual Dart_Isolate CreatePlatformIsolate(Dart_Handle entry_point,

engine/src/flutter/runtime/runtime_controller.cc

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,21 @@ std::unique_ptr<RuntimeController> RuntimeController::Spawn(
6060
fml::TaskRunnerAffineWeakPtr<ImageGeneratorRegistry>
6161
image_generator_registry,
6262
fml::TaskRunnerAffineWeakPtr<SnapshotDelegate> snapshot_delegate) const {
63-
UIDartState::Context spawned_context{context_.task_runners,
64-
std::move(snapshot_delegate),
65-
std::move(io_manager),
66-
context_.unref_queue,
67-
std::move(image_decoder),
68-
std::move(image_generator_registry),
69-
advisory_script_uri,
70-
advisory_script_entrypoint,
71-
context_.deterministic_rendering_enabled,
72-
context_.concurrent_task_runner,
73-
context_.enable_impeller,
74-
context_.enable_flutter_gpu,
75-
context_.runtime_stage_backend};
63+
UIDartState::Context spawned_context{
64+
context_.task_runners,
65+
std::move(snapshot_delegate),
66+
std::move(io_manager),
67+
context_.unref_queue,
68+
std::move(image_decoder),
69+
std::move(image_generator_registry),
70+
advisory_script_uri,
71+
advisory_script_entrypoint,
72+
context_.deterministic_rendering_enabled,
73+
context_.concurrent_task_runner,
74+
context_.runtime_stage_backend,
75+
context_.enable_impeller,
76+
context_.enable_flutter_gpu,
77+
};
7678
auto result =
7779
std::make_unique<RuntimeController>(p_client, //
7880
vm_, //

0 commit comments

Comments
 (0)