Blame SOURCES/d4937adc5cd04ac7df98fc5616e40319fb52fdee.patch

147450
From d4937adc5cd04ac7df98fc5616e40319fb52fdee Mon Sep 17 00:00:00 2001
147450
From: Austin Shafer <ashafer@nvidia.com>
147450
Date: Wed, 27 Oct 2021 06:37:07 -0400
147450
Subject: [PATCH] wayland: Fail eglGetDisplay if wl_drm is not available
147450
147450
This patch does two things:
147450
- checks if wl_drm is in use on the server, and uses it to get the name
147450
  of the drm device the compositor is driving.
147450
- Find an EGLDevice that matches the path returned by wl_drm.
147450
147450
If wl_drm and the needed extensions are not present, or if a matching
147450
EGLDevice is not found, then we fail. Right now we only support
147450
running on the same GPU as the compositor, so any of these being
147450
missing means that is not the case.
147450
---
147450
 src/wayland-egldisplay.c | 153 +++++++++++++++++++++++++++++++++++----
147450
 1 file changed, 138 insertions(+), 15 deletions(-)
147450
147450
diff --git a/src/wayland-egldisplay.c b/src/wayland-egldisplay.c
147450
index a0370a5..8b7394a 100644
147450
--- a/src/wayland-egldisplay.c
147450
+++ b/src/wayland-egldisplay.c
147450
@@ -29,13 +29,19 @@
147450
 #include "wayland-eglsurface.h"
147450
 #include "wayland-eglhandle.h"
147450
 #include "wayland-eglutils.h"
147450
+#include "wayland-drm-client-protocol.h"
147450
 #include <string.h>
147450
 #include <stdlib.h>
147450
 #include <assert.h>
147450
+#include <unistd.h>
147450
+#include <fcntl.h>
147450
 
147450
 typedef struct WlServerProtocolsRec {
147450
     EGLBoolean hasEglStream;
147450
     EGLBoolean hasDmaBuf;
147450
+    EGLBoolean hasDrm;
147450
+    struct wl_drm *wldrm;
147450
+    char *drm_name;
147450
 } WlServerProtocols;
147450
 
147450
 /* TODO: Make global display lists hang off platform data */
147450
@@ -241,6 +247,40 @@ static const struct wl_registry_listener registry_listener = {
147450
     registry_handle_global_remove
147450
 };
147450
 
147450
+static void wl_drm_device(void *data, struct wl_drm *wl_drm, const char *name)
147450
+{
147450
+    WlServerProtocols *protocols = (WlServerProtocols *)data;
147450
+    (void) wl_drm;
147450
+
147450
+    protocols->drm_name = strdup(name);
147450
+}
147450
+
147450
+static void wl_drm_authenticated(void *data, struct wl_drm *wl_drm)
147450
+{
147450
+    (void) data;
147450
+    (void) wl_drm;
147450
+}
147450
+static void wl_drm_format(void *data, struct wl_drm *wl_drm, uint32_t format)
147450
+{
147450
+    (void) data;
147450
+    (void) wl_drm;
147450
+    (void) format;
147450
+}
147450
+static void wl_drm_capabilities(void *data, struct wl_drm *wl_drm, uint32_t value)
147450
+{
147450
+    (void) data;
147450
+    (void) wl_drm;
147450
+    (void) value;
147450
+}
147450
+
147450
+static const struct wl_drm_listener drmListener = {
147450
+    .device = wl_drm_device,
147450
+    .authenticated = wl_drm_authenticated,
147450
+    .format = wl_drm_format,
147450
+    .capabilities = wl_drm_capabilities,
147450
+};
147450
+
147450
+
147450
 static void
147450
 registry_handle_global_check_protocols(
147450
                        void *data,
147450
@@ -262,6 +302,12 @@ registry_handle_global_check_protocols(
147450
         (version >= 3)) {
147450
         protocols->hasDmaBuf = EGL_TRUE;
147450
     }
147450
+
147450
+    if ((strcmp(interface, "wl_drm") == 0) && (version >= 2)) {
147450
+        protocols->hasDrm = EGL_TRUE;
147450
+        protocols->wldrm = wl_registry_bind(registry, name, &wl_drm_interface, 2);
147450
+        wl_drm_add_listener(protocols->wldrm, &drmListener, protocols);
147450
+    }
147450
 }
147450
 
147450
 static void
147450
@@ -389,8 +435,8 @@ EGLBoolean wlEglTerminateHook(EGLDisplay dpy)
147450
     return res;
147450
 }
147450
 
147450
-static void checkServerProtocols(struct wl_display *nativeDpy,
147450
-                                 WlServerProtocols *protocols)
147450
+static void getServerProtocolsInfo(struct wl_display *nativeDpy,
147450
+                                   WlServerProtocols *protocols)
147450
 {
147450
     struct wl_display     *wrapper      = NULL;
147450
     struct wl_registry    *wlRegistry   = NULL;
147450
@@ -418,6 +464,11 @@ static void checkServerProtocols(struct wl_display *nativeDpy,
147450
                                    protocols);
147450
     if (ret == 0) {
147450
         wl_display_roundtrip_queue(nativeDpy, queue);
147450
+        if (protocols->hasDrm) {
147450
+            wl_display_roundtrip_queue(nativeDpy, queue);
147450
+            /* destroy our wl_drm object */
147450
+            wl_drm_destroy(protocols->wldrm);
147450
+        }
147450
     }
147450
 
147450
     if (queue) {
147450
@@ -438,9 +489,13 @@ EGLDisplay wlEglGetPlatformDisplayExport(void *data,
147450
     WlServerProtocols      protocols;
147450
     EGLint                 numDevices      = 0;
147450
     int                    i               = 0;
147450
+    EGLDeviceEXT          *eglDeviceList   = NULL;
147450
     EGLDeviceEXT           eglDevice       = NULL;
147450
+    EGLDeviceEXT           tmpDev          = NULL;
147450
     EGLint                 err             = EGL_SUCCESS;
147450
     EGLBoolean             useInitRefCount = EGL_FALSE;
147450
+    const char *dev_exts;
147450
+    const char *dev_name;
147450
 
147450
     if (platform != EGL_PLATFORM_WAYLAND_EXT) {
147450
         wlEglSetError(data, EGL_BAD_PARAMETER);
147450
@@ -480,7 +535,6 @@ EGLDisplay wlEglGetPlatformDisplayExport(void *data,
147450
 
147450
     display = calloc(1, sizeof(*display));
147450
     if (!display) {
147450
-        wlExternalApiUnlock();
147450
         err = EGL_BAD_ALLOC;
147450
         goto fail;
147450
     }
147450
@@ -498,7 +552,6 @@ EGLDisplay wlEglGetPlatformDisplayExport(void *data,
147450
     if (!display->nativeDpy) {
147450
         display->nativeDpy = wl_display_connect(NULL);
147450
         if (!display->nativeDpy) {
147450
-            wlExternalApiUnlock();
147450
             err = EGL_BAD_ALLOC;
147450
             goto fail;
147450
         }
147450
@@ -508,26 +561,85 @@ EGLDisplay wlEglGetPlatformDisplayExport(void *data,
147450
     }
147450
 
147450
     memset(&protocols, 0, sizeof(protocols));
147450
-    checkServerProtocols(display->nativeDpy, &protocols);
147450
+    /*
147450
+     * This is where we check the supported protocols on the compositor,
147450
+     * and bind to wl_drm to get the device name.
147450
+     * protocols.drm_name will be allocated here if using wl_drm
147450
+     */
147450
+    getServerProtocolsInfo(display->nativeDpy, &protocols);
147450
 
147450
-    if (!protocols.hasEglStream && !protocols.hasDmaBuf) {
147450
-        wlExternalApiUnlock();
147450
-        goto fail;
147450
+    if (!protocols.hasDrm || (!protocols.hasEglStream && !protocols.hasDmaBuf)) {
147450
+        goto fail_cleanup_protocols;
147450
     }
147450
 
147450
-    if (!pData->egl.queryDevices(1, &eglDevice, &numDevices) || numDevices == 0) {
147450
-        wlExternalApiUnlock();
147450
-        goto fail;
147450
+    /* Get the number of devices available */
147450
+    if (!pData->egl.queryDevices(-1, NULL, &numDevices) || numDevices == 0) {
147450
+        goto fail_cleanup_protocols;
147450
+    }
147450
+
147450
+    eglDeviceList = calloc(numDevices, sizeof(*eglDeviceList));
147450
+    if (!eglDeviceList) {
147450
+        goto fail_cleanup_protocols;
147450
+    }
147450
+
147450
+    /*
147450
+     * Now we need to find an EGLDevice. If wl_drm is in use we will try to find one that
147450
+     * matches the device the compositor is using. We know that device is an nvidia device
147450
+     * since we just checked that above.
147450
+     */
147450
+    if (!pData->egl.queryDevices(numDevices, eglDeviceList, &numDevices) || numDevices == 0) {
147450
+        goto fail_cleanup_devices;
147450
     }
147450
+
147450
+    if (protocols.drm_name) {
147450
+        for (int i = 0; i < numDevices; i++) {
147450
+            tmpDev = eglDeviceList[i];
147450
+
147450
+            /*
147450
+             * To check against the wl_drm name, we need to check if we can use
147450
+             * the drm extension
147450
+             */
147450
+            dev_exts = display->data->egl.queryDeviceString(tmpDev,
147450
+                    EGL_EXTENSIONS);
147450
+            if (dev_exts) {
147450
+                if (wlEglFindExtension("EGL_EXT_device_drm_render_node", dev_exts)) {
147450
+                    dev_name =
147450
+                        display->data->egl.queryDeviceString(tmpDev,
147450
+                                EGL_DRM_RENDER_NODE_FILE_EXT);
147450
+
147450
+                    if (dev_name) {
147450
+                        /*
147450
+                         * At this point we have gotten the name from wl_drm, gotten
147450
+                         * the drm node from the EGLDevice. If they match, then
147450
+                         * this is the final device to use, since it is the compositor's
147450
+                         * device.
147450
+                         */
147450
+                        if (strcmp(dev_name, protocols.drm_name) == 0) {
147450
+                            eglDevice = eglDeviceList[0];
147450
+                            break;
147450
+                        }
147450
+                    }
147450
+                }
147450
+            }
147450
+        }
147450
+    }
147450
+
147450
+    /*
147450
+     * Right now we are pretty much limited to running on the same GPU as the
147450
+     * compositor. If we couldn't find an EGLDevice that has EGL_EXT_device_drm_render_node
147450
+     * and the same DRM device path, then fail.
147450
+     */
147450
+    if (!eglDevice) {
147450
+        goto fail_cleanup_devices;
147450
+    }
147450
+
147450
     display->devDpy = wlGetInternalDisplay(pData, eglDevice);
147450
     if (display->devDpy == NULL) {
147450
-        wlExternalApiUnlock();
147450
-        goto fail;
147450
+        goto fail_cleanup_devices;
147450
     }
147450
 
147450
     if (!wlEglInitializeMutex(&display->mutex)) {
147450
-        wlExternalApiUnlock();
147450
-        goto fail;
147450
+        goto fail_cleanup_devices;
147450
     }
147450
     display->refCount = 1;
147450
     WL_LIST_INIT(&display->wlEglSurfaceList);
147450
@@ -537,10 +649,21 @@ EGLDisplay wlEglGetPlatformDisplayExport(void *data,
147450
     // in wlEglDisplayList.
147450
     wl_list_insert(&wlEglDisplayList, &display->link);
147450
 
147450
+    free(eglDeviceList);
147450
+    if (protocols.drm_name) {
147450
+        free(protocols.drm_name);
147450
+    }
147450
     wlExternalApiUnlock();
147450
     return display;
147450
 
147450
+fail_cleanup_devices:
147450
+    free(eglDeviceList);
147450
+fail_cleanup_protocols:
147450
+    if (protocols.drm_name) {
147450
+        free(protocols.drm_name);
147450
+    }
147450
 fail:
147450
+    wlExternalApiUnlock();
147450
 
147450
     if (display->ownNativeDpy) {
147450
         wl_display_disconnect(display->nativeDpy);