blob: 505ecb0a5a89708729ef14ad8aed8d63a150dab2 [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
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070025#include "config.h"
George Kiagiadakis53868982014-06-12 16:26:49 +020026
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030027#include <stdint.h>
George Kiagiadakis53868982014-06-12 16:26:49 +020028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32#include <assert.h>
33#include <unistd.h>
34#include <sys/mman.h>
35#include <signal.h>
36#include <fcntl.h>
37
38#include <xf86drm.h>
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +000039#include <i915_drm.h>
40#include <intel_bufmgr.h>
41#include <drm_fourcc.h>
George Kiagiadakis53868982014-06-12 16:26:49 +020042
43#include <wayland-client.h>
Bryce Harrington0d1a6222016-02-11 16:42:49 -080044#include "shared/zalloc.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080045#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080046#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jonas Ådahl57e48f02015-11-17 16:00:28 +080047#include "linux-dmabuf-unstable-v1-client-protocol.h"
George Kiagiadakis53868982014-06-12 16:26:49 +020048
49struct display {
50 struct wl_display *display;
51 struct wl_registry *registry;
52 struct wl_compositor *compositor;
53 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080054 struct zwp_fullscreen_shell_v1 *fshell;
Jonas Ådahl57e48f02015-11-17 16:00:28 +080055 struct zwp_linux_dmabuf_v1 *dmabuf;
George Kiagiadakis53868982014-06-12 16:26:49 +020056 int xrgb8888_format_found;
57};
58
59struct buffer {
60 struct wl_buffer *buffer;
61 int busy;
62
63 int drm_fd;
64
65 drm_intel_bufmgr *bufmgr;
66 drm_intel_bo *bo;
67
68 uint32_t gem_handle;
69 int dmabuf_fd;
70 uint8_t *mmap;
71
72 int width;
73 int height;
74 int bpp;
75 unsigned long stride;
76};
77
Pekka Paalanend56b94a2016-06-10 13:13:01 +030078#define NUM_BUFFERS 3
79
George Kiagiadakis53868982014-06-12 16:26:49 +020080struct window {
81 struct display *display;
82 int width, height;
83 struct wl_surface *surface;
84 struct xdg_surface *xdg_surface;
Pekka Paalanend56b94a2016-06-10 13:13:01 +030085 struct buffer buffers[NUM_BUFFERS];
George Kiagiadakis53868982014-06-12 16:26:49 +020086 struct buffer *prev_buffer;
87 struct wl_callback *callback;
88};
89
90static int running = 1;
91
92static void
93buffer_release(void *data, struct wl_buffer *buffer)
94{
95 struct buffer *mybuf = data;
96
97 mybuf->busy = 0;
98}
99
100static const struct wl_buffer_listener buffer_listener = {
101 buffer_release
102};
103
104static int
105drm_connect(struct buffer *my_buf)
106{
107 /* This won't work with card0 as we need to be authenticated; instead,
108 * boot with drm.rnodes=1 and use that. */
109 my_buf->drm_fd = open("/dev/dri/renderD128", O_RDWR);
110 if (my_buf->drm_fd < 0)
111 return 0;
112
113 my_buf->bufmgr = drm_intel_bufmgr_gem_init(my_buf->drm_fd, 32);
114 if (!my_buf->bufmgr)
115 return 0;
116
117 return 1;
118}
119
120static void
121drm_shutdown(struct buffer *my_buf)
122{
123 drm_intel_bufmgr_destroy(my_buf->bufmgr);
124 close(my_buf->drm_fd);
125}
126
127static int
128alloc_bo(struct buffer *my_buf)
129{
130 /* XXX: try different tiling modes for testing FB modifiers. */
131 uint32_t tiling = I915_TILING_NONE;
132
133 assert(my_buf->bufmgr);
134
135 my_buf->bo = drm_intel_bo_alloc_tiled(my_buf->bufmgr, "test",
136 my_buf->width, my_buf->height,
137 (my_buf->bpp / 8), &tiling,
138 &my_buf->stride, 0);
139
140 printf("buffer allocated w %d, h %d, stride %lu, size %lu\n",
141 my_buf->width, my_buf->height, my_buf->stride, my_buf->bo->size);
142
143 if (!my_buf->bo)
144 return 0;
145
146 if (tiling != I915_TILING_NONE)
147 return 0;
148
149 return 1;
150}
151
152static void
153free_bo(struct buffer *my_buf)
154{
155 drm_intel_bo_unreference(my_buf->bo);
156}
157
158static int
159map_bo(struct buffer *my_buf)
160{
161 if (drm_intel_gem_bo_map_gtt(my_buf->bo) != 0)
162 return 0;
163
164 my_buf->mmap = my_buf->bo->virtual;
165
166 return 1;
167}
168
169static void
170fill_content(struct buffer *my_buf)
171{
172 int x = 0, y = 0;
173 uint32_t *pix;
174
175 assert(my_buf->mmap);
176
177 for (y = 0; y < my_buf->height; y++) {
178 pix = (uint32_t *)(my_buf->mmap + y * my_buf->stride);
179 for (x = 0; x < my_buf->width; x++) {
180 *pix++ = (0xff << 24) | ((x % 256) << 16) |
181 ((y % 256) << 8) | 0xf0;
182 }
183 }
184}
185
186static void
187unmap_bo(struct buffer *my_buf)
188{
189 drm_intel_gem_bo_unmap_gtt(my_buf->bo);
190}
191
192static void
193create_succeeded(void *data,
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800194 struct zwp_linux_buffer_params_v1 *params,
George Kiagiadakis53868982014-06-12 16:26:49 +0200195 struct wl_buffer *new_buffer)
196{
197 struct buffer *buffer = data;
198
199 buffer->buffer = new_buffer;
200 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
201
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800202 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200203}
204
205static void
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800206create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
George Kiagiadakis53868982014-06-12 16:26:49 +0200207{
208 struct buffer *buffer = data;
209
210 buffer->buffer = NULL;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000211 running = 0;
George Kiagiadakis53868982014-06-12 16:26:49 +0200212
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800213 zwp_linux_buffer_params_v1_destroy(params);
George Kiagiadakis53868982014-06-12 16:26:49 +0200214
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800215 fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200216}
217
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800218static const struct zwp_linux_buffer_params_v1_listener params_listener = {
George Kiagiadakis53868982014-06-12 16:26:49 +0200219 create_succeeded,
220 create_failed
221};
222
223static int
224create_dmabuf_buffer(struct display *display, struct buffer *buffer,
225 int width, int height)
226{
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800227 struct zwp_linux_buffer_params_v1 *params;
George Kiagiadakis53868982014-06-12 16:26:49 +0200228 uint64_t modifier;
229 uint32_t flags;
230
231 if (!drm_connect(buffer)) {
232 fprintf(stderr, "drm_connect failed\n");
233 goto error;
234 }
235
236 buffer->width = width;
237 buffer->height = height;
238 buffer->bpp = 32; /* hardcoded XRGB8888 format */
239
240 if (!alloc_bo(buffer)) {
241 fprintf(stderr, "alloc_bo failed\n");
242 goto error1;
243 }
244
245 if (!map_bo(buffer)) {
246 fprintf(stderr, "map_bo failed\n");
247 goto error2;
248 }
249 fill_content(buffer);
250 unmap_bo(buffer);
251
252 if (drm_intel_bo_gem_export_to_prime(buffer->bo, &buffer->dmabuf_fd) != 0) {
253 fprintf(stderr, "drm_intel_bo_gem_export_to_prime failed\n");
254 goto error2;
255 }
256 if (buffer->dmabuf_fd < 0) {
257 fprintf(stderr, "error: dmabuf_fd < 0\n");
258 goto error2;
259 }
260
261 /* We now have a dmabuf! It should contain 2x2 tiles (i.e. each tile
262 * is 256x256) of misc colours, and be mappable, either as ARGB8888, or
263 * XRGB8888. */
264 modifier = 0;
265 flags = 0;
266
Jonas Ådahl57e48f02015-11-17 16:00:28 +0800267 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
268 zwp_linux_buffer_params_v1_add(params,
269 buffer->dmabuf_fd,
270 0, /* plane_idx */
271 0, /* offset */
272 buffer->stride,
273 modifier >> 32,
274 modifier & 0xffffffff);
275 zwp_linux_buffer_params_v1_add_listener(params, &params_listener, buffer);
276 zwp_linux_buffer_params_v1_create(params,
277 buffer->width,
278 buffer->height,
279 DRM_FORMAT_XRGB8888,
280 flags);
George Kiagiadakis53868982014-06-12 16:26:49 +0200281
George Kiagiadakis53868982014-06-12 16:26:49 +0200282 return 0;
283
284error2:
285 free_bo(buffer);
286error1:
287 drm_shutdown(buffer);
288error:
289 return -1;
290}
291
292static void
293handle_configure(void *data, struct xdg_surface *surface,
294 int32_t width, int32_t height,
295 struct wl_array *states, uint32_t serial)
296{
297}
298
299static void
300handle_delete(void *data, struct xdg_surface *xdg_surface)
301{
302 running = 0;
303}
304
305static const struct xdg_surface_listener xdg_surface_listener = {
306 handle_configure,
307 handle_delete,
308};
309
310static struct window *
311create_window(struct display *display, int width, int height)
312{
313 struct window *window;
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000314 int i;
315 int ret;
George Kiagiadakis53868982014-06-12 16:26:49 +0200316
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800317 window = zalloc(sizeof *window);
George Kiagiadakis53868982014-06-12 16:26:49 +0200318 if (!window)
319 return NULL;
320
321 window->callback = NULL;
322 window->display = display;
323 window->width = width;
324 window->height = height;
325 window->surface = wl_compositor_create_surface(display->compositor);
326
327 if (display->shell) {
328 window->xdg_surface =
329 xdg_shell_get_xdg_surface(display->shell,
330 window->surface);
331
332 assert(window->xdg_surface);
333
334 xdg_surface_add_listener(window->xdg_surface,
335 &xdg_surface_listener, window);
336
337 xdg_surface_set_title(window->xdg_surface, "simple-dmabuf");
338 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800339 zwp_fullscreen_shell_v1_present_surface(display->fshell,
340 window->surface,
341 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
342 NULL);
George Kiagiadakis53868982014-06-12 16:26:49 +0200343 } else {
344 assert(0);
345 }
346
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300347 for (i = 0; i < NUM_BUFFERS; ++i) {
Emmanuel Gil Peyrot8ef29572016-01-11 19:04:37 +0000348 ret = create_dmabuf_buffer(display, &window->buffers[i],
349 width, height);
350
351 if (ret < 0)
352 return NULL;
353 }
354
George Kiagiadakis53868982014-06-12 16:26:49 +0200355 return window;
356}
357
358static void
359destroy_window(struct window *window)
360{
361 int i;
362
363 if (window->callback)
364 wl_callback_destroy(window->callback);
365
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300366 for (i = 0; i < NUM_BUFFERS; i++) {
George Kiagiadakis53868982014-06-12 16:26:49 +0200367 if (!window->buffers[i].buffer)
368 continue;
369
370 wl_buffer_destroy(window->buffers[i].buffer);
371 free_bo(&window->buffers[i]);
372 close(window->buffers[i].dmabuf_fd);
373 drm_shutdown(&window->buffers[i]);
374 }
375
376 if (window->xdg_surface)
377 xdg_surface_destroy(window->xdg_surface);
378 wl_surface_destroy(window->surface);
379 free(window);
380}
381
382static struct buffer *
383window_next_buffer(struct window *window)
384{
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300385 int i;
George Kiagiadakis53868982014-06-12 16:26:49 +0200386
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300387 for (i = 0; i < NUM_BUFFERS; i++)
388 if (!window->buffers[i].busy)
389 return &window->buffers[i];
George Kiagiadakis53868982014-06-12 16:26:49 +0200390
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300391 return NULL;
George Kiagiadakis53868982014-06-12 16:26:49 +0200392}
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" :
Pekka Paalanend56b94a2016-06-10 13:13:01 +0300406 "All buffers busy at redraw(). Server bug?\n");
George Kiagiadakis53868982014-06-12 16:26:49 +0200407 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}