2f5618ad08180cdb691009d5a702020d2ac955d4
[glsdk/omapdrmtest.git] / util / display-wayland.c
1 /*
2  * Copyright (C) 2011 Texas Instruments
3  * Author: Nikhil Devshatwar <nikhil.nd@ti.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include "util.h"
24 #include <xf86drmMode.h>
25 #include <wayland-client.h>
26 #include <wayland-drm-client-protocol.h>
27 #include <wayland-scaler-client-protocol.h>
29 /* NOTE: healthy dose of recycling from libdrm modetest app.. */
32 #define to_display_wl(x) container_of(x, struct display_wl, base)
33 struct display_wl {
34         struct display base;
36         uint32_t bo_flags;
37         struct wl_display *display;
38         struct wl_compositor *compositor;
39         struct wl_surface *surface;
40         struct wl_shell *shell;
41         struct wl_drm *drm;
42         struct wl_viewport *viewport;
43         struct wl_scaler *scaler;
44 };
46 #define to_buffer_wl(x) container_of(x, struct buffer_wl, base)
47 struct buffer_wl {
48         struct buffer base;
50         struct wl_buffer *wl_buf;
51         uint32_t name;
52 };
54 static int global_fd = 0;
55 static uint32_t used_planes = 0;
56 static int ndisplays = 0;
58 static struct omap_bo *
59 alloc_bo(struct display *disp, uint32_t bpp, uint32_t width, uint32_t height,
60                 uint32_t *bo_handle, uint32_t *pitch)
61 {
62         struct display_wl *disp_wl = to_display_wl(disp);
63         struct omap_bo *bo;
64         uint32_t bo_flags = disp_wl->bo_flags;
66         bo_flags |= OMAP_BO_WC;
67         bo = omap_bo_new(disp->dev, width * height * bpp / 8, bo_flags);
69         if (bo) {
70                 *bo_handle = omap_bo_handle(bo);
71                 *pitch = width * bpp / 8;
72                 if (bo_flags & OMAP_BO_TILED)
73                         *pitch = ALIGN2(*pitch, PAGE_SHIFT);
74         }
76         return bo;
77 }
79 static struct buffer *
80 alloc_buffer(struct display *disp, uint32_t fourcc, uint32_t w, uint32_t h)
81 {
82         struct display_wl *disp_wl = to_display_wl(disp);
83         struct buffer_wl *buf_wl;
84         struct buffer *buf;
85         uint32_t bo_handles[4] = {0};
86         uint32_t wl_fmt = WL_DRM_FORMAT_NV12;
88         buf_wl = calloc(1, sizeof(*buf_wl));
89         if (!buf_wl) {
90                 ERROR("allocation failed");
91                 return NULL;
92         }
93         buf = &buf_wl->base;
95         buf->fourcc = fourcc;
96         buf->width = w;
97         buf->height = h;
98         buf->multiplanar = false;
100         buf->nbo = 1;
102         if (!fourcc)
103                 fourcc = FOURCC('A','R','2','4');
105         switch (fourcc) {
106         case FOURCC('A','R','2','4'):
107                 wl_fmt = WL_DRM_FORMAT_ARGB8888;
108                 buf->nbo = 1;
109                 buf->bo[0] = alloc_bo(disp, 32, buf->width, buf->height,
110                                 &bo_handles[0], &buf->pitches[0]);
111                 omap_bo_get_name(buf->bo[0], &buf_wl->name);
112                 buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
114                 /* ARGB: stride is four times width */
115                 buf_wl->wl_buf = wl_drm_create_buffer(disp_wl->drm,
116                         buf_wl->name, buf->width, buf->height,
117                         buf->width * 4, wl_fmt);
118                 break;
119         case FOURCC('U','Y','V','Y'):
120         case FOURCC('Y','U','Y','V'):
121                 wl_fmt = WL_DRM_FORMAT_YUYV;
122                 buf->nbo = 1;
123                 buf->bo[0] = alloc_bo(disp, 16, buf->width, buf->height,
124                                 &bo_handles[0], &buf->pitches[0]);
126                 omap_bo_get_name(buf->bo[0], &buf_wl->name);
127                 buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
128                 /* YUYV; stride is double width */
129                 buf_wl->wl_buf = wl_drm_create_buffer(disp_wl->drm,
130                         buf_wl->name, buf->width, buf->height,
131                         buf->width * 2, wl_fmt);
132                 break;
133         case FOURCC('N','V','1','2'):
134                 wl_fmt = WL_DRM_FORMAT_NV12;
135                 buf->nbo = 1;
136                 buf->bo[0] = alloc_bo(disp, 8, buf->width, buf->height * 3 / 2,
137                                 &bo_handles[0], &buf->pitches[0]);
139                 omap_bo_get_name(buf->bo[0], &buf_wl->name);
140                 buf->fd[0] = omap_bo_dmabuf(buf->bo[0]);
141                 /* NV12: Create a planar buffer */
142                 buf_wl->wl_buf = wl_drm_create_planar_buffer(disp_wl->drm,
143                         buf_wl->name, buf->width, buf->height, wl_fmt,
144                         0, buf->width,
145                         buf->width * buf->height, buf->width,
146                         0, 0);
147                 break;
148         default:
149                 ERROR("invalid format: 0x%08x", fourcc);
150                 goto fail;
151         }
153         return buf;
155 fail:
156         // XXX cleanup
157         return NULL;
160 static void
161 free_buffers(struct display *disp, uint32_t n)
163         uint32_t i;
164         for (i = 0; i < n; i++) {
165                 if (disp->buf[i]) {
166                         close(disp->buf[i]->fd[0]);
167                         omap_bo_del(disp->buf[i]->bo[0]);
168                         if (disp->multiplanar) {
169                                 close(disp->buf[i]->fd[1]);
170                                 omap_bo_del(disp->buf[i]->bo[1]);
171                         }
172                 }
173         }
174         free(disp->buf);
177 static struct buffer **
178 alloc_buffers(struct display *disp, uint32_t n,
179                 uint32_t fourcc, uint32_t w, uint32_t h)
181         struct buffer **bufs;
182         uint32_t i = 0;
184         bufs = calloc(n, sizeof(*bufs));
185         if (!bufs) {
186                 ERROR("allocation failed");
187                 goto fail;
188         }
190         for (i = 0; i < n; i++) {
191                 bufs[i] = alloc_buffer(disp, fourcc, w, h);
192                 if (!bufs[i]) {
193                         ERROR("allocation failed");
194                         goto fail;
195                 }
196         }
197         disp->buf = bufs;
198         return bufs;
200 fail:
201         // XXX cleanup
202         return NULL;
205 static struct buffer **
206 get_buffers(struct display *disp, uint32_t n)
208         return alloc_buffers(disp, n, 0, disp->width, disp->height);
211 static struct buffer **
212 get_vid_buffers(struct display *disp, uint32_t n,
213                 uint32_t fourcc, uint32_t w, uint32_t h)
215         return alloc_buffers(disp, n, fourcc, w, h);
218 static int
219 post_buffer(struct display *disp, struct buffer *buf)
221         ERROR("post_buffer not supported");
223         return -1;
226 static int
227 post_vid_buffer(struct display *disp, struct buffer *buf,
228                 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
230         struct display_wl *disp_wl = to_display_wl(disp);
231         struct buffer_wl *buf_wl = to_buffer_wl(buf);
232         int ret = 0;
234         wl_surface_attach(disp_wl->surface, buf_wl->wl_buf, 0, 0);
235         wl_surface_commit(disp_wl->surface);
236         wl_display_flush(disp_wl->display);
238         return ret;
241 static void
242 close_kms(struct display *disp)
244         omap_device_del(disp->dev);
245         disp->dev = NULL;
246         if (used_planes) {
247                 used_planes >>= 1;
248         }
249         if (--ndisplays == 0) {
250                 close(global_fd);
251         }
254 void
255 disp_wayland_usage(void)
257         MSG("WAYLAND Display Options:");
258         MSG("\t-w <width>x<height>\tset the dimensions of client window");
261 static void
262 registry_handle_global(void *data, struct wl_registry *registry, uint32_t id,
263         const char *interface, uint32_t version)
265         struct display_wl *disp_wl = (struct display_wl *) data;
267         if (strcmp(interface, "wl_compositor") == 0) {
268                 disp_wl->compositor = wl_registry_bind(registry, id,
269                         &wl_compositor_interface, 3);
270         } else if (strcmp(interface, "wl_shell") == 0) {
271                 disp_wl->shell = wl_registry_bind(registry, id,
272                         &wl_shell_interface, 1);
273         } else if (strcmp(interface, "wl_drm") == 0) {
274                 disp_wl->drm = wl_registry_bind(registry, id,
275                         &wl_drm_interface, 1);
276         } else if (strcmp(interface, "wl_scaler") == 0) {
277                 disp_wl->scaler = wl_registry_bind(registry, id,
278                         &wl_scaler_interface, 2);
279         }
282 static void
283 registry_handle_global_remove(void *data, struct wl_registry *registry,
284                 uint32_t name)
287 static const struct wl_registry_listener registry_listener = {
288         registry_handle_global,
289         registry_handle_global_remove
290 };
293 struct display *
294 disp_wayland_open(int argc, char **argv)
296         struct display_wl *disp_wl = NULL;
297         struct display *disp;
298         int i, enabled = 0, width, height;
300         struct wl_registry *registry;
301         struct wl_shell_surface *shell_surface;
302         struct wl_region *region;
304         /* note: set args to NULL after we've parsed them so other modules know
305          * that it is already parsed (since the arg parsing is decentralized)
306          */
307         for (i = 1; i < argc; i++) {
308                 if (!argv[i]) {
309                         continue;
310                 }
311                 if (!strcmp("-w", argv[i])) {
312                         argv[i++] = NULL;
313                         if (sscanf(argv[i], "%dx%d", &width, &height) != 2) {
314                                 ERROR("invalid arg: -w %s", argv[i]);
315                                 goto fail;
316                         }
317                         enabled = 1;
318                 } else {
319                         /* ignore */
320                         continue;
321                 }
322                 argv[i] = NULL;
323         }
324         // If not explicitely enabled from command line, fail.
325         if (!enabled)
326                 goto fail;
328         disp_wl = calloc(1, sizeof(*disp_wl));
329         if (!disp_wl) {
330                 ERROR("allocation failed");
331                 goto fail;
332         }
333         disp = &disp_wl->base;
335         if (!global_fd) {
336                 global_fd = drmOpen("omapdrm", NULL);
337                 if (global_fd < 0) {
338                         ERROR("could not open drm device: %s (%d)",
339                                 strerror(errno), errno);
340                         goto fail;
341                 }
342         }
344         disp->fd = global_fd;
345         ndisplays++;
347         disp->dev = omap_device_new(disp->fd);
348         if (!disp->dev) {
349                 ERROR("couldn't create device");
350                 goto fail;
351         }
353         disp->get_buffers = get_buffers;
354         disp->get_vid_buffers = get_vid_buffers;
355         disp->post_buffer = post_buffer;
356         disp->post_vid_buffer = post_vid_buffer;
357         disp->close = close_kms;
358         disp->disp_free_buf = free_buffers;
360         disp->multiplanar = false;
361         disp->width = width;
362         disp->height = height;
364         disp_wl->bo_flags = OMAP_BO_SCANOUT|OMAP_BO_WC;
366         disp_wl->display = wl_display_connect(NULL);
367         if (disp_wl->display == NULL) {
368                 ERROR("failed to connect to Wayland display: %m\n");
369                 goto fail;
370         } else {
371                 MSG("wayland display opened\n");
372         }
374         /* Find out what interfaces are implemented, and initialize */
375         registry = wl_display_get_registry(disp_wl->display);
376         wl_registry_add_listener(registry, &registry_listener, disp_wl);
377         wl_display_roundtrip(disp_wl->display);
378         MSG("wayland registries obtained\n");
380         disp_wl->surface = wl_compositor_create_surface(disp_wl->compositor);
381         shell_surface = wl_shell_get_shell_surface(disp_wl->shell,
382                                                 disp_wl->surface);
383         wl_shell_surface_set_toplevel(shell_surface);
385         region = wl_compositor_create_region(disp_wl->compositor);
386         wl_region_add(region, 0, 0, disp->width, disp->height);
388         disp_wl->viewport = wl_scaler_get_viewport(disp_wl->scaler,
389                                                 disp_wl->surface);
390         wl_viewport_set_destination(disp_wl->viewport, width, height);
392         return disp;
394 fail:
395         // XXX cleanup
396         return NULL;