Blame SOURCES/d4937adc5cd04ac7df98fc5616e40319fb52fdee.patch

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