Skip to content

Commit 706edcd

Browse files
Add FlViewRendererSubsurface (#191389)
Add a Wayland subsurface based view renderer that blits engine frames directly to a subsurface using an EGL context that shares resources with the engine. The EGL context and window surface are managed by a new FlSubsurfaceEGL class, and window resizing is synchronized with the subsurface so the two stay in step. Add FlSubsurface, expose the engine's EGL display and context through FlOpenGLManager, wire up the wayland-client and wayland-egl build configs, and use this renderer automatically whenever running on Wayland (falling back to the standard OpenGL renderer otherwise).
1 parent cabb3f6 commit 706edcd

26 files changed

Lines changed: 2131 additions & 42 deletions

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ source_set("flutter_linux_sources") {
103103
"fl_window_state_monitor.h",
104104
]
105105

106-
configs += [ "//flutter/shell/platform/linux/config:gtk" ]
106+
configs += [
107+
"//flutter/shell/platform/linux/config:gtk",
108+
"//flutter/shell/platform/linux/config:wayland_client",
109+
"//flutter/shell/platform/linux/config:wayland_egl",
110+
]
107111

108112
public_configs = [ ":disable_warnings" ]
109113

@@ -162,6 +166,8 @@ source_set("flutter_linux_sources") {
162166
"fl_standard_message_codec.cc",
163167
"fl_standard_method_codec.cc",
164168
"fl_string_codec.cc",
169+
"fl_subsurface.cc",
170+
"fl_subsurface_egl.cc",
165171
"fl_task_runner.cc",
166172
"fl_text_input_channel.cc",
167173
"fl_text_input_handler.cc",
@@ -176,6 +182,8 @@ source_set("flutter_linux_sources") {
176182
"fl_view_renderer.cc",
177183
"fl_view_renderer_opengl.cc",
178184
"fl_view_renderer_software.cc",
185+
"fl_view_renderer_subsurface.cc",
186+
"fl_wayland_display.cc",
179187
"fl_window_monitor.cc",
180188
"fl_window_state_monitor.cc",
181189
]
@@ -267,14 +275,18 @@ executable("flutter_linux_unittests") {
267275
"fl_standard_message_codec_test.cc",
268276
"fl_standard_method_codec_test.cc",
269277
"fl_string_codec_test.cc",
278+
"fl_subsurface_egl_test.cc",
279+
"fl_subsurface_test.cc",
270280
"fl_task_runner_test.cc",
271281
"fl_text_input_handler_test.cc",
272282
"fl_texture_gl_test.cc",
273283
"fl_texture_registrar_test.cc",
274284
"fl_touch_manager_test.cc",
275285
"fl_value_test.cc",
276286
"fl_view_accessible_test.cc",
287+
"fl_view_renderer_subsurface_test.cc",
277288
"fl_view_test.cc",
289+
"fl_wayland_display_test.cc",
278290
"fl_window_state_monitor_test.cc",
279291
"testing/fl_mock_binary_messenger.cc",
280292
"testing/fl_test.cc",
@@ -290,13 +302,18 @@ executable("flutter_linux_unittests") {
290302
"testing/mock_settings.cc",
291303
"testing/mock_signal_handler.cc",
292304
"testing/mock_texture_registrar.cc",
305+
"testing/mock_wayland.cc",
306+
"testing/wayland_test.cc",
307+
"testing/wayland_test.h",
293308
]
294309

295310
public_configs = [ "//flutter:config" ]
296311

297312
configs += [
298313
"//flutter/shell/platform/linux/config:gtk",
299314
"//flutter/shell/platform/linux/config:epoxy",
315+
"//flutter/shell/platform/linux/config:wayland_client",
316+
"//flutter/shell/platform/linux/config:wayland_egl",
300317
]
301318

302319
defines = [

‎engine/src/flutter/shell/platform/linux/config/BUILD.gn‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ _pkg_configs = [
2222
name = "epoxy"
2323
package = "epoxy"
2424
},
25+
{
26+
name = "wayland_client"
27+
package = "wayland-client"
28+
},
29+
{
30+
name = "wayland_egl"
31+
package = "wayland-egl"
32+
},
2533
]
2634

2735
foreach(pkg, _pkg_configs) {

‎engine/src/flutter/shell/platform/linux/fl_compositor_opengl.cc‎

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,6 @@ static void fl_compositor_opengl_class_init(FlCompositorOpenGLClass* klass) {
4545

4646
static void fl_compositor_opengl_init(FlCompositorOpenGL* self) {}
4747

48-
// Checks if the current OpenGL driver is known to have a broken or unsupported
49-
// glBlitFramebuffer implementation.
50-
static gboolean driver_supports_blit() {
51-
const gchar* vendor = reinterpret_cast<const gchar*>(glGetString(GL_VENDOR));
52-
if (vendor == nullptr) {
53-
return TRUE;
54-
}
55-
56-
// Note: List of unsupported vendors due to issue
57-
// https://github.com/flutter/flutter/issues/152099
58-
const char* unsupported_vendors_exact[] = {"Vivante Corporation", "ARM"};
59-
const char* unsupported_vendors_fuzzy[] = {"NVIDIA"};
60-
61-
for (const char* unsupported : unsupported_vendors_fuzzy) {
62-
if (strstr(vendor, unsupported) != nullptr) {
63-
return FALSE;
64-
}
65-
}
66-
for (const char* unsupported : unsupported_vendors_exact) {
67-
if (strcmp(vendor, unsupported) == 0) {
68-
return FALSE;
69-
}
70-
}
71-
return TRUE;
72-
}
73-
74-
// Checks if glBlitFramebuffer can be used. It is a GLES3 / OpenGL 3.0 function
75-
// and may not be present on older drivers, so treat it as optional and fall
76-
// back to compositing with the shader when it is unavailable.
77-
static gboolean can_blit_framebuffer() {
78-
return driver_supports_blit() &&
79-
(epoxy_gl_version() >= 30 ||
80-
epoxy_has_gl_extension("GL_EXT_framebuffer_blit"));
81-
}
82-
8348
FlCompositorOpenGL* fl_compositor_opengl_new(FlOpenGLManager* opengl_manager) {
8449
FlCompositorOpenGL* self = FL_COMPOSITOR_OPENGL(
8550
g_object_new(fl_compositor_opengl_get_type(), nullptr));
@@ -89,7 +54,7 @@ FlCompositorOpenGL* fl_compositor_opengl_new(FlOpenGLManager* opengl_manager) {
8954

9055
// Determine once whether glBlitFramebuffer is available on this driver.
9156
fl_opengl_manager_make_current(opengl_manager);
92-
self->can_blit = can_blit_framebuffer();
57+
self->can_blit = fl_opengl_manager_can_blit(opengl_manager);
9358

9459
return self;
9560
}

‎engine/src/flutter/shell/platform/linux/fl_opengl_manager.cc‎

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

55
#include <epoxy/egl.h>
6+
#include <epoxy/gl.h>
67
#include <gdk/gdkwayland.h>
78
#ifdef GDK_WINDOWING_X11
89
#include <gdk/gdkx.h>
910
#endif
1011

12+
#include <cstring>
13+
1114
#include "flutter/shell/platform/linux/fl_opengl_manager.h"
1215

1316
struct _FlOpenGLManager {
@@ -114,3 +117,48 @@ gboolean fl_opengl_manager_clear_current(FlOpenGLManager* self) {
114117
return eglMakeCurrent(self->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
115118
EGL_NO_CONTEXT) == EGL_TRUE;
116119
}
120+
121+
EGLDisplay fl_opengl_manager_get_display(FlOpenGLManager* self) {
122+
return self->display;
123+
}
124+
125+
EGLContext fl_opengl_manager_get_context(FlOpenGLManager* self) {
126+
return self->render_context;
127+
}
128+
129+
// Checks if the current OpenGL driver is known to have a broken or unsupported
130+
// glBlitFramebuffer implementation.
131+
static gboolean driver_supports_blit() {
132+
const gchar* vendor = reinterpret_cast<const gchar*>(glGetString(GL_VENDOR));
133+
if (vendor == nullptr) {
134+
return TRUE;
135+
}
136+
137+
// Note: List of unsupported vendors due to issue
138+
// https://github.com/flutter/flutter/issues/152099
139+
const char* unsupported_vendors_exact[] = {"Vivante Corporation", "ARM"};
140+
const char* unsupported_vendors_fuzzy[] = {"NVIDIA"};
141+
142+
for (const char* unsupported : unsupported_vendors_fuzzy) {
143+
if (strstr(vendor, unsupported) != nullptr) {
144+
return FALSE;
145+
}
146+
}
147+
for (const char* unsupported : unsupported_vendors_exact) {
148+
if (strcmp(vendor, unsupported) == 0) {
149+
return FALSE;
150+
}
151+
}
152+
return TRUE;
153+
}
154+
155+
gboolean fl_opengl_manager_can_blit(FlOpenGLManager* self) {
156+
g_return_val_if_fail(FL_IS_OPENGL_MANAGER(self), FALSE);
157+
158+
// glBlitFramebuffer is a GLES3 / OpenGL 3.0 function and may not be present
159+
// on older drivers, so treat it as optional. Evaluated for whichever OpenGL
160+
// context is current.
161+
return driver_supports_blit() &&
162+
(epoxy_gl_version() >= 30 ||
163+
epoxy_has_gl_extension("GL_EXT_framebuffer_blit"));
164+
}

‎engine/src/flutter/shell/platform/linux/fl_opengl_manager.h‎

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

8+
// Avoid pulling in X11 headers (via <epoxy/egl.h>), which define `None` and
9+
// `Bool` as macros that collide with googletest in transitively-including test
10+
// translation units. EGLDisplay/EGLContext are platform-independent handles.
11+
#define EGL_NO_X11
12+
#include <epoxy/egl.h>
813
#include <glib-object.h>
914

1015
G_BEGIN_DECLS
@@ -64,6 +69,43 @@ gboolean fl_opengl_manager_make_platform_current(FlOpenGLManager* manager);
6469
*/
6570
gboolean fl_opengl_manager_clear_current(FlOpenGLManager* manager);
6671

72+
/**
73+
* fl_opengl_manager_get_display:
74+
* @manager: an #FlOpenGLManager.
75+
*
76+
* Gets the EGL display the engine renders to. This can be used to create
77+
* additional EGL contexts that share resources with the engine.
78+
*
79+
* Returns: an %EGLDisplay.
80+
*/
81+
EGLDisplay fl_opengl_manager_get_display(FlOpenGLManager* manager);
82+
83+
/**
84+
* fl_opengl_manager_get_context:
85+
* @manager: an #FlOpenGLManager.
86+
*
87+
* Gets the EGL context the engine renders with. This can be used as a share
88+
* context when creating additional EGL contexts so they can access textures
89+
* rendered by the engine directly (without using EGLImage).
90+
*
91+
* Returns: an %EGLContext.
92+
*/
93+
EGLContext fl_opengl_manager_get_context(FlOpenGLManager* manager);
94+
95+
/**
96+
* fl_opengl_manager_can_blit:
97+
* @manager: an #FlOpenGLManager.
98+
*
99+
* Checks whether glBlitFramebuffer can be used on the currently current OpenGL
100+
* context. glBlitFramebuffer is a GLES3 / OpenGL 3.0 function that may be
101+
* missing or broken on some drivers, so callers should fall back to shader
102+
* compositing when this returns %FALSE. Requires an OpenGL context to be
103+
* current.
104+
*
105+
* Returns: %TRUE if glBlitFramebuffer is available.
106+
*/
107+
gboolean fl_opengl_manager_can_blit(FlOpenGLManager* manager);
108+
67109
G_END_DECLS
68110

69111
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_OPENGL_MANAGER_H_
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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/linux/fl_subsurface.h"
6+
7+
#include <wayland-client.h>
8+
9+
struct _FlSubsurface {
10+
GObject parent_instance;
11+
12+
// Surface backing the subsurface and the subsurface itself.
13+
struct wl_surface* surface;
14+
struct wl_subsurface* subsurface;
15+
};
16+
17+
G_DEFINE_TYPE(FlSubsurface, fl_subsurface, G_TYPE_OBJECT)
18+
19+
static void fl_subsurface_dispose(GObject* object) {
20+
FlSubsurface* self = FL_SUBSURFACE(object);
21+
22+
g_clear_pointer(&self->subsurface, wl_subsurface_destroy);
23+
g_clear_pointer(&self->surface, wl_surface_destroy);
24+
25+
G_OBJECT_CLASS(fl_subsurface_parent_class)->dispose(object);
26+
}
27+
28+
static void fl_subsurface_class_init(FlSubsurfaceClass* klass) {
29+
G_OBJECT_CLASS(klass)->dispose = fl_subsurface_dispose;
30+
}
31+
32+
static void fl_subsurface_init(FlSubsurface* self) {}
33+
34+
FlSubsurface* fl_subsurface_new(struct wl_compositor* compositor,
35+
struct wl_subcompositor* subcompositor,
36+
struct wl_surface* parent_surface) {
37+
FlSubsurface* self =
38+
FL_SUBSURFACE(g_object_new(fl_subsurface_get_type(), nullptr));
39+
40+
self->surface = wl_compositor_create_surface(compositor);
41+
self->subsurface = wl_subcompositor_get_subsurface(
42+
subcompositor, self->surface, parent_surface);
43+
wl_subsurface_set_sync(self->subsurface);
44+
45+
// Give the subsurface an empty input region so pointer, touch and keyboard
46+
// events pass through to the parent (GTK) surface, which handles all input
47+
// for the view.
48+
struct wl_region* input_region = wl_compositor_create_region(compositor);
49+
wl_surface_set_input_region(self->surface, input_region);
50+
wl_region_destroy(input_region);
51+
52+
return self;
53+
}
54+
55+
struct wl_surface* fl_subsurface_get_surface(FlSubsurface* self) {
56+
g_return_val_if_fail(FL_IS_SUBSURFACE(self), nullptr);
57+
return self->surface;
58+
}
59+
60+
void fl_subsurface_set_position(FlSubsurface* self, gint x, gint y) {
61+
g_return_if_fail(FL_IS_SUBSURFACE(self));
62+
wl_subsurface_set_position(self->subsurface, x, y);
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_LINUX_FL_SUBSURFACE_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_SUBSURFACE_H_
7+
8+
#include <gtk/gtk.h>
9+
10+
struct wl_compositor;
11+
struct wl_subcompositor;
12+
struct wl_surface;
13+
14+
G_BEGIN_DECLS
15+
16+
G_DECLARE_FINAL_TYPE(FlSubsurface, fl_subsurface, FL, SUBSURFACE, GObject)
17+
18+
/**
19+
* FlSubsurface:
20+
*
21+
* #FlSubsurface manages a Wayland subsurface attached to another Wayland
22+
* surface. It is used to render Flutter content directly to a Wayland surface,
23+
* independently of the GTK widget hierarchy.
24+
*/
25+
26+
/**
27+
* fl_subsurface_new:
28+
* @compositor: the Wayland compositor to create the surface with.
29+
* @subcompositor: the Wayland subcompositor to create the subsurface with.
30+
* @parent_surface: the Wayland surface to attach the subsurface to.
31+
*
32+
* Creates a new Wayland subsurface on @parent_surface.
33+
*
34+
* Returns: a new #FlSubsurface.
35+
*/
36+
FlSubsurface* fl_subsurface_new(struct wl_compositor* compositor,
37+
struct wl_subcompositor* subcompositor,
38+
struct wl_surface* parent_surface);
39+
40+
/**
41+
* fl_subsurface_get_surface:
42+
* @subsurface: an #FlSubsurface.
43+
*
44+
* Gets the Wayland surface backing this subsurface.
45+
*
46+
* Returns: a `struct wl_surface`.
47+
*/
48+
struct wl_surface* fl_subsurface_get_surface(FlSubsurface* subsurface);
49+
50+
/**
51+
* fl_subsurface_set_position:
52+
* @subsurface: an #FlSubsurface.
53+
* @x: x coordinate in the parent surface.
54+
* @y: y coordinate in the parent surface.
55+
*
56+
* Moves the subsurface to the given position relative to the parent surface.
57+
*/
58+
void fl_subsurface_set_position(FlSubsurface* subsurface, gint x, gint y);
59+
60+
G_END_DECLS
61+
62+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_SUBSURFACE_H_

0 commit comments

Comments
 (0)