Skip to content

Commit 97f358f

Browse files
author
Jonah Williams
authored
[Impeller] defer impeller context initialization onto the raster thread. (flutter#167527)
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 b8aa0fd commit 97f358f

38 files changed

Lines changed: 941 additions & 348 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/gpu/context.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ std::shared_ptr<impeller::Context> Context::GetDefaultContext(
5858
dart_state->GetTaskRunners().GetIOTaskRunner(),
5959
fml::MakeCopyable([promise = std::move(context_promise),
6060
io_manager = dart_state->GetIOManager()]() mutable {
61-
promise.set_value(io_manager ? io_manager->GetImpellerContext()
62-
: nullptr);
61+
promise.set_value(io_manager->GetImpellerContext());
6362
}));
6463
auto context = impeller_context_future.get();
6564

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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_LIB_UI_IO_MANAGER_H_
66
#define FLUTTER_LIB_UI_IO_MANAGER_H_
77

8+
#include <future>
89
#include "flutter/flow/skia_gpu_object.h"
910
#include "flutter/fml/memory/weak_ptr.h"
1011
#include "flutter/fml/synchronization/sync_switch.h"
@@ -31,7 +32,10 @@ class IOManager {
3132
virtual std::shared_ptr<const fml::SyncSwitch>
3233
GetIsGpuDisabledSyncSwitch() = 0;
3334

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

3741
} // namespace flutter

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "flutter/assets/asset_manager.h"
1212
#include "flutter/fml/trace_event.h"
13+
#include "flutter/impeller/renderer/context.h"
1314
#include "flutter/impeller/runtime_stage/runtime_stage.h"
1415
#include "flutter/lib/ui/ui_dart_state.h"
1516
#include "flutter/lib/ui/window/platform_configuration.h"

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

0 commit comments

Comments
 (0)