Leigh Scott 171eaca
From 3ead05d4587991b75dc704444b1b1365c08af5ce Mon Sep 17 00:00:00 2001
Leigh Scott 171eaca
From: Erik Kurzinger <ekurzinger@nvidia.com>
Leigh Scott 171eaca
Date: Fri, 14 Jul 2023 14:07:25 -0700
Leigh Scott 171eaca
Subject: [PATCH 1/2] egl-wayland: destroy all proxies before destroying their
Leigh Scott 171eaca
 queues
Leigh Scott 171eaca
Leigh Scott 171eaca
The latest version of libwayland-client.so will print a warning if a
Leigh Scott 171eaca
queue is destroyed while a proxy is still assigned to it. This warning
Leigh Scott 171eaca
is triggered during EGL termination by our linux_dmabuf object and the
Leigh Scott 171eaca
default dmabuf feedback object for the display. It is also triggered
Leigh Scott 171eaca
during EGLSurface destruction by the surface's dmabuf feedback object.
Leigh Scott 171eaca
That last object never gets destroyed at all, in fact.
Leigh Scott 171eaca
Leigh Scott 171eaca
This change ensures that the above objects are all destroyed *before*
Leigh Scott 171eaca
their queues.
Leigh Scott 171eaca
---
Leigh Scott 171eaca
 include/wayland-egldisplay.h |  1 +
Leigh Scott 171eaca
 src/wayland-egldisplay.c     | 17 +++++++++--------
Leigh Scott 171eaca
 src/wayland-eglsurface.c     |  4 ++++
Leigh Scott 171eaca
 3 files changed, 14 insertions(+), 8 deletions(-)
Leigh Scott 171eaca
Leigh Scott 171eaca
diff --git a/include/wayland-egldisplay.h b/include/wayland-egldisplay.h
Leigh Scott 171eaca
index 4fe0418..e038668 100644
Leigh Scott 171eaca
--- a/include/wayland-egldisplay.h
Leigh Scott 171eaca
+++ b/include/wayland-egldisplay.h
Leigh Scott 171eaca
@@ -186,6 +186,7 @@ typedef struct WlEventQueueRec {
Leigh Scott 171eaca
 } WlEventQueue;
Leigh Scott 171eaca
 
Leigh Scott 171eaca
 int WlEglRegisterFeedback(WlEglDmaBufFeedback *feedback);
Leigh Scott 171eaca
+void wlEglDestroyFeedback(WlEglDmaBufFeedback *feedback);
Leigh Scott 171eaca
 EGLBoolean wlEglIsValidNativeDisplayExport(void *data, void *nativeDpy);
Leigh Scott 171eaca
 EGLBoolean wlEglBindDisplaysHook(void *data, EGLDisplay dpy, void *nativeDpy);
Leigh Scott 171eaca
 EGLBoolean wlEglUnbindDisplaysHook(EGLDisplay dpy, void *nativeDpy);
Leigh Scott 171eaca
diff --git a/src/wayland-egldisplay.c b/src/wayland-egldisplay.c
Leigh Scott 171eaca
index 3907892..3e7d74e 100644
Leigh Scott 171eaca
--- a/src/wayland-egldisplay.c
Leigh Scott 171eaca
+++ b/src/wayland-egldisplay.c
Leigh Scott 171eaca
@@ -144,7 +144,7 @@ typedef caddr_t pointer_t;
Leigh Scott 171eaca
 typedef void *pointer_t;
Leigh Scott 171eaca
 #endif
Leigh Scott 171eaca
 
Leigh Scott 171eaca
-static void
Leigh Scott 171eaca
+void
Leigh Scott 171eaca
 wlEglDestroyFeedback(WlEglDmaBufFeedback *feedback)
Leigh Scott 171eaca
 {
Leigh Scott 171eaca
     wlEglFeedbackResetTranches(feedback);
Leigh Scott 171eaca
@@ -619,6 +619,9 @@ static EGLBoolean terminateDisplay(WlEglDisplay *display, EGLBoolean globalTeard
Leigh Scott 171eaca
      * destroy the display connection itself */
Leigh Scott 171eaca
     wlEglDestroyAllSurfaces(display);
Leigh Scott 171eaca
 
Leigh Scott 171eaca
+    wlEglDestroyFormatSet(&display->formatSet);
Leigh Scott 171eaca
+    wlEglDestroyFeedback(&display->defaultFeedback);
Leigh Scott 171eaca
+
Leigh Scott 171eaca
     if (!globalTeardown || display->ownNativeDpy) {
Leigh Scott 171eaca
         if (display->wlRegistry) {
Leigh Scott 171eaca
             wl_registry_destroy(display->wlRegistry);
Leigh Scott 171eaca
@@ -636,19 +639,17 @@ static EGLBoolean terminateDisplay(WlEglDisplay *display, EGLBoolean globalTeard
Leigh Scott 171eaca
             wp_presentation_destroy(display->wpPresentation);
Leigh Scott 171eaca
             display->wpPresentation = NULL;
Leigh Scott 171eaca
         }
Leigh Scott 171eaca
-        if (display->wlEventQueue) {
Leigh Scott 171eaca
-            wl_event_queue_destroy(display->wlEventQueue);
Leigh Scott 171eaca
-            display->wlEventQueue = NULL;
Leigh Scott 171eaca
-        }
Leigh Scott 171eaca
         if (display->wlDmaBuf) {
Leigh Scott 171eaca
             zwp_linux_dmabuf_v1_destroy(display->wlDmaBuf);
Leigh Scott 171eaca
             display->wlDmaBuf = NULL;
Leigh Scott 171eaca
         }
Leigh Scott 171eaca
+        /* all proxies using the queue must be destroyed first! */
Leigh Scott 171eaca
+        if (display->wlEventQueue) {
Leigh Scott 171eaca
+            wl_event_queue_destroy(display->wlEventQueue);
Leigh Scott 171eaca
+            display->wlEventQueue = NULL;
Leigh Scott 171eaca
+        }
Leigh Scott 171eaca
     }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
-    wlEglDestroyFormatSet(&display->formatSet);
Leigh Scott 171eaca
-    wlEglDestroyFeedback(&display->defaultFeedback);
Leigh Scott 171eaca
-
Leigh Scott 171eaca
     return EGL_TRUE;
Leigh Scott 171eaca
 }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
diff --git a/src/wayland-eglsurface.c b/src/wayland-eglsurface.c
Leigh Scott 171eaca
index decde98..7b9ddd7 100644
Leigh Scott 171eaca
--- a/src/wayland-eglsurface.c
Leigh Scott 171eaca
+++ b/src/wayland-eglsurface.c
Leigh Scott 171eaca
@@ -2149,6 +2149,8 @@ static EGLBoolean wlEglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
Leigh Scott 171eaca
         free(surface->attribs);
Leigh Scott 171eaca
     }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
+    wlEglDestroyFeedback(&surface->feedback);
Leigh Scott 171eaca
+
Leigh Scott 171eaca
     if (surface->presentFeedbackQueue != NULL) {
Leigh Scott 171eaca
         wl_event_queue_destroy(surface->presentFeedbackQueue);
Leigh Scott 171eaca
         surface->presentFeedbackQueue = NULL;
Leigh Scott 171eaca
@@ -2157,6 +2159,8 @@ static EGLBoolean wlEglDestroySurface(EGLDisplay dpy, EGLSurface eglSurface)
Leigh Scott 171eaca
         wl_callback_destroy(surface->throttleCallback);
Leigh Scott 171eaca
         surface->throttleCallback = NULL;
Leigh Scott 171eaca
     }
Leigh Scott 171eaca
+
Leigh Scott 171eaca
+    /* all proxies using the queue must be destroyed first! */
Leigh Scott 171eaca
     if (surface->wlEventQueue != NULL) {
Leigh Scott 171eaca
         wl_event_queue_destroy(surface->wlEventQueue);
Leigh Scott 171eaca
         surface->wlEventQueue = NULL;
Leigh Scott 171eaca
Leigh Scott 171eaca
From ea70449fd94b5f866ea6189bf4f41f7c230cccfa Mon Sep 17 00:00:00 2001
Leigh Scott 171eaca
From: Erik Kurzinger <ekurzinger@nvidia.com>
Leigh Scott 171eaca
Date: Fri, 14 Jul 2023 13:43:43 -0700
Leigh Scott 171eaca
Subject: [PATCH 2/2] egl-wayland: handle multiple tranches for the same device
Leigh Scott 171eaca
Leigh Scott 171eaca
To determine the set of format modifiers to use when creating a surface
Leigh Scott 171eaca
we will find the first tranche for our DRM device, or else the
Leigh Scott 171eaca
compositor's main device, and then look for our DRM format in that
Leigh Scott 171eaca
tranche's list of formats to find the modifier list. If our format is
Leigh Scott 171eaca
not in the list, then we won't use explicit modifiers when creating the
Leigh Scott 171eaca
surface.
Leigh Scott 171eaca
Leigh Scott 171eaca
However, some compositors will advertise multiple tranches for a given
Leigh Scott 171eaca
DRM device with a different set of formats in each one. Kwin is one
Leigh Scott 171eaca
example. In that case, the tranche we select above might not contain our
Leigh Scott 171eaca
format, but it could still be in one of the other tranches for the same
Leigh Scott 171eaca
device.
Leigh Scott 171eaca
Leigh Scott 171eaca
Instead of simply picking the first tranche for the desired device, with
Leigh Scott 171eaca
this change we will loop over the format list of each of that device's
Leigh Scott 171eaca
tranches until we find a tranche that contains the format we want.
Leigh Scott 171eaca
---
Leigh Scott 171eaca
 src/wayland-eglsurface.c | 20 +++++++++++++-------
Leigh Scott 171eaca
 1 file changed, 13 insertions(+), 7 deletions(-)
Leigh Scott 171eaca
Leigh Scott 171eaca
diff --git a/src/wayland-eglsurface.c b/src/wayland-eglsurface.c
Leigh Scott 171eaca
index 7b9ddd7..cd8ed1e 100644
Leigh Scott 171eaca
--- a/src/wayland-eglsurface.c
Leigh Scott 171eaca
+++ b/src/wayland-eglsurface.c
Leigh Scott 171eaca
@@ -1265,12 +1265,18 @@ EGLint wlEglHandleImageStreamEvents(WlEglSurface *surface)
Leigh Scott 171eaca
 }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
 static WlEglDmaBufFormatSet *
Leigh Scott 171eaca
-WlEglGetFormatSetForDev(WlEglDmaBufFeedback *feedback, dev_t dev)
Leigh Scott 171eaca
+WlEglGetFormatSetForDev(WlEglDmaBufFeedback *feedback, dev_t dev, uint32_t format)
Leigh Scott 171eaca
 {
Leigh Scott 171eaca
     /* find the dev_t in our feedback's list of tranches */
Leigh Scott 171eaca
     for (int i = 0; i < (int)feedback->numTranches; i++) {
Leigh Scott 171eaca
         if (feedback->tranches[i].drmDev == dev) {
Leigh Scott 171eaca
-            return &feedback->tranches[i].formatSet;
Leigh Scott 171eaca
+            /* check if this tranche contains our format */
Leigh Scott 171eaca
+            WlEglDmaBufFormatSet *formatSet = &feedback->tranches[i].formatSet;
Leigh Scott 171eaca
+            for (int j = 0; j < (int)formatSet->numFormats; ++j) {
Leigh Scott 171eaca
+                if (formatSet->dmaBufFormats[j].format == format) {
Leigh Scott 171eaca
+                    return formatSet;
Leigh Scott 171eaca
+                }
Leigh Scott 171eaca
+            }
Leigh Scott 171eaca
         }
Leigh Scott 171eaca
     }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
@@ -1354,7 +1360,7 @@ static EGLint create_surface_stream_local(WlEglSurface *surface)
Leigh Scott 171eaca
     EGLint err = EGL_SUCCESS;
Leigh Scott 171eaca
     EGLint numModifiers = 0;
Leigh Scott 171eaca
     EGLuint64KHR *modifiers = NULL;
Leigh Scott 171eaca
-    EGLint format;
Leigh Scott 171eaca
+    uint32_t format;
Leigh Scott 171eaca
     WlEglDmaBufFormatSet *formatSet = NULL;
Leigh Scott 171eaca
     WlEglDmaBufFeedback *feedback = NULL;
Leigh Scott 171eaca
 
Leigh Scott 171eaca
@@ -1395,10 +1401,10 @@ static EGLint create_surface_stream_local(WlEglSurface *surface)
Leigh Scott 171eaca
                 feedback = &display->defaultFeedback;
Leigh Scott 171eaca
             }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
-            formatSet = WlEglGetFormatSetForDev(feedback, display->devDpy->dev);
Leigh Scott 171eaca
+            formatSet = WlEglGetFormatSetForDev(feedback, display->devDpy->dev, format);
Leigh Scott 171eaca
             if (!formatSet) {
Leigh Scott 171eaca
                 /* try again and see if there is a matching tranche for the render node */
Leigh Scott 171eaca
-                formatSet = WlEglGetFormatSetForDev(feedback, display->devDpy->renderNode);
Leigh Scott 171eaca
+                formatSet = WlEglGetFormatSetForDev(feedback, display->devDpy->renderNode, format);
Leigh Scott 171eaca
             }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
             /*
Leigh Scott 171eaca
@@ -1407,14 +1413,14 @@ static EGLint create_surface_stream_local(WlEglSurface *surface)
Leigh Scott 171eaca
              * us to check if the main device supports the linear modifier.
Leigh Scott 171eaca
              */
Leigh Scott 171eaca
             if (!formatSet && display->primeRenderOffload) {
Leigh Scott 171eaca
-                formatSet = WlEglGetFormatSetForDev(feedback, feedback->mainDev);
Leigh Scott 171eaca
+                formatSet = WlEglGetFormatSetForDev(feedback, feedback->mainDev, format);
Leigh Scott 171eaca
             }
Leigh Scott 171eaca
         }
Leigh Scott 171eaca
 
Leigh Scott 171eaca
         /* grab the modifier array */
Leigh Scott 171eaca
         if (formatSet) {
Leigh Scott 171eaca
             for (int i = 0; i < (int)formatSet->numFormats; i++) {
Leigh Scott 171eaca
-                if (formatSet->dmaBufFormats[i].format == (uint32_t)format) {
Leigh Scott 171eaca
+                if (formatSet->dmaBufFormats[i].format == format) {
Leigh Scott 171eaca
                     modifiers = formatSet->dmaBufFormats[i].modifiers;
Leigh Scott 171eaca
                     numModifiers = formatSet->dmaBufFormats[i].numModifiers;
Leigh Scott 171eaca
                     break;