blob: f12f8eefcc20ef5db990bd07c0ee731bfad6ef92 [file] [log] [blame]
Pekka Paalanende7f5c82014-09-23 22:08:48 -04001/*
2 * Copyright © 2014 Collabora, Ltd.
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
Pekka Paalanende7f5c82014-09-23 22:08:48 -040011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
Pekka Paalanende7f5c82014-09-23 22:08:48 -040024 */
25
26#include "config.h"
27
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040029#include <stdio.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080030#include <stdlib.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040031#include <string.h>
32#include <assert.h>
33#include <time.h>
34
Jon Cruz35b2eaa2015-06-15 15:37:08 -070035#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070036#include "shared/xalloc.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040037#include "weston-test-client-helper.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020038#include "presentation-time-client-protocol.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040039
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020040static struct wp_presentation *
Pekka Paalanende7f5c82014-09-23 22:08:48 -040041get_presentation(struct client *client)
42{
43 struct global *g;
44 struct global *global_pres = NULL;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020045 static struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040046
47 if (pres)
48 return pres;
49
50 wl_list_for_each(g, &client->global_list, link) {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020051 if (strcmp(g->interface, wp_presentation_interface.name))
Pekka Paalanende7f5c82014-09-23 22:08:48 -040052 continue;
53
54 if (global_pres)
55 assert(0 && "multiple presentation objects");
56
57 global_pres = g;
58 }
59
60 assert(global_pres && "no presentation found");
61
62 assert(global_pres->version == 1);
63
64 pres = wl_registry_bind(client->wl_registry, global_pres->name,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020065 &wp_presentation_interface, 1);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040066 assert(pres);
67
68 return pres;
69}
70
71struct feedback {
72 struct client *client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020073 struct wp_presentation_feedback *obj;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040074
75 enum {
76 FB_PENDING = 0,
77 FB_PRESENTED,
78 FB_DISCARDED
79 } result;
80
81 struct wl_output *sync_output;
82 uint64_t seq;
83 struct timespec time;
84 uint32_t refresh_nsec;
85 uint32_t flags;
86};
87
88static void
89timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
90 uint32_t tv_sec_lo, uint32_t tv_nsec)
91{
92 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
93 tm->tv_nsec = tv_nsec;
94}
95
96static void
97feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020098 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -040099 struct wl_output *output)
100{
101 struct feedback *fb = data;
102
103 assert(fb->result == FB_PENDING);
104
105 if (output)
106 fb->sync_output = output;
107}
108
109static void
110feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200111 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400112 uint32_t tv_sec_hi,
113 uint32_t tv_sec_lo,
114 uint32_t tv_nsec,
115 uint32_t refresh_nsec,
116 uint32_t seq_hi,
117 uint32_t seq_lo,
118 uint32_t flags)
119{
120 struct feedback *fb = data;
121
122 assert(fb->result == FB_PENDING);
123 fb->result = FB_PRESENTED;
124 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
125 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
126 fb->refresh_nsec = refresh_nsec;
127 fb->flags = flags;
128}
129
130static void
131feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200132 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400133{
134 struct feedback *fb = data;
135
136 assert(fb->result == FB_PENDING);
137 fb->result = FB_DISCARDED;
138}
139
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200140static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400141 feedback_sync_output,
142 feedback_presented,
143 feedback_discarded
144};
145
146static struct feedback *
147feedback_create(struct client *client, struct wl_surface *surface)
148{
149 struct feedback *fb;
150
151 fb = xzalloc(sizeof *fb);
152 fb->client = client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200153 fb->obj = wp_presentation_feedback(get_presentation(client), surface);
154 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400155
156 return fb;
157}
158
159static void
160feedback_wait(struct feedback *fb)
161{
162 while (fb->result == FB_PENDING) {
163 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
164 }
165}
166
167static char *
168pflags_to_str(uint32_t flags, char *str, unsigned len)
169{
170 static const struct {
171 uint32_t flag;
172 char sym;
173 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200174 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
175 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
176 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
177 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400178 };
179 unsigned i;
180
181 *str = '\0';
182 if (len < ARRAY_LENGTH(desc) + 1)
183 return str;
184
185 for (i = 0; i < ARRAY_LENGTH(desc); i++)
186 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
187 str[ARRAY_LENGTH(desc)] = '\0';
188
189 return str;
190}
191
192static void
193feedback_print(struct feedback *fb)
194{
195 char str[10];
196
197 switch (fb->result) {
198 case FB_PENDING:
199 printf("pending");
200 return;
201 case FB_DISCARDED:
202 printf("discarded");
203 return;
204 case FB_PRESENTED:
205 break;
206 }
207
208 pflags_to_str(fb->flags, str, sizeof str);
209 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
210 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
211 fb->refresh_nsec / 1000, str, fb->seq);
212}
213
214static void
215feedback_destroy(struct feedback *fb)
216{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200217 wp_presentation_feedback_destroy(fb->obj);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400218 free(fb);
219}
220
221TEST(test_presentation_feedback_simple)
222{
223 struct client *client;
224 struct feedback *fb;
225
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200226 client = create_client_and_test_surface(100, 50, 123, 77);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400227 assert(client);
228
229 wl_surface_attach(client->surface->wl_surface,
Pekka Paalanen924cd942016-05-20 17:25:38 +0300230 client->surface->buffer->proxy, 0, 0);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400231 fb = feedback_create(client, client->surface->wl_surface);
232 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
233 wl_surface_commit(client->surface->wl_surface);
234
235 client_roundtrip(client);
236
237 feedback_wait(fb);
238
239 printf("%s feedback:", __func__);
240 feedback_print(fb);
241 printf("\n");
242
243 feedback_destroy(fb);
244}