Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014 Collabora, Ltd. |
| 3 | * |
| 4 | * Permission to use, copy, modify, distribute, and sell this software and |
| 5 | * its documentation for any purpose is hereby granted without fee, provided |
| 6 | * that the above copyright notice appear in all copies and that both that |
| 7 | * copyright notice and this permission notice appear in supporting |
| 8 | * documentation, and that the name of the copyright holders not be used in |
| 9 | * advertising or publicity pertaining to distribution of the software |
| 10 | * without specific, written prior permission. The copyright holders make |
| 11 | * no representations about the suitability of this software for any |
| 12 | * purpose. It is provided "as is" without express or implied warranty. |
| 13 | * |
| 14 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 15 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 16 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 17 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 18 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 19 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 20 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "config.h" |
| 24 | |
| 25 | #include <stdio.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <assert.h> |
| 29 | #include <time.h> |
| 30 | |
| 31 | #include "weston-test-client-helper.h" |
| 32 | #include "presentation_timing-client-protocol.h" |
| 33 | |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 34 | static struct presentation * |
| 35 | get_presentation(struct client *client) |
| 36 | { |
| 37 | struct global *g; |
| 38 | struct global *global_pres = NULL; |
| 39 | static struct presentation *pres; |
| 40 | |
| 41 | if (pres) |
| 42 | return pres; |
| 43 | |
| 44 | wl_list_for_each(g, &client->global_list, link) { |
| 45 | if (strcmp(g->interface, "presentation")) |
| 46 | continue; |
| 47 | |
| 48 | if (global_pres) |
| 49 | assert(0 && "multiple presentation objects"); |
| 50 | |
| 51 | global_pres = g; |
| 52 | } |
| 53 | |
| 54 | assert(global_pres && "no presentation found"); |
| 55 | |
| 56 | assert(global_pres->version == 1); |
| 57 | |
| 58 | pres = wl_registry_bind(client->wl_registry, global_pres->name, |
| 59 | &presentation_interface, 1); |
| 60 | assert(pres); |
| 61 | |
| 62 | return pres; |
| 63 | } |
| 64 | |
| 65 | struct feedback { |
| 66 | struct client *client; |
| 67 | struct presentation_feedback *obj; |
| 68 | |
| 69 | enum { |
| 70 | FB_PENDING = 0, |
| 71 | FB_PRESENTED, |
| 72 | FB_DISCARDED |
| 73 | } result; |
| 74 | |
| 75 | struct wl_output *sync_output; |
| 76 | uint64_t seq; |
| 77 | struct timespec time; |
| 78 | uint32_t refresh_nsec; |
| 79 | uint32_t flags; |
| 80 | }; |
| 81 | |
| 82 | static void |
| 83 | timespec_from_proto(struct timespec *tm, uint32_t tv_sec_hi, |
| 84 | uint32_t tv_sec_lo, uint32_t tv_nsec) |
| 85 | { |
| 86 | tm->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo; |
| 87 | tm->tv_nsec = tv_nsec; |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | feedback_sync_output(void *data, |
| 92 | struct presentation_feedback *presentation_feedback, |
| 93 | struct wl_output *output) |
| 94 | { |
| 95 | struct feedback *fb = data; |
| 96 | |
| 97 | assert(fb->result == FB_PENDING); |
| 98 | |
| 99 | if (output) |
| 100 | fb->sync_output = output; |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | feedback_presented(void *data, |
| 105 | struct presentation_feedback *presentation_feedback, |
| 106 | uint32_t tv_sec_hi, |
| 107 | uint32_t tv_sec_lo, |
| 108 | uint32_t tv_nsec, |
| 109 | uint32_t refresh_nsec, |
| 110 | uint32_t seq_hi, |
| 111 | uint32_t seq_lo, |
| 112 | uint32_t flags) |
| 113 | { |
| 114 | struct feedback *fb = data; |
| 115 | |
| 116 | assert(fb->result == FB_PENDING); |
| 117 | fb->result = FB_PRESENTED; |
| 118 | fb->seq = ((uint64_t)seq_hi << 32) + seq_lo; |
| 119 | timespec_from_proto(&fb->time, tv_sec_hi, tv_sec_lo, tv_nsec); |
| 120 | fb->refresh_nsec = refresh_nsec; |
| 121 | fb->flags = flags; |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | feedback_discarded(void *data, |
| 126 | struct presentation_feedback *presentation_feedback) |
| 127 | { |
| 128 | struct feedback *fb = data; |
| 129 | |
| 130 | assert(fb->result == FB_PENDING); |
| 131 | fb->result = FB_DISCARDED; |
| 132 | } |
| 133 | |
| 134 | static const struct presentation_feedback_listener feedback_listener = { |
| 135 | feedback_sync_output, |
| 136 | feedback_presented, |
| 137 | feedback_discarded |
| 138 | }; |
| 139 | |
| 140 | static struct feedback * |
| 141 | feedback_create(struct client *client, struct wl_surface *surface) |
| 142 | { |
| 143 | struct feedback *fb; |
| 144 | |
| 145 | fb = xzalloc(sizeof *fb); |
| 146 | fb->client = client; |
| 147 | fb->obj = presentation_feedback(get_presentation(client), surface); |
| 148 | presentation_feedback_add_listener(fb->obj, &feedback_listener, fb); |
| 149 | |
| 150 | return fb; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | feedback_wait(struct feedback *fb) |
| 155 | { |
| 156 | while (fb->result == FB_PENDING) { |
| 157 | assert(wl_display_dispatch(fb->client->wl_display) >= 0); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static char * |
| 162 | pflags_to_str(uint32_t flags, char *str, unsigned len) |
| 163 | { |
| 164 | static const struct { |
| 165 | uint32_t flag; |
| 166 | char sym; |
| 167 | } desc[] = { |
Pekka Paalanen | 6349586 | 2014-12-17 16:20:42 +0200 | [diff] [blame] | 168 | { PRESENTATION_FEEDBACK_KIND_VSYNC, 's' }, |
| 169 | { PRESENTATION_FEEDBACK_KIND_HW_CLOCK, 'c' }, |
| 170 | { PRESENTATION_FEEDBACK_KIND_HW_COMPLETION, 'e' }, |
| 171 | { PRESENTATION_FEEDBACK_KIND_ZERO_COPY, 'z' }, |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 172 | }; |
| 173 | unsigned i; |
| 174 | |
| 175 | *str = '\0'; |
| 176 | if (len < ARRAY_LENGTH(desc) + 1) |
| 177 | return str; |
| 178 | |
| 179 | for (i = 0; i < ARRAY_LENGTH(desc); i++) |
| 180 | str[i] = flags & desc[i].flag ? desc[i].sym : '_'; |
| 181 | str[ARRAY_LENGTH(desc)] = '\0'; |
| 182 | |
| 183 | return str; |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | feedback_print(struct feedback *fb) |
| 188 | { |
| 189 | char str[10]; |
| 190 | |
| 191 | switch (fb->result) { |
| 192 | case FB_PENDING: |
| 193 | printf("pending"); |
| 194 | return; |
| 195 | case FB_DISCARDED: |
| 196 | printf("discarded"); |
| 197 | return; |
| 198 | case FB_PRESENTED: |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | pflags_to_str(fb->flags, str, sizeof str); |
| 203 | printf("presented %lld.%09lld, refresh %u us, [%s] seq %" PRIu64, |
| 204 | (long long)fb->time.tv_sec, (long long)fb->time.tv_nsec, |
| 205 | fb->refresh_nsec / 1000, str, fb->seq); |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | feedback_destroy(struct feedback *fb) |
| 210 | { |
| 211 | presentation_feedback_destroy(fb->obj); |
| 212 | free(fb); |
| 213 | } |
| 214 | |
| 215 | TEST(test_presentation_feedback_simple) |
| 216 | { |
| 217 | struct client *client; |
| 218 | struct feedback *fb; |
| 219 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 220 | client = create_client_and_test_surface(100, 50, 123, 77); |
Pekka Paalanen | de7f5c8 | 2014-09-23 22:08:48 -0400 | [diff] [blame] | 221 | assert(client); |
| 222 | |
| 223 | wl_surface_attach(client->surface->wl_surface, |
| 224 | client->surface->wl_buffer, 0, 0); |
| 225 | fb = feedback_create(client, client->surface->wl_surface); |
| 226 | 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 | |
| 233 | printf("%s feedback:", __func__); |
| 234 | feedback_print(fb); |
| 235 | printf("\n"); |
| 236 | |
| 237 | feedback_destroy(fb); |
| 238 | } |