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

Commit 00d7d23

Browse files
authored
Reland "[Windows] Move to FlutterCompositor for rendering" (#49262)
## Reland #48849 was reverted as it incorrectly expected to receive always 1 layer. However, the engine will present 0 layers on an ["empty" app](https://github.com/flutter/flutter/blob/6981fe6fd3aacb95bfc4a6c427ee5d493f43c5dc/dev/integration_tests/ui/lib/empty.dart#L8-L19). This pull request is split into two commits: 1. df604a1 is the original pull request, unchanged 2. c30b369 adds the ability to "clear" the view if the engine presents 0 layers ## Original pull request description 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. Addresses flutter/flutter#128904 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 2e55306 commit 00d7d23

22 files changed

+932
-44
lines changed

ci/licenses_golden/excluded_files

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@
369369
../../../flutter/shell/platform/windows/client_wrapper/flutter_view_unittests.cc
370370
../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
371371
../../../flutter/shell/platform/windows/client_wrapper/testing
372+
../../../flutter/shell/platform/windows/compositor_opengl_unittests.cc
373+
../../../flutter/shell/platform/windows/compositor_software_unittests.cc
372374
../../../flutter/shell/platform/windows/cursor_handler_unittests.cc
373375
../../../flutter/shell/platform/windows/direct_manipulation_unittests.cc
374376
../../../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
@@ -7201,6 +7201,11 @@ ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/f
72017201
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h + ../../../flutter/LICENSE
72027202
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h + ../../../flutter/LICENSE
72037203
ORIGIN: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h + ../../../flutter/LICENSE
7204+
ORIGIN: ../../../flutter/shell/platform/windows/compositor.h + ../../../flutter/LICENSE
7205+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_opengl.cc + ../../../flutter/LICENSE
7206+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_opengl.h + ../../../flutter/LICENSE
7207+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_software.cc + ../../../flutter/LICENSE
7208+
ORIGIN: ../../../flutter/shell/platform/windows/compositor_software.h + ../../../flutter/LICENSE
72047209
ORIGIN: ../../../flutter/shell/platform/windows/cursor_handler.cc + ../../../flutter/LICENSE
72057210
ORIGIN: ../../../flutter/shell/platform/windows/cursor_handler.h + ../../../flutter/LICENSE
72067211
ORIGIN: ../../../flutter/shell/platform/windows/direct_manipulation.cc + ../../../flutter/LICENSE
@@ -10039,6 +10044,11 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flu
1003910044
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h
1004010045
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h
1004110046
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h
10047+
FILE: ../../../flutter/shell/platform/windows/compositor.h
10048+
FILE: ../../../flutter/shell/platform/windows/compositor_opengl.cc
10049+
FILE: ../../../flutter/shell/platform/windows/compositor_opengl.h
10050+
FILE: ../../../flutter/shell/platform/windows/compositor_software.cc
10051+
FILE: ../../../flutter/shell/platform/windows/compositor_software.h
1004210052
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
1004310053
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
1004410054
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+
// Creates 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: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
constexpr uint32_t kWindowFrameBufferId = 0;
15+
16+
// The metadata for an OpenGL framebuffer backing store.
17+
struct FramebufferBackingStore {
18+
uint32_t framebuffer_id;
19+
uint32_t texture_id;
20+
};
21+
22+
// Based off Skia's logic:
23+
// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116
24+
int GetSupportedTextureFormat(const impeller::DescriptionGLES* description) {
25+
if (description->HasExtension("GL_EXT_texture_format_BGRA8888")) {
26+
return GL_BGRA8_EXT;
27+
} else if (description->HasExtension("GL_APPLE_texture_format_BGRA8888") &&
28+
description->GetGlVersion().IsAtLeast(impeller::Version(3, 0))) {
29+
return GL_BGRA8_EXT;
30+
} else {
31+
return GL_RGBA8;
32+
}
33+
}
34+
35+
} // namespace
36+
37+
CompositorOpenGL::CompositorOpenGL(FlutterWindowsEngine* engine,
38+
impeller::ProcTableGLES::Resolver resolver)
39+
: engine_(engine), resolver_(resolver) {}
40+
41+
bool CompositorOpenGL::CreateBackingStore(
42+
const FlutterBackingStoreConfig& config,
43+
FlutterBackingStore* result) {
44+
if (!is_initialized_ && !Initialize()) {
45+
return false;
46+
}
47+
48+
auto store = std::make_unique<FramebufferBackingStore>();
49+
50+
gl_->GenTextures(1, &store->texture_id);
51+
gl_->GenFramebuffers(1, &store->framebuffer_id);
52+
53+
gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id);
54+
55+
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id);
56+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
57+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
58+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
59+
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
60+
gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, config.size.width,
61+
config.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
62+
gl_->BindTexture(GL_TEXTURE_2D, 0);
63+
64+
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT,
65+
GL_TEXTURE_2D, store->texture_id, 0);
66+
67+
result->type = kFlutterBackingStoreTypeOpenGL;
68+
result->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer;
69+
result->open_gl.framebuffer.name = store->framebuffer_id;
70+
result->open_gl.framebuffer.target = format_;
71+
result->open_gl.framebuffer.user_data = store.release();
72+
result->open_gl.framebuffer.destruction_callback = [](void* user_data) {
73+
// Backing store destroyed in `CompositorOpenGL::CollectBackingStore`, set
74+
// on FlutterCompositor.collect_backing_store_callback during engine start.
75+
};
76+
return true;
77+
}
78+
79+
bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) {
80+
FML_DCHECK(is_initialized_);
81+
FML_DCHECK(store->type == kFlutterBackingStoreTypeOpenGL);
82+
FML_DCHECK(store->open_gl.type == kFlutterOpenGLTargetTypeFramebuffer);
83+
84+
auto user_data = static_cast<FramebufferBackingStore*>(
85+
store->open_gl.framebuffer.user_data);
86+
87+
gl_->DeleteFramebuffers(1, &user_data->framebuffer_id);
88+
gl_->DeleteTextures(1, &user_data->texture_id);
89+
90+
delete user_data;
91+
return true;
92+
}
93+
94+
bool CompositorOpenGL::Present(const FlutterLayer** layers,
95+
size_t layers_count) {
96+
if (!engine_->view()) {
97+
return false;
98+
}
99+
100+
// Clear the view if there are no layers to present.
101+
if (layers_count == 0) {
102+
// Normally the compositor is initialized when the first backing store is
103+
// created. However, on an empty frame no backing stores are created and
104+
// the present needs to initialize the compositor.
105+
if (!is_initialized_ && !Initialize()) {
106+
return false;
107+
}
108+
109+
return ClearSurface();
110+
}
111+
112+
// TODO: Support compositing layers and platform views.
113+
// See: https://github.com/flutter/flutter/issues/31713
114+
FML_DCHECK(is_initialized_);
115+
FML_DCHECK(layers_count == 1);
116+
FML_DCHECK(layers[0]->offset.x == 0 && layers[0]->offset.y == 0);
117+
FML_DCHECK(layers[0]->type == kFlutterLayerContentTypeBackingStore);
118+
FML_DCHECK(layers[0]->backing_store->type == kFlutterBackingStoreTypeOpenGL);
119+
FML_DCHECK(layers[0]->backing_store->open_gl.type ==
120+
kFlutterOpenGLTargetTypeFramebuffer);
121+
122+
auto width = layers[0]->size.width;
123+
auto height = layers[0]->size.height;
124+
125+
// Check if this frame can be presented. This resizes the surface if a resize
126+
// is pending and |width| and |height| match the target size.
127+
if (!engine_->view()->OnFrameGenerated(width, height)) {
128+
return false;
129+
}
130+
131+
if (!engine_->surface_manager()->MakeCurrent()) {
132+
return false;
133+
}
134+
135+
auto source_id = layers[0]->backing_store->open_gl.framebuffer.name;
136+
gl_->BindFramebuffer(GL_READ_FRAMEBUFFER, source_id);
137+
gl_->BindFramebuffer(GL_DRAW_FRAMEBUFFER, kWindowFrameBufferId);
138+
139+
gl_->BlitFramebuffer(0, // srcX0
140+
0, // srcY0
141+
width, // srcX1
142+
height, // srcY1
143+
0, // dstX0
144+
0, // dstY0
145+
width, // dstX1
146+
height, // dstY1
147+
GL_COLOR_BUFFER_BIT, // mask
148+
GL_NEAREST // filter
149+
);
150+
151+
return engine_->view()->SwapBuffers();
152+
}
153+
154+
bool CompositorOpenGL::Initialize() {
155+
FML_DCHECK(!is_initialized_);
156+
157+
if (!engine_->surface_manager()->MakeCurrent()) {
158+
return false;
159+
}
160+
161+
gl_ = std::make_unique<impeller::ProcTableGLES>(resolver_);
162+
if (!gl_->IsValid()) {
163+
gl_.reset();
164+
return false;
165+
}
166+
167+
format_ = GetSupportedTextureFormat(gl_->GetDescription());
168+
is_initialized_ = true;
169+
return true;
170+
}
171+
172+
bool CompositorOpenGL::ClearSurface() {
173+
FML_DCHECK(is_initialized_);
174+
175+
// Resize the surface if needed.
176+
engine_->view()->OnEmptyFrameGenerated();
177+
178+
if (!engine_->surface_manager()->MakeCurrent()) {
179+
return false;
180+
}
181+
182+
gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
183+
gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
184+
185+
return engine_->view()->SwapBuffers();
186+
}
187+
188+
} // namespace flutter
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// Clear the view's surface and removes any previously presented layers.
56+
bool ClearSurface();
57+
};
58+
59+
} // namespace flutter
60+
61+
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_OPENGL_H_

0 commit comments

Comments
 (0)