blob: cb5ff1920a22cc1cfe0d736db87f6a7d0d611b8a [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
Jon Cruz35b2eaa2015-06-15 15:37:08 -070034#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070035#include "shared/xalloc.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040036#include "weston-test-client-helper.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020037#include "presentation-time-client-protocol.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040038
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020039static struct wp_presentation *
Pekka Paalanende7f5c82014-09-23 22:08:48 -040040get_presentation(struct client *client)
41{
42 struct global *g;
43 struct global *global_pres = NULL;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020044 static struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040045
46 if (pres)
47 return pres;
48
49 wl_list_for_each(g, &client->global_list, link) {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020050 if (strcmp(g->interface, wp_presentation_interface.name))
Pekka Paalanende7f5c82014-09-23 22:08:48 -040051 continue;
52
53 if (global_pres)
54 assert(0 && "multiple presentation objects");
55
56 global_pres = g;
57 }
58
59 assert(global_pres && "no presentation found");
60
61 assert(global_pres->version == 1);
62
63 pres = wl_registry_bind(client->wl_registry, global_pres->name,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020064 &wp_presentation_interface, 1);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040065 assert(pres);
66
67 return pres;
68}
69
70struct feedback {
71 struct client *client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020072 struct wp_presentation_feedback *obj;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040073
74 enum {
75 FB_PENDING = 0,
76 FB_PRESENTED,
77 FB_DISCARDED
78 } result;
79
80 struct wl_output *sync_output;
81 uint64_t seq;
82 struct timespec time;
83 uint32_t refresh_nsec;
84 uint32_t flags;
85};
86
87static void
88timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi,
89 uint32_t tv_sec_lo, uint32_t tv_nsec)
90{
91 tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
92 tm->tv_nsec = tv_nsec;
93}
94
95static void
96feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020097 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -040098 struct wl_output *output)
99{
100 struct feedback *fb = data;
101
102 assert(fb->result == FB_PENDING);
103
104 if (output)
105 fb->sync_output = output;
106}
107
108static void
109feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200110 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400111 uint32_t tv_sec_hi,
112 uint32_t tv_sec_lo,
113 uint32_t tv_nsec,
114 uint32_t refresh_nsec,
115 uint32_t seq_hi,
116 uint32_t seq_lo,
117 uint32_t flags)
118{
119 struct feedback *fb = data;
120
121 assert(fb->result == FB_PENDING);
122 fb->result = FB_PRESENTED;
123 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
124 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
125 fb->refresh_nsec = refresh_nsec;
126 fb->flags = flags;
127}
128
129static void
130feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200131 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400132{
133 struct feedback *fb = data;
134
135 assert(fb->result == FB_PENDING);
136 fb->result = FB_DISCARDED;
137}
138
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200139static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400140 feedback_sync_output,
141 feedback_presented,
142 feedback_discarded
143};
144
145static struct feedback *
146feedback_create(struct client *client, struct wl_surface *surface)
147{
148 struct feedback *fb;
149
150 fb = xzalloc(sizeof *fb);
151 fb->client = client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200152 fb->obj = wp_presentation_feedback(get_presentation(client), surface);
153 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400154
155 return fb;
156}
157
158static void
159feedback_wait(struct feedback *fb)
160{
161 while (fb->result == FB_PENDING) {
162 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
163 }
164}
165
166static char *
167pflags_to_str(uint32_t flags, char *str, unsigned len)
168{
169 static const struct {
170 uint32_t flag;
171 char sym;
172 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200173 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
174 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
175 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
176 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400177 };
178 unsigned i;
179
180 *str = '\0';
181 if (len < ARRAY_LENGTH(desc) + 1)
182 return str;
183
184 for (i = 0; i < ARRAY_LENGTH(desc); i++)
185 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
186 str[ARRAY_LENGTH(desc)] = '\0';
187
188 return str;
189}
190
191static void
192feedback_print(struct feedback *fb)
193{
194 char str[10];
195
196 switch (fb->result) {
197 case FB_PENDING:
198 printf("pending");
199 return;
200 case FB_DISCARDED:
201 printf("discarded");
202 return;
203 case FB_PRESENTED:
204 break;
205 }
206
207 pflags_to_str(fb->flags, str, sizeof str);
208 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
209 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
210 fb->refresh_nsec / 1000, str, fb->seq);
211}
212
213static void
214feedback_destroy(struct feedback *fb)
215{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200216 wp_presentation_feedback_destroy(fb->obj);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400217 free(fb);
218}
219
220TEST(test_presentation_feedback_simple)
221{
222 struct client *client;
223 struct feedback *fb;
224
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200225 client = create_client_and_test_surface(100, 50, 123, 77);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400226 assert(client);
227
228 wl_surface_attach(client->surface->wl_surface,
Pekka Paalanen924cd942016-05-20 17:25:38 +0300229 client->surface->buffer->proxy, 0, 0);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400230 fb = feedback_create(client, client->surface->wl_surface);
231 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
232 wl_surface_commit(client->surface->wl_surface);
233
234 client_roundtrip(client);
235
236 feedback_wait(fb);
237
238 printf("%s feedback:", __func__);
239 feedback_print(fb);
240 printf("\n");
241
242 feedback_destroy(fb);
243}