Flutter Linux Embedder
fl_subsurface_egl.cc
Go to the documentation of this file.
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 
6 
7 #include <epoxy/egl.h>
8 #include <epoxy/gl.h>
9 #include <wayland-client.h>
10 #include <wayland-egl.h>
11 
13 
15  GObject parent_instance;
16 
17  // OpenGL manager providing the shared EGL display and context. The display is
18  // owned by the engine and must not be terminated here.
19  FlOpenGLManager* opengl_manager;
20 
21  // Subsurface frames are presented to. A reference is held so its Wayland
22  // surface outlives the EGL surface created from it.
23  FlSubsurface* subsurface;
24 
25  // Native Wayland window backing the EGL surface.
26  struct wl_egl_window* egl_window;
27 
28  // EGL context used to blit engine frames. Shares resources with the engine's
29  // render context.
30  EGLContext egl_context;
31 
32  // EGL surface that draws onto egl_window.
33  EGLSurface egl_surface;
34 
35  // TRUE if glBlitFramebuffer can be used to copy the engine frame to the
36  // window surface. When FALSE the frame is drawn with [shader] instead.
37  gboolean can_blit;
38 
39  // Framebuffer used to read the engine's frame texture. Created lazily on the
40  // first present and reused for subsequent frames. Only used when [can_blit].
42 
43  // Shader used to draw the engine frame when glBlitFramebuffer is unavailable.
44  // NULL when [can_blit].
45  FlCompositorOpenGLShader* shader;
46 
47  // The buffer scale set on the subsurface. Buffer sizes have to be an integer
48  // multiple of this.
49  gint scale;
50 
51  // The size egl_window has been resized to. The buffer for the next frame is
52  // acquired when the current one is swapped, so this is the size that buffer
53  // will be, not the size of the buffer being drawn into now.
54  size_t window_width;
55  size_t window_height;
56 
57  // The size of the buffer the next frame is drawn into. eglQuerySurface()
58  // reports the size of the buffer that was last swapped rather than this one,
59  // so the size is tracked here instead.
60  size_t buffer_width;
61  size_t buffer_height;
62 };
63 
64 G_DEFINE_TYPE(FlSubsurfaceEGL, fl_subsurface_egl, G_TYPE_OBJECT)
65 
66 // Rounds a size in device pixels up to a whole number of logical pixels.
67 // Wayland requires buffer sizes to be an integer multiple of the buffer scale,
68 // and a view that hasn't been allocated yet can produce a frame that isn't,
69 // e.g. a single pixel frame on a display with a scale of two.
70 static size_t round_to_scale(size_t size, gint scale) {
71  return ((size + scale - 1) / scale) * scale;
72 }
73 
74 // Gets the EGL display the engine renders to. The subsurface shares this
75 // display so its context can access the engine's frame texture directly.
76 static EGLDisplay get_display(FlSubsurfaceEGL* self) {
77  return fl_opengl_manager_get_display(self->opengl_manager);
78 }
79 
80 // Sets up the EGL context and window surface for the subsurface. If this
81 // fails a warning is printed and the object is left without a usable context;
82 // subsequent operations will fail with the usual EGL/OpenGL errors.
83 static void setup(FlSubsurfaceEGL* self,
84  size_t width,
85  size_t height,
86  gint scale) {
87  // The native window is created in device pixels below.
88  self->scale = scale;
89  self->window_width = width * scale;
90  self->window_height = height * scale;
91  self->buffer_width = self->window_width;
92  self->buffer_height = self->window_height;
93 
94  // Share the engine's EGL display and render context so the engine's frame
95  // texture can be accessed directly, without using EGLImage.
96  EGLDisplay egl_display = get_display(self);
97  if (egl_display == EGL_NO_DISPLAY) {
98  g_warning("Failed to get EGL display for subsurface");
99  return;
100  }
101 
102  static const EGLint config_attributes[] = {EGL_SURFACE_TYPE,
103  EGL_WINDOW_BIT,
104  EGL_RENDERABLE_TYPE,
105  EGL_OPENGL_ES2_BIT,
106  EGL_RED_SIZE,
107  8,
108  EGL_GREEN_SIZE,
109  8,
110  EGL_BLUE_SIZE,
111  8,
112  EGL_ALPHA_SIZE,
113  8,
114  EGL_NONE};
115  EGLConfig egl_config;
116  EGLint num_config;
117  if (!eglChooseConfig(egl_display, config_attributes, &egl_config, 1,
118  &num_config) ||
119  num_config == 0) {
120  g_warning("Failed to choose EGL config for subsurface");
121  return;
122  }
123 
124  eglBindAPI(EGL_OPENGL_ES_API);
125 
126  static const EGLint context_attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2,
127  EGL_NONE};
128  EGLContext share_context =
129  fl_opengl_manager_get_context(self->opengl_manager);
130  self->egl_context = eglCreateContext(egl_display, egl_config, share_context,
131  context_attributes);
132  if (self->egl_context == EGL_NO_CONTEXT) {
133  g_warning("Failed to create EGL context for subsurface");
134  return;
135  }
136 
137  struct wl_surface* surface = fl_subsurface_get_surface(self->subsurface);
138  self->egl_window =
139  wl_egl_window_create(surface, width * scale, height * scale);
140  if (self->egl_window == nullptr) {
141  g_warning("Failed to create wl_egl_window for subsurface");
142  return;
143  }
144 
145  self->egl_surface = eglCreateWindowSurface(
146  egl_display, egl_config,
147  reinterpret_cast<EGLNativeWindowType>(self->egl_window), nullptr);
148  if (self->egl_surface == EGL_NO_SURFACE) {
149  g_warning("Failed to create EGL window surface for subsurface");
150  return;
151  }
152 
153  wl_surface_set_buffer_scale(surface, scale);
154 
155  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
156  self->egl_context);
157  eglSwapInterval(egl_display, 0);
158 
159  // Determine whether this context can use glBlitFramebuffer to copy the engine
160  // frame to the window surface; if not, fall back to compositing the frame
161  // with a shader, matching the OpenGL renderer.
162  self->can_blit = fl_opengl_manager_can_blit(self->opengl_manager);
163  eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
164 
165  if (!self->can_blit) {
166  // The shader is created in the engine's context, which the subsurface
167  // context shares, so its program and buffers are usable when the subsurface
168  // context is current.
169  self->shader = fl_compositor_opengl_shader_new(self->opengl_manager);
170  fl_opengl_manager_clear_current(self->opengl_manager);
171  }
172 }
173 
174 static void fl_subsurface_egl_dispose(GObject* object) {
175  FlSubsurfaceEGL* self = FL_SUBSURFACE_EGL(object);
176 
177  // The EGL display is owned by the engine (same Wayland display), so only the
178  // surface and context created here are destroyed; the display must not be
179  // terminated.
180  if (self->opengl_manager != nullptr) {
181  EGLDisplay egl_display = get_display(self);
182  if (self->egl_context != EGL_NO_CONTEXT) {
183  if (self->read_framebuffer != 0) {
184  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
185  self->egl_context);
186  glDeleteFramebuffers(1, &self->read_framebuffer);
187  self->read_framebuffer = 0;
188  eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
189  EGL_NO_CONTEXT);
190  }
191  }
192  if (self->egl_surface != EGL_NO_SURFACE) {
193  eglDestroySurface(egl_display, self->egl_surface);
194  self->egl_surface = EGL_NO_SURFACE;
195  }
196  if (self->egl_context != EGL_NO_CONTEXT) {
197  eglDestroyContext(egl_display, self->egl_context);
198  self->egl_context = EGL_NO_CONTEXT;
199  }
200  }
201  if (self->egl_window != nullptr) {
202  wl_egl_window_destroy(self->egl_window);
203  self->egl_window = nullptr;
204  }
205  g_clear_object(&self->shader);
206  g_clear_object(&self->subsurface);
207  g_clear_object(&self->opengl_manager);
208 
209  G_OBJECT_CLASS(fl_subsurface_egl_parent_class)->dispose(object);
210 }
211 
212 static void fl_subsurface_egl_class_init(FlSubsurfaceEGLClass* klass) {
213  G_OBJECT_CLASS(klass)->dispose = fl_subsurface_egl_dispose;
214 }
215 
216 static void fl_subsurface_egl_init(FlSubsurfaceEGL* self) {
217  self->egl_context = EGL_NO_CONTEXT;
218  self->egl_surface = EGL_NO_SURFACE;
219 }
220 
221 FlSubsurfaceEGL* fl_subsurface_egl_new(FlOpenGLManager* opengl_manager,
222  FlSubsurface* subsurface,
223  size_t width,
224  size_t height,
225  gint scale) {
226  FlSubsurfaceEGL* self =
227  FL_SUBSURFACE_EGL(g_object_new(fl_subsurface_egl_get_type(), nullptr));
228  self->opengl_manager = FL_OPENGL_MANAGER(g_object_ref(opengl_manager));
229  self->subsurface = FL_SUBSURFACE(g_object_ref(subsurface));
230 
231  setup(self, width, height, scale);
232 
233  return self;
234 }
235 
236 void fl_subsurface_egl_resize(FlSubsurfaceEGL* self,
237  size_t width,
238  size_t height) {
239  g_return_if_fail(FL_IS_SUBSURFACE_EGL(self));
240 
241  if (self->egl_window != nullptr) {
242  width = round_to_scale(width, self->scale);
243  height = round_to_scale(height, self->scale);
244  wl_egl_window_resize(self->egl_window, width, height, 0, 0);
245  self->window_width = width;
246  self->window_height = height;
247  }
248 }
249 
250 void fl_subsurface_egl_present(FlSubsurfaceEGL* self,
251  GLuint texture_id,
252  size_t width,
253  size_t height,
254  FlGLFence* fence) {
255  g_return_if_fail(FL_IS_SUBSURFACE_EGL(self));
256 
257  // Present the composited frame to the subsurface window surface using the
258  // subsurface's own EGL context.
259  EGLDisplay egl_display = get_display(self);
260  eglMakeCurrent(egl_display, self->egl_surface, self->egl_surface,
261  self->egl_context);
262 
263  // Make this context wait for the frame to have finished rendering in the
264  // engine's context before it reads the texture below. This context is the one
265  // that reads the frame, so the waiting can be left to OpenGL and this thread
266  // doesn't have to block.
267  if (fence != nullptr) {
268  fl_gl_fence_wait(fence);
269  }
270 
271  size_t target_width = round_to_scale(width, self->scale);
272  size_t target_height = round_to_scale(height, self->scale);
273 
274  if (self->egl_window != nullptr && (self->window_width != target_width ||
275  self->window_height != target_height)) {
276  wl_egl_window_resize(self->egl_window, target_width, target_height, 0, 0);
277  self->window_width = target_width;
278  self->window_height = target_height;
279  }
280 
281  if (self->egl_window != nullptr && (self->buffer_width != target_width ||
282  self->buffer_height != target_height)) {
283  // The buffer a frame is drawn into is acquired when the previous frame is
284  // swapped, so resizing the window doesn't reach the buffer this frame
285  // would be drawn into. Swap that buffer away unused so this frame is drawn
286  // into one of the size it was rendered at. A window sized to its content
287  // only produces one frame at each size, so leaving this until the next
288  // frame would leave it showing a buffer that was never painted.
289  eglSwapBuffers(egl_display, self->egl_surface);
290  self->buffer_width = self->window_width;
291  self->buffer_height = self->window_height;
292  }
293 
294  // The frame and the buffer are both window sized, so their dimensions fit in
295  // a GLint and the arithmetic below can stay signed.
296  GLint frame_width = static_cast<GLint>(width);
297  GLint frame_height = static_cast<GLint>(height);
298 
299  // OpenGL puts the origin at the bottom left of the buffer but Wayland puts it
300  // at the top left, so the frame has to be written at the top of the buffer
301  // when the buffer is taller, which happens when the frame size was rounded up
302  // to a whole logical pixel above.
303  GLint dst_y0 = MAX(static_cast<GLint>(self->buffer_height) - frame_height, 0);
304 
305  // The subsurface context shares resources with the engine, so the engine's
306  // frame texture can be read directly without using EGLImage.
307  if (self->can_blit) {
308  // Attach the frame texture to a persistent framebuffer and blit it to the
309  // window surface. The framebuffer is created lazily on the first present
310  // and reused for subsequent frames.
311  if (self->read_framebuffer == 0) {
312  glGenFramebuffers(1, &self->read_framebuffer);
313  }
314  glBindFramebuffer(GL_READ_FRAMEBUFFER, self->read_framebuffer);
315  glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
316  GL_TEXTURE_2D, texture_id, 0);
317  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
318  glBlitFramebuffer(0, 0, frame_width, frame_height, 0, dst_y0, frame_width,
319  dst_y0 + frame_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
320  } else {
321  // glBlitFramebuffer is unavailable; draw the frame texture as a fullscreen
322  // quad with the shader instead.
323  glBindFramebuffer(GL_FRAMEBUFFER, 0);
324  glViewport(0, dst_y0, frame_width, frame_height);
325  fl_compositor_opengl_shader_use(self->shader);
326  fl_compositor_opengl_shader_set_offset(self->shader, 0, 0);
327  fl_compositor_opengl_shader_set_scale(self->shader, 1, 1);
328  glBindTexture(GL_TEXTURE_2D, texture_id);
329  glDrawArrays(GL_TRIANGLES, 0, 6);
330  }
331  eglSwapBuffers(egl_display, self->egl_surface);
332  self->buffer_width = self->window_width;
333  self->buffer_height = self->window_height;
334 
335  // Restore the engine's rendering context so the raster thread can continue
336  // rendering after this present.
337  fl_opengl_manager_make_current(self->opengl_manager);
338 }
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
void fl_compositor_opengl_shader_set_offset(FlCompositorOpenGLShader *self, double x, double y)
FlCompositorOpenGLShader * fl_compositor_opengl_shader_new(FlOpenGLManager *opengl_manager)
void fl_compositor_opengl_shader_set_scale(FlCompositorOpenGLShader *self, double x, double y)
void fl_compositor_opengl_shader_use(FlCompositorOpenGLShader *self)
void fl_gl_fence_wait(FlGLFence *self)
Definition: fl_gl_fence.cc:84
gboolean fl_opengl_manager_clear_current(FlOpenGLManager *self)
EGLDisplay fl_opengl_manager_get_display(FlOpenGLManager *self)
gboolean fl_opengl_manager_can_blit(FlOpenGLManager *self)
gboolean fl_opengl_manager_make_current(FlOpenGLManager *self)
EGLContext fl_opengl_manager_get_context(FlOpenGLManager *self)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
struct wl_surface * fl_subsurface_get_surface(FlSubsurface *self)
static void fl_subsurface_egl_init(FlSubsurfaceEGL *self)
FlSubsurfaceEGL * fl_subsurface_egl_new(FlOpenGLManager *opengl_manager, FlSubsurface *subsurface, size_t width, size_t height, gint scale)
static void fl_subsurface_egl_dispose(GObject *object)
static EGLDisplay get_display(FlSubsurfaceEGL *self)
static void fl_subsurface_egl_class_init(FlSubsurfaceEGLClass *klass)
void fl_subsurface_egl_present(FlSubsurfaceEGL *self, GLuint texture_id, size_t width, size_t height, FlGLFence *fence)
void fl_subsurface_egl_resize(FlSubsurfaceEGL *self, size_t width, size_t height)
static size_t round_to_scale(size_t size, gint scale)
static void setup(FlSubsurfaceEGL *self, size_t width, size_t height, gint scale)
G_BEGIN_DECLS FlSubsurface size_t size_t gint scale
G_BEGIN_DECLS FlSubsurface * subsurface
struct wl_egl_window * egl_window
FlCompositorOpenGLShader * shader
FlOpenGLManager * opengl_manager
FlSubsurface * subsurface
int64_t texture_id