Visitar URL original
[Linux] Use fences to synchronize frames between OpenGL contexts by robert-ancell · Pull Request #192098 · flutter/flutter · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Linux] Name the fence waits after the OpenGL calls they make
The waits were named fl_gl_fence_wait() and fl_gl_fence_wait_gpu(),
which was the opposite way around to the calls they make - OpenGL uses
the plain name for the wait that doesn't block the caller and qualifies
the one that does. The name also described the wait as being done by the
GPU, which OpenGL is allowed to do on the CPU instead.

Rename them to fl_gl_fence_client_wait() and fl_gl_fence_wait() to match.
  • Loading branch information
robert-ancell committed Sep 3, 2026
commit 1029ccf66b0495a69b9e70f2647b70287d046ea6
14 changes: 7 additions & 7 deletions engine/src/flutter/shell/platform/linux/fl_gl_fence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ struct _FlGLFence {

EGLSyncKHR sync;

// TRUE if the current context can be made to wait without blocking.
gboolean can_wait_gpu;
// TRUE if OpenGL can wait for the fence, i.e. without blocking this thread.
gboolean can_wait_sync;
};

G_DEFINE_TYPE(FlGLFence, fl_gl_fence, G_TYPE_OBJECT)
Expand Down Expand Up @@ -64,12 +64,12 @@ FlGLFence* fl_gl_fence_new(FlOpenGLManager* opengl_manager) {

self->display = display;
self->sync = sync;
self->can_wait_gpu = fl_opengl_manager_can_wait_sync(opengl_manager);
self->can_wait_sync = fl_opengl_manager_can_wait_sync(opengl_manager);

return self;
}

void fl_gl_fence_wait(FlGLFence* self) {
void fl_gl_fence_client_wait(FlGLFence* self) {
g_return_if_fail(FL_IS_GL_FENCE(self));

// Nothing to wait for if the fence couldn't be created.
Expand All @@ -87,16 +87,16 @@ void fl_gl_fence_wait(FlGLFence* self) {
}
}

void fl_gl_fence_wait_gpu(FlGLFence* self) {
void fl_gl_fence_wait(FlGLFence* self) {
g_return_if_fail(FL_IS_GL_FENCE(self));

// Nothing to wait for if the fence couldn't be created.
if (self->sync == EGL_NO_SYNC_KHR) {
return;
}

if (!self->can_wait_gpu) {
fl_gl_fence_wait(self);
if (!self->can_wait_sync) {
fl_gl_fence_client_wait(self);
return;
}

Expand Down
22 changes: 11 additions & 11 deletions engine/src/flutter/shell/platform/linux/fl_gl_fence.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ G_DECLARE_FINAL_TYPE(FlGLFence, fl_gl_fence, FL, GL_FENCE, GObject)
* @opengl_manager: an #FlOpenGLManager.
*
* Creates a fence that is reached when the commands submitted to the current
* OpenGL context so far have completed. Must be called with the context that
* did the rendering current.
* OpenGL context so far have completed. Requires the context that did the
* rendering to be current.
*
* Only use this if fl_opengl_manager_can_fence() returns %TRUE; drivers without
* fences have to fall back to waiting for the rendering to complete, e.g. with
Expand All @@ -44,29 +44,29 @@ G_DECLARE_FINAL_TYPE(FlGLFence, fl_gl_fence, FL, GL_FENCE, GObject)
FlGLFence* fl_gl_fence_new(FlOpenGLManager* opengl_manager);

/**
* fl_gl_fence_wait:
* fl_gl_fence_client_wait:
* @fence: an #FlGLFence.
*
* Blocks the calling thread until @fence is reached.
*
* Use this when the context that is going to use the rendering isn't the
* Use this when the context that is going to use the rendered texture isn't the
* current one, e.g. when handing a frame to a toolkit that makes its own
* context current before reading it.
*/
void fl_gl_fence_wait(FlGLFence* fence);
void fl_gl_fence_client_wait(FlGLFence* fence);

/**
* fl_gl_fence_wait_gpu:
* fl_gl_fence_wait:
* @fence: an #FlGLFence.
*
* Makes the current OpenGL context wait for @fence before executing any
* commands submitted after this point. Must be called with the context that is
* going to use the rendering current.
* commands submitted after this point. Requires the context that is going to
* use the rendered texture to be current.
*
* This doesn't block the calling thread. Falls back to blocking if the driver
* doesn't support waiting without blocking.
* This doesn't block the calling thread, the waiting is done by OpenGL. Falls
* back to fl_gl_fence_client_wait() if the driver can't wait this way.
*/
void fl_gl_fence_wait_gpu(FlGLFence* fence);
void fl_gl_fence_wait(FlGLFence* fence);

G_END_DECLS

Expand Down
16 changes: 8 additions & 8 deletions engine/src/flutter/shell/platform/linux/fl_gl_fence_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,37 @@ TEST_F(FlGLFenceTest, NewFails) {

EXPECT_CALL(epoxy, eglClientWaitSyncKHR).Times(0);
EXPECT_CALL(epoxy, eglWaitSyncKHR).Times(0);
fl_gl_fence_client_wait(fence);
fl_gl_fence_wait(fence);
fl_gl_fence_wait_gpu(fence);
}

// Waiting blocks the calling thread until the fence is reached.
TEST_F(FlGLFenceTest, Wait) {
TEST_F(FlGLFenceTest, ClientWait) {
g_autoptr(FlGLFence) fence = fl_gl_fence_new(opengl_manager);
ASSERT_NE(fence, nullptr);

EXPECT_CALL(epoxy, eglClientWaitSyncKHR(::testing::_, ::testing::_,
::testing::_, ::testing::_))
.WillOnce(::testing::Return(EGL_CONDITION_SATISFIED_KHR));

fl_gl_fence_wait(fence);
fl_gl_fence_client_wait(fence);
}

// Waiting on the GPU doesn't block the calling thread.
TEST_F(FlGLFenceTest, WaitGpu) {
// OpenGL can do the waiting, which doesn't block the calling thread.
TEST_F(FlGLFenceTest, Wait) {
g_autoptr(FlGLFence) fence = fl_gl_fence_new(opengl_manager);
ASSERT_NE(fence, nullptr);

EXPECT_CALL(epoxy, eglWaitSyncKHR(::testing::_, ::testing::_, 0))
.WillOnce(::testing::Return(EGL_TRUE));
EXPECT_CALL(epoxy, eglClientWaitSyncKHR).Times(0);

fl_gl_fence_wait_gpu(fence);
fl_gl_fence_wait(fence);
}

// Drivers that can create fences but can't wait on them without blocking fall
// back to blocking, which is slower but still correct.
TEST_F(FlGLFenceTest, WaitGpuUnsupported) {
TEST_F(FlGLFenceTest, WaitUnsupported) {
EXPECT_CALL(epoxy, epoxy_has_egl_extension(
::testing::_, ::testing::StrEq("EGL_KHR_wait_sync")))
.WillRepeatedly(::testing::Return(false));
Expand All @@ -83,7 +83,7 @@ TEST_F(FlGLFenceTest, WaitGpuUnsupported) {
EXPECT_CALL(epoxy, eglClientWaitSyncKHR)
.WillOnce(::testing::Return(EGL_CONDITION_SATISFIED_KHR));

fl_gl_fence_wait_gpu(fence);
fl_gl_fence_wait(fence);
}

// The fence is destroyed with the display it was created on, so it doesn't
Expand Down
4 changes: 2 additions & 2 deletions engine/src/flutter/shell/platform/linux/fl_opengl_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ gboolean fl_opengl_frame_draw(FlOpenGLFrame* self,
if (fl_framebuffer_get_shareable(self->framebuffer)) {
// Wait for the frame to have finished rendering before GTK reads it. GTK
// draws the frame using the window's paint context rather than the one
// that is current here, so the wait can't be left to the GPU - it would
// that is current here, so the waiting can't be left to OpenGL - it would
// only order the context it was issued in.
if (self->fence != nullptr) {
fl_gl_fence_wait(self->fence);
fl_gl_fence_client_wait(self->fence);
g_clear_object(&self->fence);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ void fl_subsurface_egl_present(FlSubsurfaceEGL* self,

// Make this context wait for the frame to have finished rendering in the
// engine's context before it reads the texture below. This context is the one
// that reads the frame, so the wait can be left to the GPU and this thread
// that reads the frame, so the waiting can be left to OpenGL and this thread
// doesn't have to block.
if (fence != nullptr) {
fl_gl_fence_wait_gpu(fence);
fl_gl_fence_wait(fence);
}

EGLint surface_width = 0, surface_height = 0;
Expand Down