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