summarylogtreecommitdiffstats
path: root/0046-egl-null-introduce-NULL_DRM_DISPLAY.patch
blob: b6b635f66540ec259f3c6a7845c5a55a825ecbd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
From 0b1ae87824cf08199352af63f89f4cdec4c35485 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 e2a1383..7e631e0 100644
--- a/src/egl/drivers/dri2/platform_null.c
+++ b/src/egl/drivers/dri2/platform_null.c
@@ -1889,6 +1889,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;
 
@@ -1946,34 +1948,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;
@@ -2071,7 +2095,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;
    }