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