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