blob: 27f33daa65966b12ecf23bfbd03a985fdfff1962 [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
28#include <stdio.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080029#include <stdlib.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040030#include <string.h>
31#include <assert.h>
32#include <time.h>
33
Jon Cruz35b2eaa2015-06-15 15:37:08 -070034#include "shared/helpers.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040035#include "weston-test-client-helper.h"
36#include "presentation_timing-client-protocol.h"
37
Pekka Paalanende7f5c82014-09-23 22:08:48 -040038static struct presentation *
39get_presentation(struct client *client)
40{
41 struct global *g;
42 struct global *global_pres = NULL;
43 static struct presentation *pres;
44
45 if (pres)
46 return pres;
47
48 wl_list_for_each(g, &client->global_list, link) {
49 if (strcmp(g->interface, "presentation"))
50 continue;
51
52 if (global_pres)
53 assert(0 && "multiple presentation objects");
54
55 global_pres = g;
56 }
57
58 assert(global_pres && "no presentation found");
59
60 assert(global_pres->version == 1);
61
62 pres = wl_registry_bind(client->wl_registry, global_pres->name,
63 &presentation_interface, 1);
64 assert(pres);
65
66 return pres;
67}
68
69struct feedback {
70 struct client *client;
71 struct presentation_feedback *obj;
72
73 enum {
74 FB_PENDING = 0,
75 FB_PRESENTED,
76 FB_DISCARDED
77 } result;
78
79 struct wl_output *sync_output;
80 uint64_t seq;
81 struct timespec time;
82 uint32_t refresh_nsec;
83 uint32_t flags;
84};
85
86static void
87timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
88 uint32_t tv_sec_lo, uint32_t tv_nsec)
89{
90 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
91 tm->tv_nsec = tv_nsec;
92}
93
94static void
95feedback_sync_output(void *data,
96 struct presentation_feedback *presentation_feedback,
97 struct wl_output *output)
98{
99 struct feedback *fb = data;
100
101 assert(fb->result == FB_PENDING);
102
103 if (output)
104 fb->sync_output = output;
105}
106
107static void
108feedback_presented(void *data,
109 struct presentation_feedback *presentation_feedback,
110 uint32_t tv_sec_hi,
111 uint32_t tv_sec_lo,
112 uint32_t tv_nsec,
113 uint32_t refresh_nsec,
114 uint32_t seq_hi,
115 uint32_t seq_lo,
116 uint32_t flags)
117{
118 struct feedback *fb = data;
119
120 assert(fb->result == FB_PENDING);
121 fb->result = FB_PRESENTED;
122 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
123 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
124 fb->refresh_nsec = refresh_nsec;
125 fb->flags = flags;
126}
127
128static void
129feedback_discarded(void *data,
130 struct presentation_feedback *presentation_feedback)
131{
132 struct feedback *fb = data;
133
134 assert(fb->result == FB_PENDING);
135 fb->result = FB_DISCARDED;
136}
137
138static const struct presentation_feedback_listener feedback_listener = {
139 feedback_sync_output,
140 feedback_presented,
141 feedback_discarded
142};
143
144static struct feedback *
145feedback_create(struct client *client, struct wl_surface *surface)
146{
147 struct feedback *fb;
148
149 fb = xzalloc(sizeof *fb);
150 fb->client = client;
151 fb->obj = presentation_feedback(get_presentation(client), surface);
152 presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
153
154 return fb;
155}
156
157static void
158feedback_wait(struct feedback *fb)
159{
160 while (fb->result == FB_PENDING) {
161 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
162 }
163}
164
165static char *
166pflags_to_str(uint32_t flags, char *str, unsigned len)
167{
168 static const struct {
169 uint32_t flag;
170 char sym;
171 } desc[] = {
Pekka Paalanen63495862014-12-17 16:20:42 +0200172 { PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
173 { PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
174 { PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
175 { PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400176 };
177 unsigned i;
178
179 *str = '\0';
180 if (len < ARRAY_LENGTH(desc) + 1)
181 return str;
182
183 for (i = 0; i < ARRAY_LENGTH(desc); i++)
184 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
185 str[ARRAY_LENGTH(desc)] = '\0';
186
187 return str;
188}
189
190static void
191feedback_print(struct feedback *fb)
192{
193 char str[10];
194
195 switch (fb->result) {
196 case FB_PENDING:
197 printf("pending");
198 return;
199 case FB_DISCARDED:
200 printf("discarded");
201 return;
202 case FB_PRESENTED:
203 break;
204 }
205
206 pflags_to_str(fb->flags, str, sizeof str);
207 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
208 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
209 fb->refresh_nsec / 1000, str, fb->seq);
210}
211
212static void
213feedback_destroy(struct feedback *fb)
214{
215 presentation_feedback_destroy(fb->obj);
216 free(fb);
217}
218
219TEST(test_presentation_feedback_simple)
220{
221 struct client *client;
222 struct feedback *fb;
223
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200224 client = create_client_and_test_surface(100, 50, 123, 77);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400225 assert(client);
226
227 wl_surface_attach(client->surface->wl_surface,
228 client->surface->wl_buffer, 0, 0);
229 fb = feedback_create(client, client->surface->wl_surface);
230 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
231 wl_surface_commit(client->surface->wl_surface);
232
233 client_roundtrip(client);
234
235 feedback_wait(fb);
236
237 printf("%s feedback:", __func__);
238 feedback_print(fb);
239 printf("\n");
240
241 feedback_destroy(fb);
242}