blob: 421eddc11878485c952e5b8499065c85e0a22523 [file] [log] [blame]
Pekka Paalanende7f5c82014-09-23 22:08:48 -04001/*
2 * Copyright © 2014 Collabora, Ltd.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include "config.h"
24
25#include <stdio.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080026#include <stdlib.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040027#include <string.h>
28#include <assert.h>
29#include <time.h>
30
31#include "weston-test-client-helper.h"
32#include "presentation_timing-client-protocol.h"
33
Pekka Paalanende7f5c82014-09-23 22:08:48 -040034static struct presentation *
35get_presentation(struct client *client)
36{
37 struct global *g;
38 struct global *global_pres = NULL;
39 static struct presentation *pres;
40
41 if (pres)
42 return pres;
43
44 wl_list_for_each(g, &client->global_list, link) {
45 if (strcmp(g->interface, "presentation"))
46 continue;
47
48 if (global_pres)
49 assert(0 && "multiple presentation objects");
50
51 global_pres = g;
52 }
53
54 assert(global_pres && "no presentation found");
55
56 assert(global_pres->version == 1);
57
58 pres = wl_registry_bind(client->wl_registry, global_pres->name,
59 &presentation_interface, 1);
60 assert(pres);
61
62 return pres;
63}
64
65struct feedback {
66 struct client *client;
67 struct presentation_feedback *obj;
68
69 enum {
70 FB_PENDING = 0,
71 FB_PRESENTED,
72 FB_DISCARDED
73 } result;
74
75 struct wl_output *sync_output;
76 uint64_t seq;
77 struct timespec time;
78 uint32_t refresh_nsec;
79 uint32_t flags;
80};
81
82static void
83timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
84 uint32_t tv_sec_lo, uint32_t tv_nsec)
85{
86 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
87 tm->tv_nsec = tv_nsec;
88}
89
90static void
91feedback_sync_output(void *data,
92 struct presentation_feedback *presentation_feedback,
93 struct wl_output *output)
94{
95 struct feedback *fb = data;
96
97 assert(fb->result == FB_PENDING);
98
99 if (output)
100 fb->sync_output = output;
101}
102
103static void
104feedback_presented(void *data,
105 struct presentation_feedback *presentation_feedback,
106 uint32_t tv_sec_hi,
107 uint32_t tv_sec_lo,
108 uint32_t tv_nsec,
109 uint32_t refresh_nsec,
110 uint32_t seq_hi,
111 uint32_t seq_lo,
112 uint32_t flags)
113{
114 struct feedback *fb = data;
115
116 assert(fb->result == FB_PENDING);
117 fb->result = FB_PRESENTED;
118 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
119 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
120 fb->refresh_nsec = refresh_nsec;
121 fb->flags = flags;
122}
123
124static void
125feedback_discarded(void *data,
126 struct presentation_feedback *presentation_feedback)
127{
128 struct feedback *fb = data;
129
130 assert(fb->result == FB_PENDING);
131 fb->result = FB_DISCARDED;
132}
133
134static const struct presentation_feedback_listener feedback_listener = {
135 feedback_sync_output,
136 feedback_presented,
137 feedback_discarded
138};
139
140static struct feedback *
141feedback_create(struct client *client, struct wl_surface *surface)
142{
143 struct feedback *fb;
144
145 fb = xzalloc(sizeof *fb);
146 fb->client = client;
147 fb->obj = presentation_feedback(get_presentation(client), surface);
148 presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
149
150 return fb;
151}
152
153static void
154feedback_wait(struct feedback *fb)
155{
156 while (fb->result == FB_PENDING) {
157 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
158 }
159}
160
161static char *
162pflags_to_str(uint32_t flags, char *str, unsigned len)
163{
164 static const struct {
165 uint32_t flag;
166 char sym;
167 } desc[] = {
Pekka Paalanen63495862014-12-17 16:20:42 +0200168 { PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
169 { PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
170 { PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
171 { PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400172 };
173 unsigned i;
174
175 *str = '\0';
176 if (len < ARRAY_LENGTH(desc) + 1)
177 return str;
178
179 for (i = 0; i < ARRAY_LENGTH(desc); i++)
180 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
181 str[ARRAY_LENGTH(desc)] = '\0';
182
183 return str;
184}
185
186static void
187feedback_print(struct feedback *fb)
188{
189 char str[10];
190
191 switch (fb->result) {
192 case FB_PENDING:
193 printf("pending");
194 return;
195 case FB_DISCARDED:
196 printf("discarded");
197 return;
198 case FB_PRESENTED:
199 break;
200 }
201
202 pflags_to_str(fb->flags, str, sizeof str);
203 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
204 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
205 fb->refresh_nsec / 1000, str, fb->seq);
206}
207
208static void
209feedback_destroy(struct feedback *fb)
210{
211 presentation_feedback_destroy(fb->obj);
212 free(fb);
213}
214
215TEST(test_presentation_feedback_simple)
216{
217 struct client *client;
218 struct feedback *fb;
219
220 client = client_create(100, 50, 123, 77);
221 assert(client);
222
223 wl_surface_attach(client->surface->wl_surface,
224 client->surface->wl_buffer, 0, 0);
225 fb = feedback_create(client, client->surface->wl_surface);
226 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
227 wl_surface_commit(client->surface->wl_surface);
228
229 client_roundtrip(client);
230
231 feedback_wait(fb);
232
233 printf("%s feedback:", __func__);
234 feedback_print(fb);
235 printf("\n");
236
237 feedback_destroy(fb);
238}