1 /*
2 * Copyright © 2014 Collabora, Ltd.
3 *
4 * 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:
11 *
12 * 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.
24 */
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <time.h>
34
35 #include "shared/helpers.h"
36 #include "shared/xalloc.h"
37 #include "shared/timespec-util.h"
38 #include "weston-test-client-helper.h"
39 #include "presentation-time-client-protocol.h"
40 #include "weston-test-fixture-compositor.h"
41
42 static enum test_result_code
fixture_setup(struct weston_test_harness * harness)43 fixture_setup(struct weston_test_harness *harness)
44 {
45 struct compositor_setup setup;
46
47 compositor_setup_defaults(&setup);
48
49 return weston_test_harness_execute_as_client(harness, &setup);
50 }
51 DECLARE_FIXTURE_SETUP(fixture_setup);
52
53 static struct wp_presentation *
get_presentation(struct client * client)54 get_presentation(struct client *client)
55 {
56 struct global *g;
57 struct global *global_pres = NULL;
58 struct wp_presentation *pres;
59
60 wl_list_for_each(g, &client->global_list, link) {
61 if (strcmp(g->interface, wp_presentation_interface.name))
62 continue;
63
64 if (global_pres)
65 assert(0 && "multiple presentation objects");
66
67 global_pres = g;
68 }
69
70 assert(global_pres && "no presentation found");
71
72 assert(global_pres->version == 1);
73
74 pres = wl_registry_bind(client->wl_registry, global_pres->name,
75 &wp_presentation_interface, 1);
76 assert(pres);
77
78 return pres;
79 }
80
81 struct feedback {
82 struct client *client;
83 struct wp_presentation_feedback *obj;
84
85 enum {
86 FB_PENDING = 0,
87 FB_PRESENTED,
88 FB_DISCARDED
89 } result;
90
91 struct wl_output *sync_output;
92 uint64_t seq;
93 struct timespec time;
94 uint32_t refresh_nsec;
95 uint32_t flags;
96 };
97
98 static void
feedback_sync_output(void * data,struct wp_presentation_feedback * presentation_feedback,struct wl_output * output)99 feedback_sync_output(void *data,
100 struct wp_presentation_feedback *presentation_feedback,
101 struct wl_output *output)
102 {
103 struct feedback *fb = data;
104
105 assert(fb->result == FB_PENDING);
106
107 if (output)
108 fb->sync_output = output;
109 }
110
111 static void
feedback_presented(void * data,struct wp_presentation_feedback * presentation_feedback,uint32_t tv_sec_hi,uint32_t tv_sec_lo,uint32_t tv_nsec,uint32_t refresh_nsec,uint32_t seq_hi,uint32_t seq_lo,uint32_t flags)112 feedback_presented(void *data,
113 struct wp_presentation_feedback *presentation_feedback,
114 uint32_t tv_sec_hi,
115 uint32_t tv_sec_lo,
116 uint32_t tv_nsec,
117 uint32_t refresh_nsec,
118 uint32_t seq_hi,
119 uint32_t seq_lo,
120 uint32_t flags)
121 {
122 struct feedback *fb = data;
123
124 assert(fb->result == FB_PENDING);
125 fb->result = FB_PRESENTED;
126 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
127 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
128 fb->refresh_nsec = refresh_nsec;
129 fb->flags = flags;
130 }
131
132 static void
feedback_discarded(void * data,struct wp_presentation_feedback * presentation_feedback)133 feedback_discarded(void *data,
134 struct wp_presentation_feedback *presentation_feedback)
135 {
136 struct feedback *fb = data;
137
138 assert(fb->result == FB_PENDING);
139 fb->result = FB_DISCARDED;
140 }
141
142 static const struct wp_presentation_feedback_listener feedback_listener = {
143 feedback_sync_output,
144 feedback_presented,
145 feedback_discarded
146 };
147
148 static struct feedback *
feedback_create(struct client * client,struct wl_surface * surface,struct wp_presentation * pres)149 feedback_create(struct client *client,
150 struct wl_surface *surface,
151 struct wp_presentation *pres)
152 {
153 struct feedback *fb;
154
155 fb = xzalloc(sizeof *fb);
156 fb->client = client;
157 fb->obj = wp_presentation_feedback(pres, surface);
158 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
159
160 return fb;
161 }
162
163 static void
feedback_wait(struct feedback * fb)164 feedback_wait(struct feedback *fb)
165 {
166 while (fb->result == FB_PENDING) {
167 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
168 }
169 }
170
171 static char *
pflags_to_str(uint32_t flags,char * str,unsigned len)172 pflags_to_str(uint32_t flags, char *str, unsigned len)
173 {
174 static const struct {
175 uint32_t flag;
176 char sym;
177 } desc[] = {
178 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
179 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
180 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
181 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
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
196 static void
feedback_print(struct feedback * fb)197 feedback_print(struct feedback *fb)
198 {
199 char str[10];
200
201 switch (fb->result) {
202 case FB_PENDING:
203 testlog("pending");
204 return;
205 case FB_DISCARDED:
206 testlog("discarded");
207 return;
208 case FB_PRESENTED:
209 break;
210 }
211
212 pflags_to_str(fb->flags, str, sizeof str);
213 testlog("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
218 static void
feedback_destroy(struct feedback * fb)219 feedback_destroy(struct feedback *fb)
220 {
221 wp_presentation_feedback_destroy(fb->obj);
222 free(fb);
223 }
224
TEST(test_presentation_feedback_simple)225 TEST(test_presentation_feedback_simple)
226 {
227 struct client *client;
228 struct feedback *fb;
229 struct wp_presentation *pres;
230
231 client = create_client_and_test_surface(100, 50, 123, 77);
232 assert(client);
233 pres = get_presentation(client);
234
235 wl_surface_attach(client->surface->wl_surface,
236 client->surface->buffer->proxy, 0, 0);
237 fb = feedback_create(client, client->surface->wl_surface, pres);
238 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
239 wl_surface_commit(client->surface->wl_surface);
240
241 client_roundtrip(client);
242
243 feedback_wait(fb);
244
245 testlog("%s feedback:", __func__);
246 feedback_print(fb);
247 testlog("\n");
248
249 feedback_destroy(fb);
250 }
251