blob: b85f3205c13f99e24117b96165199a277c12f484 [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>
38#include <libdrm/i915_drm.h>
39#include <libdrm/intel_bufmgr.h>
40#include <libdrm/drm_fourcc.h>
41
42#include <wayland-client.h>
43#include "xdg-shell-client-protocol.h"
44#include "fullscreen-shell-client-protocol.h"
45#include "linux-dmabuf-client-protocol.h"
46
47struct display {
48 struct wl_display *display;
49 struct wl_registry *registry;
50 struct wl_compositor *compositor;
51 struct xdg_shell *shell;
52 struct _wl_fullscreen_shell *fshell;
53 struct zlinux_dmabuf *dmabuf;
54 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,
190 struct zlinux_buffer_params *params,
191 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
198 zlinux_buffer_params_destroy(params);
199}
200
201static void
202create_failed(void *data, struct zlinux_buffer_params *params)
203{
204 struct buffer *buffer = data;
205
206 buffer->buffer = NULL;
207
208 zlinux_buffer_params_destroy(params);
209
210 fprintf(stderr, "Error: zlinux_buffer_params.create failed.\n");
211}
212
213static const struct zlinux_buffer_params_listener params_listener = {
214 create_succeeded,
215 create_failed
216};
217
218static int
219create_dmabuf_buffer(struct display *display, struct buffer *buffer,
220 int width, int height)
221{
222 struct zlinux_buffer_params *params;
223 uint64_t modifier;
224 uint32_t flags;
225
226 if (!drm_connect(buffer)) {
227 fprintf(stderr, "drm_connect failed\n");
228 goto error;
229 }
230
231 buffer->width = width;
232 buffer->height = height;
233 buffer->bpp = 32; /* hardcoded XRGB8888 format */
234
235 if (!alloc_bo(buffer)) {
236 fprintf(stderr, "alloc_bo failed\n");
237 goto error1;
238 }
239
240 if (!map_bo(buffer)) {
241 fprintf(stderr, "map_bo failed\n");
242 goto error2;
243 }
244 fill_content(buffer);
245 unmap_bo(buffer);
246
247 if (drm_intel_bo_gem_export_to_prime(buffer->bo, &buffer->dmabuf_fd) != 0) {
248 fprintf(stderr, "drm_intel_bo_gem_export_to_prime failed\n");
249 goto error2;
250 }
251 if (buffer->dmabuf_fd < 0) {
252 fprintf(stderr, "error: dmabuf_fd < 0\n");
253 goto error2;
254 }
255
256 /* We now have a dmabuf! It should contain 2x2 tiles (i.e. each tile
257 * is 256x256) of misc colours, and be mappable, either as ARGB8888, or
258 * XRGB8888. */
259 modifier = 0;
260 flags = 0;
261
262 params = zlinux_dmabuf_create_params(display->dmabuf);
263 zlinux_buffer_params_add(params,
264 buffer->dmabuf_fd,
265 0, /* plane_idx */
266 0, /* offset */
267 buffer->stride,
268 modifier >> 32,
269 modifier & 0xffffffff);
270 zlinux_buffer_params_add_listener(params, &params_listener, buffer);
271 zlinux_buffer_params_create(params,
272 buffer->width,
273 buffer->height,
274 DRM_FORMAT_XRGB8888,
275 flags);
276
277 /* params is destroyed by the event handlers */
278
279 wl_display_roundtrip(display->display);
280 if (buffer->buffer == NULL) {
281 goto error2;
282 }
283
284 return 0;
285
286error2:
287 free_bo(buffer);
288error1:
289 drm_shutdown(buffer);
290error:
291 return -1;
292}
293
294static void
295handle_configure(void *data, struct xdg_surface *surface,
296 int32_t width, int32_t height,
297 struct wl_array *states, uint32_t serial)
298{
299}
300
301static void
302handle_delete(void *data, struct xdg_surface *xdg_surface)
303{
304 running = 0;
305}
306
307static const struct xdg_surface_listener xdg_surface_listener = {
308 handle_configure,
309 handle_delete,
310};
311
312static struct window *
313create_window(struct display *display, int width, int height)
314{
315 struct window *window;
316
317 window = calloc(1, sizeof *window);
318 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) {
339 _wl_fullscreen_shell_present_surface(display->fshell,
340 window->surface,
341 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,
342 NULL);
343 } else {
344 assert(0);
345 }
346
347 return window;
348}
349
350static void
351destroy_window(struct window *window)
352{
353 int i;
354
355 if (window->callback)
356 wl_callback_destroy(window->callback);
357
358 for (i = 0; i < 2; i++) {
359 if (!window->buffers[i].buffer)
360 continue;
361
362 wl_buffer_destroy(window->buffers[i].buffer);
363 free_bo(&window->buffers[i]);
364 close(window->buffers[i].dmabuf_fd);
365 drm_shutdown(&window->buffers[i]);
366 }
367
368 if (window->xdg_surface)
369 xdg_surface_destroy(window->xdg_surface);
370 wl_surface_destroy(window->surface);
371 free(window);
372}
373
374static struct buffer *
375window_next_buffer(struct window *window)
376{
377 struct buffer *buffer;
378 int ret = 0;
379
380 if (!window->buffers[0].busy)
381 buffer = &window->buffers[0];
382 else if (!window->buffers[1].busy)
383 buffer = &window->buffers[1];
384 else
385 return NULL;
386
387 if (!buffer->buffer) {
388 ret = create_dmabuf_buffer(window->display, buffer,
389 window->width, window->height);
390
391 if (ret < 0)
392 return NULL;
393 }
394
395 return buffer;
396}
397
398static const struct wl_callback_listener frame_listener;
399
400static void
401redraw(void *data, struct wl_callback *callback, uint32_t time)
402{
403 struct window *window = data;
404 struct buffer *buffer;
405
406 buffer = window_next_buffer(window);
407 if (!buffer) {
408 fprintf(stderr,
409 !callback ? "Failed to create the first buffer.\n" :
410 "Both buffers busy at redraw(). Server bug?\n");
411 abort();
412 }
413
414 /* XXX: would be nice to draw something that changes here... */
415
416 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
417 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
418
419 if (callback)
420 wl_callback_destroy(callback);
421
422 window->callback = wl_surface_frame(window->surface);
423 wl_callback_add_listener(window->callback, &frame_listener, window);
424 wl_surface_commit(window->surface);
425 buffer->busy = 1;
426}
427
428static const struct wl_callback_listener frame_listener = {
429 redraw
430};
431
432static void
433dmabuf_format(void *data, struct zlinux_dmabuf *zlinux_dmabuf, uint32_t format)
434{
435 struct display *d = data;
436
437 if (format == DRM_FORMAT_XRGB8888)
438 d->xrgb8888_format_found = 1;
439}
440
441static const struct zlinux_dmabuf_listener dmabuf_listener = {
442 dmabuf_format
443};
444
445static void
446xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
447{
448 xdg_shell_pong(shell, serial);
449}
450
451static const struct xdg_shell_listener xdg_shell_listener = {
452 xdg_shell_ping,
453};
454
455#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
456#ifdef static_assert
457static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
458 "Interface version doesn't match implementation version");
459#endif
460
461static void
462registry_handle_global(void *data, struct wl_registry *registry,
463 uint32_t id, const char *interface, uint32_t version)
464{
465 struct display *d = data;
466
467 if (strcmp(interface, "wl_compositor") == 0) {
468 d->compositor =
469 wl_registry_bind(registry,
470 id, &wl_compositor_interface, 1);
471 } else if (strcmp(interface, "xdg_shell") == 0) {
472 d->shell = wl_registry_bind(registry,
473 id, &xdg_shell_interface, 1);
474 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
475 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
476 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
477 d->fshell = wl_registry_bind(registry,
478 id, &_wl_fullscreen_shell_interface, 1);
479 } else if (strcmp(interface, "zlinux_dmabuf") == 0) {
480 d->dmabuf = wl_registry_bind(registry,
481 id, &zlinux_dmabuf_interface, 1);
482 zlinux_dmabuf_add_listener(d->dmabuf, &dmabuf_listener, d);
483 }
484}
485
486static void
487registry_handle_global_remove(void *data, struct wl_registry *registry,
488 uint32_t name)
489{
490}
491
492static const struct wl_registry_listener registry_listener = {
493 registry_handle_global,
494 registry_handle_global_remove
495};
496
497static struct display *
498create_display(void)
499{
500 struct display *display;
501
502 display = malloc(sizeof *display);
503 if (display == NULL) {
504 fprintf(stderr, "out of memory\n");
505 exit(1);
506 }
507 display->display = wl_display_connect(NULL);
508 assert(display->display);
509
510 /* XXX: fake, because the compositor does not yet advertise anything */
511 display->xrgb8888_format_found = 1;
512
513 display->registry = wl_display_get_registry(display->display);
514 wl_registry_add_listener(display->registry,
515 &registry_listener, display);
516 wl_display_roundtrip(display->display);
517 if (display->dmabuf == NULL) {
518 fprintf(stderr, "No zlinux_dmabuf global\n");
519 exit(1);
520 }
521
522 wl_display_roundtrip(display->display);
523
524 if (!display->xrgb8888_format_found) {
525 fprintf(stderr, "DRM_FORMAT_XRGB8888 not available\n");
526 exit(1);
527 }
528
529 return display;
530}
531
532static void
533destroy_display(struct display *display)
534{
535 if (display->dmabuf)
536 zlinux_dmabuf_destroy(display->dmabuf);
537
538 if (display->shell)
539 xdg_shell_destroy(display->shell);
540
541 if (display->fshell)
542 _wl_fullscreen_shell_release(display->fshell);
543
544 if (display->compositor)
545 wl_compositor_destroy(display->compositor);
546
547 wl_registry_destroy(display->registry);
548 wl_display_flush(display->display);
549 wl_display_disconnect(display->display);
550 free(display);
551}
552
553static void
554signal_int(int signum)
555{
556 running = 0;
557}
558
559int
560main(int argc, char **argv)
561{
562 struct sigaction sigint;
563 struct display *display;
564 struct window *window;
565 int ret = 0;
566
567 display = create_display();
568 window = create_window(display, 250, 250);
569 if (!window)
570 return 1;
571
572 sigint.sa_handler = signal_int;
573 sigemptyset(&sigint.sa_mask);
574 sigint.sa_flags = SA_RESETHAND;
575 sigaction(SIGINT, &sigint, NULL);
576
577 /* Initialise damage to full surface, so the padding gets painted */
578 wl_surface_damage(window->surface, 0, 0,
579 window->width, window->height);
580
581 redraw(window, NULL, 0);
582
583 while (running && ret != -1)
584 ret = wl_display_dispatch(display->display);
585
586 fprintf(stderr, "simple-dmabuf exiting\n");
587 destroy_window(window);
588 destroy_display(display);
589
590 return 0;
591}