[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
@@ -196,6 +196,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// the exportable semaphore, and its sync_file is published to each as a write and a read.
// From then on an image's first acquire per frame also waits for its pending reads.
Bool PublishSharedImageAccesses();
// EGL_ANDROID_native_fence_sync's fence command (BackendObject::ExportNativeFence): the
// submission's sync_file in `*fence` (-1: none, and the work is waited out here), and - for
// a session that writes images - the boundary PublishSharedImageAccesses makes.
Bool ExportNativeFence(int* fence);
// EGL_BUFFER_AGE_EXT of the active target's default framebuffer for the next frame: the age
// of the swapchain image acquired for it (SwapchainObject::BufferAgeOf). Asking makes the
// target keep its presented images' content from then on.
@@ -726,8 +730,10 @@ namespace MobileGL::MG_Backend::DirectVulkan {
// m_sharedImageSyncFd: the device imports and exports SYNC_FD binary semaphores; without
// it the waits happen on the CPU and nothing is exported (see WireSharedImage.inc).
Bool m_sharedImageSyncFd = false;
// The session ends frames at publishing flushes (PublishSharedImageAccesses): it writes the
// images it acquires, so a first acquire waits for the image's pending reads as well.
// The session writes images - it ends frames at publishing flushes
// (PublishSharedImageAccesses), or it reached one through a framebuffer (ResolveWireImage):
// a first acquire waits for the image's pending reads as well, and its boundaries publish
// what it used as written.
Bool m_sharedImageImplicitSync = false;
PFN_vkImportSemaphoreFdKHR m_importSemaphoreFd = nullptr;
PFN_vkGetSemaphoreFdKHR m_getSemaphoreFd = nullptr;
@@ -161,7 +161,12 @@
auto* resource = m_textureManager->SyncTextureResourceByHandle(out.storage, renderbuffer);
if (!resource) MagmaWireFatal("framebuffer-resource");
// Another session writes it: acquired (WireSharedImage.inc) before the layout is read below.
if (resource->sharedImageId != 0) AcquireSharedImage(*resource);
// An image reached through a framebuffer is one this session renders into (or copies
// through), which makes it a writer of images from now on.
if (resource->sharedImageId != 0) {
m_sharedImageImplicitSync = true;
AcquireSharedImage(*resource);
}
out.image = resource->image;
out.format = viewFormat != VK_FORMAT_UNDEFINED ? viewFormat : resource->format;
out.extent = {std::max(resource->extent.width >> level, 1u),
@@ -625,6 +625,47 @@
return true;
}
// THE NATIVE FENCE. A sync_file of everything the session submitted so far, for the client's
// EGL_ANDROID_native_fence_sync: the application hands the descriptor on (Chrome's GPU process,
// to the browser that commits the buffer), so it has to signal no earlier than the last of that
// work. With nothing recorded, an empty
// recording is submitted to carry the signal: the in-order queue runs it after every earlier
// submission, and it is tracked like any other (the semaphore's reuse waits for it).
Bool VulkanRenderer::ExportNativeFence(int* fence) {
*fence = -1;
if (m_device == VK_NULL_HANDLE || m_graphicsQueue == VK_NULL_HANDLE || m_frameContext.GetFrameCount() == 0)
return true; // nothing was ever submitted: nothing to fence
// A session that writes images ends its frame here, as at a flush; a compositor that only
// samples them keeps its reads for its present, which follows the fence's work anyway.
const Bool boundary = m_sharedImageImplicitSync;
if (boundary) ReleaseHeldSharedImages();
if (m_sharedImageSyncFd && m_sharedImageSignal == VK_NULL_HANDLE)
m_sharedImageSignal = TakeSharedImageSemaphore(/*exportable=*/true);
m_sharedImageSignalSubmitted = VK_NULL_HANDLE;
if (!HasPendingRecordedWork() && m_sharedImageSignal != VK_NULL_HANDLE)
m_frameContext.BeginCommandRecording();
const Bool submitted = HasPendingRecordedWork() && FlushPendingCommands();
if (HasPendingRecordedWork()) MGLOG_E_ONCE("Magma: a native fence's commands could not be submitted");
const VkSemaphore signal =
submitted ? std::exchange(m_sharedImageSignalSubmitted, VK_NULL_HANDLE) : VK_NULL_HANDLE;
int exported = -1;
if (signal == VK_NULL_HANDLE || !ExportSharedImageSignal(signal, exported)) exported = -1;
if (exported < 0) {
// No descriptor: the work is waited out, so the caller may report it completed.
if (!WaitForSubmitsUpTo(m_submitCounter, UINT64_MAX))
MGLOG_E_ONCE("Magma: a native fence's work could not be waited for");
}
if (boundary && m_sharedImageReads.Pending() != 0) {
int published = -1;
#if !defined(_WIN32)
if (exported >= 0) published = ::dup(exported);
#endif
m_sharedImageReads.PublishFrameAsWrite(published);
}
*fence = exported;
return true;
}
void VulkanRenderer::PublishSharedImageReads() {
const VkSemaphore signal = std::exchange(m_sharedImageSignalSubmitted, VK_NULL_HANDLE);
int fence = -1;