Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 45b95f2

Browse files
authored
[Windows] Move to FlutterCompositor for rendering (#48849)
This migrates the Windows embedder to `FlutterCompositor` so that the engine renders off-screen to a framebuffer instead of directly onto the window's surface. This will allow us to support platform views and multiple views on Windows. <details> <summary>Tests...</summary> * Verify OpenGL compositor's raster time isn't regressed and memory increase is reasonable * Software compositor's raster time and memory isn't regressed Test device configurations * [x] Windows 11 (hardware acceleration enabled/disabled) * [x] Windows Arm64 (hardware acceleration enabled/disabled) * [x] Windows 7 (hardware acceleration enabled/disabled, DWM enabled/disabled) </details> Addresses flutter/flutter#128904 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 9f7004e commit 45b95f2

16 files changed

+753
-29
lines changed

ci/licenses_golden/excluded_files

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@
366366
../../../flutter/shell/platform/windows/client_wrapper/flutter_view_unittests.cc
367367
../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
368368
../../../flutter/shell/platform/windows/client_wrapper/testing
369+
../../../flutter/shell/platform/windows/compositor_opengl_unittests.cc
370+
../../../flutter/shell/platform/windows/compositor_software_unittests.cc
369371
../../../flutter/shell/platform/windows/cursor_handler_unittests.cc
370372
../../../flutter/shell/platform/windows/direct_manipulation_unittests.cc
371373
../../../flutter/shell/platform/windows/dpi_utils_unittests.cc

ci/licenses_golden/licenses_flutter

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7113,6 +7113,11 @@ ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/f
71137113
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h + ../../../flutter/LICENSE
71147114
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h + ../../../flutter/LICENSE
71157115
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h + ../../../flutter/LICENSE
7116+
ORIGIN: ../../../flutter/shell/platform/windows/compositor.h + ../../../flutter/LICENSE
7117+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_opengl.cc + ../../../flutter/LICENSE
7118+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_opengl.h + ../../../flutter/LICENSE
7119+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_software.cc + ../../../flutter/LICENSE
7120+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_software.h + ../../../flutter/LICENSE
71167121
ORIGIN: ../../../flutter/shell/platform/windows/cursor_handler.cc + ../../../flutter/LICENSE
71177122
ORIGIN: ../../../flutter/shell/platform/windows/cursor_handler.h + ../../../flutter/LICENSE
71187123
ORIGIN: ../../../flutter/shell/platform/windows/direct_manipulation.cc + ../../../flutter/LICENSE
@@ -9942,6 +9947,11 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flu
99429947
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h
99439948
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h
99449949
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h
9950+
FILE: ../../../flutter/shell/platform/windows/compositor.h
9951+
FILE: ../../../flutter/shell/platform/windows/compositor_opengl.cc
9952+
FILE: ../../../flutter/shell/platform/windows/compositor_opengl.h
9953+
FILE: ../../../flutter/shell/platform/windows/compositor_software.cc
9954+
FILE: ../../../flutter/shell/platform/windows/compositor_software.h
99459955
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
99469956
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
99479957
FILE: ../../../flutter/shell/platform/windows/direct_manipulation.cc

impeller/renderer/backend/gles/description_gles.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ std::string DescriptionGLES::GetString() const {
156156
return stream.str();
157157
}
158158

159+
Version DescriptionGLES::GetGlVersion() const {
160+
return gl_version_;
161+
}
162+
159163
bool DescriptionGLES::IsES() const {
160164
return is_es_;
161165
}

impeller/renderer/backend/gles/description_gles.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class DescriptionGLES {
2727

2828
std::string GetString() const;
2929

30+
Version GetGlVersion() const;
31+
3032
bool HasExtension(const std::string& ext) const;
3133

3234
/// @brief Returns whether GLES includes the debug extension.

shell/platform/linux/fl_backing_store_provider.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ uint32_t fl_backing_store_provider_get_gl_format(FlBackingStoreProvider* self) {
8282
// In Linux kN32_SkColorType is assumed to be kBGRA_8888_SkColorType.
8383
// So we must choose a valid gl format to be compatible with surface format
8484
// BGRA8.
85-
// Following logics are copied from Skia GrGLCaps.cpp.
85+
// Following logics are copied from Skia GrGLCaps.cpp:
86+
// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116
8687

8788
if (epoxy_is_desktop_gl()) {
8889
// For OpenGL.

shell/platform/windows/BUILD.gn

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ source_set("flutter_windows_source") {
4242
"accessibility_bridge_windows.h",
4343
"angle_surface_manager.cc",
4444
"angle_surface_manager.h",
45+
"compositor.h",
46+
"compositor_opengl.cc",
47+
"compositor_opengl.h",
48+
"compositor_software.cc",
49+
"compositor_software.h",
4550
"cursor_handler.cc",
4651
"cursor_handler.h",
4752
"direct_manipulation.cc",
@@ -134,6 +139,7 @@ source_set("flutter_windows_source") {
134139
deps = [
135140
":flutter_windows_headers",
136141
"//flutter/fml:fml",
142+
"//flutter/impeller/renderer/backend/gles",
137143
"//flutter/shell/platform/common:common_cpp",
138144
"//flutter/shell/platform/common:common_cpp_input",
139145
"//flutter/shell/platform/common:common_cpp_switches",
@@ -175,6 +181,8 @@ executable("flutter_windows_unittests") {
175181
# Common Windows test sources.
176182
sources = [
177183
"accessibility_bridge_windows_unittests.cc",
184+
"compositor_opengl_unittests.cc",
185+
"compositor_software_unittests.cc",
178186
"cursor_handler_unittests.cc",
179187
"direct_manipulation_unittests.cc",
180188
"dpi_utils_unittests.cc",
@@ -235,6 +243,7 @@ executable("flutter_windows_unittests") {
235243
":flutter_windows_fixtures",
236244
":flutter_windows_headers",
237245
":flutter_windows_source",
246+
"//flutter/impeller/renderer/backend/gles",
238247
"//flutter/shell/platform/common:common_cpp",
239248
"//flutter/shell/platform/common/client_wrapper:client_wrapper",
240249
"//flutter/shell/platform/embedder:embedder_as_internal_library",

shell/platform/windows/compositor.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_
7+
8+
#include "flutter/shell/platform/embedder/embedder.h"
9+
10+
namespace flutter {
11+
12+
// Enables the Flutter engine to render content on Windows.
13+
//
14+
// The engine uses this to:
15+
//
16+
// 1. Create backing stores used for rendering Flutter content
17+
// 2. Composite and present Flutter content and platform views onto a view
18+
//
19+
// Platform views are not yet supported.
20+
class Compositor {
21+
public:
22+
virtual ~Compositor() = default;
23+
24+
// Create a backing store used for rendering Flutter content.
25+
//
26+
// The backing store's configuration is stored in |backing_store_out|.
27+
virtual bool CreateBackingStore(const FlutterBackingStoreConfig& config,
28+
FlutterBackingStore* backing_store_out) = 0;
29+
30+
// Destroys a backing store and releases its resources.
31+
virtual bool CollectBackingStore(const FlutterBackingStore* store) = 0;
32+
33+
// Present Flutter content and platform views onto the view.
34+
virtual bool Present(const FlutterLayer** layers, size_t layers_count) = 0;
35+
};
36+
37+
} // namespace flutter
38+
39+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/windows/compositor_opengl.h"
6+
7+
#include "GLES3/gl3.h"
8+
#include "flutter/shell/platform/windows/flutter_windows_view.h"
9+
10+
namespace flutter {
11+
12+
namespace {
13+
14+
// The metadata for an OpenGL framebuffer backing store.
15+
struct FramebufferBackingStore {
16+
uint32_t framebuffer_id;
17+
uint32_t texture_id;
18+
};
19+
20+
// Based off Skia's logic:
21+
// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116
22+
int GetSupportedTextureFormat(const impeller::DescriptionGLES* description) {
23+
if (description->HasExtension("GL_EXT_texture_format_BGRA8888")) {
24+
return GL_BGRA8_EXT;
25+
} else if (description->HasExtension("GL_APPLE_texture_format_BGRA8888") &&
26+
description->GetGlVersion().IsAtLeast(impeller::Version(3, 0))) {
27+
return GL_BGRA8_EXT;
28+
} else {
29+
return GL_RGBA8;
30+
}
31+
}
32+
33+
} // namespace
34+
35+
CompositorOpenGL::CompositorOpenGL(FlutterWindowsEngine* engine,
36+
impeller::ProcTableGLES::Resolver resolver)
37+
: engine_(engine), resolver_(resolver) {}
38+
39+
bool CompositorOpenGL::CreateBackingStore(
40+
const FlutterBackingStoreConfig& config,
41+
FlutterBackingStore* result) {
42+
if (!is_initialized_ && !Initialize()) {
43+
return false;
44+
}
45+
46+
auto store = std::make_unique<FramebufferBackingStore>();
47+
48+
gl_->GenTextures(1, &store->texture_id);
49+
gl_->GenFramebuffers(1, &store->framebuffer_id);
50+
51+
gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id);
52+
53+
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
54+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
55+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
56+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
57+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
58+
gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, config.size.width,
59+
config.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
60+
gl_->BindTexture(GL_TEXTURE_2D, 0);
61+
62+
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
63+
GL_TEXTURE_2D, store->texture_id, 0);
64+
65+
result->type = kFlutterBackingStoreTypeOpenGL;
66+
result->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
67+
result->open_gl.framebuffer.name = store->framebuffer_id;
68+
result->open_gl.framebuffer.target = format_;
69+
result->open_gl.framebuffer.user_data = store.release();
70+
result->open_gl.framebuffer.destruction_callback = [](void* user_data) {
71+
// Backing store destroyed in `CompositorOpenGL::CollectBackingStore`, set
72+
// on FlutterCompositor.collect_backing_store_callback during engine start.
73+
};
74+
return true;
75+
}
76+
77+
bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) {
78+
FML_DCHECK(is_initialized_);
79+
FML_DCHECK(store->type == kFlutterBackingStoreTypeOpenGL);
80+
FML_DCHECK(store->open_gl.type == kFlutterOpenGLTargetTypeFramebuffer);
81+
82+
auto user_data = static_cast<FramebufferBackingStore*>(
83+
store->open_gl.framebuffer.user_data);
84+
85+
gl_->DeleteFramebuffers(1, &user_data->framebuffer_id);
86+
gl_->DeleteTextures(1, &user_data->texture_id);
87+
88+
delete user_data;
89+
return true;
90+
}
91+
92+
bool CompositorOpenGL::Present(const FlutterLayer** layers,
93+
size_t layers_count) {
94+
// TODO: Support compositing layers and platform views.
95+
// See: https://github.com/flutter/flutter/issues/31713
96+
FML_DCHECK(is_initialized_);
97+
FML_DCHECK(layers_count == 1);
98+
FML_DCHECK(layers[0]->type == kFlutterLayerContentTypeBackingStore);
99+
FML_DCHECK(layers[0]->backing_store->type == kFlutterBackingStoreTypeOpenGL);
100+
FML_DCHECK(layers[0]->backing_store->open_gl.type ==
101+
kFlutterOpenGLTargetTypeFramebuffer);
102+
103+
if (!engine_->view()) {
104+
return false;
105+
}
106+
107+
auto width = layers[0]->size.width;
108+
auto height = layers[0]->size.height;
109+
110+
// Acquiring the view's framebuffer ID resizes its surface if necessary.
111+
auto destination_id = engine_->view()->GetFrameBufferId(width, height);
112+
auto source_id = layers[0]->backing_store->open_gl.framebuffer.name;
113+
114+
if (!engine_->surface_manager()->MakeCurrent()) {
115+
return false;
116+
}
117+
118+
gl_->BindFramebuffer(GL_READ_FRAMEBUFFER, source_id);
119+
gl_->BindFramebuffer(GL_DRAW_FRAMEBUFFER, destination_id);
120+
121+
gl_->BlitFramebuffer(0, // srcX0
122+
0, // srcY0
123+
width, // srcX1
124+
height, // srcY1
125+
0, // dstX0
126+
0, // dstY0
127+
width, // dstX1
128+
height, // dstY1
129+
GL_COLOR_BUFFER_BIT, // mask
130+
GL_NEAREST // filter
131+
);
132+
133+
return engine_->view()->SwapBuffers();
134+
}
135+
136+
bool CompositorOpenGL::Initialize() {
137+
FML_DCHECK(!is_initialized_);
138+
139+
if (!engine_->surface_manager()->MakeCurrent()) {
140+
return false;
141+
}
142+
143+
gl_ = std::make_unique<impeller::ProcTableGLES>(resolver_);
144+
if (!gl_->IsValid()) {
145+
gl_.reset();
146+
return false;
147+
}
148+
149+
format_ = GetSupportedTextureFormat(gl_->GetDescription());
150+
is_initialized_ = true;
151+
return true;
152+
}
153+
154+
} // namespace flutter
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_OPENGL_H_
6+
#define FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_OPENGL_H_
7+
8+
#include <memory>
9+
10+
#include "flutter/impeller/renderer/backend/gles/proc_table_gles.h"
11+
#include "flutter/shell/platform/embedder/embedder.h"
12+
#include "flutter/shell/platform/windows/compositor.h"
13+
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
14+
15+
namespace flutter {
16+
17+
// Enables the Flutter engine to render content on Windows using OpenGL.
18+
class CompositorOpenGL : public Compositor {
19+
public:
20+
CompositorOpenGL(FlutterWindowsEngine* engine,
21+
impeller::ProcTableGLES::Resolver resolver);
22+
23+
/// |Compositor|
24+
bool CreateBackingStore(const FlutterBackingStoreConfig& config,
25+
FlutterBackingStore* result) override;
26+
27+
/// |Compositor|
28+
bool CollectBackingStore(const FlutterBackingStore* store) override;
29+
30+
/// |Compositor|
31+
bool Present(const FlutterLayer** layers, size_t layers_count) override;
32+
33+
private:
34+
// The Flutter engine that manages the views to render.
35+
FlutterWindowsEngine* engine_;
36+
37+
private:
38+
// The compositor initializes itself lazily once |CreateBackingStore| is
39+
// called. True if initialization completed successfully.
40+
bool is_initialized_ = false;
41+
42+
// Function used to resolve GLES functions.
43+
impeller::ProcTableGLES::Resolver resolver_ = nullptr;
44+
45+
// Table of resolved GLES functions. Null until the compositor is initialized.
46+
std::unique_ptr<impeller::ProcTableGLES> gl_ = nullptr;
47+
48+
// The OpenGL texture target format for backing stores. Invalid value until
49+
// the compositor is initialized.
50+
uint32_t format_ = 0;
51+
52+
// Initialize the compositor. This must run on the raster thread.
53+
bool Initialize();
54+
};
55+
56+
} // namespace flutter
57+
58+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_OPENGL_H_

0 commit comments

Comments
 (0)