blob: f6ffe4805dca8ccf71f370b914f1b7710eb3e8a1 [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
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030028#include <stdint.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040029#include <stdio.h>
Bryce Harringtona7680262014-11-19 17:18:34 -080030#include <stdlib.h>
Pekka Paalanende7f5c82014-09-23 22:08:48 -040031#include <string.h>
32#include <assert.h>
33#include <time.h>
34
Jon Cruz35b2eaa2015-06-15 15:37:08 -070035#include "shared/helpers.h"
Bryce Harringtone99e4bf2016-03-16 14:15:18 -070036#include "shared/xalloc.h"
Alexandros Frantzis787fa612017-12-13 13:27:53 +020037#include "shared/timespec-util.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040038#include "weston-test-client-helper.h"
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020039#include "presentation-time-client-protocol.h"
Pekka Paalanende7f5c82014-09-23 22:08:48 -040040
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020041static struct wp_presentation *
Pekka Paalanende7f5c82014-09-23 22:08:48 -040042get_presentation(struct client *client)
43{
44 struct global *g;
45 struct global *global_pres = NULL;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020046 static struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040047
48 if (pres)
49 return pres;
50
51 wl_list_for_each(g, &client->global_list, link) {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020052 if (strcmp(g->interface, wp_presentation_interface.name))
Pekka Paalanende7f5c82014-09-23 22:08:48 -040053 continue;
54
55 if (global_pres)
56 assert(0 && "multiple presentation objects");
57
58 global_pres = g;
59 }
60
61 assert(global_pres && "no presentation found");
62
63 assert(global_pres->version == 1);
64
65 pres = wl_registry_bind(client->wl_registry, global_pres->name,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020066 &wp_presentation_interface, 1);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040067 assert(pres);
68
69 return pres;
70}
71
72struct feedback {
73 struct client *client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020074 struct wp_presentation_feedback *obj;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040075
76 enum {
77 FB_PENDING = 0,
78 FB_PRESENTED,
79 FB_DISCARDED
80 } result;
81
82 struct wl_output *sync_output;
83 uint64_t seq;
84 struct timespec time;
85 uint32_t refresh_nsec;
86 uint32_t flags;
87};
88
89static void
Pekka Paalanende7f5c82014-09-23 22:08:48 -040090feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020091 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -040092 struct wl_output *output)
93{
94 struct feedback *fb = data;
95
96 assert(fb->result == FB_PENDING);
97
98 if (output)
99 fb->sync_output = output;
100}
101
102static void
103feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200104 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400105 uint32_t tv_sec_hi,
106 uint32_t tv_sec_lo,
107 uint32_t tv_nsec,
108 uint32_t refresh_nsec,
109 uint32_t seq_hi,
110 uint32_t seq_lo,
111 uint32_t flags)
112{
113 struct feedback *fb = data;
114
115 assert(fb->result == FB_PENDING);
116 fb->result = FB_PRESENTED;
117 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
118 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
119 fb->refresh_nsec = refresh_nsec;
120 fb->flags = flags;
121}
122
123static void
124feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200125 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400126{
127 struct feedback *fb = data;
128
129 assert(fb->result == FB_PENDING);
130 fb->result = FB_DISCARDED;
131}
132
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200133static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400134 feedback_sync_output,
135 feedback_presented,
136 feedback_discarded
137};
138
139static struct feedback *
140feedback_create(struct client *client, struct wl_surface *surface)
141{
142 struct feedback *fb;
143
144 fb = xzalloc(sizeof *fb);
145 fb->client = client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200146 fb->obj = wp_presentation_feedback(get_presentation(client), surface);
147 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400148
149 return fb;
150}
151
152static void
153feedback_wait(struct feedback *fb)
154{
155 while (fb->result == FB_PENDING) {
156 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
157 }
158}
159
160static char *
161pflags_to_str(uint32_t flags, char *str, unsigned len)
162{
163 static const struct {
164 uint32_t flag;
165 char sym;
166 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200167 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
168 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
169 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
170 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400171 };
172 unsigned i;
173
174 *str = '\0';
175 if (len < ARRAY_LENGTH(desc) + 1)
176 return str;
177
178 for (i = 0; i < ARRAY_LENGTH(desc); i++)
179 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
180 str[ARRAY_LENGTH(desc)] = '\0';
181
182 return str;
183}
184
185static void
186feedback_print(struct feedback *fb)
187{
188 char str[10];
189
190 switch (fb->result) {
191 case FB_PENDING:
192 printf("pending");
193 return;
194 case FB_DISCARDED:
195 printf("discarded");
196 return;
197 case FB_PRESENTED:
198 break;
199 }
200
201 pflags_to_str(fb->flags, str, sizeof str);
202 printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
203 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
204 fb->refresh_nsec / 1000, str, fb->seq);
205}
206
207static void
208feedback_destroy(struct feedback *fb)
209{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200210 wp_presentation_feedback_destroy(fb->obj);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400211 free(fb);
212}
213
214TEST(test_presentation_feedback_simple)
215{
216 struct client *client;
217 struct feedback *fb;
218
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200219 client = create_client_and_test_surface(100, 50, 123, 77);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400220 assert(client);
221
222 wl_surface_attach(client->surface->wl_surface,
Pekka Paalanen924cd942016-05-20 17:25:38 +0300223 client->surface->buffer->proxy, 0, 0);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400224 fb = feedback_create(client, client->surface->wl_surface);
225 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
226 wl_surface_commit(client->surface->wl_surface);
227
228 client_roundtrip(client);
229
230 feedback_wait(fb);
231
232 printf("%s feedback:", __func__);
233 feedback_print(fb);
234 printf("\n");
235
236 feedback_destroy(fb);
237}