Add agl-drm-lease feature
[AGL/meta-agl-devel.git] / meta-agl-drm-lease / recipes-graphics / kmscube / kmscube / 0002-Add-DRM-lease-support.patch
1 From 3a766fb852b53316f4b4c0b37ec801a5b188093b Mon Sep 17 00:00:00 2001
2 From: Damian Hobson-Garcia <dhobsong@igel.co.jp>
3 Date: Wed, 16 Dec 2020 16:21:57 +0900
4 Subject: [PATCH] Add DRM lease support
5
6 ---
7  drm-atomic.c      |  4 ++--
8  drm-common.c      | 21 +++++++++++++++++++--
9  drm-common.h      |  9 ++++++---
10  drm-legacy.c      |  4 ++--
11  kmscube.c         | 12 +++++++++---
12  meson.build       |  5 +++++
13  meson_options.txt |  5 +++++
14  texturator.c      |  2 +-
15  8 files changed, 49 insertions(+), 13 deletions(-)
16
17 diff --git a/drm-atomic.c b/drm-atomic.c
18 index d748772..8cb3ca5 100644
19 --- a/drm-atomic.c
20 +++ b/drm-atomic.c
21 @@ -337,12 +337,12 @@ static int get_plane_id(void)
22         return ret;
23  }
24  
25 -const struct drm * init_drm_atomic(const char *device, const char *mode_str, unsigned int vrefresh)
26 +const struct drm * init_drm_atomic(const char *device, const char *mode_str, unsigned int vrefresh, int lease)
27  {
28         uint32_t plane_id;
29         int ret;
30  
31 -       ret = init_drm(&drm, device, mode_str, vrefresh);
32 +       ret = init_drm(&drm, device, mode_str, vrefresh, lease);
33         if (ret)
34                 return NULL;
35  
36 diff --git a/drm-common.c b/drm-common.c
37 index b9d61c1..6e7d1ce 100644
38 --- a/drm-common.c
39 +++ b/drm-common.c
40 @@ -32,6 +32,10 @@
41  #include "common.h"
42  #include "drm-common.h"
43  
44 +#ifdef HAVE_DRM_LEASE
45 +#include "dlmclient.h"
46 +#endif
47 +
48  WEAK uint64_t
49  gbm_bo_get_modifier(struct gbm_bo *bo);
50  
51 @@ -169,6 +173,19 @@ static int get_resources(int fd, drmModeRes **resources)
52         return 0;
53  }
54  
55 +static int open_device(struct drm *drm, const char *device, int lease)
56 +{
57 +        if (!lease)
58 +                return open(device, O_RDWR);
59 +
60 +#ifdef HAVE_DRM_LEASE
61 +       drm->lease = dlm_get_lease(device);
62 +       if (drm->lease)
63 +           return dlm_lease_fd(drm->lease);
64 +#endif
65 +       return -1;
66 +}
67 +
68  #define MAX_DRM_DEVICES 64
69  
70  static int find_drm_device(drmModeRes **resources)
71 @@ -208,7 +225,7 @@ static int find_drm_device(drmModeRes **resources)
72         return fd;
73  }
74  
75 -int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned int vrefresh)
76 +int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned int vrefresh, int lease)
77  {
78         drmModeRes *resources;
79         drmModeConnector *connector = NULL;
80 @@ -216,7 +233,7 @@ int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned
81         int i, ret, area;
82  
83         if (device) {
84 -               drm->fd = open(device, O_RDWR);
85 +               drm->fd = open_device(drm, device, lease);
86                 ret = get_resources(drm->fd, &resources);
87                 if (ret < 0 && errno == EOPNOTSUPP)
88                         printf("%s does not look like a modeset device\n", device);
89 diff --git a/drm-common.h b/drm-common.h
90 index c4eb886..5119e79 100644
91 --- a/drm-common.h
92 +++ b/drm-common.h
93 @@ -51,6 +51,9 @@ struct connector {
94  struct drm {
95         int fd;
96  
97 +        /* only used for DRM lease */
98 +        struct dlm_lease *lease;
99 +
100         /* only used for atomic: */
101         struct plane *plane;
102         struct crtc *crtc;
103 @@ -73,8 +76,8 @@ struct drm_fb {
104  
105  struct drm_fb * drm_fb_get_from_bo(struct gbm_bo *bo);
106  
107 -int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned int vrefresh);
108 -const struct drm * init_drm_legacy(const char *device, const char *mode_str, unsigned int vrefresh);
109 -const struct drm * init_drm_atomic(const char *device, const char *mode_str, unsigned int vrefresh);
110 +int init_drm(struct drm *drm, const char *device, const char *mode_str, unsigned int vrefresh, int lease);
111 +const struct drm * init_drm_legacy(const char *device, const char *mode_str, unsigned int vrefresh, int lease);
112 +const struct drm * init_drm_atomic(const char *device, const char *mode_str, unsigned int vrefresh, int lease);
113  
114  #endif /* _DRM_COMMON_H */
115 diff --git a/drm-legacy.c b/drm-legacy.c
116 index 56a0fed..03db13d 100644
117 --- a/drm-legacy.c
118 +++ b/drm-legacy.c
119 @@ -122,11 +122,11 @@ static int legacy_run(const struct gbm *gbm, const struct egl *egl)
120         return 0;
121  }
122  
123 -const struct drm * init_drm_legacy(const char *device, const char *mode_str, unsigned int vrefresh)
124 +const struct drm * init_drm_legacy(const char *device, const char *mode_str, unsigned int vrefresh, int lease)
125  {
126         int ret;
127  
128 -       ret = init_drm(&drm, device, mode_str, vrefresh);
129 +       ret = init_drm(&drm, device, mode_str, vrefresh, lease);
130         if (ret)
131                 return NULL;
132  
133 diff --git a/kmscube.c b/kmscube.c
134 index f14c1d9..abb5bdd 100644
135 --- a/kmscube.c
136 +++ b/kmscube.c
137 @@ -41,12 +41,13 @@ static const struct egl *egl;
138  static const struct gbm *gbm;
139  static const struct drm *drm;
140  
141 -static const char *shortopts = "AD:M:m:V:v:";
142 +static const char *shortopts = "AD:LM:m:V:v:";
143  
144  static const struct option longopts[] = {
145         {"atomic", no_argument,       0, 'A'},
146         {"device", required_argument, 0, 'D'},
147         {"format", required_argument, 0, 'f'},
148 +       {"drm-lease", no_argument, 0, 'L'},
149         {"mode",   required_argument, 0, 'M'},
150         {"modifier", required_argument, 0, 'm'},
151         {"samples",  required_argument, 0, 's'},
152 @@ -62,6 +63,9 @@ static void usage(const char *name)
153                         "options:\n"
154                         "    -A, --atomic             use atomic modesetting and fencing\n"
155                         "    -D, --device=DEVICE      use the given device\n"
156 +#ifdef HAVE_DRM_LEASE
157 +                       "    -L, --drm-lease          the given device is the name of a DRM lease:\n"
158 +#endif
159                         "    -M, --mode=MODE          specify mode, one of:\n"
160                         "        smooth    -  smooth shaded cube (default)\n"
161                         "        rgba      -  rgba textured cube\n"
162 @@ -87,6 +89,7 @@ int main(int argc, char *argv[])
163         uint64_t modifier = DRM_FORMAT_MOD_LINEAR;
164         int samples = 0;
165         int atomic = 0;
166 +       int lease = 0;
167         int opt;
168         unsigned int len;
169         unsigned int vrefresh = 0;
170 @@ -119,6 +122,11 @@ int main(int argc, char *argv[])
171                                              fourcc[2], fourcc[3]);
172                         break;
173                 }
174 +#ifdef HAVE_DRM_LEASE
175 +               case 'L':
176 +                        lease = 1;
177 +                        break;
178 +#endif
179                 case 'M':
180                         if (strcmp(optarg, "smooth") == 0) {
181                                 mode = SMOOTH;
182 @@ -164,9 +170,9 @@ int main(int argc, char *argv[])
183         }
184  
185         if (atomic)
186 -               drm = init_drm_atomic(device, mode_str, vrefresh);
187 +               drm = init_drm_atomic(device, mode_str, vrefresh, lease);
188         else
189 -               drm = init_drm_legacy(device, mode_str, vrefresh);
190 +               drm = init_drm_legacy(device, mode_str, vrefresh, lease);
191         if (!drm) {
192                 printf("failed to initialize %s DRM\n", atomic ? "atomic" : "legacy");
193                 return -1;
194 diff --git a/meson.build b/meson.build
195 index df9c315..1b47a52 100644
196 --- a/meson.build
197 +++ b/meson.build
198 @@ -91,6 +91,11 @@ else
199    message('Building without gstreamer support')
200  endif
201  
202 +if get_option('drm_lease').enabled()
203 +  dep_common += dependency('libdlmclient')
204 +  add_project_arguments('-DHAVE_DRM_LEASE', language : 'c')
205 +endif
206 +
207  executable('kmscube', sources, dependencies : dep_common, install : true)
208  
209  
210 diff --git a/meson_options.txt b/meson_options.txt
211 index 1ed8abc..d932a1c 100644
212 --- a/meson_options.txt
213 +++ b/meson_options.txt
214 @@ -3,3 +3,8 @@ option(
215    type : 'feature',
216    description : 'Enable support for gstreamer and cube-video'
217  )
218 +option(
219 +  'drm_lease',
220 +  type : 'feature',
221 +  description : 'Enable support for running as a DRM lease client'
222 +)
223 diff --git a/texturator.c b/texturator.c
224 index 3d09e9e..9b87f0d 100644
225 --- a/texturator.c
226 +++ b/texturator.c
227 @@ -948,7 +948,7 @@ int main(int argc, char *argv[])
228         print_summary();
229  
230         /* no real need for atomic here: */
231 -       drm = init_drm_legacy(device, mode_str, vrefresh);
232 +       drm = init_drm_legacy(device, mode_str, vrefresh, 0);
233         if (!drm) {
234                 printf("failed to initialize DRM\n");
235                 return -1;