blob: f6e3a7bc384539e6ecc86996623d32b53d60762a [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 Paalanen701676d2019-11-13 15:45:10 +020040#include "weston-test-fixture-compositor.h"
41
42static enum test_result_code
43fixture_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}
51DECLARE_FIXTURE_SETUP(fixture_setup);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040052
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020053static struct wp_presentation *
Pekka Paalanende7f5c82014-09-23 22:08:48 -040054get_presentation(struct client *client)
55{
56 struct global *g;
57 struct global *global_pres = NULL;
Pekka Paalanenf38d4452019-11-13 15:50:51 +020058 struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040059
60 wl_list_for_each(g, &client->global_list, link) {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020061 if (strcmp(g->interface, wp_presentation_interface.name))
Pekka Paalanende7f5c82014-09-23 22:08:48 -040062 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,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020075 &wp_presentation_interface, 1);
Pekka Paalanende7f5c82014-09-23 22:08:48 -040076 assert(pres);
77
78 return pres;
79}
80
81struct feedback {
82 struct client *client;
Pekka Paalanenb00c79b2016-02-18 16:53:27 +020083 struct wp_presentation_feedback *obj;
Pekka Paalanende7f5c82014-09-23 22:08:48 -040084
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
98static void
Pekka Paalanende7f5c82014-09-23 22:08:48 -040099feedback_sync_output(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200100 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400101 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
111static void
112feedback_presented(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200113 struct wp_presentation_feedback *presentation_feedback,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400114 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
132static void
133feedback_discarded(void *data,
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200134 struct wp_presentation_feedback *presentation_feedback)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400135{
136 struct feedback *fb = data;
137
138 assert(fb->result == FB_PENDING);
139 fb->result = FB_DISCARDED;
140}
141
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200142static const struct wp_presentation_feedback_listener feedback_listener = {
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400143 feedback_sync_output,
144 feedback_presented,
145 feedback_discarded
146};
147
148static struct feedback *
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200149feedback_create(struct client *client,
150 struct wl_surface *surface,
151 struct wp_presentation *pres)
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400152{
153 struct feedback *fb;
154
155 fb = xzalloc(sizeof *fb);
156 fb->client = client;
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200157 fb->obj = wp_presentation_feedback(pres, surface);
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200158 wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400159
160 return fb;
161}
162
163static void
164feedback_wait(struct feedback *fb)
165{
166 while (fb->result == FB_PENDING) {
167 assert(wl_display_dispatch(fb->client->wl_display) >= 0);
168 }
169}
170
171static char *
172pflags_to_str(uint32_t flags, char *str, unsigned len)
173{
174 static const struct {
175 uint32_t flag;
176 char sym;
177 } desc[] = {
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200178 { 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' },
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400182 };
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:
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200203 testlog("pending");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400204 return;
205 case FB_DISCARDED:
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200206 testlog("discarded");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400207 return;
208 case FB_PRESENTED:
209 break;
210 }
211
212 pflags_to_str(fb->flags, str, sizeof str);
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200213 testlog("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64,
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400214 (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{
Pekka Paalanenb00c79b2016-02-18 16:53:27 +0200221 wp_presentation_feedback_destroy(fb->obj);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400222 free(fb);
223}
224
225TEST(test_presentation_feedback_simple)
226{
227 struct client *client;
228 struct feedback *fb;
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200229 struct wp_presentation *pres;
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400230
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200231 client = create_client_and_test_surface(100, 50, 123, 77);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400232 assert(client);
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200233 pres = get_presentation(client);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400234
235 wl_surface_attach(client->surface->wl_surface,
Pekka Paalanen924cd942016-05-20 17:25:38 +0300236 client->surface->buffer->proxy, 0, 0);
Pekka Paalanenf38d4452019-11-13 15:50:51 +0200237 fb = feedback_create(client, client->surface->wl_surface, pres);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400238 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
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200245 testlog("%s feedback:", __func__);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400246 feedback_print(fb);
Pekka Paalanen12a138d2019-11-06 15:59:33 +0200247 testlog("\n");
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400248
249 feedback_destroy(fb);
Pekka Paalanen16004312021-06-15 16:53:52 +0300250 wp_presentation_destroy(pres);
251 client_destroy(client);
Pekka Paalanende7f5c82014-09-23 22:08:48 -0400252}