From 58e030c23402e2ea7a5e7627daabe1e68f1d8130 Mon Sep 17 00:00:00 2001
From: Brendan King <Brendan.King@imgtec.com>
Date: Mon, 28 Jun 2021 16:36:15 +0100
Subject: [PATCH] egl/null: introduce NULL_DRM_DISPLAY

Introduce the NULL_DRM_DISPLAY environment variable, which allows
a particular DRM display to be selected, rather than the first
suitable DRM device found.

To select a particular display, NULL_DRM_DISPLAY should be set to
the card number (i.e. minor number) of the DRM device representing
the display. For example, NULL_DRM_DISPLAY=2 will select
/dev/dri/card2.

---
 src/egl/drivers/dri2/platform_null.c | 65 ++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 19 deletions(-)

diff --git a/src/egl/drivers/dri2/platform_null.c b/src/egl/drivers/dri2/platform_null.c
index 0d81151..c9409c0 100644
--- a/src/egl/drivers/dri2/platform_null.c
+++ b/src/egl/drivers/dri2/platform_null.c
@@ -1819,6 +1819,8 @@ dri2_null_try_device(_EGLDisplay *disp)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
 
+   dri2_dpy->fd = -1;
+
    if (!dri2_null_device_is_kms(dri2_dpy->fd_dpy))
       return false;
 
@@ -1876,34 +1878,56 @@ dri2_null_try_device(_EGLDisplay *disp)
 }
 
 static bool
-dri2_null_probe_device(_EGLDisplay *disp)
+dri2_null_probe_device(_EGLDisplay *disp, unsigned minor)
 {
    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+   char *card_path;
 
-   dri2_dpy->fd = -1;
-   dri2_dpy->fd_dpy = -1;
+   if (asprintf(&card_path, DRM_DEV_NAME, DRM_DIR_NAME, minor) < 0)
+      goto cleanup;
 
-   for (unsigned i = 0; i <= NULL_CARD_MINOR_MAX; i++) {
-      char *card_path;
+   dri2_dpy->fd_dpy = loader_open_device(card_path);
+   free(card_path);
+   if (dri2_dpy->fd_dpy < 0)
+      goto cleanup;
 
-      if (asprintf(&card_path, DRM_DEV_NAME, DRM_DIR_NAME, i) < 0)
-         continue;
+   if (dri2_null_try_device(disp))
+      return true;
 
-      dri2_dpy->fd_dpy = loader_open_device(card_path);
-      free(card_path);
-      if (dri2_dpy->fd_dpy < 0)
-         continue;
+   close(dri2_dpy->fd_dpy);
 
-      if (dri2_null_try_device(disp))
-         return true;
+   if (dri2_dpy->fd >= 0 && dri2_dpy->fd != dri2_dpy->fd_dpy)
+      close(dri2_dpy->fd);
 
-      close(dri2_dpy->fd_dpy);
+cleanup:
+   dri2_dpy->fd_dpy = -1;
+   dri2_dpy->fd = -1;
 
-      if (dri2_dpy->fd >= 0 && dri2_dpy->fd != dri2_dpy->fd_dpy)
-         close(dri2_dpy->fd);
+   return false;
+}
+
+static bool
+dri2_null_probe_devices(_EGLDisplay *disp)
+{
+   const char *null_drm_display = getenv("NULL_DRM_DISPLAY");
+
+   if (null_drm_display) {
+      char *endptr;
+      long val = strtol(null_drm_display, &endptr, 10);
 
-      dri2_dpy->fd_dpy = -1;
-      dri2_dpy->fd = -1;
+      if (endptr != null_drm_display && !*endptr &&
+          val >= 0 && val <= NULL_CARD_MINOR_MAX) {
+         if (dri2_null_probe_device(disp, (unsigned)val))
+            return true;
+      } else {
+         _eglLog(_EGL_FATAL, "NULL_DRM_DISPLAY is invalid: %s",
+                 null_drm_display);
+      }
+   } else {
+      for (unsigned i = 0; i <= NULL_CARD_MINOR_MAX; i++) {
+         if (dri2_null_probe_device(disp, i))
+            return true;
+      }
    }
 
    return false;
@@ -2001,7 +2025,10 @@ dri2_initialize_null(_EGLDisplay *disp)
 
    disp->DriverData = (void *) dri2_dpy;
 
-   if (!dri2_null_probe_device(disp)) {
+   dri2_dpy->fd_dpy = -1;
+   dri2_dpy->fd = -1;
+
+   if (!dri2_null_probe_devices(disp)) {
       _eglError(EGL_NOT_INITIALIZED, "failed to load driver");
       goto cleanup;
    }
