Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014 Collabora, Ltd. |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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: |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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. |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame] | 28 | #include <stdint.h> |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 29 | #include <stdio.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 30 | #include <stdlib.h> |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 31 | #include <string.h> |
| 32 | #include <assert.h> |
| 33 | #include <time.h> |
| 34 | |
Jon Cruz | 35b2eaa | 2015-06-15 15:37:08 -0700 | [diff] [blame] | 35 | #include "shared/helpers.h" |
Bryce Harrington | e99e4bf | 2016-03-16 14:15:18 -0700 | [diff] [blame] | 36 | #include "shared/xalloc.h" |
Alexandros Frantzis | 787fa61 | 2017-12-13 13:27:53 +0200 | [diff] [blame] | 37 | #include "shared/timespec-util.h" |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 38 | #include "weston-test-client-helper.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 39 | #include "presentation-time-client-protocol.h" |
Pekka Paalanen | 701676d | 2019-11-13 15:45:10 +0200 | [diff] [blame] | 40 | #include "weston-test-fixture-compositor.h" |
| 41 | |
| 42 | static enum test_result_code |
| 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); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 52 | |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 53 | static struct wp_presentation * |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 54 | get_presentation(struct client *client) |
| 55 | { |
| 56 | struct global *g; |
| 57 | struct global *global_pres = NULL; |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 58 | struct wp_presentation *pres; |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 59 | |
| 60 | wl_list_for_each(g, &client->global_list, link) { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 61 | if (strcmp(g->interface, wp_presentation_interface.name)) |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 75 | &wp_presentation_interface, 1); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 76 | assert(pres); |
| 77 | |
| 78 | return pres; |
| 79 | } |
| 80 | |
| 81 | struct feedback { |
| 82 | struct client *client; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 83 | struct wp_presentation_feedback *obj; |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 99 | feedback_sync_output(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 100 | struct wp_presentation_feedback *presentation_feedback, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 |
| 112 | feedback_presented(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 113 | struct wp_presentation_feedback *presentation_feedback, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 |
| 133 | feedback_discarded(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 134 | struct wp_presentation_feedback *presentation_feedback) |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 135 | { |
| 136 | struct feedback *fb = data; |
| 137 | |
| 138 | assert(fb->result == FB_PENDING); |
| 139 | fb->result = FB_DISCARDED; |
| 140 | } |
| 141 | |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 142 | static const struct wp_presentation_feedback_listener feedback_listener = { |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 143 | feedback_sync_output, |
| 144 | feedback_presented, |
| 145 | feedback_discarded |
| 146 | }; |
| 147 | |
| 148 | static struct feedback * |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 149 | feedback_create(struct client *client, |
| 150 | struct wl_surface *surface, |
| 151 | struct wp_presentation *pres) |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 152 | { |
| 153 | struct feedback *fb; |
| 154 | |
| 155 | fb = xzalloc(sizeof *fb); |
| 156 | fb->client = client; |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 157 | fb->obj = wp_presentation_feedback(pres, surface); |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 158 | wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 159 | |
| 160 | return fb; |
| 161 | } |
| 162 | |
| 163 | static void |
| 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 * |
| 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[] = { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 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' }, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 |
| 197 | feedback_print(struct feedback *fb) |
| 198 | { |
| 199 | char str[10]; |
| 200 | |
| 201 | switch (fb->result) { |
| 202 | case FB_PENDING: |
Pekka Paalanen | 12a138d | 2019-11-06 15:59:33 +0200 | [diff] [blame] | 203 | testlog("pending"); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 204 | return; |
| 205 | case FB_DISCARDED: |
Pekka Paalanen | 12a138d | 2019-11-06 15:59:33 +0200 | [diff] [blame] | 206 | testlog("discarded"); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 207 | return; |
| 208 | case FB_PRESENTED: |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | pflags_to_str(fb->flags, str, sizeof str); |
Pekka Paalanen | 12a138d | 2019-11-06 15:59:33 +0200 | [diff] [blame] | 213 | testlog("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 |
| 219 | feedback_destroy(struct feedback *fb) |
| 220 | { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 221 | wp_presentation_feedback_destroy(fb->obj); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 222 | free(fb); |
| 223 | } |
| 224 | |
| 225 | TEST(test_presentation_feedback_simple) |
| 226 | { |
| 227 | struct client *client; |
| 228 | struct feedback *fb; |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 229 | struct wp_presentation *pres; |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 230 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 231 | client = create_client_and_test_surface(100, 50, 123, 77); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 232 | assert(client); |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 233 | pres = get_presentation(client); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 234 | |
| 235 | wl_surface_attach(client->surface->wl_surface, |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 236 | client->surface->buffer->proxy, 0, 0); |
Pekka Paalanen | f38d445 | 2019-11-13 15:50:51 +0200 | [diff] [blame] | 237 | fb = feedback_create(client, client->surface->wl_surface, pres); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 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 | |
Pekka Paalanen | 12a138d | 2019-11-06 15:59:33 +0200 | [diff] [blame] | 245 | testlog("%s feedback:", __func__); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 246 | feedback_print(fb); |
Pekka Paalanen | 12a138d | 2019-11-06 15:59:33 +0200 | [diff] [blame] | 247 | testlog("\n"); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 248 | |
| 249 | feedback_destroy(fb); |
Pekka Paalanen | 1600431 | 2021-06-15 16:53:52 +0300 | [diff] [blame^] | 250 | wp_presentation_destroy(pres); |
| 251 | client_destroy(client); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 252 | } |