blob: 0ceefebbacc325cbdd253ea548673d74221b23f0 [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>
Jonas Ådahl2a229332015-11-17 16:00:32 +080043#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080044#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080045#include "linux-dmabuf-unstable-v1-client-protocol.h"
George Kiagiadakis53868982014-06-12 16:26:49 +020046
47struct display {
48 struct wl_display *display;
49 struct wl_registry *registry;
50 struct wl_compositor *compositor;
51 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080052 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahl57e48f02015-11-17 16:00:28 +080053 struct zwp_linux_dmabuf_v1 *dmabuf;
George Kiagiadakis53868982014-06-12 16:26:49 +020054 int xrgb8888_format_found;
55};
56
57struct buffer {
58 struct wl_buffer *buffer;
59 int busy;
60
61 int drm_fd;
62
63 drm_intel_bufmgr *bufmgr;
64 drm_intel_bo *bo;
65
66 uint32_t gem_handle;
67 int dmabuf_fd;
68 uint8_t *mmap;
69
70 int width;
71 int height;
72 int bpp;
73 unsigned long stride;
74};
75
76struct window {
77 struct display *display;
78 int width, height;
79 struct wl_surface *surface;
80 struct xdg_surface *xdg_surface;
81 struct buffer buffers[2];
82 struct buffer *prev_buffer;
83 struct wl_callback *callback;
84};
85
86static int running = 1;
87
88static void
89buffer_release(void *data, struct wl_buffer *buffer)
90{
91 struct buffer *mybuf = data;
92
93 mybuf->busy = 0;
94}
95
96static const struct wl_buffer_listener buffer_listener = {
97 buffer_release
98};
99
100static int
101drm_connect(struct buffer *my_buf)
102{
103 /* This won't work with card0 as we need to be authenticated; instead,
104 * boot with drm.rnodes=1 and use that. */
105 my_buf->drm_fd = open("/dev/dri/renderD128", O_RDWR);
106 if (my_buf->drm_fd < 0)
107 return 0;
108
109 my_buf->bufmgr = drm_intel_bufmgr_gem_init(my_buf->drm_fd, 32);
110 if (!my_buf->bufmgr)
111 return 0;
112
113 return 1;
114}
115
116static void
117drm_shutdown(struct buffer *my_buf)
118{
119 drm_intel_bufmgr_destroy(my_buf->bufmgr);
120 close(my_buf->drm_fd);
121}
122
123static int
124alloc_bo(struct buffer *my_buf)
125{
126 /* XXX: try different tiling modes for testing FB modifiers. */
127 uint32_t tiling = I915_TILING_NONE;
128
129 assert(my_buf->bufmgr);
130
131 my_buf->bo = drm_intel_bo_alloc_tiled(my_buf->bufmgr, "test",
132 my_buf->width, my_buf->height,
133 (my_buf->bpp / 8), &tiling,
134 &my_buf->stride, 0);
135
136 printf("buffer allocated w %d, h %d, stride %lu, size %lu\n",
137 my_buf->width, my_buf->height, my_buf->stride, my_buf->bo->size);
138
139 if (!my_buf->bo)
140 return 0;
141
142 if (tiling != I915_TILING_NONE)
143 return 0;
144
145 return 1;
146}
147
148static void
149free_bo(struct buffer *my_buf)
150{
151 drm_intel_bo_unreference(my_buf->bo);
152}
153
154static int
155map_bo(struct buffer *my_buf)
156{
157 if (drm_intel_gem_bo_map_gtt(my_buf->bo) != 0)
158 return 0;
159
160 my_buf->mmap = my_buf->bo->virtual;
161
162 return 1;
163}
164
165static void
166fill_content(struct buffer *my_buf)
167{
168 int x = 0, y = 0;
169 uint32_t *pix;
170
171 assert(my_buf->mmap);
172
173 for (y = 0; y < my_buf->height; y++) {
174 pix = (uint32_t *)(my_buf->mmap + y * my_buf->stride);
175 for (x = 0; x < my_buf->width; x++) {
176 *pix++ = (0xff << 24) | ((x % 256) << 16) |
177 ((y % 256) << 8) | 0xf0;
178 }
179 }
180}
181
182static void
183unmap_bo(struct buffer *my_buf)
184{
185 drm_intel_gem_bo_unmap_gtt(my_buf->bo);
186}
187
188static void
189create_succeeded(void *data,
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800190 struct zwp_linux_buffer_params_v1 *params,
George Kiagiadakis53868982014-06-12 16:26:49 +0200191 struct wl_buffer *new_buffer)
192{
193 struct buffer *buffer = data;
194
195 buffer->buffer = new_buffer;
196 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
197
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800198 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200199}
200
201static void
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800202create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
George Kiagiadakis53868982014-06-12 16:26:49 +0200203{
204 struct buffer *buffer = data;
205
206 buffer->buffer = NULL;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000207 running = 0;
George Kiagiadakis53868982014-06-12 16:26:49 +0200208
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800209 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200210
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800211 fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200212}
213
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800214static const struct zwp_linux_buffer_params_v1_listener params_listener = {
George Kiagiadakis53868982014-06-12 16:26:49 +0200215 create_succeeded,
216 create_failed
217};
218
219static int
220create_dmabuf_buffer(struct display *display, struct buffer *buffer,
221 int width, int height)
222{
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800223 struct zwp_linux_buffer_params_v1 *params;
George Kiagiadakis53868982014-06-12 16:26:49 +0200224 uint64_t modifier;
225 uint32_t flags;
226
227 if (!drm_connect(buffer)) {
228 fprintf(stderr, "drm_connect failed\n");
229 goto error;
230 }
231
232 buffer->width = width;
233 buffer->height = height;
234 buffer->bpp = 32; /* hardcoded XRGB8888 format */
235
236 if (!alloc_bo(buffer)) {
237 fprintf(stderr, "alloc_bo failed\n");
238 goto error1;
239 }
240
241 if (!map_bo(buffer)) {
242 fprintf(stderr, "map_bo failed\n");
243 goto error2;
244 }
245 fill_content(buffer);
246 unmap_bo(buffer);
247
248 if (drm_intel_bo_gem_export_to_prime(buffer->bo, &buffer->dmabuf_fd) != 0) {
249 fprintf(stderr, "drm_intel_bo_gem_export_to_prime failed\n");
250 goto error2;
251 }
252 if (buffer->dmabuf_fd < 0) {
253 fprintf(stderr, "error: dmabuf_fd < 0\n");
254 goto error2;
255 }
256
257 /* We now have a dmabuf! It should contain 2x2 tiles (i.e. each tile
258 * is 256x256) of misc colours, and be mappable, either as ARGB8888, or
259 * XRGB8888. */
260 modifier = 0;
261 flags = 0;
262
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800263 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
264 zwp_linux_buffer_params_v1_add(params,
265 buffer->dmabuf_fd,
266 0, /* plane_idx */
267 0, /* offset */
268 buffer->stride,
269 modifier >> 32,
270 modifier & 0xffffffff);
271 zwp_linux_buffer_params_v1_add_listener(params, &params_listener, buffer);
272 zwp_linux_buffer_params_v1_create(params,
273 buffer->width,
274 buffer->height,
275 DRM_FORMAT_XRGB8888,
276 flags);
George Kiagiadakis53868982014-06-12 16:26:49 +0200277
George Kiagiadakis53868982014-06-12 16:26:49 +0200278 return 0;
279
280error2:
281 free_bo(buffer);
282error1:
283 drm_shutdown(buffer);
284error:
285 return -1;
286}
287
288static void
289handle_configure(void *data, struct xdg_surface *surface,
290 int32_t width, int32_t height,
291 struct wl_array *states, uint32_t serial)
292{
293}
294
295static void
296handle_delete(void *data, struct xdg_surface *xdg_surface)
297{
298 running = 0;
299}
300
301static const struct xdg_surface_listener xdg_surface_listener = {
302 handle_configure,
303 handle_delete,
304};
305
306static struct window *
307create_window(struct display *display, int width, int height)
308{
309 struct window *window;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000310 int i;
311 int ret;
George Kiagiadakis53868982014-06-12 16:26:49 +0200312
313 window = calloc(1, sizeof *window);
314 if (!window)
315 return NULL;
316
317 window->callback = NULL;
318 window->display = display;
319 window->width = width;
320 window->height = height;
321 window->surface = wl_compositor_create_surface(display->compositor);
322
323 if (display->shell) {
324 window->xdg_surface =
325 xdg_shell_get_xdg_surface(display->shell,
326 window->surface);
327
328 assert(window->xdg_surface);
329
330 xdg_surface_add_listener(window->xdg_surface,
331 &xdg_surface_listener, window);
332
333 xdg_surface_set_title(window->xdg_surface, "simple-dmabuf");
334 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800335 zwp_fullscreen_shell_v1_present_surface(display->fshell,
336 window->surface,
337 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
338 NULL);
George Kiagiadakis53868982014-06-12 16:26:49 +0200339 } else {
340 assert(0);
341 }
342
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000343 for (i = 0; i < 2; ++i) {
344 ret = create_dmabuf_buffer(display, &window->buffers[i],
345 width, height);
346
347 if (ret < 0)
348 return NULL;
349 }
350
George Kiagiadakis53868982014-06-12 16:26:49 +0200351 return window;
352}
353
354static void
355destroy_window(struct window *window)
356{
357 int i;
358
359 if (window->callback)
360 wl_callback_destroy(window->callback);
361
362 for (i = 0; i < 2; i++) {
363 if (!window->buffers[i].buffer)
364 continue;
365
366 wl_buffer_destroy(window->buffers[i].buffer);
367 free_bo(&window->buffers[i]);
368 close(window->buffers[i].dmabuf_fd);
369 drm_shutdown(&window->buffers[i]);
370 }
371
372 if (window->xdg_surface)
373 xdg_surface_destroy(window->xdg_surface);
374 wl_surface_destroy(window->surface);
375 free(window);
376}
377
378static struct buffer *
379window_next_buffer(struct window *window)
380{
381 struct buffer *buffer;
George Kiagiadakis53868982014-06-12 16:26:49 +0200382
383 if (!window->buffers[0].busy)
384 buffer = &window->buffers[0];
385 else if (!window->buffers[1].busy)
386 buffer = &window->buffers[1];
387 else
388 return NULL;
389
George Kiagiadakis53868982014-06-12 16:26:49 +0200390 return buffer;
391}
392
393static const struct wl_callback_listener frame_listener;
394
395static void
396redraw(void *data, struct wl_callback *callback, uint32_t time)
397{
398 struct window *window = data;
399 struct buffer *buffer;
400
401 buffer = window_next_buffer(window);
402 if (!buffer) {
403 fprintf(stderr,
404 !callback ? "Failed to create the first buffer.\n" :
405 "Both buffers busy at redraw(). Server bug?\n");
406 abort();
407 }
408
409 /* XXX: would be nice to draw something that changes here... */
410
411 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
412 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
413
414 if (callback)
415 wl_callback_destroy(callback);
416
417 window->callback = wl_surface_frame(window->surface);
418 wl_callback_add_listener(window->callback, &frame_listener, window);
419 wl_surface_commit(window->surface);
420 buffer->busy = 1;
421}
422
423static const struct wl_callback_listener frame_listener = {
424 redraw
425};
426
427static void
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800428dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, uint32_t format)
George Kiagiadakis53868982014-06-12 16:26:49 +0200429{
430 struct display *d = data;
431
432 if (format == DRM_FORMAT_XRGB8888)
433 d->xrgb8888_format_found = 1;
434}
435
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800436static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
George Kiagiadakis53868982014-06-12 16:26:49 +0200437 dmabuf_format
438};
439
440static void
441xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
442{
443 xdg_shell_pong(shell, serial);
444}
445
446static const struct xdg_shell_listener xdg_shell_listener = {
447 xdg_shell_ping,
448};
449
450#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
451#ifdef static_assert
452static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
453 "Interface version doesn't match implementation version");
454#endif
455
456static void
457registry_handle_global(void *data, struct wl_registry *registry,
458 uint32_t id, const char *interface, uint32_t version)
459{
460 struct display *d = data;
461
462 if (strcmp(interface, "wl_compositor") == 0) {
463 d->compositor =
464 wl_registry_bind(registry,
465 id, &wl_compositor_interface, 1);
466 } else if (strcmp(interface, "xdg_shell") == 0) {
467 d->shell = wl_registry_bind(registry,
468 id, &xdg_shell_interface, 1);
469 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
470 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800471 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
George Kiagiadakis53868982014-06-12 16:26:49 +0200472 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800473 id, &zwp_fullscreen_shell_v1_interface, 1);
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800474 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
George Kiagiadakis53868982014-06-12 16:26:49 +0200475 d->dmabuf = wl_registry_bind(registry,
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800476 id, &zwp_linux_dmabuf_v1_interface, 1);
477 zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener, d);
George Kiagiadakis53868982014-06-12 16:26:49 +0200478 }
479}
480
481static void
482registry_handle_global_remove(void *data, struct wl_registry *registry,
483 uint32_t name)
484{
485}
486
487static const struct wl_registry_listener registry_listener = {
488 registry_handle_global,
489 registry_handle_global_remove
490};
491
492static struct display *
493create_display(void)
494{
495 struct display *display;
496
497 display = malloc(sizeof *display);
498 if (display == NULL) {
499 fprintf(stderr, "out of memory\n");
500 exit(1);
501 }
502 display->display = wl_display_connect(NULL);
503 assert(display->display);
504
505 /* XXX: fake, because the compositor does not yet advertise anything */
506 display->xrgb8888_format_found = 1;
507
508 display->registry = wl_display_get_registry(display->display);
509 wl_registry_add_listener(display->registry,
510 &registry_listener, display);
511 wl_display_roundtrip(display->display);
512 if (display->dmabuf == NULL) {
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800513 fprintf(stderr, "No zwp_linux_dmabuf global\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200514 exit(1);
515 }
516
517 wl_display_roundtrip(display->display);
518
519 if (!display->xrgb8888_format_found) {
520 fprintf(stderr, "DRM_FORMAT_XRGB8888 not available\n");
521 exit(1);
522 }
523
524 return display;
525}
526
527static void
528destroy_display(struct display *display)
529{
530 if (display->dmabuf)
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800531 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
George Kiagiadakis53868982014-06-12 16:26:49 +0200532
533 if (display->shell)
534 xdg_shell_destroy(display->shell);
535
536 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800537 zwp_fullscreen_shell_v1_release(display->fshell);
George Kiagiadakis53868982014-06-12 16:26:49 +0200538
539 if (display->compositor)
540 wl_compositor_destroy(display->compositor);
541
542 wl_registry_destroy(display->registry);
543 wl_display_flush(display->display);
544 wl_display_disconnect(display->display);
545 free(display);
546}
547
548static void
549signal_int(int signum)
550{
551 running = 0;
552}
553
554int
555main(int argc, char **argv)
556{
557 struct sigaction sigint;
558 struct display *display;
559 struct window *window;
560 int ret = 0;
561
562 display = create_display();
563 window = create_window(display, 250, 250);
564 if (!window)
565 return 1;
566
567 sigint.sa_handler = signal_int;
568 sigemptyset(&sigint.sa_mask);
569 sigint.sa_flags = SA_RESETHAND;
570 sigaction(SIGINT, &sigint, NULL);
571
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000572 /* Here we retrieve the linux-dmabuf objects, or error */
573 wl_display_roundtrip(display->display);
574
575 if (!running)
576 return 1;
George Kiagiadakis53868982014-06-12 16:26:49 +0200577
578 redraw(window, NULL, 0);
579
580 while (running && ret != -1)
581 ret = wl_display_dispatch(display->display);
582
583 fprintf(stderr, "simple-dmabuf exiting\n");
584 destroy_window(window);
585 destroy_display(display);
586
587 return 0;
588}