blob: 2ac2200562c2f5716402b8aa2247cf56c279bb09 [file] [log] [blame]
George Kiagiadakis53868982014-06-12 16:26:49 +02001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 * Copyright © 2014 Collabora Ltd.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25#include <config.h>
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdbool.h>
31#include <assert.h>
32#include <unistd.h>
33#include <sys/mman.h>
34#include <signal.h>
35#include <fcntl.h>
36
37#include <xf86drm.h>
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +000038#include <i915_drm.h>
39#include <intel_bufmgr.h>
40#include <drm_fourcc.h>
George Kiagiadakis53868982014-06-12 16:26:49 +020041
42#include <wayland-client.h>
Bryce Harrington0d1a6222016-02-11 16:42:49 -080043#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080044#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080045#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080046#include "linux-dmabuf-unstable-v1-client-protocol.h"
George Kiagiadakis53868982014-06-12 16:26:49 +020047
48struct display {
49 struct wl_display *display;
50 struct wl_registry *registry;
51 struct wl_compositor *compositor;
52 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080053 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahl57e48f02015-11-17 16:00:28 +080054 struct zwp_linux_dmabuf_v1 *dmabuf;
George Kiagiadakis53868982014-06-12 16:26:49 +020055 int xrgb8888_format_found;
56};
57
58struct buffer {
59 struct wl_buffer *buffer;
60 int busy;
61
62 int drm_fd;
63
64 drm_intel_bufmgr *bufmgr;
65 drm_intel_bo *bo;
66
67 uint32_t gem_handle;
68 int dmabuf_fd;
69 uint8_t *mmap;
70
71 int width;
72 int height;
73 int bpp;
74 unsigned long stride;
75};
76
77struct window {
78 struct display *display;
79 int width, height;
80 struct wl_surface *surface;
81 struct xdg_surface *xdg_surface;
82 struct buffer buffers[2];
83 struct buffer *prev_buffer;
84 struct wl_callback *callback;
85};
86
87static int running = 1;
88
89static void
90buffer_release(void *data, struct wl_buffer *buffer)
91{
92 struct buffer *mybuf = data;
93
94 mybuf->busy = 0;
95}
96
97static const struct wl_buffer_listener buffer_listener = {
98 buffer_release
99};
100
101static int
102drm_connect(struct buffer *my_buf)
103{
104 /* This won't work with card0 as we need to be authenticated; instead,
105 * boot with drm.rnodes=1 and use that. */
106 my_buf->drm_fd = open("/dev/dri/renderD128", O_RDWR);
107 if (my_buf->drm_fd < 0)
108 return 0;
109
110 my_buf->bufmgr = drm_intel_bufmgr_gem_init(my_buf->drm_fd, 32);
111 if (!my_buf->bufmgr)
112 return 0;
113
114 return 1;
115}
116
117static void
118drm_shutdown(struct buffer *my_buf)
119{
120 drm_intel_bufmgr_destroy(my_buf->bufmgr);
121 close(my_buf->drm_fd);
122}
123
124static int
125alloc_bo(struct buffer *my_buf)
126{
127 /* XXX: try different tiling modes for testing FB modifiers. */
128 uint32_t tiling = I915_TILING_NONE;
129
130 assert(my_buf->bufmgr);
131
132 my_buf->bo = drm_intel_bo_alloc_tiled(my_buf->bufmgr, "test",
133 my_buf->width, my_buf->height,
134 (my_buf->bpp / 8), &tiling,
135 &my_buf->stride, 0);
136
137 printf("buffer allocated w %d, h %d, stride %lu, size %lu\n",
138 my_buf->width, my_buf->height, my_buf->stride, my_buf->bo->size);
139
140 if (!my_buf->bo)
141 return 0;
142
143 if (tiling != I915_TILING_NONE)
144 return 0;
145
146 return 1;
147}
148
149static void
150free_bo(struct buffer *my_buf)
151{
152 drm_intel_bo_unreference(my_buf->bo);
153}
154
155static int
156map_bo(struct buffer *my_buf)
157{
158 if (drm_intel_gem_bo_map_gtt(my_buf->bo) != 0)
159 return 0;
160
161 my_buf->mmap = my_buf->bo->virtual;
162
163 return 1;
164}
165
166static void
167fill_content(struct buffer *my_buf)
168{
169 int x = 0, y = 0;
170 uint32_t *pix;
171
172 assert(my_buf->mmap);
173
174 for (y = 0; y < my_buf->height; y++) {
175 pix = (uint32_t *)(my_buf->mmap + y * my_buf->stride);
176 for (x = 0; x < my_buf->width; x++) {
177 *pix++ = (0xff << 24) | ((x % 256) << 16) |
178 ((y % 256) << 8) | 0xf0;
179 }
180 }
181}
182
183static void
184unmap_bo(struct buffer *my_buf)
185{
186 drm_intel_gem_bo_unmap_gtt(my_buf->bo);
187}
188
189static void
190create_succeeded(void *data,
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800191 struct zwp_linux_buffer_params_v1 *params,
George Kiagiadakis53868982014-06-12 16:26:49 +0200192 struct wl_buffer *new_buffer)
193{
194 struct buffer *buffer = data;
195
196 buffer->buffer = new_buffer;
197 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
198
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800199 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200200}
201
202static void
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800203create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
George Kiagiadakis53868982014-06-12 16:26:49 +0200204{
205 struct buffer *buffer = data;
206
207 buffer->buffer = NULL;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000208 running = 0;
George Kiagiadakis53868982014-06-12 16:26:49 +0200209
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800210 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200211
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800212 fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200213}
214
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800215static const struct zwp_linux_buffer_params_v1_listener params_listener = {
George Kiagiadakis53868982014-06-12 16:26:49 +0200216 create_succeeded,
217 create_failed
218};
219
220static int
221create_dmabuf_buffer(struct display *display, struct buffer *buffer,
222 int width, int height)
223{
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800224 struct zwp_linux_buffer_params_v1 *params;
George Kiagiadakis53868982014-06-12 16:26:49 +0200225 uint64_t modifier;
226 uint32_t flags;
227
228 if (!drm_connect(buffer)) {
229 fprintf(stderr, "drm_connect failed\n");
230 goto error;
231 }
232
233 buffer->width = width;
234 buffer->height = height;
235 buffer->bpp = 32; /* hardcoded XRGB8888 format */
236
237 if (!alloc_bo(buffer)) {
238 fprintf(stderr, "alloc_bo failed\n");
239 goto error1;
240 }
241
242 if (!map_bo(buffer)) {
243 fprintf(stderr, "map_bo failed\n");
244 goto error2;
245 }
246 fill_content(buffer);
247 unmap_bo(buffer);
248
249 if (drm_intel_bo_gem_export_to_prime(buffer->bo, &buffer->dmabuf_fd) != 0) {
250 fprintf(stderr, "drm_intel_bo_gem_export_to_prime failed\n");
251 goto error2;
252 }
253 if (buffer->dmabuf_fd < 0) {
254 fprintf(stderr, "error: dmabuf_fd < 0\n");
255 goto error2;
256 }
257
258 /* We now have a dmabuf! It should contain 2x2 tiles (i.e. each tile
259 * is 256x256) of misc colours, and be mappable, either as ARGB8888, or
260 * XRGB8888. */
261 modifier = 0;
262 flags = 0;
263
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800264 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
265 zwp_linux_buffer_params_v1_add(params,
266 buffer->dmabuf_fd,
267 0, /* plane_idx */
268 0, /* offset */
269 buffer->stride,
270 modifier >> 32,
271 modifier & 0xffffffff);
272 zwp_linux_buffer_params_v1_add_listener(params, &params_listener, buffer);
273 zwp_linux_buffer_params_v1_create(params,
274 buffer->width,
275 buffer->height,
276 DRM_FORMAT_XRGB8888,
277 flags);
George Kiagiadakis53868982014-06-12 16:26:49 +0200278
George Kiagiadakis53868982014-06-12 16:26:49 +0200279 return 0;
280
281error2:
282 free_bo(buffer);
283error1:
284 drm_shutdown(buffer);
285error:
286 return -1;
287}
288
289static void
290handle_configure(void *data, struct xdg_surface *surface,
291 int32_t width, int32_t height,
292 struct wl_array *states, uint32_t serial)
293{
294}
295
296static void
297handle_delete(void *data, struct xdg_surface *xdg_surface)
298{
299 running = 0;
300}
301
302static const struct xdg_surface_listener xdg_surface_listener = {
303 handle_configure,
304 handle_delete,
305};
306
307static struct window *
308create_window(struct display *display, int width, int height)
309{
310 struct window *window;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000311 int i;
312 int ret;
George Kiagiadakis53868982014-06-12 16:26:49 +0200313
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800314 window = zalloc(sizeof *window);
George Kiagiadakis53868982014-06-12 16:26:49 +0200315 if (!window)
316 return NULL;
317
318 window->callback = NULL;
319 window->display = display;
320 window->width = width;
321 window->height = height;
322 window->surface = wl_compositor_create_surface(display->compositor);
323
324 if (display->shell) {
325 window->xdg_surface =
326 xdg_shell_get_xdg_surface(display->shell,
327 window->surface);
328
329 assert(window->xdg_surface);
330
331 xdg_surface_add_listener(window->xdg_surface,
332 &xdg_surface_listener, window);
333
334 xdg_surface_set_title(window->xdg_surface, "simple-dmabuf");
335 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800336 zwp_fullscreen_shell_v1_present_surface(display->fshell,
337 window->surface,
338 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
339 NULL);
George Kiagiadakis53868982014-06-12 16:26:49 +0200340 } else {
341 assert(0);
342 }
343
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000344 for (i = 0; i < 2; ++i) {
345 ret = create_dmabuf_buffer(display, &window->buffers[i],
346 width, height);
347
348 if (ret < 0)
349 return NULL;
350 }
351
George Kiagiadakis53868982014-06-12 16:26:49 +0200352 return window;
353}
354
355static void
356destroy_window(struct window *window)
357{
358 int i;
359
360 if (window->callback)
361 wl_callback_destroy(window->callback);
362
363 for (i = 0; i < 2; i++) {
364 if (!window->buffers[i].buffer)
365 continue;
366
367 wl_buffer_destroy(window->buffers[i].buffer);
368 free_bo(&window->buffers[i]);
369 close(window->buffers[i].dmabuf_fd);
370 drm_shutdown(&window->buffers[i]);
371 }
372
373 if (window->xdg_surface)
374 xdg_surface_destroy(window->xdg_surface);
375 wl_surface_destroy(window->surface);
376 free(window);
377}
378
379static struct buffer *
380window_next_buffer(struct window *window)
381{
382 struct buffer *buffer;
George Kiagiadakis53868982014-06-12 16:26:49 +0200383
384 if (!window->buffers[0].busy)
385 buffer = &window->buffers[0];
386 else if (!window->buffers[1].busy)
387 buffer = &window->buffers[1];
388 else
389 return NULL;
390
George Kiagiadakis53868982014-06-12 16:26:49 +0200391 return buffer;
392}
393
394static const struct wl_callback_listener frame_listener;
395
396static void
397redraw(void *data, struct wl_callback *callback, uint32_t time)
398{
399 struct window *window = data;
400 struct buffer *buffer;
401
402 buffer = window_next_buffer(window);
403 if (!buffer) {
404 fprintf(stderr,
405 !callback ? "Failed to create the first buffer.\n" :
406 "Both buffers busy at redraw(). Server bug?\n");
407 abort();
408 }
409
410 /* XXX: would be nice to draw something that changes here... */
411
412 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
413 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
414
415 if (callback)
416 wl_callback_destroy(callback);
417
418 window->callback = wl_surface_frame(window->surface);
419 wl_callback_add_listener(window->callback, &frame_listener, window);
420 wl_surface_commit(window->surface);
421 buffer->busy = 1;
422}
423
424static const struct wl_callback_listener frame_listener = {
425 redraw
426};
427
428static void
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800429dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, uint32_t format)
George Kiagiadakis53868982014-06-12 16:26:49 +0200430{
431 struct display *d = data;
432
433 if (format == DRM_FORMAT_XRGB8888)
434 d->xrgb8888_format_found = 1;
435}
436
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800437static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
George Kiagiadakis53868982014-06-12 16:26:49 +0200438 dmabuf_format
439};
440
441static void
442xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
443{
444 xdg_shell_pong(shell, serial);
445}
446
447static const struct xdg_shell_listener xdg_shell_listener = {
448 xdg_shell_ping,
449};
450
451#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
452#ifdef static_assert
453static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
454 "Interface version doesn't match implementation version");
455#endif
456
457static void
458registry_handle_global(void *data, struct wl_registry *registry,
459 uint32_t id, const char *interface, uint32_t version)
460{
461 struct display *d = data;
462
463 if (strcmp(interface, "wl_compositor") == 0) {
464 d->compositor =
465 wl_registry_bind(registry,
466 id, &wl_compositor_interface, 1);
467 } else if (strcmp(interface, "xdg_shell") == 0) {
468 d->shell = wl_registry_bind(registry,
469 id, &xdg_shell_interface, 1);
470 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
471 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800472 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
George Kiagiadakis53868982014-06-12 16:26:49 +0200473 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800474 id, &zwp_fullscreen_shell_v1_interface, 1);
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800475 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
George Kiagiadakis53868982014-06-12 16:26:49 +0200476 d->dmabuf = wl_registry_bind(registry,
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800477 id, &zwp_linux_dmabuf_v1_interface, 1);
478 zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener, d);
George Kiagiadakis53868982014-06-12 16:26:49 +0200479 }
480}
481
482static void
483registry_handle_global_remove(void *data, struct wl_registry *registry,
484 uint32_t name)
485{
486}
487
488static const struct wl_registry_listener registry_listener = {
489 registry_handle_global,
490 registry_handle_global_remove
491};
492
493static struct display *
494create_display(void)
495{
496 struct display *display;
497
498 display = malloc(sizeof *display);
499 if (display == NULL) {
500 fprintf(stderr, "out of memory\n");
501 exit(1);
502 }
503 display->display = wl_display_connect(NULL);
504 assert(display->display);
505
506 /* XXX: fake, because the compositor does not yet advertise anything */
507 display->xrgb8888_format_found = 1;
508
509 display->registry = wl_display_get_registry(display->display);
510 wl_registry_add_listener(display->registry,
511 &registry_listener, display);
512 wl_display_roundtrip(display->display);
513 if (display->dmabuf == NULL) {
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800514 fprintf(stderr, "No zwp_linux_dmabuf global\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200515 exit(1);
516 }
517
518 wl_display_roundtrip(display->display);
519
520 if (!display->xrgb8888_format_found) {
521 fprintf(stderr, "DRM_FORMAT_XRGB8888 not available\n");
522 exit(1);
523 }
524
525 return display;
526}
527
528static void
529destroy_display(struct display *display)
530{
531 if (display->dmabuf)
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800532 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
George Kiagiadakis53868982014-06-12 16:26:49 +0200533
534 if (display->shell)
535 xdg_shell_destroy(display->shell);
536
537 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800538 zwp_fullscreen_shell_v1_release(display->fshell);
George Kiagiadakis53868982014-06-12 16:26:49 +0200539
540 if (display->compositor)
541 wl_compositor_destroy(display->compositor);
542
543 wl_registry_destroy(display->registry);
544 wl_display_flush(display->display);
545 wl_display_disconnect(display->display);
546 free(display);
547}
548
549static void
550signal_int(int signum)
551{
552 running = 0;
553}
554
555int
556main(int argc, char **argv)
557{
558 struct sigaction sigint;
559 struct display *display;
560 struct window *window;
561 int ret = 0;
562
563 display = create_display();
564 window = create_window(display, 250, 250);
565 if (!window)
566 return 1;
567
568 sigint.sa_handler = signal_int;
569 sigemptyset(&sigint.sa_mask);
570 sigint.sa_flags = SA_RESETHAND;
571 sigaction(SIGINT, &sigint, NULL);
572
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000573 /* Here we retrieve the linux-dmabuf objects, or error */
574 wl_display_roundtrip(display->display);
575
576 if (!running)
577 return 1;
George Kiagiadakis53868982014-06-12 16:26:49 +0200578
579 redraw(window, NULL, 0);
580
581 while (running && ret != -1)
582 ret = wl_display_dispatch(display->display);
583
584 fprintf(stderr, "simple-dmabuf exiting\n");
585 destroy_window(window);
586 destroy_display(display);
587
588 return 0;
589}