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