7387be9c5fee4a9750852aa8e41df318a0e275a4
[AGL/meta-agl-devel.git] / meta-agl-drm-lease / recipes-graphics / weston / weston / 0001-backend-drm-Add-method-to-import-DRM-fd.patch
1 From 9ce172053169bbfd27ef8c18eb50ebac348f5bc2 Mon Sep 17 00:00:00 2001
2 From: Damian Hobson-Garcia <dhobsong@igel.co.jp>
3 Date: Mon, 11 Apr 2022 18:50:45 +0900
4 Subject: [PATCH 1/3] backend-drm: Add method to import DRM fd
5
6 Allow the compositor to provide a file descriptor for a
7 DRM device.
8
9 This allows the compositor to bypass the launcher backends
10 and to get a DRM file descriptor from an external
11 resource manager, such as one that can create DRM leases,
12 and pass it to the DRM backend for use.
13
14 Having the DRM device management in the compositor allows for
15 integrating a platform specific resource manager without having
16 to add extra dependencies to the generic libweston code.
17
18 %% original patch: 0001-backend-drm-Add-method-to-import-DRM-fd.patch
19 ---
20  include/libweston/backend-drm.h |  7 +++
21  libweston/backend-drm/drm.c     | 75 ++++++++++++++++++++++++---------
22  2 files changed, 63 insertions(+), 19 deletions(-)
23
24 diff --git a/include/libweston/backend-drm.h b/include/libweston/backend-drm.h
25 index af2da4a..2c12b17 100644
26 --- a/include/libweston/backend-drm.h
27 +++ b/include/libweston/backend-drm.h
28 @@ -223,6 +223,13 @@ struct weston_drm_backend_config {
29  
30         /** Use shadow buffer if using Pixman-renderer. */
31         bool use_pixman_shadow;
32 +
33 +       /** DRM device file descriptor to use
34 +         *
35 +         * An openeded DRM device file descriptor.  If <0, open a DRM
36 +         * device in the backend using `specific_device` or heuristics.
37 +         */
38 +       int device_fd;
39  };
40  
41  #ifdef  __cplusplus
42 diff --git a/libweston/backend-drm/drm.c b/libweston/backend-drm/drm.c
43 index 4278770..0707db7 100644
44 --- a/libweston/backend-drm/drm.c
45 +++ b/libweston/backend-drm/drm.c
46 @@ -40,6 +40,7 @@
47  #include <linux/vt.h>
48  #include <assert.h>
49  #include <sys/mman.h>
50 +#include <sys/stat.h>
51  #include <time.h>
52  
53  #include <xf86drm.h>
54 @@ -2670,29 +2671,22 @@ drm_device_changed(struct weston_compositor *compositor,
55         wl_signal_emit(&compositor->session_signal, compositor);
56  }
57  
58 -/**
59 - * Determines whether or not a device is capable of modesetting. If successful,
60 - * sets b->drm.fd and b->drm.filename to the opened device.
61 - */
62  static bool
63 -drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
64 +drm_backend_update_kms_device(struct drm_backend *b, struct udev_device *device,
65 +                const char *name, int drm_fd)
66  {
67 -       const char *filename = udev_device_get_devnode(device);
68         const char *sysnum = udev_device_get_sysnum(device);
69         dev_t devnum = udev_device_get_devnum(device);
70         drmModeRes *res;
71 -       int id = -1, fd;
72 +       int id = -1;
73  
74 -       if (!filename)
75 +       if (!name)
76                 return false;
77  
78 -       fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
79 -       if (fd < 0)
80 -               return false;
81  
82 -       res = drmModeGetResources(fd);
83 +       res = drmModeGetResources(drm_fd);
84         if (!res)
85 -               goto out_fd;
86 +               return false;
87  
88         if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
89             res->count_encoders <= 0)
90 @@ -2701,7 +2695,7 @@ drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
91         if (sysnum)
92                 id = atoi(sysnum);
93         if (!sysnum || id < 0) {
94 -               weston_log("couldn't get sysnum for device %s\n", filename);
95 +               weston_log("couldn't get sysnum for device %s\n", name);
96                 goto out_res;
97         }
98  
99 @@ -2711,9 +2705,9 @@ drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
100                 weston_launcher_close(b->compositor->launcher, b->drm.fd);
101         free(b->drm.filename);
102  
103 -       b->drm.fd = fd;
104 +       b->drm.fd = drm_fd;
105         b->drm.id = id;
106 -       b->drm.filename = strdup(filename);
107 +       b->drm.filename = strdup(name);
108         b->drm.devnum = devnum;
109  
110         drmModeFreeResources(res);
111 @@ -2722,11 +2716,33 @@ drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
112  
113  out_res:
114         drmModeFreeResources(res);
115 -out_fd:
116 -       weston_launcher_close(b->compositor->launcher, fd);
117         return false;
118  }
119  
120 +/**
121 + * Determines whether or not a device is capable of modesetting. If successful,
122 + * sets b->drm.fd and b->drm.filename to the opened device.
123 + */
124 +static bool
125 +drm_device_is_kms(struct drm_backend *b, struct udev_device *device)
126 +{
127 +       int fd;
128 +       const char *filename = udev_device_get_devnode(device);
129 +       if (!filename)
130 +               return false;
131 +
132 +       fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
133 +       if (fd < 0)
134 +               return false;
135 +
136 +       if (!drm_backend_update_kms_device(b, device, filename, fd)) {
137 +               weston_launcher_close(b->compositor->launcher, fd);
138 +               return false;
139 +       }
140 +
141 +       return true;
142 +}
143 +
144  /*
145   * Find primary GPU
146   * Some systems may have multiple DRM devices attached to a single seat. This
147 @@ -2814,6 +2830,25 @@ find_primary_gpu(struct drm_backend *b, const char *seat)
148         return drm_device;
149  }
150  
151 +static struct udev_device *
152 +import_drm_device_fd(struct drm_backend *b, int fd)
153 +{
154 +       struct udev_device *device;
155 +       struct stat s;
156 +
157 +       if (fstat(fd, &s) < 0 || !S_ISCHR(s.st_mode))
158 +               return NULL;
159 +
160 +       device = udev_device_new_from_devnum(b->udev, 'c', s.st_rdev);
161 +       if (!device)
162 +               return NULL;
163 +
164 +       if (!drm_backend_update_kms_device(b, device, "imported DRM device fd", fd))
165 +               return NULL;
166 +
167 +       return device;
168 +}
169 +
170  static struct udev_device *
171  open_specific_drm_device(struct drm_backend *b, const char *name)
172  {
173 @@ -3038,7 +3073,9 @@ drm_backend_create(struct weston_compositor *compositor,
174         b->session_listener.notify = session_notify;
175         wl_signal_add(&compositor->session_signal, &b->session_listener);
176  
177 -       if (config->specific_device)
178 +       if (config->device_fd > 0)
179 +               drm_device = import_drm_device_fd(b, config->device_fd);
180 +       else if (config->specific_device)
181                 drm_device = open_specific_drm_device(b, config->specific_device);
182         else
183                 drm_device = find_primary_gpu(b, seat_id);
184 -- 
185 2.17.1
186