Skip to content

Commit 26486ad

Browse files
committed
[Linux] Double buffer frames presented to a subsurface
Frames were composited into a single framebuffer, which the subsurface then read to present. Presenting doesn't wait for that read to finish - eglSwapBuffers() submits the work and making the engine context current again only flushes the subsurface context. The next frame was composited into the same framebuffer, so it could overwrite a frame the GPU was still reading, showing a mix of two frames. Composite into two framebuffers alternately so a frame is never written into the framebuffer the last frame is being read from. See flutter#191775
1 parent ce1f5ff commit 26486ad

1 file changed

Lines changed: 45 additions & 20 deletions

File tree

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

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
// Maximum time to wait for a frame to be ready before giving up and rendering.
2020
static constexpr gint64 kRenderTimeoutMicroseconds = 100000; // 100ms
2121

22+
// Number of framebuffers frames are composited into. The subsurface reads the
23+
// frame texture on the GPU, and that read isn't complete when the frame is
24+
// presented. Using more than one framebuffer means the next frame is
25+
// composited into a different one, so it can't overwrite a frame that is still
26+
// being read.
27+
static constexpr size_t kFrameBufferCount = 2;
28+
2229
struct _FlViewRendererSubsurface {
2330
FlViewRenderer parent_instance;
2431

@@ -38,8 +45,13 @@ struct _FlViewRendererSubsurface {
3845
// Combines layers into a frame.
3946
FlCompositorOpenGL* compositor;
4047

41-
// Framebuffer the current frame is composited into.
42-
FlFramebuffer* framebuffer;
48+
// Framebuffers the frames are composited into. Cycled through so a frame
49+
// isn't composited into a framebuffer the subsurface may still be reading
50+
// from.
51+
FlFramebuffer* framebuffers[kFrameBufferCount];
52+
53+
// The framebuffer in @framebuffers the next frame will be composited into.
54+
size_t framebuffer_index;
4355

4456
// Task runner used to wait for frames while resizing.
4557
FlTaskRunner* task_runner;
@@ -52,14 +64,22 @@ G_DEFINE_TYPE(FlViewRendererSubsurface,
5264
fl_view_renderer_subsurface,
5365
fl_view_renderer_get_type())
5466

67+
// Get the framebuffer the last frame was composited into, or %NULL if no frame
68+
// has been composited yet. Must be called with the frame mutex held.
69+
static FlFramebuffer* get_frame_framebuffer(FlViewRendererSubsurface* self) {
70+
return self->framebuffers[(self->framebuffer_index + kFrameBufferCount - 1) %
71+
kFrameBufferCount];
72+
}
73+
5574
// Get the size of the current frame in pixels. The size is zero if there is no
5675
// frame yet. Must be called with the frame mutex held.
5776
static void get_frame_size(FlViewRendererSubsurface* self,
5877
size_t* width,
5978
size_t* height) {
60-
if (self->framebuffer != nullptr) {
61-
*width = fl_framebuffer_get_width(self->framebuffer);
62-
*height = fl_framebuffer_get_height(self->framebuffer);
79+
FlFramebuffer* framebuffer = get_frame_framebuffer(self);
80+
if (framebuffer != nullptr) {
81+
*width = fl_framebuffer_get_width(framebuffer);
82+
*height = fl_framebuffer_get_height(framebuffer);
6383
} else {
6484
*width = 0;
6585
*height = 0;
@@ -283,24 +303,28 @@ static void fl_view_renderer_subsurface_present_layers(
283303
size_t width = layers[0]->size.width;
284304
size_t height = layers[0]->size.height;
285305

286-
// Recreate the framebuffer if the frame size has changed. The subsurface's
287-
// EGL context shares resources with the engine, so the frame texture is read
288-
// directly and doesn't need to be shareable or copied via CPU memory.
289-
if (self->framebuffer == nullptr ||
290-
fl_framebuffer_get_width(self->framebuffer) != width ||
291-
fl_framebuffer_get_height(self->framebuffer) != height) {
306+
// Composite into a different framebuffer to the last frame, which the
307+
// subsurface may still be reading. Recreate it if the frame size has changed.
308+
// The subsurface's EGL context shares resources with the engine, so the frame
309+
// texture is read directly and doesn't need to be shareable or copied via CPU
310+
// memory.
311+
size_t index = self->framebuffer_index;
312+
self->framebuffer_index = (index + 1) % kFrameBufferCount;
313+
FlFramebuffer* framebuffer = self->framebuffers[index];
314+
if (framebuffer == nullptr ||
315+
fl_framebuffer_get_width(framebuffer) != width ||
316+
fl_framebuffer_get_height(framebuffer) != height) {
292317
GLint general_format =
293318
fl_compositor_opengl_get_frame_format(layers, layers_count);
294-
g_clear_object(&self->framebuffer);
295-
self->framebuffer =
296-
fl_framebuffer_new(general_format, width, height, FALSE);
319+
g_clear_object(&self->framebuffers[index]);
320+
framebuffer = fl_framebuffer_new(general_format, width, height, FALSE);
321+
self->framebuffers[index] = framebuffer;
297322
}
298323

299324
// Bind the target framebuffer so the compositor draws into it.
300325
GLint saved_draw_framebuffer_binding;
301326
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &saved_draw_framebuffer_binding);
302-
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,
303-
fl_framebuffer_get_id(self->framebuffer));
327+
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fl_framebuffer_get_id(framebuffer));
304328

305329
fl_compositor_opengl_composite_layers(self->compositor, layers, layers_count);
306330

@@ -316,9 +340,8 @@ static void fl_view_renderer_subsurface_present_layers(
316340
// Present the composited frame directly to the subsurface using its own EGL
317341
// context. This reads the engine's frame texture directly, as the subsurface
318342
// context shares resources with the engine.
319-
fl_subsurface_egl_present(self->egl,
320-
fl_framebuffer_get_texture_id(self->framebuffer),
321-
width, height);
343+
fl_subsurface_egl_present(
344+
self->egl, fl_framebuffer_get_texture_id(framebuffer), width, height);
322345

323346
g_mutex_unlock(&self->frame_mutex);
324347

@@ -349,7 +372,9 @@ static void fl_view_renderer_subsurface_finalize(GObject* object) {
349372
// holds a strong reference on the view (and thus this renderer) while
350373
// presenting.
351374
g_clear_object(&self->compositor);
352-
g_clear_object(&self->framebuffer);
375+
for (size_t i = 0; i < kFrameBufferCount; i++) {
376+
g_clear_object(&self->framebuffers[i]);
377+
}
353378

354379
G_OBJECT_CLASS(fl_view_renderer_subsurface_parent_class)->finalize(object);
355380
}

0 commit comments

Comments
 (0)