[Fix] (EGL, SharedImage): real EGL native fences publish a dma-buf producer's writes, so Chrome's GPU process frames reach the compositor.

This commit is contained in:
2026-10-03 10:59:02 -04:00
parent 2a1889b817
commit 4f6f4125ac
26 changed files with 790 additions and 28 deletions
+206 -6
View File
@@ -19,6 +19,13 @@
//
// And the implicit-sync flush (docs/Disaggregated/notes/anland/plan-x11-gpu.md): the shared_image
// Flush verb the client sends from glFlush is answered by the server.
//
// Chrome's GPU process on the same kind of display (a GBM one) is the other dma-buf producer, and it
// never flushes: when the compositor's kernel takes dma-buf fences it ends each frame by creating an
// EGL_ANDROID_native_fence_sync fence (through ANGLE, which offers fences only when the native EGL
// has EGL_KHR_fence_sync and the extension, and loads the KHR entry points by name) and hands its
// descriptor to the browser with the buffer. That fence is the only point at which the frame's
// writes into the buffer can reach the compositor's session, so it must be a real one.
#include "P12ServerRig.h"
@@ -173,6 +180,158 @@ namespace {
(void)EGL::Terminate(dpy);
}
struct ChromeFenceReport {
Int32 initialized = 0;
Int32 fenceSyncExtension = 0;
Int32 waitSyncExtension = 0;
Int32 nativeFenceExtension = 0;
Int32 entryPoints = 0;
Int32 current = 0;
Int32 imageBound = 0;
Int32 framebufferComplete = 0;
Int32 noContextRefused = 0;
Int32 fenceCreated = 0;
Int32 fenceType = 0;
Int32 fenceWaited = 0;
Int32 fenceSignaled = 0;
Int32 dupFd = -2;
Int32 dupError = 0;
Int32 dupSignaled = 0;
Int32 importCreated = 0;
Int32 importWaited = 0;
Int32 eglError = 0;
char note[128] = {};
};
// ANGLE's FunctionsEGL and Chrome's GLFenceAndroidNativeFenceSync / GbmSurfacelessWayland,
// reduced to the calls they make on the native EGL.
void RunChromeGpuFence(ChromeFenceReport& r) {
using CreateSyncFn = EGLSyncKHR (*)(EGLDisplay, EGLenum, const EGLint*);
using DestroySyncFn = EGLBoolean (*)(EGLDisplay, EGLSyncKHR);
using ClientWaitFn = EGLint (*)(EGLDisplay, EGLSyncKHR, EGLint, EGLTimeKHR);
using GetSyncAttribFn = EGLBoolean (*)(EGLDisplay, EGLSyncKHR, EGLint, EGLint*);
using WaitSyncFn = EGLint (*)(EGLDisplay, EGLSyncKHR, EGLint);
using DupFn = EGLint (*)(EGLDisplay, EGLSyncKHR);
static int fakeGbmDevice[16] = {};
const EGLDisplay dpy = EGL::GetPlatformDisplay(kPlatformGbm, fakeGbmDevice, nullptr);
if (dpy == EGL_NO_DISPLAY) return;
EGLint major = 0, minor = 0;
r.initialized = EGL::Initialize(dpy, &major, &minor) == EGL_TRUE ? 1 : 0;
if (!r.initialized) return;
const char* exts = EGL::QueryString(dpy, EGL_EXTENSIONS);
r.fenceSyncExtension = HasToken(exts, "EGL_KHR_fence_sync") ? 1 : 0;
r.waitSyncExtension = HasToken(exts, "EGL_KHR_wait_sync") ? 1 : 0;
r.nativeFenceExtension = HasToken(exts, "EGL_ANDROID_native_fence_sync") ? 1 : 0;
const auto createSync = reinterpret_cast<CreateSyncFn>(EGL::GetProcAddress("eglCreateSyncKHR"));
const auto destroySync = reinterpret_cast<DestroySyncFn>(EGL::GetProcAddress("eglDestroySyncKHR"));
const auto clientWait = reinterpret_cast<ClientWaitFn>(EGL::GetProcAddress("eglClientWaitSyncKHR"));
const auto getSyncAttrib = reinterpret_cast<GetSyncAttribFn>(EGL::GetProcAddress("eglGetSyncAttribKHR"));
const auto waitSync = reinterpret_cast<WaitSyncFn>(EGL::GetProcAddress("eglWaitSyncKHR"));
const auto dupFence = reinterpret_cast<DupFn>(EGL::GetProcAddress("eglDupNativeFenceFDANDROID"));
r.entryPoints = createSync && destroySync && clientWait && getSyncAttrib && waitSync && dupFence ? 1 : 0;
if (!r.entryPoints) return;
// A fence command needs a current context.
r.noContextRefused = createSync(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr) == EGL_NO_SYNC_KHR &&
EGL::GetError() == EGL_BAD_MATCH
? 1
: 0;
// Chrome's GPU process: ES through ANGLE's identity, surfaceless, no config.
(void)EGL::BindAPI(EGL_OPENGL_ES_API);
const EGLint es3[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE};
const EGLContext ctx = EGL::CreateContext(dpy, EGL_NO_CONFIG_KHR, EGL_NO_CONTEXT, es3);
if (ctx == EGL_NO_CONTEXT) {
r.eglError = EGL::GetError();
return;
}
r.current = EGL::MakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, ctx) == EGL_TRUE ? 1 : 0;
if (!r.current) {
r.eglError = EGL::GetError();
return;
}
// A GBM scanout buffer (a shared image), bound as a texture and rendered into through a
// framebuffer - and then no glFlush, as Chrome does when it has native fences.
auto* backend = MG_Backend::pActiveBackendObject.get();
MG_Backend::SharedImageExport image;
EGLImage eglImage = EGL_NO_IMAGE;
using GenFn = void (*)(int, unsigned*);
using BindFn = void (*)(unsigned, unsigned);
using ImageTargetFn = void (*)(unsigned, void*);
using FbTexFn = void (*)(unsigned, unsigned, unsigned, unsigned, int);
using StatusFn = unsigned (*)(unsigned);
using ClearColorFn = void (*)(float, float, float, float);
using ClearFn = void (*)(unsigned);
const auto genTextures = reinterpret_cast<GenFn>(EGL::GetProcAddress("glGenTextures"));
const auto bindTexture = reinterpret_cast<BindFn>(EGL::GetProcAddress("glBindTexture"));
const auto imageTarget = reinterpret_cast<ImageTargetFn>(EGL::GetProcAddress("glEGLImageTargetTexture2DOES"));
const auto genFramebuffers = reinterpret_cast<GenFn>(EGL::GetProcAddress("glGenFramebuffers"));
const auto bindFramebuffer = reinterpret_cast<BindFn>(EGL::GetProcAddress("glBindFramebuffer"));
const auto framebufferTexture = reinterpret_cast<FbTexFn>(EGL::GetProcAddress("glFramebufferTexture2D"));
const auto checkStatus = reinterpret_cast<StatusFn>(EGL::GetProcAddress("glCheckFramebufferStatus"));
const auto clearColor = reinterpret_cast<ClearColorFn>(EGL::GetProcAddress("glClearColor"));
const auto clear = reinterpret_cast<ClearFn>(EGL::GetProcAddress("glClear"));
if (backend != nullptr && backend->AllocateSharedImage(64, 32, kArgb8888, &image) && image.Fd >= 0) {
const EGLAttrib attribs[] = {EGL_WIDTH, 64, EGL_HEIGHT, 32, EGL_LINUX_DRM_FOURCC_EXT, kArgb8888,
EGL_DMA_BUF_PLANE0_FD_EXT, image.Fd, EGL_DMA_BUF_PLANE0_OFFSET_EXT,
static_cast<EGLAttrib>(image.Offset), EGL_DMA_BUF_PLANE0_PITCH_EXT,
static_cast<EGLAttrib>(image.Stride), EGL_NONE};
eglImage = EGL::CreateImage(dpy, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, nullptr, attribs);
}
if (eglImage != EGL_NO_IMAGE && genTextures && bindTexture && imageTarget && genFramebuffers &&
bindFramebuffer && framebufferTexture && checkStatus && clearColor && clear) {
unsigned texture = 0, framebuffer = 0;
genTextures(1, &texture);
bindTexture(0x0DE1 /* GL_TEXTURE_2D */, texture);
imageTarget(0x0DE1, eglImage);
r.imageBound = 1;
genFramebuffers(1, &framebuffer);
bindFramebuffer(0x8D40 /* GL_FRAMEBUFFER */, framebuffer);
framebufferTexture(0x8D40, 0x8CE0 /* GL_COLOR_ATTACHMENT0 */, 0x0DE1, texture, 0);
r.framebufferComplete = checkStatus(0x8D40) == 0x8CD5 /* GL_FRAMEBUFFER_COMPLETE */ ? 1 : 0;
clearColor(0.0f, 1.0f, 1.0f, 1.0f);
clear(0x4000);
}
// GLFenceAndroidNativeFenceSync::CreateForGpuFence, then GetGpuFenceHandle.
const EGLSyncKHR fence = createSync(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
r.fenceCreated = fence != EGL_NO_SYNC_KHR ? 1 : 0;
if (!r.fenceCreated) {
r.eglError = EGL::GetError();
} else {
EGLint value = 0;
if (getSyncAttrib(dpy, fence, EGL_SYNC_TYPE_KHR, &value)) r.fenceType = value;
r.fenceWaited = clientWait(dpy, fence, 0, EGL_FOREVER_KHR) == EGL_CONDITION_SATISFIED_KHR ? 1 : 0;
if (getSyncAttrib(dpy, fence, EGL_SYNC_STATUS_KHR, &value)) r.fenceSignaled = value == EGL_SIGNALED_KHR;
r.dupFd = dupFence(dpy, fence);
r.dupError = r.dupFd < 0 ? EGL::GetError() : EGL_SUCCESS;
if (r.dupFd >= 0) {
pollfd entry{r.dupFd, POLLIN, 0};
r.dupSignaled = ::poll(&entry, 1, 5000) == 1 ? 1 : 0;
// GLFenceAndroidNativeFenceSync::CreateFromGpuFenceHandle + ServerWait: the
// descriptor imported back (the sync takes it) and waited on.
const EGLint importAttribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, r.dupFd, EGL_NONE};
const EGLSyncKHR imported = createSync(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, importAttribs);
r.importCreated = imported != EGL_NO_SYNC_KHR ? 1 : 0;
if (imported != EGL_NO_SYNC_KHR) {
r.importWaited = waitSync(dpy, imported, 0) == EGL_TRUE ? 1 : 0;
(void)destroySync(dpy, imported);
} else {
::close(r.dupFd);
}
}
(void)destroySync(dpy, fence);
}
if (eglImage != EGL_NO_IMAGE) (void)EGL::DestroyImage(dpy, eglImage);
if (image.Fd >= 0) ::close(image.Fd);
if (backend != nullptr && image.Id != 0) (void)backend->ReleaseSharedImage(image.Id);
(void)EGL::MakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
(void)EGL::DestroyContext(dpy, ctx);
(void)EGL::Terminate(dpy);
}
// The supervisor on an abstract unix socket, as the device's is: shared images travel as
// descriptors on its aux socket, which a TCP endpoint does not have.
bool LaunchUnixSupervisor(ServerProcess* server) {
@@ -190,8 +349,10 @@ namespace {
return WaitFor([&] { return server->Log().find("listening on") != std::string::npos; }, 10000);
}
GlamorReport RunGlamorPeer(const ServerProcess& server, const std::string& clientLogBase) {
GlamorReport report{};
// One forked client running `run` against the server, its report back through a pipe.
template <class Report>
Report RunPeer(const ServerProcess& server, const std::string& clientLogBase, void (*run)(Report&)) {
Report report{};
int fds[2] = {-1, -1};
if (::pipe(fds) != 0) return report;
std::fflush(nullptr);
@@ -203,8 +364,8 @@ namespace {
::setenv("MOBILEGL_IPC_CONTROL", server.endpoint.c_str(), 1);
::setenv("MOBILEGL_BACKEND_TYPE", "DirectGLES", 1);
::setenv("MOBILEGL_IPC_SURFACE", "offscreen", 1);
GlamorReport r{};
RunGlamor(r);
Report r{};
run(r);
const ssize_t wrote = ::write(fds[1], &r, sizeof(r));
(void)wrote;
std::fflush(nullptr);
@@ -218,7 +379,7 @@ namespace {
pollfd pfd{fds[0], POLLIN, 0};
if (::poll(&pfd, 1, 90000) > 0) {
if (::read(fds[0], &report, sizeof(report)) != static_cast<ssize_t>(sizeof(report))) {
report = GlamorReport{};
report = Report{};
std::snprintf(report.note, sizeof(report.note), "peer died before reporting");
}
} else {
@@ -237,7 +398,7 @@ TEST(GlamorEglSequence, XwaylandFindsEverythingGlamorNeeds) {
ASSERT_TRUE(LaunchUnixSupervisor(&server)) << server.Log();
const std::string clientLog = "/tmp/mgl-glamor-client-" + std::to_string(::getpid()) + ".log";
Debug::TruncateRoleLogs(clientLog.c_str());
const GlamorReport r = RunGlamorPeer(server, clientLog);
const GlamorReport r = RunPeer<GlamorReport>(server, clientLog, RunGlamor);
ASSERT_EQ(r.display, 1) << r.note;
EXPECT_EQ(r.platformGbmExtension, 1);
ASSERT_EQ(r.initialized, 1);
@@ -263,3 +424,42 @@ TEST(GlamorEglSequence, XwaylandFindsEverythingGlamorNeeds) {
std::filesystem::remove(clientLog, ec);
}
}
TEST(GlamorEglSequence, ChromesGpuProcessGetsANativeFenceOfItsDmaBufWrites) {
ServerProcess server;
ASSERT_TRUE(LaunchUnixSupervisor(&server)) << server.Log();
const std::string clientLog = "/tmp/mgl-chrome-fence-client-" + std::to_string(::getpid()) + ".log";
Debug::TruncateRoleLogs(clientLog.c_str());
const ChromeFenceReport r = RunPeer<ChromeFenceReport>(server, clientLog, RunChromeGpuFence);
ASSERT_EQ(r.initialized, 1) << r.note;
// ANGLE offers Chrome fences only with all three, and loads the KHR names.
EXPECT_EQ(r.fenceSyncExtension, 1);
EXPECT_EQ(r.waitSyncExtension, 1);
EXPECT_EQ(r.nativeFenceExtension, 1);
ASSERT_EQ(r.entryPoints, 1) << "eglCreateSyncKHR & co. or eglDupNativeFenceFDANDROID did not resolve";
EXPECT_EQ(r.noContextRefused, 1);
ASSERT_EQ(r.current, 1) << "error 0x" << std::hex << r.eglError;
std::printf("[ chrome ] image bound %d, framebuffer complete %d, fence descriptor %d (error 0x%x)\n",
r.imageBound, r.framebufferComplete, r.dupFd, r.dupError);
// The fence command reaches the server, which publishes the context's image writes and answers.
ASSERT_EQ(r.fenceCreated, 1) << "the native fence was refused, error 0x" << std::hex << r.eglError;
EXPECT_EQ(r.fenceType, EGL_SYNC_NATIVE_FENCE_ANDROID);
EXPECT_EQ(r.fenceWaited, 1);
EXPECT_EQ(r.fenceSignaled, 1);
if (r.dupFd >= 0) {
// A sync_file of the work, which signals; imported back it is waited on.
EXPECT_EQ(r.dupSignaled, 1);
EXPECT_EQ(r.importCreated, 1);
EXPECT_EQ(r.importWaited, 1);
} else {
// A server that cannot export fences (a host driver without them) waits the work out and
// has no descriptor: the copy is refused by name, not answered with garbage.
EXPECT_EQ(r.dupError, EGL_BAD_PARAMETER);
}
std::error_code ec;
if (!::testing::Test::HasFailure()) {
std::filesystem::remove(Debug::RoleLogPath(clientLog.c_str(), Debug::LogRole::Client), ec);
std::filesystem::remove(Debug::RoleLogPath(clientLog.c_str(), Debug::LogRole::Server), ec);
std::filesystem::remove(clientLog, ec);
}
}