blob: 6790ab6638ac2d53362b1f1ab1832a874a83702b [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 Paalanenf38d4452019-11-13 15:50:51 +020046 struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040047
48 wl_list_for_each(g, &client->global_list, link) {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020049 if (strcmp(g->interface, wp_presentation_interface.name))
Pekka Paalanende7f5c82014-09-23 22:08:48 -040050 continue;
51
52 if (global_pres)
53 assert(0 && "multiple presentation objects");
54
55 global_pres = g;
56 }
57
58 assert(global_pres && "no presentation found");
59
60 assert(global_pres->version == 1);
61
62 pres = wl_registry_bind(client->wl_registry, global_pres->name,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020063 &wp_presentation_interface, 1);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040064 assert(pres);
65
66 return pres;
67}
68
69struct feedback {
70 struct client *client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020071 struct wp_presentation_feedback *obj;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040072
73 enum {
74 FB_PENDING = 0,
75 FB_PRESENTED,
76 FB_DISCARDED
77 } result;
78
79 struct wl_output *sync_output;
80 uint64_t seq;
81 struct timespec time;
82 uint32_t refresh_nsec;
83 uint32_t flags;
84};
85
86static void
Pekka Paalanende7f5c82014-09-23 22:08:48 -040087feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020088 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -040089 struct wl_output *output)
90{
91 struct feedback *fb = data;
92
93 assert(fb->result == FB_PENDING);
94
95 if (output)
96 fb->sync_output = output;
97}
98
99static void
100feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200101 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400102 uint32_t tv_sec_hi,
103 uint32_t tv_sec_lo,
104 uint32_t tv_nsec,
105 uint32_t refresh_nsec,
106 uint32_t seq_hi,
107 uint32_t seq_lo,
108 uint32_t flags)
109{
110 struct feedback *fb = data;
111
112 assert(fb->result == FB_PENDING);
113 fb->result = FB_PRESENTED;
114 fb->seq = ((uint64_t)seq_hi << 32) + seq_lo;
115 timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec);
116 fb->refresh_nsec = refresh_nsec;
117 fb->flags = flags;
118}
119
120static void
121feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200122 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400123{
124 struct feedback *fb = data;
125
126 assert(fb->result == FB_PENDING);
127 fb->result = FB_DISCARDED;
128}
129
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200130static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400131 feedback_sync_output,
132 feedback_presented,
133 feedback_discarded
134};
135
136static struct feedback *
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200137feedback_create(struct client *client,
138 struct wl_surface *surface,
139 struct wp_presentation *pres)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400140{
141 struct feedback *fb;
142
143 fb = xzalloc(sizeof *fb);
144 fb->client = client;
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200145 fb->obj = wp_presentation_feedback(pres, surface);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200146 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400147
148 return fb;
149}
150
151static void
152feedback_wait(struct feedback *fb)
153{
154 while (fb->result == FB_PENDING) {
155 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
156 }
157}
158
159static char *
160pflags_to_str(uint32_t flags, char *str, unsigned len)
161{
162 static const struct {
163 uint32_t flag;
164 char sym;
165 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200166 { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' },
167 { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' },
168 { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' },
169 { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400170 };
171 unsigned i;
172
173 *str = '\0';
174 if (len < ARRAY_LENGTH(desc) + 1)
175 return str;
176
177 for (i = 0; i < ARRAY_LENGTH(desc); i++)
178 str[i] = flags & desc[i].flag ? desc[i].sym : '_';
179 str[ARRAY_LENGTH(desc)] = '\0';
180
181 return str;
182}
183
184static void
185feedback_print(struct feedback *fb)
186{
187 char str[10];
188
189 switch (fb->result) {
190 case FB_PENDING:
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200191 testlog("pending");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400192 return;
193 case FB_DISCARDED:
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200194 testlog("discarded");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400195 return;
196 case FB_PRESENTED:
197 break;
198 }
199
200 pflags_to_str(fb->flags, str, sizeof str);
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200201 testlog("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400202 (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec,
203 fb->refresh_nsec / 1000, str, fb->seq);
204}
205
206static void
207feedback_destroy(struct feedback *fb)
208{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200209 wp_presentation_feedback_destroy(fb->obj);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400210 free(fb);
211}
212
213TEST(test_presentation_feedback_simple)
214{
215 struct client *client;
216 struct feedback *fb;
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200217 struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400218
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);
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200221 pres = get_presentation(client);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400222
223 wl_surface_attach(client->surface->wl_surface,
Pekka Paalanen924cd942016-05-20 17:25:38 +0300224 client->surface->buffer->proxy, 0, 0);
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200225 fb = feedback_create(client, client->surface->wl_surface, pres);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400226 wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
227 wl_surface_commit(client->surface->wl_surface);
228
229 client_roundtrip(client);
230
231 feedback_wait(fb);
232
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200233 testlog("%s feedback:", __func__);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400234 feedback_print(fb);
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200235 testlog("\n");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400236
237 feedback_destroy(fb);
238}