Blame SOURCES/d4937adc5cd04ac7df98fc5616e40319fb52fdee.patch

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