blob: 2d9b88523b09cc90da77628c0cf77bbd08fa541d [file] [log] [blame]
Pekka Paalanenef2b5922014-09-23 22:08:49 -04001/*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 * Copyright © 2014 Collabora, Ltd.
5 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07006 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
Pekka Paalanenef2b5922014-09-23 22:08:49 -040012 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070013 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
Pekka Paalanenef2b5922014-09-23 22:08:49 -040024 */
25
26#include <config.h>
27
28#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 <time.h>
37
38#include <wayland-client.h>
Jon Cruz35b2eaa2015-06-15 15:37:08 -070039#include "shared/helpers.h"
Bryce Harrington0d1a6222016-02-11 16:42:49 -080040#include "shared/zalloc.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070041#include "shared/os-compatibility.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020042#include "presentation-time-client-protocol.h"
Pekka Paalanenef2b5922014-09-23 22:08:49 -040043
Pekka Paalanenef2b5922014-09-23 22:08:49 -040044enum run_mode {
45 RUN_MODE_FEEDBACK,
46 RUN_MODE_FEEDBACK_IDLE,
47 RUN_MODE_PRESENT,
48};
49
Pekka Paalanen4d012bc2015-02-06 13:50:37 +020050static const char * const run_mode_name[] = {
51 [RUN_MODE_FEEDBACK] = "feedback",
52 [RUN_MODE_FEEDBACK_IDLE] = "feedback-idle",
53 [RUN_MODE_PRESENT] = "low-lat present",
54};
55
Pekka Paalanenef2b5922014-09-23 22:08:49 -040056struct output {
57 struct wl_output *output;
58 uint32_t name;
59 struct wl_list link;
60};
61
62struct display {
63 struct wl_display *display;
64 struct wl_registry *registry;
65 struct wl_compositor *compositor;
66 struct wl_shell *shell;
67
68 struct wl_shm *shm;
69 uint32_t formats;
70
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020071 struct wp_presentation *presentation;
Pekka Paalanenef2b5922014-09-23 22:08:49 -040072 clockid_t clk_id;
73
74 struct wl_list output_list; /* struct output::link */
75};
76
77struct feedback {
78 struct window *window;
79 unsigned frame_no;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020080 struct wp_presentation_feedback *feedback;
Pekka Paalanenef2b5922014-09-23 22:08:49 -040081 struct timespec commit;
82 struct timespec target;
83 uint32_t frame_stamp;
84 struct wl_list link;
85 struct timespec present;
86};
87
88struct buffer {
89 struct wl_buffer *buffer;
90 void *shm_data;
91 int busy;
92};
93
94struct window {
95 struct display *display;
96 int width, height;
97 enum run_mode mode;
98 struct wl_surface *surface;
99 struct wl_shell_surface *shell_surface;
100
101 struct buffer *buffers;
102 int num_buffers;
103 int next;
104 int refresh_nsec;
Mario Kleinerc1809b22015-06-21 21:25:16 +0200105 int commit_delay_msecs;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400106
107 struct wl_callback *callback;
108 struct wl_list feedback_list;
109
110 struct feedback *received_feedback;
111};
112
113#define NSEC_PER_SEC 1000000000
114
115static void
116buffer_release(void *data, struct wl_buffer *buffer)
117{
118 struct buffer *mybuf = data;
119
120 mybuf->busy = 0;
121}
122
123static const struct wl_buffer_listener buffer_listener = {
124 buffer_release
125};
126
127static int
128create_shm_buffers(struct display *display, struct buffer **buffers,
129 int num_buffers, int width, int height, uint32_t format)
130{
131 struct buffer *bufs;
132 struct wl_shm_pool *pool;
133 int fd, size, stride, offset;
134 void *data;
135 int i;
136
137 stride = width * 4;
138 size = stride * height * num_buffers;
139
140 fd = os_create_anonymous_file(size);
141 if (fd < 0) {
142 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
143 size);
144 return -1;
145 }
146
147 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
148 if (data == MAP_FAILED) {
149 fprintf(stderr, "mmap failed: %m\n");
150 close(fd);
151 return -1;
152 }
153
154 pool = wl_shm_create_pool(display->shm, fd, size);
155 offset = 0;
156
157 bufs = calloc(num_buffers, sizeof(*bufs));
158 assert(bufs);
159
160 for (i = 0; i < num_buffers; i++) {
161 bufs[i].buffer = wl_shm_pool_create_buffer(pool, offset,
162 width, height,
163 stride, format);
164 assert(bufs[i].buffer);
165 wl_buffer_add_listener(bufs[i].buffer,
166 &buffer_listener, &bufs[i]);
167
168 bufs[i].shm_data = (char *)data + offset;
169 offset += stride * height;
170 }
171
172 wl_shm_pool_destroy(pool);
173 close(fd);
174
175 *buffers = bufs;
176
177 return 0;
178}
179
180static void
181handle_ping(void *data, struct wl_shell_surface *shell_surface,
182 uint32_t serial)
183{
184 wl_shell_surface_pong(shell_surface, serial);
185}
186
187static void
188handle_configure(void *data, struct wl_shell_surface *shell_surface,
189 uint32_t edges, int32_t width, int32_t height)
190{
191}
192
193static void
194handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
195{
196}
197
198static const struct wl_shell_surface_listener shell_surface_listener = {
199 handle_ping,
200 handle_configure,
201 handle_popup_done
202};
203
204static struct window *
205create_window(struct display *display, int width, int height,
Mario Kleinerc1809b22015-06-21 21:25:16 +0200206 enum run_mode mode, int commit_delay_msecs)
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400207{
208 struct window *window;
Pekka Paalanen4d012bc2015-02-06 13:50:37 +0200209 char title[128];
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400210 int ret;
211
Pekka Paalanen4d012bc2015-02-06 13:50:37 +0200212 snprintf(title, sizeof(title),
Mario Kleinerc1809b22015-06-21 21:25:16 +0200213 "presentation-shm: %s [Delay %i msecs]", run_mode_name[mode],
214 commit_delay_msecs);
Pekka Paalanen4d012bc2015-02-06 13:50:37 +0200215
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800216 window = zalloc(sizeof *window);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400217 if (!window)
218 return NULL;
219
Mario Kleinerc1809b22015-06-21 21:25:16 +0200220 window->commit_delay_msecs = commit_delay_msecs;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400221 window->mode = mode;
222 window->callback = NULL;
223 wl_list_init(&window->feedback_list);
224 window->display = display;
225 window->width = width;
226 window->height = height;
227 window->surface = wl_compositor_create_surface(display->compositor);
228 window->shell_surface = wl_shell_get_shell_surface(display->shell,
229 window->surface);
230
231 if (window->shell_surface)
232 wl_shell_surface_add_listener(window->shell_surface,
233 &shell_surface_listener, window);
234
Pekka Paalanen4d012bc2015-02-06 13:50:37 +0200235 wl_shell_surface_set_title(window->shell_surface, title);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400236
237 wl_shell_surface_set_toplevel(window->shell_surface);
238
239 window->num_buffers = 60;
240 window->refresh_nsec = NSEC_PER_SEC / 60; /* 60 Hz guess */
241 window->next = 0;
242 ret = create_shm_buffers(window->display,
243 &window->buffers, window->num_buffers,
244 window->width, window->height,
245 WL_SHM_FORMAT_XRGB8888);
246 assert(ret == 0);
247
248 return window;
249}
250
251static void
252destroy_feedback(struct feedback *feedback)
253{
254 if (feedback->feedback)
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200255 wp_presentation_feedback_destroy(feedback->feedback);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400256
257 wl_list_remove(&feedback->link);
258 free(feedback);
259}
260
261static void
262destroy_window(struct window *window)
263{
264 int i;
265
266 while (!wl_list_empty(&window->feedback_list)) {
267 struct feedback *f;
268
269 f = wl_container_of(window->feedback_list.next, f, link);
270 printf("clean up feedback %u\n", f->frame_no);
271 destroy_feedback(f);
272 }
273
274 if (window->callback)
275 wl_callback_destroy(window->callback);
276
277 wl_shell_surface_destroy(window->shell_surface);
278 wl_surface_destroy(window->surface);
279
280 for (i = 0; i < window->num_buffers; i++)
281 wl_buffer_destroy(window->buffers[i].buffer);
282 /* munmap(window->buffers[0].shm_data, size); */
283 free(window->buffers);
284
285 free(window);
286}
287
288static struct buffer *
289window_next_buffer(struct window *window)
290{
291 struct buffer *buf = &window->buffers[window->next];
292
293 window->next = (window->next + 1) % window->num_buffers;
294
295 return buf;
296}
297
298static void
299paint_pixels(void *image, int width, int height, uint32_t phase)
300{
301 const int halfh = height / 2;
302 const int halfw = width / 2;
303 uint32_t *pixel = image;
304 int y, or;
305 double ang = M_PI * 2.0 / 1000000.0 * phase;
306 double s = sin(ang);
307 double c = cos(ang);
308
309 /* squared radii thresholds */
310 or = (halfw < halfh ? halfw : halfh) - 16;
311 or *= or;
312
313 for (y = 0; y < height; y++) {
314 int x;
315 int oy = y - halfh;
316 int y2 = oy * oy;
317
318 for (x = 0; x < width; x++) {
319 int ox = x - halfw;
320 uint32_t v = 0xff000000;
321 double rx, ry;
322
323 if (ox * ox + y2 > or) {
324 if (ox * oy > 0)
325 *pixel++ = 0xff000000;
326 else
327 *pixel++ = 0xffffffff;
328 continue;
329 }
330
331 rx = c * ox + s * oy;
332 ry = -s * ox + c * oy;
333
334 if (rx < 0.0)
335 v |= 0x00ff0000;
336 if (ry < 0.0)
337 v |= 0x0000ff00;
338 if ((rx < 0.0) == (ry < 0.0))
339 v |= 0x000000ff;
340
341 *pixel++ = v;
342 }
343 }
344}
345
346static void
347feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200348 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400349 struct wl_output *output)
350{
351 /* not interested */
352}
353
354static char *
355pflags_to_str(uint32_t flags, char *str, unsigned len)
356{
357 static const struct {
358 uint32_t flag;
359 char sym;
360 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200361 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
362 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
363 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
364 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400365 };
366 unsigned i;
367
368 *str = '\0';
369 if (len < ARRAY_LENGTH(desc) + 1)
370 return str;
371
372 for (i = 0; i < ARRAY_LENGTH(desc); i++)
373 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
374 str[ARRAY_LENGTH(desc)] = '\0';
375
376 return str;
377}
378
379static uint32_t
380timespec_to_ms(const struct timespec *ts)
381{
382 return (uint32_t)ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
383}
384
385static void
386timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
387 uint32_t tv_sec_lo, uint32_t tv_nsec)
388{
389 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
390 tm->tv_nsec = tv_nsec;
391}
392
393static int
394timespec_diff_to_usec(const struct timespec *a, const struct timespec *b)
395{
396 time_t secs = a->tv_sec - b->tv_sec;
397 long nsec = a->tv_nsec - b->tv_nsec;
398
399 return secs * 1000000 + nsec / 1000;
400}
401
402static void
403feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200404 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400405 uint32_t tv_sec_hi,
406 uint32_t tv_sec_lo,
407 uint32_t tv_nsec,
408 uint32_t refresh_nsec,
409 uint32_t seq_hi,
410 uint32_t seq_lo,
411 uint32_t flags)
412{
413 struct feedback *feedback = data;
414 struct window *window = feedback->window;
415 struct feedback *prev_feedback = window->received_feedback;
416 uint64_t seq = ((uint64_t)seq_hi << 32) + seq_lo;
417 const struct timespec *prevpresent;
418 uint32_t commit, present;
419 uint32_t f2c, c2p, f2p;
420 int p2p, t2p;
421 char flagstr[10];
422
423 timespec_from_proto(&feedback->present, tv_sec_hi, tv_sec_lo, tv_nsec);
424 commit = timespec_to_ms(&feedback->commit);
425 present = timespec_to_ms(&feedback->present);
426
427 if (prev_feedback)
428 prevpresent = &prev_feedback->present;
429 else
430 prevpresent = &feedback->present;
431
432 f2c = commit - feedback->frame_stamp;
433 c2p = present - commit;
434 f2p = present - feedback->frame_stamp;
435 p2p = timespec_diff_to_usec(&feedback->present, prevpresent);
436 t2p = timespec_diff_to_usec(&feedback->present, &feedback->target);
437
438 switch (window->mode) {
439 case RUN_MODE_PRESENT:
440 printf("%6u: c2p %4u ms, p2p %5d us, t2p %6d us, [%s] "
441 "seq %" PRIu64 "\n", feedback->frame_no, c2p,
442 p2p, t2p,
443 pflags_to_str(flags, flagstr, sizeof(flagstr)), seq);
444 break;
445 case RUN_MODE_FEEDBACK:
446 case RUN_MODE_FEEDBACK_IDLE:
447 printf("%6u: f2c %2u ms, c2p %2u ms, f2p %2u ms, p2p %5d us, "
448 "t2p %6d, [%s], seq %" PRIu64 "\n", feedback->frame_no,
449 f2c, c2p, f2p, p2p, t2p,
450 pflags_to_str(flags, flagstr, sizeof(flagstr)), seq);
451 }
452
453 if (window->received_feedback)
454 destroy_feedback(window->received_feedback);
455 window->received_feedback = feedback;
456}
457
458static void
459feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200460 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400461{
462 struct feedback *feedback = data;
463
464 printf("discarded %u\n", feedback->frame_no);
465
466 destroy_feedback(feedback);
467}
468
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200469static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400470 feedback_sync_output,
471 feedback_presented,
472 feedback_discarded
473};
474
475static void
Mario Kleinerc1809b22015-06-21 21:25:16 +0200476window_emulate_rendering(struct window *window)
477{
478 struct timespec delay;
479 int ret;
480
481 if (window->commit_delay_msecs <= 0)
482 return;
483
484 delay.tv_sec = window->commit_delay_msecs / 1000;
485 delay.tv_nsec = (window->commit_delay_msecs % 1000) * 1000000;
486
487 ret = nanosleep(&delay, NULL);
488 if (ret)
489 printf("nanosleep failed: %m\n");
490}
491
492static void
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400493window_create_feedback(struct window *window, uint32_t frame_stamp)
494{
495 static unsigned seq;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200496 struct wp_presentation *pres = window->display->presentation;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400497 struct feedback *feedback;
498
499 seq++;
500
501 if (!pres)
502 return;
503
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800504 feedback = zalloc(sizeof *feedback);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400505 if (!feedback)
506 return;
507
508 feedback->window = window;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200509 feedback->feedback = wp_presentation_feedback(pres, window->surface);
510 wp_presentation_feedback_add_listener(feedback->feedback,
511 &feedback_listener, feedback);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400512
513 feedback->frame_no = seq;
Mario Kleinerc1809b22015-06-21 21:25:16 +0200514
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400515 clock_gettime(window->display->clk_id, &feedback->commit);
516 feedback->frame_stamp = frame_stamp;
517 feedback->target = feedback->commit;
518
519 wl_list_insert(&window->feedback_list, &feedback->link);
520}
521
522static void
523window_commit_next(struct window *window)
524{
525 struct buffer *buffer;
526
527 buffer = window_next_buffer(window);
528 assert(buffer);
529
530 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
531 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
532 wl_surface_commit(window->surface);
533 buffer->busy = 1;
534}
535
536static const struct wl_callback_listener frame_listener_mode_feedback;
537
538static void
539redraw_mode_feedback(void *data, struct wl_callback *callback, uint32_t time)
540{
541 struct window *window = data;
542
543 if (callback && window->mode == RUN_MODE_FEEDBACK_IDLE)
544 sleep(1);
545
546 if (callback)
547 wl_callback_destroy(callback);
548
Mario Kleinerc1809b22015-06-21 21:25:16 +0200549 window_emulate_rendering(window);
550
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400551 window->callback = wl_surface_frame(window->surface);
552 wl_callback_add_listener(window->callback,
553 &frame_listener_mode_feedback, window);
554
555 window_create_feedback(window, time);
556 window_commit_next(window);
557}
558
559static const struct wl_callback_listener frame_listener_mode_feedback = {
560 redraw_mode_feedback
561};
562
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200563static const struct wp_presentation_feedback_listener feedkick_listener;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400564
565static void
566window_feedkick(struct window *window)
567{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200568 struct wp_presentation *pres = window->display->presentation;
569 struct wp_presentation_feedback *fback;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400570
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200571 fback = wp_presentation_feedback(pres, window->surface);
572 wp_presentation_feedback_add_listener(fback, &feedkick_listener,
573 window);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400574}
575
576static void
577feedkick_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200578 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400579 uint32_t tv_sec_hi,
580 uint32_t tv_sec_lo,
581 uint32_t tv_nsec,
582 uint32_t refresh_nsec,
583 uint32_t seq_hi,
584 uint32_t seq_lo,
585 uint32_t flags)
586{
587 struct window *window = data;
588
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200589 wp_presentation_feedback_destroy(presentation_feedback);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400590 window->refresh_nsec = refresh_nsec;
591
592 switch (window->mode) {
593 case RUN_MODE_PRESENT:
Mario Kleinerc1809b22015-06-21 21:25:16 +0200594 window_emulate_rendering(window);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400595 window_create_feedback(window, 0);
596 window_feedkick(window);
597 window_commit_next(window);
598 break;
599 case RUN_MODE_FEEDBACK:
600 case RUN_MODE_FEEDBACK_IDLE:
601 assert(0 && "bad mode");
602 }
603}
604
605static void
606feedkick_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200607 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400608{
609 struct window *window = data;
610
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200611 wp_presentation_feedback_destroy(presentation_feedback);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400612
613 switch (window->mode) {
614 case RUN_MODE_PRESENT:
Mario Kleinerc1809b22015-06-21 21:25:16 +0200615 window_emulate_rendering(window);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400616 window_create_feedback(window, 0);
617 window_feedkick(window);
618 window_commit_next(window);
619 break;
620 case RUN_MODE_FEEDBACK:
621 case RUN_MODE_FEEDBACK_IDLE:
622 assert(0 && "bad mode");
623 }
624}
625
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200626static const struct wp_presentation_feedback_listener feedkick_listener = {
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400627 feedback_sync_output,
628 feedkick_presented,
629 feedkick_discarded
630};
631
632static void
633firstdraw_mode_burst(struct window *window)
634{
Mario Kleinerc1809b22015-06-21 21:25:16 +0200635 window_emulate_rendering(window);
636
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400637 switch (window->mode) {
638 case RUN_MODE_PRESENT:
639 window_create_feedback(window, 0);
640 break;
641 case RUN_MODE_FEEDBACK:
642 case RUN_MODE_FEEDBACK_IDLE:
643 assert(0 && "bad mode");
644 }
645
646 window_feedkick(window);
647 window_commit_next(window);
648}
649
650static void
651window_prerender(struct window *window)
652{
653 int i;
654 int timefactor = 1000000 / window->num_buffers;
655
656 for (i = 0; i < window->num_buffers; i++) {
657 struct buffer *buf = &window->buffers[i];
658
659 if (buf->busy)
660 fprintf(stderr, "wl_buffer id %u) busy\n",
661 wl_proxy_get_id(
662 (struct wl_proxy *)buf->buffer));
663
664 paint_pixels(buf->shm_data, window->width, window->height,
665 i * timefactor);
666 }
667}
668
669static void
670output_destroy(struct output *o)
671{
672 wl_output_destroy(o->output);
673 wl_list_remove(&o->link);
674 free(o);
675}
676
677static void
678display_add_output(struct display *d, uint32_t name, uint32_t version)
679{
680 struct output *o;
681
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800682 o = zalloc(sizeof(*o));
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400683 assert(o);
684
685 o->output = wl_registry_bind(d->registry, name,
686 &wl_output_interface, 1);
687 o->name = name;
688 wl_list_insert(&d->output_list, &o->link);
689}
690
691static void
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200692presentation_clock_id(void *data, struct wp_presentation *presentation,
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400693 uint32_t clk_id)
694{
695 struct display *d = data;
696
697 d->clk_id = clk_id;
698}
699
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200700static const struct wp_presentation_listener presentation_listener = {
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400701 presentation_clock_id
702};
703
704static void
705shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
706{
707 struct display *d = data;
708
709 d->formats |= (1 << format);
710}
711
712static const struct wl_shm_listener shm_listener = {
713 shm_format
714};
715
716static void
717registry_handle_global(void *data, struct wl_registry *registry,
718 uint32_t name, const char *interface, uint32_t version)
719{
720 struct display *d = data;
721
722 if (strcmp(interface, "wl_compositor") == 0) {
723 d->compositor =
724 wl_registry_bind(registry,
725 name, &wl_compositor_interface, 1);
726 } else if (strcmp(interface, "wl_shell") == 0) {
727 d->shell = wl_registry_bind(registry,
728 name, &wl_shell_interface, 1);
729 } else if (strcmp(interface, "wl_shm") == 0) {
730 d->shm = wl_registry_bind(registry,
731 name, &wl_shm_interface, 1);
732 wl_shm_add_listener(d->shm, &shm_listener, d);
733 } else if (strcmp(interface, "wl_output") == 0) {
734 display_add_output(d, name, version);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200735 } else if (strcmp(interface, wp_presentation_interface.name) == 0) {
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400736 d->presentation =
737 wl_registry_bind(registry,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200738 name, &wp_presentation_interface, 1);
739 wp_presentation_add_listener(d->presentation,
740 &presentation_listener, d);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400741 }
742}
743
744static void
745registry_handle_global_remove(void *data, struct wl_registry *registry,
746 uint32_t name)
747{
748 struct display *d = data;
749 struct output *output, *otmp;
750
751 wl_list_for_each_safe(output, otmp, &d->output_list, link) {
752 if (output->name != name)
753 continue;
754
755 output_destroy(output);
756 }
757}
758
759static const struct wl_registry_listener registry_listener = {
760 registry_handle_global,
761 registry_handle_global_remove
762};
763
764static struct display *
765create_display(void)
766{
767 struct display *display;
768
769 display = malloc(sizeof *display);
770 if (display == NULL) {
771 fprintf(stderr, "out of memory\n");
772 exit(1);
773 }
774 display->display = wl_display_connect(NULL);
775 assert(display->display);
776
777 display->formats = 0;
778 display->clk_id = -1;
779 wl_list_init(&display->output_list);
780 display->registry = wl_display_get_registry(display->display);
781 wl_registry_add_listener(display->registry,
782 &registry_listener, display);
783 wl_display_roundtrip(display->display);
784 if (display->shm == NULL) {
785 fprintf(stderr, "No wl_shm global\n");
786 exit(1);
787 }
788
789 wl_display_roundtrip(display->display);
790
791 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
792 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
793 exit(1);
794 }
795
796 wl_display_get_fd(display->display);
797
798 return display;
799}
800
801static void
802destroy_display(struct display *display)
803{
804 while (!wl_list_empty(&display->output_list)) {
805 struct output *o;
806
807 o = wl_container_of(display->output_list.next, o, link);
808 output_destroy(o);
809 }
810
811 if (display->shm)
812 wl_shm_destroy(display->shm);
813
814 if (display->shell)
815 wl_shell_destroy(display->shell);
816
817 if (display->compositor)
818 wl_compositor_destroy(display->compositor);
819
820 wl_registry_destroy(display->registry);
821 wl_display_flush(display->display);
822 wl_display_disconnect(display->display);
823 free(display);
824}
825
826static int running = 1;
827
828static void
829signal_int(int signum)
830{
831 running = 0;
832}
833
834static void
835usage(const char *prog, int exit_code)
836{
837 fprintf(stderr, "Usage: %s [mode] [options]\n"
838 "where 'mode' is one of\n"
Mario Kleinerc1809b22015-06-21 21:25:16 +0200839 " -f\t\trun in feedback mode (default)\n"
840 " -i\t\trun in feedback-idle mode; sleep 1s between frames\n"
841 " -p\t\trun in low-latency presentation mode\n"
842 "and 'options' may include\n"
843 " -d msecs\temulate the time used for rendering by a delay \n"
844 "\t\tof the given milliseconds before commit\n\n",
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400845 prog);
846
847 fprintf(stderr, "Printed timing statistics, depending on mode:\n"
848 " commit sequence number\n"
849 " f2c: time from frame callback timestamp to commit\n"
850 " c2p: time from commit to presentation\n"
851 " f2p: time from frame callback timestamp to presentation\n"
852 " p2p: time from previous presentation to this one\n"
853 " t2p: time from target timestamp to presentation\n"
854 " seq: MSC\n");
855
856
857 exit(exit_code);
858}
859
860int
861main(int argc, char **argv)
862{
863 struct sigaction sigint;
864 struct display *display;
865 struct window *window;
866 int ret = 0;
867 enum run_mode mode = RUN_MODE_FEEDBACK;
868 int i;
Mario Kleinerc1809b22015-06-21 21:25:16 +0200869 int commit_delay_msecs = 0;
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400870
871 for (i = 1; i < argc; i++) {
872 if (strcmp("-f", argv[i]) == 0)
873 mode = RUN_MODE_FEEDBACK;
874 else if (strcmp("-i", argv[i]) == 0)
875 mode = RUN_MODE_FEEDBACK_IDLE;
876 else if (strcmp("-p", argv[i]) == 0)
877 mode = RUN_MODE_PRESENT;
Mario Kleinerc1809b22015-06-21 21:25:16 +0200878 else if ((strcmp("-d", argv[i]) == 0) && (i + 1 < argc)) {
879 i++;
880 commit_delay_msecs = atoi(argv[i]);
881 }
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400882 else
883 usage(argv[0], EXIT_FAILURE);
884 }
885
886 display = create_display();
Mario Kleinerc1809b22015-06-21 21:25:16 +0200887 window = create_window(display, 250, 250, mode, commit_delay_msecs);
Pekka Paalanenef2b5922014-09-23 22:08:49 -0400888 if (!window)
889 return 1;
890
891 sigint.sa_handler = signal_int;
892 sigemptyset(&sigint.sa_mask);
893 sigint.sa_flags = SA_RESETHAND;
894 sigaction(SIGINT, &sigint, NULL);
895
896 window_prerender(window);
897
898 switch (mode) {
899 case RUN_MODE_FEEDBACK:
900 case RUN_MODE_FEEDBACK_IDLE:
901 redraw_mode_feedback(window, NULL, 0);
902 break;
903 case RUN_MODE_PRESENT:
904 firstdraw_mode_burst(window);
905 break;
906 }
907
908 while (running && ret != -1)
909 ret = wl_display_dispatch(display->display);
910
911 fprintf(stderr, "presentation-shm exiting\n");
912 destroy_window(window);
913 destroy_display(display);
914
915 return 0;
916}