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