Skip to content

Commit 05d827d

Browse files
committed
[Windows] Keep ANGLE's compiled GL programs across launches
On Windows, Impeller draws through ANGLE, which compiles every GL program with the Direct3D shader compiler when it is linked. The engine links its built-in programs at startup, so every launch spends most of its startup time in that compiler, and apps pay again for each runtime effect. ANGLE supports persistent program binaries through EGL_ANDROID_blob_cache when the application registers the callbacks. The Windows embedder now registers a disk-backed cache right after eglInitialize, before any context exists. - ANGLE's keys cover the shader sources, its program format version, the GPU and driver version (GL_RENDERER) and bindings, and it rejects a binary whose adapter or feature level differs. A rejected or undecodable entry is relinked and replaced. - One file per entry, with the key, sizes and a checksum, written through a temporary file and an atomic rename on a background thread. A failing file reads as a miss and is deleted. - The default folder is %LOCALAPPDATA%\<CompanyName>\<ProductName>\flutter_program_cache, named from the executable's version resource like path_provider's application cache folder. FlutterDesktopEngineProperties::program_cache_path and DartProject::set_program_cache_path() move it, or turn it off when empty; --program-cache-path=<folder> overrides it in debug and profile builds. - Entries live in a folder per engine version; the first write of a new version removes other versions' cache files. The total is bounded to 32 MiB, least recently used first. Part of flutter#191860.
1 parent 2abcfdc commit 05d827d

16 files changed

Lines changed: 1534 additions & 9 deletions

‎engine/src/flutter/shell/platform/windows/BUILD.gn‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ source_set("flutter_windows_source") {
6464
"egl/manager.h",
6565
"egl/proc_table.cc",
6666
"egl/proc_table.h",
67+
"egl/program_cache.cc",
68+
"egl/program_cache.h",
6769
"egl/surface.cc",
6870
"egl/surface.h",
6971
"egl/window_surface.cc",
@@ -154,6 +156,9 @@ source_set("flutter_windows_source") {
154156
libs = [
155157
"dwmapi.lib",
156158
"imm32.lib",
159+
"ole32.lib",
160+
"shell32.lib",
161+
"version.lib",
157162
]
158163

159164
configs += [
@@ -186,6 +191,7 @@ source_set("flutter_windows_source") {
186191
"//flutter/shell/platform/common/client_wrapper",
187192
"//flutter/shell/platform/embedder:embedder_as_internal_library",
188193
"//flutter/shell/platform/windows/client_wrapper:client_wrapper_windows",
194+
"//flutter/shell/version",
189195

190196
# libEGL_static MUST be ordered before libGLESv2_static in this list. If
191197
# reversed, a linker error will occur, reporting DllMain already defined in
@@ -228,6 +234,7 @@ executable("flutter_windows_unittests") {
228234
"direct_manipulation_unittests.cc",
229235
"display_manager_unittests.cc",
230236
"dpi_utils_unittests.cc",
237+
"egl/program_cache_unittests.cc",
231238
"flutter_project_bundle_unittests.cc",
232239
"flutter_window_unittests.cc",
233240
"flutter_windows_engine_unittests.cc",

‎engine/src/flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include <memory>
6+
#include <optional>
67
#include <string>
78

89
#include "flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h"
@@ -26,6 +27,12 @@ class DartProjectTest : public ::testing::Test {
2627
std::wstring GetProjectAotLibraryPath(const DartProject& project) {
2728
return project.aot_library_path();
2829
}
30+
31+
// Wrapper for accessing private program_cache_path_.
32+
std::optional<std::wstring> GetProjectProgramCachePath(
33+
const DartProject& project) {
34+
return project.program_cache_path();
35+
}
2936
};
3037

3138
TEST_F(DartProjectTest, StandardProjectFormat) {
@@ -66,4 +73,15 @@ TEST_F(DartProjectTest, EnableFlutterGpu) {
6673
EXPECT_FALSE(project.enable_flutter_gpu());
6774
}
6875

76+
TEST_F(DartProjectTest, ProgramCachePath) {
77+
DartProject project(L"test");
78+
EXPECT_FALSE(GetProjectProgramCachePath(project).has_value());
79+
80+
project.set_program_cache_path(L"C:\\cache");
81+
EXPECT_EQ(GetProjectProgramCachePath(project), L"C:\\cache");
82+
83+
project.set_program_cache_path(L"");
84+
EXPECT_EQ(GetProjectProgramCachePath(project), L"");
85+
}
86+
6987
} // namespace flutter

‎engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ FlutterEngine::FlutterEngine(const DartProject& project) {
2828
c_engine_properties.impeller_switch =
2929
static_cast<FlutterDesktopImpellerSwitch>(project.impeller_switch());
3030
c_engine_properties.enable_flutter_gpu = project.enable_flutter_gpu();
31+
c_engine_properties.program_cache_path =
32+
project.program_cache_path().has_value()
33+
? project.program_cache_path()->c_str()
34+
: nullptr;
3135

3236
const std::vector<std::string>& entrypoint_args =
3337
project.dart_entrypoint_arguments();

‎engine/src/flutter/shell/platform/windows/client_wrapper/flutter_engine_unittests.cc‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#include <memory>
6+
#include <optional>
67
#include <string>
78

89
#include "flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_engine.h"
@@ -28,6 +29,10 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
2829
dart_entrypoint_arguments_.push_back(
2930
std::string(engine_properties.dart_entrypoint_argv[i]));
3031
}
32+
program_cache_path_.reset();
33+
if (engine_properties.program_cache_path != nullptr) {
34+
program_cache_path_ = engine_properties.program_cache_path;
35+
}
3136
return reinterpret_cast<FlutterDesktopEngineRef>(1);
3237
}
3338

@@ -79,6 +84,10 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
7984
return dart_entrypoint_arguments_;
8085
}
8186

87+
const std::optional<std::wstring>& program_cache_path() {
88+
return program_cache_path_;
89+
}
90+
8291
bool has_next_frame_callback() { return next_frame_callback_ != nullptr; }
8392
void run_next_frame_callback() {
8493
next_frame_callback_(next_frame_user_data_);
@@ -93,6 +102,7 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
93102
bool destroy_called_ = false;
94103
bool reload_fonts_called_ = false;
95104
std::vector<std::string> dart_entrypoint_arguments_;
105+
std::optional<std::wstring> program_cache_path_;
96106
VoidCallback next_frame_callback_ = nullptr;
97107
void* next_frame_user_data_ = nullptr;
98108
UINT last_external_message_ = 0;
@@ -115,6 +125,28 @@ TEST(FlutterEngineTest, CreateDestroy) {
115125
EXPECT_EQ(test_api->destroy_called(), true);
116126
}
117127

128+
TEST(FlutterEngineTest, PassesTheProgramCachePath) {
129+
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
130+
std::make_unique<TestFlutterWindowsApi>());
131+
auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
132+
{
133+
FlutterEngine engine(DartProject(L"fake/project/path"));
134+
EXPECT_FALSE(test_api->program_cache_path().has_value());
135+
}
136+
{
137+
DartProject project(L"fake/project/path");
138+
project.set_program_cache_path(L"C:\\cache");
139+
FlutterEngine engine(project);
140+
EXPECT_EQ(test_api->program_cache_path(), L"C:\\cache");
141+
}
142+
{
143+
DartProject project(L"fake/project/path");
144+
project.set_program_cache_path(L"");
145+
FlutterEngine engine(project);
146+
EXPECT_EQ(test_api->program_cache_path(), L"");
147+
}
148+
}
149+
118150
TEST(FlutterEngineTest, CreateDestroyWithCustomEntrypoint) {
119151
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
120152
std::make_unique<TestFlutterWindowsApi>());

‎engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/dart_project.h‎

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

8+
#include <optional>
89
#include <string>
910
#include <vector>
1011

@@ -163,6 +164,14 @@ class DartProject {
163164
// Defaults to false.
164165
bool enable_flutter_gpu() const { return enable_flutter_gpu_; }
165166

167+
// Sets the folder in which the engine keeps the GPU programs it compiles,
168+
// so that later launches load them instead of compiling them again. An
169+
// empty path turns this off. By default the engine uses a folder under
170+
// the user's local application data folder.
171+
void set_program_cache_path(const std::wstring& path) {
172+
program_cache_path_ = path;
173+
}
174+
166175
private:
167176
// Accessors for internals are private, so that they can be changed if more
168177
// flexible options for project structures are needed later without it
@@ -175,6 +184,9 @@ class DartProject {
175184
const std::wstring& assets_path() const { return assets_path_; }
176185
const std::wstring& icu_data_path() const { return icu_data_path_; }
177186
const std::wstring& aot_library_path() const { return aot_library_path_; }
187+
const std::optional<std::wstring>& program_cache_path() const {
188+
return program_cache_path_;
189+
}
178190

179191
// The path to the assets directory.
180192
std::wstring assets_path_;
@@ -197,6 +209,8 @@ class DartProject {
197209
ImpellerSwitch impeller_switch_ = ImpellerSwitch::Default;
198210
// Whether the Flutter GPU API is enabled.
199211
bool enable_flutter_gpu_ = false;
212+
// The folder for compiled GPU programs, if set.
213+
std::optional<std::wstring> program_cache_path_;
200214
};
201215

202216
} // namespace flutter

‎engine/src/flutter/shell/platform/windows/egl/manager.cc‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ namespace egl {
1414

1515
int Manager::instance_count_ = 0;
1616

17-
std::unique_ptr<Manager> Manager::Create(GpuPreference gpu_preference) {
17+
std::unique_ptr<Manager> Manager::Create(
18+
GpuPreference gpu_preference,
19+
std::unique_ptr<ProgramCache> program_cache) {
1820
std::unique_ptr<Manager> manager;
19-
manager.reset(new Manager(gpu_preference));
21+
manager.reset(new Manager(gpu_preference, std::move(program_cache)));
2022
if (!manager->IsValid()) {
2123
return nullptr;
2224
}
2325
return std::move(manager);
2426
}
2527

26-
Manager::Manager(GpuPreference gpu_preference) {
28+
Manager::Manager(GpuPreference gpu_preference,
29+
std::unique_ptr<ProgramCache> program_cache) {
2730
++instance_count_;
2831

29-
if (!InitializeDisplay(gpu_preference)) {
32+
if (!InitializeDisplay(gpu_preference, std::move(program_cache))) {
3033
return;
3134
}
3235

@@ -46,7 +49,8 @@ Manager::~Manager() {
4649
--instance_count_;
4750
}
4851

49-
bool Manager::InitializeDisplay(GpuPreference gpu_preference) {
52+
bool Manager::InitializeDisplay(GpuPreference gpu_preference,
53+
std::unique_ptr<ProgramCache> program_cache) {
5054
// If the request for a low power GPU is provided,
5155
// we will attempt to select GPU explicitly, via ANGLE extension
5256
// that allows to specify the GPU to use via LUID.
@@ -178,6 +182,11 @@ bool Manager::InitializeDisplay(GpuPreference gpu_preference) {
178182
continue;
179183
}
180184

185+
// Before any context exists, so that ANGLE offers every program it
186+
// links to the cache.
187+
if (program_cache) {
188+
ProgramCache::InstallForDisplay(display_, std::move(program_cache));
189+
}
181190
return true;
182191
}
183192

‎engine/src/flutter/shell/platform/windows/egl/manager.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "flutter/fml/macros.h"
2525
#include "flutter/shell/platform/windows/egl/context.h"
26+
#include "flutter/shell/platform/windows/egl/program_cache.h"
2627
#include "flutter/shell/platform/windows/egl/surface.h"
2728
#include "flutter/shell/platform/windows/egl/window_surface.h"
2829

@@ -39,7 +40,11 @@ enum class GpuPreference {
3940
// destroy surfaces
4041
class Manager {
4142
public:
42-
static std::unique_ptr<Manager> Create(GpuPreference gpu_preference);
43+
// Keeps the GL programs that ANGLE compiles in |program_cache|, if there is
44+
// one, so that later launches do not compile them again.
45+
static std::unique_ptr<Manager> Create(
46+
GpuPreference gpu_preference,
47+
std::unique_ptr<ProgramCache> program_cache = nullptr);
4348

4449
virtual ~Manager();
4550

@@ -90,7 +95,8 @@ class Manager {
9095
protected:
9196
// Creates a new surface manager retaining reference to the passed-in target
9297
// for the lifetime of the manager.
93-
explicit Manager(GpuPreference gpu_preference);
98+
explicit Manager(GpuPreference gpu_preference,
99+
std::unique_ptr<ProgramCache> program_cache = nullptr);
94100

95101
private:
96102
// Number of active instances of Manager
@@ -101,7 +107,8 @@ class Manager {
101107
DXGI_GPU_PREFERENCE preference);
102108

103109
// Initialize the EGL display.
104-
bool InitializeDisplay(GpuPreference gpu_preference);
110+
bool InitializeDisplay(GpuPreference gpu_preference,
111+
std::unique_ptr<ProgramCache> program_cache);
105112

106113
// Initialize the EGL configs.
107114
bool InitializeConfig();

0 commit comments

Comments
 (0)