PR #21042 opened by russelltg
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21042
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21042.patch

When a frame is exported to DRM, it may be written to to read to in an 
asyncronous fashion. Make sure, on unmap of a Vulkan frame that was mapped to 
DRM, to import any fences that were put on the dmabuf.

The context for myself here is I'm working on a vulkan-video based screen 
recorder, which allocates vulkan frames using ffmpeg, then exports them as DRM 
dmabuf to send to the compositor, which then writes to it.

Without this change, I see all sorts of frame corruption because the read from 
ffmpeg's vulkan filter happens before/during the compositor writing to it. 


>From 52c81b688fcc77f6274c64911c67ace14cb7f4ec Mon Sep 17 00:00:00 2001
From: Russell Greene <[email protected]>
Date: Fri, 28 Nov 2025 23:04:57 -0700
Subject: [PATCH] hwcontext_vulkan: add support for implict DRM sync for export

When a frame is exported to DRM, it may be written to to read to in an 
asyncronous fashion. Make sure, on unmap of a Vulkan frame that was mapped to 
DRM, to import any fences that were put on the dmabuf
---
 libavutil/hwcontext_vulkan.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/libavutil/hwcontext_vulkan.c b/libavutil/hwcontext_vulkan.c
index a2caaa0959..5b0607089c 100644
--- a/libavutil/hwcontext_vulkan.c
+++ b/libavutil/hwcontext_vulkan.c
@@ -3488,7 +3488,7 @@ fail:
 }
 
 static int vulkan_map_from_drm_frame_sync(AVHWFramesContext *hwfc, AVFrame 
*dst,
-                                          const AVFrame *src, int flags)
+                                          const AVDRMFrameDescriptor *desc, 
int flags)
 {
     int err;
     VkResult ret;
@@ -3498,8 +3498,6 @@ static int 
vulkan_map_from_drm_frame_sync(AVHWFramesContext *hwfc, AVFrame *dst,
     AVVulkanDeviceContext *hwctx = &p->p;
     FFVulkanFunctions *vk = &p->vkctx.vkfn;
 
-    const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->data[0];
-
 #ifdef DMA_BUF_IOCTL_EXPORT_SYNC_FILE
     if (p->vkctx.extensions & FF_VK_EXT_EXTERNAL_FD_SEM) {
         VkCommandBuffer cmd_buf;
@@ -3619,6 +3617,7 @@ static int vulkan_map_from_drm(AVHWFramesContext *hwfc, 
AVFrame *dst,
 {
     int err = 0;
     AVVkFrame *f;
+    const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->data[0];
 
     if ((err = vulkan_map_from_drm_frame_desc(hwfc, &f, src, flags)))
         return err;
@@ -3633,7 +3632,7 @@ static int vulkan_map_from_drm(AVHWFramesContext *hwfc, 
AVFrame *dst,
     if (err < 0)
         goto fail;
 
-    err = vulkan_map_from_drm_frame_sync(hwfc, dst, src, flags);
+    err = vulkan_map_from_drm_frame_sync(hwfc, dst, desc, flags);
     if (err < 0)
         return err;
 
@@ -4037,6 +4036,10 @@ static void vulkan_unmap_to_drm(AVHWFramesContext *hwfc, 
HWMapDescriptor *hwmap)
 {
     AVDRMFrameDescriptor *drm_desc = hwmap->priv;
 
+    /* on unmap from DRM, make sure to import sync objects so that we are 
sync'd with any work that was
+     * done on the buffer while exported. We don't know if who used the dmabuf 
did reads or writes, so protect against both */
+    vulkan_map_from_drm_frame_sync(hwfc, hwmap->source, drm_desc, 
AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_WRITE);
+
     for (int i = 0; i < drm_desc->nb_objects; i++)
         close(drm_desc->objects[i].fd);
 
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to