Flutter Linux Embedder
fl_view_renderer_subsurface.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/gl.h>
8 #include <gdk/gdkwayland.h>
9 
18 
19 // Maximum time to wait for a frame to be ready before giving up and rendering.
20 static constexpr gint64 kRenderTimeoutMicroseconds = 100000; // 100ms
21 
23  FlViewRenderer parent_instance;
24 
25  // Engine this widget is rendering.
26  FlEngine* engine;
27 
28  // TRUE if the view size should be controlled by Flutter.
29  gboolean sized_to_content;
30 
31  // Wayland subsurface the frame is rendered into.
32  FlSubsurface* subsurface;
33 
34  // The surface the subsurface was created on, and its Wayland object ID. Not
35  // owned by this object, they are only used to detect when GTK has replaced
36  // the surface. Both are compared because GTK may allocate the replacement at
37  // the same address.
38  struct wl_surface* parent_surface;
40 
41  // Manages the EGL context and surface used to present frames to the
42  // subsurface.
43  FlSubsurfaceEGL* egl;
44 
45  // Combines layers into a frame.
46  FlCompositorOpenGL* compositor;
47 
48  // Framebuffer the current frame is composited into.
49  FlFramebuffer* framebuffer;
50 
51  // Task runner used to wait for frames while resizing.
52  FlTaskRunner* task_runner;
53 
54  // Ensure Flutter and GTK can access the frame stored in the compositor.
55  GMutex frame_mutex;
56 };
57 
58 G_DEFINE_TYPE(FlViewRendererSubsurface,
59  fl_view_renderer_subsurface,
60  fl_view_renderer_get_type())
61 
62 // Get the size of the current frame in pixels. The size is zero if there is no
63 // frame yet. Must be called with the frame mutex held.
64 static void get_frame_size(FlViewRendererSubsurface* self,
65  size_t* width,
66  size_t* height) {
67  if (self->framebuffer != nullptr) {
68  *width = fl_framebuffer_get_width(self->framebuffer);
69  *height = fl_framebuffer_get_height(self->framebuffer);
70  } else {
71  *width = 0;
72  *height = 0;
73  }
74 }
75 
76 // Wait for a frame matching the given size to be presented, or until the
77 // timeout expires. This blocks the GTK thread so a window resize stays in step
78 // with the subsurface content. Must be called with the frame mutex held; the
79 // mutex is still held when this function returns.
80 static void wait_for_frame(FlViewRendererSubsurface* self,
81  size_t width,
82  size_t height) {
83  gint64 expiry_time = g_get_monotonic_time() + kRenderTimeoutMicroseconds;
84  while (true) {
85  size_t frame_width, frame_height;
86  get_frame_size(self, &frame_width, &frame_height);
87  if (frame_width == width && frame_height == height) {
88  break;
89  }
90 
91  if (g_get_monotonic_time() > expiry_time) {
92  g_warning(
93  "Timed out waiting for subsurface frame of size %zdx%zd (have "
94  "%zdx%zd)",
95  width, height, frame_width, frame_height);
96  break;
97  }
98 
99  g_mutex_unlock(&self->frame_mutex);
100  fl_task_runner_wait(self->task_runner, expiry_time);
101  g_mutex_lock(&self->frame_mutex);
102  }
103 }
104 
105 // Gets the Wayland surface of the window this widget is inside, or nullptr if
106 // it doesn't have one.
107 static struct wl_surface* get_parent_surface(GtkWidget* widget) {
108  GdkWindow* window = gtk_widget_get_window(gtk_widget_get_toplevel(widget));
109  if (window == nullptr || !GDK_IS_WAYLAND_WINDOW(window)) {
110  return nullptr;
111  }
112  return gdk_wayland_window_get_wl_surface(window);
113 }
114 
115 // Gets the Wayland object ID of @surface, or zero if it doesn't exist.
116 static uint32_t get_surface_id(struct wl_surface* surface) {
117  return surface == nullptr
118  ? 0
119  : wl_proxy_get_id(reinterpret_cast<struct wl_proxy*>(surface));
120 }
121 
122 // Move the subsurface to match the position of the widget in the toplevel.
123 static void update_subsurface_position(FlViewRendererSubsurface* self) {
124  if (self->subsurface == nullptr) {
125  return;
126  }
127  GtkWidget* widget = GTK_WIDGET(self);
128  GtkWidget* toplevel = gtk_widget_get_toplevel(widget);
129  gint x, y;
130  gtk_widget_translate_coordinates(widget, toplevel, 0, 0, &x, &y);
131  fl_subsurface_set_position(self->subsurface, x, y);
132 }
133 
134 // Make a subsurface on @parent_surface and an EGL context that presents frames
135 // to it. Must be called with the frame mutex held, as the raster thread
136 // presents to these objects.
137 static void create_subsurface(FlViewRendererSubsurface* self,
138  struct wl_surface* parent_surface) {
139  GtkWidget* widget = GTK_WIDGET(self);
140 
141  FlWaylandDisplay* wayland_display =
142  fl_wayland_display_get_for_display(gtk_widget_get_display(widget));
143  if (wayland_display == nullptr) {
144  return;
145  }
146 
147  self->subsurface =
148  fl_wayland_display_create_subsurface(wayland_display, parent_surface);
149  update_subsurface_position(self);
150 
151  GtkAllocation allocation;
152  gtk_widget_get_allocation(widget, &allocation);
153  self->egl = fl_subsurface_egl_new(
154  fl_engine_get_opengl_manager(self->engine), self->subsurface,
155  allocation.width, allocation.height, gtk_widget_get_scale_factor(widget));
156 }
157 
158 // GTK destroys the window's Wayland surface when it is hidden and makes a new
159 // one if it is shown again. The subsurface belongs to the old surface, so it is
160 // never shown again and using it is a protocol error. Detect this and move to
161 // the new surface.
162 static void update_parent_surface(FlViewRendererSubsurface* self) {
163  // Nothing to do before the widget is realized or after it is unrealized.
164  if (self->compositor == nullptr) {
165  return;
166  }
167 
168  struct wl_surface* parent_surface = get_parent_surface(GTK_WIDGET(self));
169  uint32_t parent_surface_id = get_surface_id(parent_surface);
170  if (parent_surface == self->parent_surface &&
171  parent_surface_id == self->parent_surface_id) {
172  return;
173  }
174 
175  // Drop the old subsurface. The surface it was on may already be destroyed,
176  // in which case there is nothing to replace it with until the window is
177  // shown again.
178  g_mutex_lock(&self->frame_mutex);
179  g_clear_object(&self->egl);
180  g_clear_object(&self->subsurface);
181  self->parent_surface = parent_surface;
182  self->parent_surface_id = parent_surface_id;
183  if (parent_surface != nullptr) {
184  create_subsurface(self, parent_surface);
185  }
186  g_mutex_unlock(&self->frame_mutex);
187 
188  if (parent_surface == nullptr) {
189  return;
190  }
191 
192  // The new subsurface has nothing in it, so ask for the frame to be rendered
193  // again.
194  fl_engine_schedule_frame(self->engine);
195 }
196 
197 // Redraw the view from the GTK thread.
198 static gboolean redraw_cb(gpointer user_data) {
199  g_autoptr(FlViewRendererSubsurface) self =
200  FL_VIEW_RENDERER_SUBSURFACE(user_data);
201 
202  if (self->compositor == nullptr) {
203  return G_SOURCE_REMOVE;
204  }
205 
206  fl_view_renderer_notify_frame(FL_VIEW_RENDERER(self));
207 
208  if (self->sized_to_content) {
209  size_t frame_width, frame_height;
210  g_mutex_lock(&self->frame_mutex);
211  get_frame_size(self, &frame_width, &frame_height);
212  g_mutex_unlock(&self->frame_mutex);
213  if (fl_view_renderer_resize_to_frame(FL_VIEW_RENDERER(self), frame_width,
214  frame_height)) {
215  return G_SOURCE_REMOVE;
216  }
217  }
218 
219  gtk_widget_queue_draw(GTK_WIDGET(self));
220 
221  return G_SOURCE_REMOVE;
222 }
223 
224 // Implements GtkWidget::realize.
225 static void fl_view_renderer_subsurface_realize(GtkWidget* widget) {
226  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(widget);
227 
228  GTK_WIDGET_CLASS(fl_view_renderer_subsurface_parent_class)->realize(widget);
229 
230  GdkDisplay* gdk_display = gtk_widget_get_display(widget);
231  if (!GDK_IS_WAYLAND_DISPLAY(gdk_display)) {
232  g_warning("FlViewRendererSubsurface requires a Wayland display");
233  return;
234  }
235 
236  // The subsurface's EGL context shares resources with the engine, so the
237  // engine's frame texture is accessed directly without using EGLImage.
238  self->task_runner =
239  FL_TASK_RUNNER(g_object_ref(fl_engine_get_task_runner(self->engine)));
240  self->compositor =
242 
243  // Create a subsurface on the toplevel's surface. This asks for a frame, as
244  // any rendered before this point were dropped.
245  update_parent_surface(self);
246 }
247 
248 // Implements GtkWidget::unrealize.
249 static void fl_view_renderer_subsurface_unrealize(GtkWidget* widget) {
250  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(widget);
251 
252  // Hold frame_mutex while releasing these objects so the raster thread cannot
253  // be using them in present_layers concurrently. The unref may run their
254  // destructors, so the calling code (here) owns the locking rather than the
255  // objects themselves.
256  g_mutex_lock(&self->frame_mutex);
257  g_clear_object(&self->compositor);
258 
259  // The EGL context and surface are released here; the EGL display is owned by
260  // the engine and is left untouched by FlSubsurfaceEGL.
261  g_clear_object(&self->egl);
262  g_clear_object(&self->subsurface);
263  self->parent_surface = nullptr;
264  self->parent_surface_id = 0;
265  g_mutex_unlock(&self->frame_mutex);
266 
267  GTK_WIDGET_CLASS(fl_view_renderer_subsurface_parent_class)->unrealize(widget);
268 }
269 
270 // Implements GtkWidget::map.
271 static void fl_view_renderer_subsurface_map(GtkWidget* widget) {
272  GTK_WIDGET_CLASS(fl_view_renderer_subsurface_parent_class)->map(widget);
273 
274  update_parent_surface(FL_VIEW_RENDERER_SUBSURFACE(widget));
275 }
276 
277 // Implements GtkWidget::draw.
278 static gboolean fl_view_renderer_subsurface_draw(GtkWidget* widget,
279  cairo_t* cr) {
280  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(widget);
281 
282  // Flutter content is shown directly by the subsurface; only paint the
283  // background behind it. The composited frame is blitted to the subsurface in
284  // present_layers, not here.
285  fl_view_renderer_paint_background(FL_VIEW_RENDERER(self), cr);
286 
287  return TRUE;
288 }
289 
290 // Implements GtkWidget::size_allocate.
291 static void fl_view_renderer_subsurface_size_allocate(
292  GtkWidget* widget,
293  GtkAllocation* allocation) {
294  GTK_WIDGET_CLASS(fl_view_renderer_subsurface_parent_class)
295  ->size_allocate(widget, allocation);
296 
297  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(widget);
298 
299  update_parent_surface(self);
300  update_subsurface_position(self);
301 
302  // Hold frame_mutex while resizing, as the raster thread uses the sizes this
303  // updates when it presents a frame.
304  g_mutex_lock(&self->frame_mutex);
305  if (self->egl != nullptr) {
306  gint scale_factor = gtk_widget_get_scale_factor(widget);
307  fl_subsurface_egl_resize(self->egl, allocation->width * scale_factor,
308  allocation->height * scale_factor);
309  }
310  g_mutex_unlock(&self->frame_mutex);
311 }
312 
313 // Runs after the size_allocate default handler and, crucially, after FlView has
314 // sent the new window metrics to the engine (connected with
315 // g_signal_connect_after). By this point the engine is already producing a
316 // frame of the new size, so block the GTK thread until that frame has been
317 // presented to the subsurface. This keeps the subsurface in step with the
318 // toplevel window while resizing.
319 static void fl_view_renderer_subsurface_size_allocate_after(
320  GtkWidget* widget,
321  GdkRectangle* allocation,
322  gpointer user_data) {
323  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(widget);
324 
325  // Nothing to synchronize against before the subsurface exists, or when
326  // Flutter controls the size (the frame drives the window size in that case).
327  if (self->egl == nullptr || self->sized_to_content) {
328  return;
329  }
330 
331  gint scale_factor = gtk_widget_get_scale_factor(widget);
332  size_t width = allocation->width * scale_factor;
333  size_t height = allocation->height * scale_factor;
334 
335  g_mutex_lock(&self->frame_mutex);
336  wait_for_frame(self, width, height);
337  g_mutex_unlock(&self->frame_mutex);
338 }
339 
340 // Implements FlViewRenderer::present_layers.
341 static void fl_view_renderer_subsurface_present_layers(
342  FlViewRenderer* renderer,
343  const FlutterLayer** layers,
344  size_t layers_count) {
345  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(renderer);
346 
347  g_mutex_lock(&self->frame_mutex);
348 
349  // Frames may be presented before the widget is realized, or after it has been
350  // unrealized; ignore them. Checked under frame_mutex so unrealize cannot
351  // release these objects while a frame is being presented.
352  if (self->compositor == nullptr || self->egl == nullptr) {
353  g_mutex_unlock(&self->frame_mutex);
354  return;
355  }
356 
357  size_t width, height;
358  if (layers_count > 0) {
359  width = layers[0]->size.width;
360  height = layers[0]->size.height;
361  } else if (self->framebuffer != nullptr) {
362  // A frame with nothing to rasterize, e.g. everything painted was fully
363  // transparent, has no layers and so no size. Keep the current size and
364  // present the frame, which is cleared when the layers are composited
365  // below, so the previous frame is not left on screen.
366  width = fl_framebuffer_get_width(self->framebuffer);
367  height = fl_framebuffer_get_height(self->framebuffer);
368  } else {
369  // Nothing has been rendered yet, so there is nothing to clear. The GTK
370  // thread is still notified below so the window is shown.
371  width = 0;
372  height = 0;
373  }
374 
375  if (width > 0 && height > 0) {
376  // Recreate the framebuffer if the frame size has changed. The subsurface's
377  // EGL context shares resources with the engine, so the frame texture is
378  // read directly and doesn't need to be shareable or copied via CPU memory.
379  if (self->framebuffer == nullptr ||
380  fl_framebuffer_get_width(self->framebuffer) != width ||
381  fl_framebuffer_get_height(self->framebuffer) != height) {
382  GLint general_format =
384  g_clear_object(&self->framebuffer);
385  self->framebuffer =
386  fl_framebuffer_new(general_format, width, height, FALSE);
387  }
388 
389  // Bind the target framebuffer so the compositor draws into it.
390  GLint saved_draw_framebuffer_binding;
391  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &saved_draw_framebuffer_binding);
392  glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
393  fl_framebuffer_get_id(self->framebuffer));
394 
396  layers_count);
397 
398  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, saved_draw_framebuffer_binding);
399  }
400 
401  // The frame is presented below using the subsurface's own OpenGL context.
402  // Sharing a texture between contexts requires the rendering to have completed
403  // before the other context uses it - making a context current only flushes
404  // the previous one, which guarantees the commands have been submitted, not
405  // that they have finished.
406  g_autoptr(FlGLFence) fence = nullptr;
407  if (fl_compositor_opengl_can_fence(self->compositor)) {
408  fence = fl_gl_fence_new(fl_engine_get_opengl_manager(self->engine));
409  } else {
410  // No fences on this driver, wait for the rendering here instead.
411  glFinish();
412  }
413 
414  // Present the composited frame directly to the subsurface using its own EGL
415  // context. This reads the engine's frame texture directly, as the subsurface
416  // context shares resources with the engine.
417  if (width > 0 && height > 0) {
418  fl_subsurface_egl_present(self->egl,
419  fl_framebuffer_get_texture_id(self->framebuffer),
420  width, height, fence);
421  }
422 
423  g_mutex_unlock(&self->frame_mutex);
424 
425  // Wake up the GTK thread if it is blocked in size_allocate waiting for this
426  // frame.
427  fl_task_runner_stop_wait(self->task_runner);
428 
429  // Notify the GTK thread so it can emit the first frame and resize the view.
430  g_idle_add(redraw_cb, g_object_ref(self));
431 }
432 
433 static void fl_view_renderer_subsurface_dispose(GObject* object) {
434  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(object);
435 
436  g_clear_object(&self->engine);
437  g_clear_object(&self->task_runner);
438  g_mutex_clear(&self->frame_mutex);
439 
440  G_OBJECT_CLASS(fl_view_renderer_subsurface_parent_class)->dispose(object);
441 }
442 
443 static void fl_view_renderer_subsurface_finalize(GObject* object) {
444  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(object);
445 
446  // The compositor is released here rather than in dispose() so it outlives a
447  // forced dispose (e.g. gtk_widget_destroy()) and is only freed once the last
448  // reference is dropped. This keeps it alive for the raster thread, which
449  // holds a strong reference on the view (and thus this renderer) while
450  // presenting.
451  g_clear_object(&self->compositor);
452  g_clear_object(&self->framebuffer);
453 
454  G_OBJECT_CLASS(fl_view_renderer_subsurface_parent_class)->finalize(object);
455 }
456 
457 static void fl_view_renderer_subsurface_class_init(
458  FlViewRendererSubsurfaceClass* klass) {
459  GObjectClass* object_class = G_OBJECT_CLASS(klass);
460  object_class->dispose = fl_view_renderer_subsurface_dispose;
461  object_class->finalize = fl_view_renderer_subsurface_finalize;
462 
463  GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass);
464  widget_class->realize = fl_view_renderer_subsurface_realize;
465  widget_class->unrealize = fl_view_renderer_subsurface_unrealize;
466  widget_class->map = fl_view_renderer_subsurface_map;
467  widget_class->draw = fl_view_renderer_subsurface_draw;
468  widget_class->size_allocate = fl_view_renderer_subsurface_size_allocate;
469 
470  FlViewRendererClass* renderer_class = FL_VIEW_RENDERER_CLASS(klass);
471  renderer_class->present_layers = fl_view_renderer_subsurface_present_layers;
472 }
473 
474 static void fl_view_renderer_subsurface_init(FlViewRendererSubsurface* self) {
475  g_mutex_init(&self->frame_mutex);
476 
477  // Connected after the default handler so this runs once FlView has sent the
478  // new window metrics to the engine, allowing the frame of the new size to be
479  // waited for.
480  g_signal_connect_after(
481  self, "size-allocate",
482  G_CALLBACK(fl_view_renderer_subsurface_size_allocate_after), nullptr);
483 }
484 
485 FlViewRendererSubsurface* fl_view_renderer_subsurface_new(
486  FlEngine* engine,
487  gboolean sized_to_content) {
488  g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
489 
490  FlViewRendererSubsurface* self = FL_VIEW_RENDERER_SUBSURFACE(
491  g_object_new(fl_view_renderer_subsurface_get_type(), nullptr));
492  self->engine = FL_ENGINE(g_object_ref(engine));
493  self->sized_to_content = sized_to_content;
494  return self;
495 }
g_autoptr(FlEngine) engine
const char FlTextDirection FlAssertiveness gpointer user_data
void fl_compositor_opengl_composite_layers(FlCompositorOpenGL *self, const FlutterLayer **layers, size_t layers_count)
FlCompositorOpenGL * fl_compositor_opengl_new(FlOpenGLManager *opengl_manager)
GLint fl_compositor_opengl_get_frame_format(const FlutterLayer **layers, size_t layers_count)
gboolean fl_compositor_opengl_can_fence(FlCompositorOpenGL *self)
FlOpenGLManager * fl_engine_get_opengl_manager(FlEngine *self)
Definition: fl_engine.cc:751
FlTaskRunner * fl_engine_get_task_runner(FlEngine *self)
Definition: fl_engine.cc:1591
void fl_engine_schedule_frame(FlEngine *self)
Definition: fl_engine.cc:1185
size_t fl_framebuffer_get_height(FlFramebuffer *self)
GLuint fl_framebuffer_get_id(FlFramebuffer *self)
size_t fl_framebuffer_get_width(FlFramebuffer *self)
GLuint fl_framebuffer_get_texture_id(FlFramebuffer *self)
FlFramebuffer * fl_framebuffer_new(GLint format, size_t width, size_t height, gboolean depth_stencil)
FlGLFence * fl_gl_fence_new(FlOpenGLManager *opengl_manager)
Definition: fl_gl_fence.cc:50
const uint8_t uint32_t uint32_t * height
void fl_subsurface_set_position(FlSubsurface *self, gint x, gint y)
FlSubsurfaceEGL * fl_subsurface_egl_new(FlOpenGLManager *opengl_manager, FlSubsurface *subsurface, size_t width, size_t height, gint scale)
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)
void fl_task_runner_stop_wait(FlTaskRunner *self)
void fl_task_runner_wait(FlTaskRunner *self, gint64 expiry_time)
const FlutterLayer size_t layers_count
const FlutterLayer ** layers
gboolean fl_view_renderer_resize_to_frame(FlViewRenderer *self, size_t frame_width, size_t frame_height)
void fl_view_renderer_notify_frame(FlViewRenderer *self)
void fl_view_renderer_paint_background(FlViewRenderer *self, cairo_t *cr)
G_BEGIN_DECLS gboolean sized_to_content
static constexpr gint64 kRenderTimeoutMicroseconds
size_t * width
G_DEFINE_TYPE(FlViewRendererSubsurface, fl_view_renderer_subsurface, fl_view_renderer_get_type()) static void get_frame_size(FlViewRendererSubsurface *self
FlSubsurface * fl_wayland_display_create_subsurface(FlWaylandDisplay *self, struct wl_surface *parent_surface)
FlWaylandDisplay * fl_wayland_display_get_for_display(GdkDisplay *gdk_display)