blob: 59cca8e1f0ca408738cc6a581f393af92012097a [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[] = {
168 { 1, '1' }, /* dummy placeholder */
169 };
170 unsigned i;
171
172 *str = '\0';
173 if (len < ARRAY_LENGTH(desc) + 1)
174 return str;
175
176 for (i = 0; i < ARRAY_LENGTH(desc); i++)
177 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
178 str[ARRAY_LENGTH(desc)] = '\0';
179
180 return str;
181}
182
183static void
184feedback_print(struct feedback *fb)
185{
186 char str[10];
187
188 switch (fb->result) {
189 case FB_PENDING:
190 printf("pending");
191 return;
192 case FB_DISCARDED:
193 printf("discarded");
194 return;
195 case FB_PRESENTED:
196 break;
197 }
198
199 pflags_to_str(fb->flags, str, sizeof str);
200 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
201 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
202 fb->refresh_nsec / 1000, str, fb->seq);
203}
204
205static void
206feedback_destroy(struct feedback *fb)
207{
208 presentation_feedback_destroy(fb->obj);
209 free(fb);
210}
211
212TEST(test_presentation_feedback_simple)
213{
214 struct client *client;
215 struct feedback *fb;
216
217 client = client_create(100, 50, 123, 77);
218 assert(client);
219
220 wl_surface_attach(client->surface->wl_surface,
221 client->surface->wl_buffer, 0, 0);
222 fb = feedback_create(client, client->surface->wl_surface);
223 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
224 wl_surface_commit(client->surface->wl_surface);
225
226 client_roundtrip(client);
227
228 feedback_wait(fb);
229
230 printf("%s feedback:", __func__);
231 feedback_print(fb);
232 printf("\n");
233
234 feedback_destroy(fb);
235}