1 /*
2 * Copyright © 2011 Benjamin Franzke
3 * Copyright © 2010 Intel Corporation
4 * Copyright © 2014 Collabora, Ltd.
5 *
6 * 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:
12 *
13 * 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.
24 */
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>
39 #include "shared/helpers.h"
40 #include "shared/os-compatibility.h"
41 #include "presentation_timing-client-protocol.h"
42
43 enum run_mode {
44 RUN_MODE_FEEDBACK,
45 RUN_MODE_FEEDBACK_IDLE,
46 RUN_MODE_PRESENT,
47 };
48
49 static const char * const run_mode_name[] = {
50 [RUN_MODE_FEEDBACK] = "feedback",
51 [RUN_MODE_FEEDBACK_IDLE] = "feedback-idle",
52 [RUN_MODE_PRESENT] = "low-lat present",
53 };
54
55 struct output {
56 struct wl_output *output;
57 uint32_t name;
58 struct wl_list link;
59 };
60
61 struct display {
62 struct wl_display *display;
63 struct wl_registry *registry;
64 struct wl_compositor *compositor;
65 struct wl_shell *shell;
66
67 struct wl_shm *shm;
68 uint32_t formats;
69
70 struct presentation *presentation;
71 clockid_t clk_id;
72
73 struct wl_list output_list; /* struct output::link */
74 };
75
76 struct feedback {
77 struct window *window;
78 unsigned frame_no;
79 struct presentation_feedback *feedback;
80 struct timespec commit;
81 struct timespec target;
82 uint32_t frame_stamp;
83 struct wl_list link;
84 struct timespec present;
85 };
86
87 struct buffer {
88 struct wl_buffer *buffer;
89 void *shm_data;
90 int busy;
91 };
92
93 struct window {
94 struct display *display;
95 int width, height;
96 enum run_mode mode;
97 struct wl_surface *surface;
98 struct wl_shell_surface *shell_surface;
99
100 struct buffer *buffers;
101 int num_buffers;
102 int next;
103 int refresh_nsec;
104 int commit_delay_msecs;
105
106 struct wl_callback *callback;
107 struct wl_list feedback_list;
108
109 struct feedback *received_feedback;
110 };
111
112 #define NSEC_PER_SEC 1000000000
113
114 static void
buffer_release(void * data,struct wl_buffer * buffer)115 buffer_release(void *data, struct wl_buffer *buffer)
116 {
117 struct buffer *mybuf = data;
118
119 mybuf->busy = 0;
120 }
121
122 static const struct wl_buffer_listener buffer_listener = {
123 buffer_release
124 };
125
126 static int
create_shm_buffers(struct display * display,struct buffer ** buffers,int num_buffers,int width,int height,uint32_t format)127 create_shm_buffers(struct display *display, struct buffer **buffers,
128 int num_buffers, int width, int height, uint32_t format)
129 {
130 struct buffer *bufs;
131 struct wl_shm_pool *pool;
132 int fd, size, stride, offset;
133 void *data;
134 int i;
135
136 stride = width * 4;
137 size = stride * height * num_buffers;
138
139 fd = os_create_anonymous_file(size);
140 if (fd < 0) {
141 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
142 size);
143 return -1;
144 }
145
146 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
147 if (data == MAP_FAILED) {
148 fprintf(stderr, "mmap failed: %m\n");
149 close(fd);
150 return -1;
151 }
152
153 pool = wl_shm_create_pool(display->shm, fd, size);
154 offset = 0;
155
156 bufs = calloc(num_buffers, sizeof(*bufs));
157 assert(bufs);
158
159 for (i = 0; i < num_buffers; i++) {
160 bufs[i].buffer = wl_shm_pool_create_buffer(pool, offset,
161 width, height,
162 stride, format);
163 assert(bufs[i].buffer);
164 wl_buffer_add_listener(bufs[i].buffer,
165 &buffer_listener, &bufs[i]);
166
167 bufs[i].shm_data = (char *)data + offset;
168 offset += stride * height;
169 }
170
171 wl_shm_pool_destroy(pool);
172 close(fd);
173
174 *buffers = bufs;
175
176 return 0;
177 }
178
179 static void
handle_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)180 handle_ping(void *data, struct wl_shell_surface *shell_surface,
181 uint32_t serial)
182 {
183 wl_shell_surface_pong(shell_surface, serial);
184 }
185
186 static void
handle_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)187 handle_configure(void *data, struct wl_shell_surface *shell_surface,
188 uint32_t edges, int32_t width, int32_t height)
189 {
190 }
191
192 static void
handle_popup_done(void * data,struct wl_shell_surface * shell_surface)193 handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
194 {
195 }
196
197 static const struct wl_shell_surface_listener shell_surface_listener = {
198 handle_ping,
199 handle_configure,
200 handle_popup_done
201 };
202
203 static struct window *
create_window(struct display * display,int width,int height,enum run_mode mode,int commit_delay_msecs)204 create_window(struct display *display, int width, int height,
205 enum run_mode mode, int commit_delay_msecs)
206 {
207 struct window *window;
208 char title[128];
209 int ret;
210
211 snprintf(title, sizeof(title),
212 "presentation-shm: %s [Delay %i msecs]", run_mode_name[mode],
213 commit_delay_msecs);
214
215 window = calloc(1, sizeof *window);
216 if (!window)
217 return NULL;
218
219 window->commit_delay_msecs = commit_delay_msecs;
220 window->mode = mode;
221 window->callback = NULL;
222 wl_list_init(&window->feedback_list);
223 window->display = display;
224 window->width = width;
225 window->height = height;
226 window->surface = wl_compositor_create_surface(display->compositor);
227 window->shell_surface = wl_shell_get_shell_surface(display->shell,
228 window->surface);
229
230 if (window->shell_surface)
231 wl_shell_surface_add_listener(window->shell_surface,
232 &shell_surface_listener, window);
233
234 wl_shell_surface_set_title(window->shell_surface, title);
235
236 wl_shell_surface_set_toplevel(window->shell_surface);
237
238 window->num_buffers = 60;
239 window->refresh_nsec = NSEC_PER_SEC / 60; /* 60 Hz guess */
240 window->next = 0;
241 ret = create_shm_buffers(window->display,
242 &window->buffers, window->num_buffers,
243 window->width, window->height,
244 WL_SHM_FORMAT_XRGB8888);
245 assert(ret == 0);
246
247 return window;
248 }
249
250 static void
destroy_feedback(struct feedback * feedback)251 destroy_feedback(struct feedback *feedback)
252 {
253 if (feedback->feedback)
254 presentation_feedback_destroy(feedback->feedback);
255
256 wl_list_remove(&feedback->link);
257 free(feedback);
258 }
259
260 static void
destroy_window(struct window * window)261 destroy_window(struct window *window)
262 {
263 int i;
264
265 while (!wl_list_empty(&window->feedback_list)) {
266 struct feedback *f;
267
268 f = wl_container_of(window->feedback_list.next, f, link);
269 printf("clean up feedback %u\n", f->frame_no);
270 destroy_feedback(f);
271 }
272
273 if (window->callback)
274 wl_callback_destroy(window->callback);
275
276 wl_shell_surface_destroy(window->shell_surface);
277 wl_surface_destroy(window->surface);
278
279 for (i = 0; i < window->num_buffers; i++)
280 wl_buffer_destroy(window->buffers[i].buffer);
281 /* munmap(window->buffers[0].shm_data, size); */
282 free(window->buffers);
283
284 free(window);
285 }
286
287 static struct buffer *
window_next_buffer(struct window * window)288 window_next_buffer(struct window *window)
289 {
290 struct buffer *buf = &window->buffers[window->next];
291
292 window->next = (window->next + 1) % window->num_buffers;
293
294 return buf;
295 }
296
297 static void
paint_pixels(void * image,int width,int height,uint32_t phase)298 paint_pixels(void *image, int width, int height, uint32_t phase)
299 {
300 const int halfh = height / 2;
301 const int halfw = width / 2;
302 uint32_t *pixel = image;
303 int y, or;
304 double ang = M_PI * 2.0 / 1000000.0 * phase;
305 double s = sin(ang);
306 double c = cos(ang);
307
308 /* squared radii thresholds */
309 or = (halfw < halfh ? halfw : halfh) - 16;
310 or *= or;
311
312 for (y = 0; y < height; y++) {
313 int x;
314 int oy = y - halfh;
315 int y2 = oy * oy;
316
317 for (x = 0; x < width; x++) {
318 int ox = x - halfw;
319 uint32_t v = 0xff000000;
320 double rx, ry;
321
322 if (ox * ox + y2 > or) {
323 if (ox * oy > 0)
324 *pixel++ = 0xff000000;
325 else
326 *pixel++ = 0xffffffff;
327 continue;
328 }
329
330 rx = c * ox + s * oy;
331 ry = -s * ox + c * oy;
332
333 if (rx < 0.0)
334 v |= 0x00ff0000;
335 if (ry < 0.0)
336 v |= 0x0000ff00;
337 if ((rx < 0.0) == (ry < 0.0))
338 v |= 0x000000ff;
339
340 *pixel++ = v;
341 }
342 }
343 }
344
345 static void
feedback_sync_output(void * data,struct presentation_feedback * presentation_feedback,struct wl_output * output)346 feedback_sync_output(void *data,
347 struct presentation_feedback *presentation_feedback,
348 struct wl_output *output)
349 {
350 /* not interested */
351 }
352
353 static char *
pflags_to_str(uint32_t flags,char * str,unsigned len)354 pflags_to_str(uint32_t flags, char *str, unsigned len)
355 {
356 static const struct {
357 uint32_t flag;
358 char sym;
359 } desc[] = {
360 { PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
361 { PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
362 { PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
363 { PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
364 };
365 unsigned i;
366
367 *str = '\0';
368 if (len < ARRAY_LENGTH(desc) + 1)
369 return str;
370
371 for (i = 0; i < ARRAY_LENGTH(desc); i++)
372 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
373 str[ARRAY_LENGTH(desc)] = '\0';
374
375 return str;
376 }
377
378 static uint32_t
timespec_to_ms(const struct timespec * ts)379 timespec_to_ms(const struct timespec *ts)
380 {
381 return (uint32_t)ts->tv_sec * 1000 + ts->tv_nsec / 1000000;
382 }
383
384 static void
timespec_from_proto(struct timespec * tm,uint32_t tv_sec_hi,uint32_t tv_sec_lo,uint32_t tv_nsec)385 timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
386 uint32_t tv_sec_lo, uint32_t tv_nsec)
387 {
388 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
389 tm->tv_nsec = tv_nsec;
390 }
391
392 static int
timespec_diff_to_usec(const struct timespec * a,const struct timespec * b)393 timespec_diff_to_usec(const struct timespec *a, const struct timespec *b)
394 {
395 time_t secs = a->tv_sec - b->tv_sec;
396 long nsec = a->tv_nsec - b->tv_nsec;
397
398 return secs * 1000000 + nsec / 1000;
399 }
400
401 static void
feedback_presented(void * data,struct presentation_feedback * presentation_feedback,uint32_t tv_sec_hi,uint32_t tv_sec_lo,uint32_t tv_nsec,uint32_t refresh_nsec,uint32_t seq_hi,uint32_t seq_lo,uint32_t flags)402 feedback_presented(void *data,
403 struct presentation_feedback *presentation_feedback,
404 uint32_t tv_sec_hi,
405 uint32_t tv_sec_lo,
406 uint32_t tv_nsec,
407 uint32_t refresh_nsec,
408 uint32_t seq_hi,
409 uint32_t seq_lo,
410 uint32_t flags)
411 {
412 struct feedback *feedback = data;
413 struct window *window = feedback->window;
414 struct feedback *prev_feedback = window->received_feedback;
415 uint64_t seq = ((uint64_t)seq_hi << 32) + seq_lo;
416 const struct timespec *prevpresent;
417 uint32_t commit, present;
418 uint32_t f2c, c2p, f2p;
419 int p2p, t2p;
420 char flagstr[10];
421
422 timespec_from_proto(&feedback->present, tv_sec_hi, tv_sec_lo, tv_nsec);
423 commit = timespec_to_ms(&feedback->commit);
424 present = timespec_to_ms(&feedback->present);
425
426 if (prev_feedback)
427 prevpresent = &prev_feedback->present;
428 else
429 prevpresent = &feedback->present;
430
431 f2c = commit - feedback->frame_stamp;
432 c2p = present - commit;
433 f2p = present - feedback->frame_stamp;
434 p2p = timespec_diff_to_usec(&feedback->present, prevpresent);
435 t2p = timespec_diff_to_usec(&feedback->present, &feedback->target);
436
437 switch (window->mode) {
438 case RUN_MODE_PRESENT:
439 printf("%6u: c2p %4u ms, p2p %5d us, t2p %6d us, [%s] "
440 "seq %" PRIu64 "\n", feedback->frame_no, c2p,
441 p2p, t2p,
442 pflags_to_str(flags, flagstr, sizeof(flagstr)), seq);
443 break;
444 case RUN_MODE_FEEDBACK:
445 case RUN_MODE_FEEDBACK_IDLE:
446 printf("%6u: f2c %2u ms, c2p %2u ms, f2p %2u ms, p2p %5d us, "
447 "t2p %6d, [%s], seq %" PRIu64 "\n", feedback->frame_no,
448 f2c, c2p, f2p, p2p, t2p,
449 pflags_to_str(flags, flagstr, sizeof(flagstr)), seq);
450 }
451
452 if (window->received_feedback)
453 destroy_feedback(window->received_feedback);
454 window->received_feedback = feedback;
455 }
456
457 static void
feedback_discarded(void * data,struct presentation_feedback * presentation_feedback)458 feedback_discarded(void *data,
459 struct presentation_feedback *presentation_feedback)
460 {
461 struct feedback *feedback = data;
462
463 printf("discarded %u\n", feedback->frame_no);
464
465 destroy_feedback(feedback);
466 }
467
468 static const struct presentation_feedback_listener feedback_listener = {
469 feedback_sync_output,
470 feedback_presented,
471 feedback_discarded
472 };
473
474 static void
window_emulate_rendering(struct window * window)475 window_emulate_rendering(struct window *window)
476 {
477 struct timespec delay;
478 int ret;
479
480 if (window->commit_delay_msecs <= 0)
481 return;
482
483 delay.tv_sec = window->commit_delay_msecs / 1000;
484 delay.tv_nsec = (window->commit_delay_msecs % 1000) * 1000000;
485
486 ret = nanosleep(&delay, NULL);
487 if (ret)
488 printf("nanosleep failed: %m\n");
489 }
490
491 static void
window_create_feedback(struct window * window,uint32_t frame_stamp)492 window_create_feedback(struct window *window, uint32_t frame_stamp)
493 {
494 static unsigned seq;
495 struct presentation *pres = window->display->presentation;
496 struct feedback *feedback;
497
498 seq++;
499
500 if (!pres)
501 return;
502
503 feedback = calloc(1, sizeof *feedback);
504 if (!feedback)
505 return;
506
507 feedback->window = window;
508 feedback->feedback = presentation_feedback(pres, window->surface);
509 presentation_feedback_add_listener(feedback->feedback,
510 &feedback_listener, feedback);
511
512 feedback->frame_no = seq;
513
514 clock_gettime(window->display->clk_id, &feedback->commit);
515 feedback->frame_stamp = frame_stamp;
516 feedback->target = feedback->commit;
517
518 wl_list_insert(&window->feedback_list, &feedback->link);
519 }
520
521 static void
window_commit_next(struct window * window)522 window_commit_next(struct window *window)
523 {
524 struct buffer *buffer;
525
526 buffer = window_next_buffer(window);
527 assert(buffer);
528
529 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
530 wl_surface_damage(window->surface, 0, 0, window->width, window->height);
531 wl_surface_commit(window->surface);
532 buffer->busy = 1;
533 }
534
535 static const struct wl_callback_listener frame_listener_mode_feedback;
536
537 static void
redraw_mode_feedback(void * data,struct wl_callback * callback,uint32_t time)538 redraw_mode_feedback(void *data, struct wl_callback *callback, uint32_t time)
539 {
540 struct window *window = data;
541
542 if (callback && window->mode == RUN_MODE_FEEDBACK_IDLE)
543 sleep(1);
544
545 if (callback)
546 wl_callback_destroy(callback);
547
548 window_emulate_rendering(window);
549
550 window->callback = wl_surface_frame(window->surface);
551 wl_callback_add_listener(window->callback,
552 &frame_listener_mode_feedback, window);
553
554 window_create_feedback(window, time);
555 window_commit_next(window);
556 }
557
558 static const struct wl_callback_listener frame_listener_mode_feedback = {
559 redraw_mode_feedback
560 };
561
562 static const struct presentation_feedback_listener feedkick_listener;
563
564 static void
window_feedkick(struct window * window)565 window_feedkick(struct window *window)
566 {
567 struct presentation *pres = window->display->presentation;
568 struct presentation_feedback *fback;
569
570 fback = presentation_feedback(pres, window->surface);
571 presentation_feedback_add_listener(fback, &feedkick_listener, window);
572 }
573
574 static void
feedkick_presented(void * data,struct presentation_feedback * presentation_feedback,uint32_t tv_sec_hi,uint32_t tv_sec_lo,uint32_t tv_nsec,uint32_t refresh_nsec,uint32_t seq_hi,uint32_t seq_lo,uint32_t flags)575 feedkick_presented(void *data,
576 struct presentation_feedback *presentation_feedback,
577 uint32_t tv_sec_hi,
578 uint32_t tv_sec_lo,
579 uint32_t tv_nsec,
580 uint32_t refresh_nsec,
581 uint32_t seq_hi,
582 uint32_t seq_lo,
583 uint32_t flags)
584 {
585 struct window *window = data;
586
587 presentation_feedback_destroy(presentation_feedback);
588 window->refresh_nsec = refresh_nsec;
589
590 switch (window->mode) {
591 case RUN_MODE_PRESENT:
592 window_emulate_rendering(window);
593 window_create_feedback(window, 0);
594 window_feedkick(window);
595 window_commit_next(window);
596 break;
597 case RUN_MODE_FEEDBACK:
598 case RUN_MODE_FEEDBACK_IDLE:
599 assert(0 && "bad mode");
600 }
601 }
602
603 static void
feedkick_discarded(void * data,struct presentation_feedback * presentation_feedback)604 feedkick_discarded(void *data,
605 struct presentation_feedback *presentation_feedback)
606 {
607 struct window *window = data;
608
609 presentation_feedback_destroy(presentation_feedback);
610
611 switch (window->mode) {
612 case RUN_MODE_PRESENT:
613 window_emulate_rendering(window);
614 window_create_feedback(window, 0);
615 window_feedkick(window);
616 window_commit_next(window);
617 break;
618 case RUN_MODE_FEEDBACK:
619 case RUN_MODE_FEEDBACK_IDLE:
620 assert(0 && "bad mode");
621 }
622 }
623
624 static const struct presentation_feedback_listener feedkick_listener = {
625 feedback_sync_output,
626 feedkick_presented,
627 feedkick_discarded
628 };
629
630 static void
firstdraw_mode_burst(struct window * window)631 firstdraw_mode_burst(struct window *window)
632 {
633 window_emulate_rendering(window);
634
635 switch (window->mode) {
636 case RUN_MODE_PRESENT:
637 window_create_feedback(window, 0);
638 break;
639 case RUN_MODE_FEEDBACK:
640 case RUN_MODE_FEEDBACK_IDLE:
641 assert(0 && "bad mode");
642 }
643
644 window_feedkick(window);
645 window_commit_next(window);
646 }
647
648 static void
window_prerender(struct window * window)649 window_prerender(struct window *window)
650 {
651 int i;
652 int timefactor = 1000000 / window->num_buffers;
653
654 for (i = 0; i < window->num_buffers; i++) {
655 struct buffer *buf = &window->buffers[i];
656
657 if (buf->busy)
658 fprintf(stderr, "wl_buffer id %u) busy\n",
659 wl_proxy_get_id(
660 (struct wl_proxy *)buf->buffer));
661
662 paint_pixels(buf->shm_data, window->width, window->height,
663 i * timefactor);
664 }
665 }
666
667 static void
output_destroy(struct output * o)668 output_destroy(struct output *o)
669 {
670 wl_output_destroy(o->output);
671 wl_list_remove(&o->link);
672 free(o);
673 }
674
675 static void
display_add_output(struct display * d,uint32_t name,uint32_t version)676 display_add_output(struct display *d, uint32_t name, uint32_t version)
677 {
678 struct output *o;
679
680 o = calloc(1, sizeof(*o));
681 assert(o);
682
683 o->output = wl_registry_bind(d->registry, name,
684 &wl_output_interface, 1);
685 o->name = name;
686 wl_list_insert(&d->output_list, &o->link);
687 }
688
689 static void
presentation_clock_id(void * data,struct presentation * presentation,uint32_t clk_id)690 presentation_clock_id(void *data, struct presentation *presentation,
691 uint32_t clk_id)
692 {
693 struct display *d = data;
694
695 d->clk_id = clk_id;
696 }
697
698 static const struct presentation_listener presentation_listener = {
699 presentation_clock_id
700 };
701
702 static void
shm_format(void * data,struct wl_shm * wl_shm,uint32_t format)703 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
704 {
705 struct display *d = data;
706
707 d->formats |= (1 << format);
708 }
709
710 static const struct wl_shm_listener shm_listener = {
711 shm_format
712 };
713
714 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)715 registry_handle_global(void *data, struct wl_registry *registry,
716 uint32_t name, const char *interface, uint32_t version)
717 {
718 struct display *d = data;
719
720 if (strcmp(interface, "wl_compositor") == 0) {
721 d->compositor =
722 wl_registry_bind(registry,
723 name, &wl_compositor_interface, 1);
724 } else if (strcmp(interface, "wl_shell") == 0) {
725 d->shell = wl_registry_bind(registry,
726 name, &wl_shell_interface, 1);
727 } else if (strcmp(interface, "wl_shm") == 0) {
728 d->shm = wl_registry_bind(registry,
729 name, &wl_shm_interface, 1);
730 wl_shm_add_listener(d->shm, &shm_listener, d);
731 } else if (strcmp(interface, "wl_output") == 0) {
732 display_add_output(d, name, version);
733 } else if (strcmp(interface, "presentation") == 0) {
734 d->presentation =
735 wl_registry_bind(registry,
736 name, &presentation_interface, 1);
737 presentation_add_listener(d->presentation,
738 &presentation_listener, d);
739 }
740 }
741
742 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)743 registry_handle_global_remove(void *data, struct wl_registry *registry,
744 uint32_t name)
745 {
746 struct display *d = data;
747 struct output *output, *otmp;
748
749 wl_list_for_each_safe(output, otmp, &d->output_list, link) {
750 if (output->name != name)
751 continue;
752
753 output_destroy(output);
754 }
755 }
756
757 static const struct wl_registry_listener registry_listener = {
758 registry_handle_global,
759 registry_handle_global_remove
760 };
761
762 static struct display *
create_display(void)763 create_display(void)
764 {
765 struct display *display;
766
767 display = malloc(sizeof *display);
768 if (display == NULL) {
769 fprintf(stderr, "out of memory\n");
770 exit(1);
771 }
772 display->display = wl_display_connect(NULL);
773 assert(display->display);
774
775 display->formats = 0;
776 display->clk_id = -1;
777 wl_list_init(&display->output_list);
778 display->registry = wl_display_get_registry(display->display);
779 wl_registry_add_listener(display->registry,
780 ®istry_listener, display);
781 wl_display_roundtrip(display->display);
782 if (display->shm == NULL) {
783 fprintf(stderr, "No wl_shm global\n");
784 exit(1);
785 }
786
787 wl_display_roundtrip(display->display);
788
789 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
790 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
791 exit(1);
792 }
793
794 wl_display_get_fd(display->display);
795
796 return display;
797 }
798
799 static void
destroy_display(struct display * display)800 destroy_display(struct display *display)
801 {
802 while (!wl_list_empty(&display->output_list)) {
803 struct output *o;
804
805 o = wl_container_of(display->output_list.next, o, link);
806 output_destroy(o);
807 }
808
809 if (display->shm)
810 wl_shm_destroy(display->shm);
811
812 if (display->shell)
813 wl_shell_destroy(display->shell);
814
815 if (display->compositor)
816 wl_compositor_destroy(display->compositor);
817
818 wl_registry_destroy(display->registry);
819 wl_display_flush(display->display);
820 wl_display_disconnect(display->display);
821 free(display);
822 }
823
824 static int running = 1;
825
826 static void
signal_int(int signum)827 signal_int(int signum)
828 {
829 running = 0;
830 }
831
832 static void
usage(const char * prog,int exit_code)833 usage(const char *prog, int exit_code)
834 {
835 fprintf(stderr, "Usage: %s [mode] [options]\n"
836 "where 'mode' is one of\n"
837 " -f\t\trun in feedback mode (default)\n"
838 " -i\t\trun in feedback-idle mode; sleep 1s between frames\n"
839 " -p\t\trun in low-latency presentation mode\n"
840 "and 'options' may include\n"
841 " -d msecs\temulate the time used for rendering by a delay \n"
842 "\t\tof the given milliseconds before commit\n\n",
843 prog);
844
845 fprintf(stderr, "Printed timing statistics, depending on mode:\n"
846 " commit sequence number\n"
847 " f2c: time from frame callback timestamp to commit\n"
848 " c2p: time from commit to presentation\n"
849 " f2p: time from frame callback timestamp to presentation\n"
850 " p2p: time from previous presentation to this one\n"
851 " t2p: time from target timestamp to presentation\n"
852 " seq: MSC\n");
853
854
855 exit(exit_code);
856 }
857
858 int
main(int argc,char ** argv)859 main(int argc, char **argv)
860 {
861 struct sigaction sigint;
862 struct display *display;
863 struct window *window;
864 int ret = 0;
865 enum run_mode mode = RUN_MODE_FEEDBACK;
866 int i;
867 int commit_delay_msecs = 0;
868
869 for (i = 1; i < argc; i++) {
870 if (strcmp("-f", argv[i]) == 0)
871 mode = RUN_MODE_FEEDBACK;
872 else if (strcmp("-i", argv[i]) == 0)
873 mode = RUN_MODE_FEEDBACK_IDLE;
874 else if (strcmp("-p", argv[i]) == 0)
875 mode = RUN_MODE_PRESENT;
876 else if ((strcmp("-d", argv[i]) == 0) && (i + 1 < argc)) {
877 i++;
878 commit_delay_msecs = atoi(argv[i]);
879 }
880 else
881 usage(argv[0], EXIT_FAILURE);
882 }
883
884 display = create_display();
885 window = create_window(display, 250, 250, mode, commit_delay_msecs);
886 if (!window)
887 return 1;
888
889 sigint.sa_handler = signal_int;
890 sigemptyset(&sigint.sa_mask);
891 sigint.sa_flags = SA_RESETHAND;
892 sigaction(SIGINT, &sigint, NULL);
893
894 window_prerender(window);
895
896 switch (mode) {
897 case RUN_MODE_FEEDBACK:
898 case RUN_MODE_FEEDBACK_IDLE:
899 redraw_mode_feedback(window, NULL, 0);
900 break;
901 case RUN_MODE_PRESENT:
902 firstdraw_mode_burst(window);
903 break;
904 }
905
906 while (running && ret != -1)
907 ret = wl_display_dispatch(display->display);
908
909 fprintf(stderr, "presentation-shm exiting\n");
910 destroy_window(window);
911 destroy_display(display);
912
913 return 0;
914 }
915