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" |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 37 | #include "weston-test-client-helper.h" |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 38 | #include "presentation-time-client-protocol.h" |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 39 | |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 40 | static struct wp_presentation * |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 41 | get_presentation(struct client *client) |
| 42 | { |
| 43 | struct global *g; |
| 44 | struct global *global_pres = NULL; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 45 | static struct wp_presentation *pres; |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 46 | |
| 47 | if (pres) |
| 48 | return pres; |
| 49 | |
| 50 | wl_list_for_each(g, &client->global_list, link) { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 51 | if (strcmp(g->interface, wp_presentation_interface.name)) |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 52 | continue; |
| 53 | |
| 54 | if (global_pres) |
| 55 | assert(0 && "multiple presentation objects"); |
| 56 | |
| 57 | global_pres = g; |
| 58 | } |
| 59 | |
| 60 | assert(global_pres && "no presentation found"); |
| 61 | |
| 62 | assert(global_pres->version == 1); |
| 63 | |
| 64 | pres = wl_registry_bind(client->wl_registry, global_pres->name, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 65 | &wp_presentation_interface, 1); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 66 | assert(pres); |
| 67 | |
| 68 | return pres; |
| 69 | } |
| 70 | |
| 71 | struct feedback { |
| 72 | struct client *client; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 73 | struct wp_presentation_feedback *obj; |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 74 | |
| 75 | enum { |
| 76 | FB_PENDING = 0, |
| 77 | FB_PRESENTED, |
| 78 | FB_DISCARDED |
| 79 | } result; |
| 80 | |
| 81 | struct wl_output *sync_output; |
| 82 | uint64_t seq; |
| 83 | struct timespec time; |
| 84 | uint32_t refresh_nsec; |
| 85 | uint32_t flags; |
| 86 | }; |
| 87 | |
| 88 | static void |
| 89 | timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi, |
| 90 | uint32_t tv_sec_lo, uint32_t tv_nsec) |
| 91 | { |
| 92 | tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo; |
| 93 | tm->tv_nsec = tv_nsec; |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | feedback_sync_output(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 98 | struct wp_presentation_feedback *presentation_feedback, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 99 | struct wl_output *output) |
| 100 | { |
| 101 | struct feedback *fb = data; |
| 102 | |
| 103 | assert(fb->result == FB_PENDING); |
| 104 | |
| 105 | if (output) |
| 106 | fb->sync_output = output; |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | feedback_presented(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 111 | struct wp_presentation_feedback *presentation_feedback, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 112 | uint32_t tv_sec_hi, |
| 113 | uint32_t tv_sec_lo, |
| 114 | uint32_t tv_nsec, |
| 115 | uint32_t refresh_nsec, |
| 116 | uint32_t seq_hi, |
| 117 | uint32_t seq_lo, |
| 118 | uint32_t flags) |
| 119 | { |
| 120 | struct feedback *fb = data; |
| 121 | |
| 122 | assert(fb->result == FB_PENDING); |
| 123 | fb->result = FB_PRESENTED; |
| 124 | fb->seq = ((uint64_t)seq_hi << 32) + seq_lo; |
| 125 | timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 126 | fb->refresh_nsec = refresh_nsec; |
| 127 | fb->flags = flags; |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | feedback_discarded(void *data, |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 132 | struct wp_presentation_feedback *presentation_feedback) |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 133 | { |
| 134 | struct feedback *fb = data; |
| 135 | |
| 136 | assert(fb->result == FB_PENDING); |
| 137 | fb->result = FB_DISCARDED; |
| 138 | } |
| 139 | |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 140 | static const struct wp_presentation_feedback_listener feedback_listener = { |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 141 | feedback_sync_output, |
| 142 | feedback_presented, |
| 143 | feedback_discarded |
| 144 | }; |
| 145 | |
| 146 | static struct feedback * |
| 147 | feedback_create(struct client *client, struct wl_surface *surface) |
| 148 | { |
| 149 | struct feedback *fb; |
| 150 | |
| 151 | fb = xzalloc(sizeof *fb); |
| 152 | fb->client = client; |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 153 | fb->obj = wp_presentation_feedback(get_presentation(client), surface); |
| 154 | wp_presentation_feedback_add_listener(fb->obj, &feedback_listener, fb); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 155 | |
| 156 | return fb; |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | feedback_wait(struct feedback *fb) |
| 161 | { |
| 162 | while (fb->result == FB_PENDING) { |
| 163 | assert(wl_display_dispatch(fb->client->wl_display) >= 0); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static char * |
| 168 | pflags_to_str(uint32_t flags, char *str, unsigned len) |
| 169 | { |
| 170 | static const struct { |
| 171 | uint32_t flag; |
| 172 | char sym; |
| 173 | } desc[] = { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 174 | { WP_PRESENTATION_FEEDBACK_KIND_VSYNC, 's' }, |
| 175 | { WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' }, |
| 176 | { WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' }, |
| 177 | { WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' }, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 178 | }; |
| 179 | unsigned i; |
| 180 | |
| 181 | *str = '\0'; |
| 182 | if (len < ARRAY_LENGTH(desc) + 1) |
| 183 | return str; |
| 184 | |
| 185 | for (i = 0; i < ARRAY_LENGTH(desc); i++) |
| 186 | str[i] = flags & desc[i].flag ? desc[i].sym : '_'; |
| 187 | str[ARRAY_LENGTH(desc)] = '\0'; |
| 188 | |
| 189 | return str; |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | feedback_print(struct feedback *fb) |
| 194 | { |
| 195 | char str[10]; |
| 196 | |
| 197 | switch (fb->result) { |
| 198 | case FB_PENDING: |
| 199 | printf("pending"); |
| 200 | return; |
| 201 | case FB_DISCARDED: |
| 202 | printf("discarded"); |
| 203 | return; |
| 204 | case FB_PRESENTED: |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | pflags_to_str(fb->flags, str, sizeof str); |
| 209 | printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64, |
| 210 | (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec, |
| 211 | fb->refresh_nsec / 1000, str, fb->seq); |
| 212 | } |
| 213 | |
| 214 | static void |
| 215 | feedback_destroy(struct feedback *fb) |
| 216 | { |
Pekka Paalanen | b00c79b | 2016-02-18 16:53:27 +0200 | [diff] [blame] | 217 | wp_presentation_feedback_destroy(fb->obj); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 218 | free(fb); |
| 219 | } |
| 220 | |
| 221 | TEST(test_presentation_feedback_simple) |
| 222 | { |
| 223 | struct client *client; |
| 224 | struct feedback *fb; |
| 225 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 226 | client = create_client_and_test_surface(100, 50, 123, 77); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 227 | assert(client); |
| 228 | |
| 229 | wl_surface_attach(client->surface->wl_surface, |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 230 | client->surface->buffer->proxy, 0, 0); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 231 | fb = feedback_create(client, client->surface->wl_surface); |
| 232 | wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100); |
| 233 | wl_surface_commit(client->surface->wl_surface); |
| 234 | |
| 235 | client_roundtrip(client); |
| 236 | |
| 237 | feedback_wait(fb); |
| 238 | |
| 239 | printf("%s feedback:", __func__); |
| 240 | feedback_print(fb); |
| 241 | printf("\n"); |
| 242 | |
| 243 | feedback_destroy(fb); |
| 244 | } |