Alexandros Frantzis | 27d7c39 | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2018 Collabora, Ltd. |
| 3 | * |
| 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: |
| 11 | * |
| 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. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <string.h> |
Alexandros Frantzis | acff29b | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 29 | #include <unistd.h> |
Alexandros Frantzis | 27d7c39 | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 30 | |
| 31 | #include "linux-explicit-synchronization-unstable-v1-client-protocol.h" |
| 32 | #include "weston-test-client-helper.h" |
| 33 | #include "wayland-server-protocol.h" |
| 34 | |
| 35 | static struct zwp_linux_explicit_synchronization_v1 * |
| 36 | get_linux_explicit_synchronization(struct client *client) |
| 37 | { |
| 38 | struct global *g; |
| 39 | struct global *global_sync = NULL; |
| 40 | struct zwp_linux_explicit_synchronization_v1 *sync = NULL; |
| 41 | |
| 42 | wl_list_for_each(g, &client->global_list, link) { |
| 43 | if (strcmp(g->interface, |
| 44 | zwp_linux_explicit_synchronization_v1_interface.name)) |
| 45 | continue; |
| 46 | |
| 47 | if (global_sync) |
| 48 | assert(!"Multiple linux explicit sync objects"); |
| 49 | |
| 50 | global_sync = g; |
| 51 | } |
| 52 | |
| 53 | assert(global_sync); |
| 54 | assert(global_sync->version == 1); |
| 55 | |
| 56 | sync = wl_registry_bind( |
| 57 | client->wl_registry, global_sync->name, |
| 58 | &zwp_linux_explicit_synchronization_v1_interface, 1); |
| 59 | assert(sync); |
| 60 | |
| 61 | return sync; |
| 62 | } |
| 63 | |
| 64 | static struct client * |
| 65 | create_test_client(void) |
| 66 | { |
| 67 | struct client *cl = create_client_and_test_surface(0, 0, 100, 100); |
| 68 | assert(cl); |
| 69 | return cl; |
| 70 | } |
| 71 | |
| 72 | TEST(second_surface_synchronization_on_surface_raises_error) |
| 73 | { |
| 74 | struct client *client = create_test_client(); |
| 75 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 76 | get_linux_explicit_synchronization(client); |
| 77 | struct zwp_linux_surface_synchronization_v1 *surface_sync1; |
| 78 | struct zwp_linux_surface_synchronization_v1 *surface_sync2; |
| 79 | |
| 80 | surface_sync1 = |
| 81 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 82 | sync, client->surface->wl_surface); |
| 83 | client_roundtrip(client); |
| 84 | |
| 85 | /* Second surface_synchronization creation should fail */ |
| 86 | surface_sync2 = |
| 87 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 88 | sync, client->surface->wl_surface); |
| 89 | expect_protocol_error( |
| 90 | client, |
| 91 | &zwp_linux_explicit_synchronization_v1_interface, |
| 92 | ZWP_LINUX_EXPLICIT_SYNCHRONIZATION_V1_ERROR_SYNCHRONIZATION_EXISTS); |
| 93 | |
| 94 | zwp_linux_surface_synchronization_v1_destroy(surface_sync2); |
| 95 | zwp_linux_surface_synchronization_v1_destroy(surface_sync1); |
| 96 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 97 | } |
Alexandros Frantzis | acff29b | 2018-10-19 12:14:11 +0300 | [diff] [blame] | 98 | |
| 99 | TEST(set_acquire_fence_with_invalid_fence_raises_error) |
| 100 | { |
| 101 | struct client *client = create_test_client(); |
| 102 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 103 | get_linux_explicit_synchronization(client); |
| 104 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 105 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 106 | sync, client->surface->wl_surface); |
| 107 | int pipefd[2] = { -1, -1 }; |
| 108 | |
| 109 | assert(pipe(pipefd) == 0); |
| 110 | |
| 111 | zwp_linux_surface_synchronization_v1_set_acquire_fence(surface_sync, |
| 112 | pipefd[0]); |
| 113 | expect_protocol_error( |
| 114 | client, |
| 115 | &zwp_linux_surface_synchronization_v1_interface, |
| 116 | ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_INVALID_FENCE); |
| 117 | |
| 118 | close(pipefd[0]); |
| 119 | close(pipefd[1]); |
| 120 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 121 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 122 | } |
| 123 | |
| 124 | TEST(set_acquire_fence_on_destroyed_surface_raises_error) |
| 125 | { |
| 126 | struct client *client = create_test_client(); |
| 127 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 128 | get_linux_explicit_synchronization(client); |
| 129 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 130 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 131 | sync, client->surface->wl_surface); |
| 132 | int pipefd[2] = { -1, -1 }; |
| 133 | |
| 134 | assert(pipe(pipefd) == 0); |
| 135 | |
| 136 | wl_surface_destroy(client->surface->wl_surface); |
| 137 | zwp_linux_surface_synchronization_v1_set_acquire_fence(surface_sync, |
| 138 | pipefd[0]); |
| 139 | expect_protocol_error( |
| 140 | client, |
| 141 | &zwp_linux_surface_synchronization_v1_interface, |
| 142 | ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_SURFACE); |
| 143 | |
| 144 | close(pipefd[0]); |
| 145 | close(pipefd[1]); |
| 146 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 147 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 148 | } |
Alexandros Frantzis | 6762967 | 2018-10-19 12:14:11 +0300 | [diff] [blame^] | 149 | |
| 150 | TEST(second_buffer_release_in_commit_raises_error) |
| 151 | { |
| 152 | struct client *client = create_test_client(); |
| 153 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 154 | get_linux_explicit_synchronization(client); |
| 155 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 156 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 157 | sync, client->surface->wl_surface); |
| 158 | struct zwp_linux_buffer_release_v1 *buffer_release1; |
| 159 | struct zwp_linux_buffer_release_v1 *buffer_release2; |
| 160 | |
| 161 | buffer_release1 = |
| 162 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 163 | client_roundtrip(client); |
| 164 | |
| 165 | /* Second buffer_release creation should fail */ |
| 166 | buffer_release2 = |
| 167 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 168 | expect_protocol_error( |
| 169 | client, |
| 170 | &zwp_linux_surface_synchronization_v1_interface, |
| 171 | ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_DUPLICATE_RELEASE); |
| 172 | |
| 173 | zwp_linux_buffer_release_v1_destroy(buffer_release2); |
| 174 | zwp_linux_buffer_release_v1_destroy(buffer_release1); |
| 175 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 176 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 177 | } |
| 178 | |
| 179 | TEST(get_release_without_buffer_raises_commit_error) |
| 180 | { |
| 181 | struct client *client = create_test_client(); |
| 182 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 183 | get_linux_explicit_synchronization(client); |
| 184 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 185 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 186 | sync, client->surface->wl_surface); |
| 187 | struct wl_surface *surface = client->surface->wl_surface; |
| 188 | struct zwp_linux_buffer_release_v1 *buffer_release; |
| 189 | |
| 190 | buffer_release = |
| 191 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 192 | wl_surface_commit(surface); |
| 193 | expect_protocol_error( |
| 194 | client, |
| 195 | &zwp_linux_surface_synchronization_v1_interface, |
| 196 | ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_BUFFER); |
| 197 | |
| 198 | zwp_linux_buffer_release_v1_destroy(buffer_release); |
| 199 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 200 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 201 | } |
| 202 | |
| 203 | TEST(get_release_on_destroyed_surface_raises_error) |
| 204 | { |
| 205 | struct client *client = create_test_client(); |
| 206 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 207 | get_linux_explicit_synchronization(client); |
| 208 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 209 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 210 | sync, client->surface->wl_surface); |
| 211 | struct zwp_linux_buffer_release_v1 *buffer_release; |
| 212 | |
| 213 | wl_surface_destroy(client->surface->wl_surface); |
| 214 | buffer_release = |
| 215 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 216 | expect_protocol_error( |
| 217 | client, |
| 218 | &zwp_linux_surface_synchronization_v1_interface, |
| 219 | ZWP_LINUX_SURFACE_SYNCHRONIZATION_V1_ERROR_NO_SURFACE); |
| 220 | |
| 221 | zwp_linux_buffer_release_v1_destroy(buffer_release); |
| 222 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 223 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 224 | } |
| 225 | |
| 226 | TEST(get_release_after_commit_succeeds) |
| 227 | { |
| 228 | struct client *client = create_test_client(); |
| 229 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 230 | get_linux_explicit_synchronization(client); |
| 231 | struct wl_surface *surface = client->surface->wl_surface; |
| 232 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 233 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 234 | sync, surface); |
| 235 | struct buffer *buf1 = create_shm_buffer_a8r8g8b8(client, 100, 100); |
| 236 | struct zwp_linux_buffer_release_v1 *buffer_release1; |
| 237 | struct zwp_linux_buffer_release_v1 *buffer_release2; |
| 238 | |
| 239 | buffer_release1 = |
| 240 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 241 | client_roundtrip(client); |
| 242 | |
| 243 | wl_surface_attach(surface, buf1->proxy, 0, 0); |
| 244 | wl_surface_commit(surface); |
| 245 | |
| 246 | buffer_release2 = |
| 247 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 248 | client_roundtrip(client); |
| 249 | |
| 250 | buffer_destroy(buf1); |
| 251 | zwp_linux_buffer_release_v1_destroy(buffer_release2); |
| 252 | zwp_linux_buffer_release_v1_destroy(buffer_release1); |
| 253 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 254 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | buffer_release_fenced_handler(void *data, |
| 259 | struct zwp_linux_buffer_release_v1 *buffer_release, |
| 260 | int32_t fence) |
| 261 | { |
| 262 | assert(!"Fenced release not supported yet"); |
| 263 | } |
| 264 | |
| 265 | static void |
| 266 | buffer_release_immediate_handler(void *data, |
| 267 | struct zwp_linux_buffer_release_v1 *buffer_release) |
| 268 | { |
| 269 | int *released = data; |
| 270 | |
| 271 | *released += 1; |
| 272 | } |
| 273 | |
| 274 | struct zwp_linux_buffer_release_v1_listener buffer_release_listener = { |
| 275 | buffer_release_fenced_handler, |
| 276 | buffer_release_immediate_handler |
| 277 | }; |
| 278 | |
| 279 | TEST(get_release_events_are_emitted) |
| 280 | { |
| 281 | struct client *client = create_test_client(); |
| 282 | struct zwp_linux_explicit_synchronization_v1 *sync = |
| 283 | get_linux_explicit_synchronization(client); |
| 284 | struct zwp_linux_surface_synchronization_v1 *surface_sync = |
| 285 | zwp_linux_explicit_synchronization_v1_get_synchronization( |
| 286 | sync, client->surface->wl_surface); |
| 287 | struct buffer *buf1 = create_shm_buffer_a8r8g8b8(client, 100, 100); |
| 288 | struct buffer *buf2 = create_shm_buffer_a8r8g8b8(client, 100, 100); |
| 289 | struct wl_surface *surface = client->surface->wl_surface; |
| 290 | struct zwp_linux_buffer_release_v1 *buffer_release1; |
| 291 | struct zwp_linux_buffer_release_v1 *buffer_release2; |
| 292 | int buf_released1 = 0; |
| 293 | int buf_released2 = 0; |
| 294 | int frame; |
| 295 | |
| 296 | buffer_release1 = |
| 297 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 298 | zwp_linux_buffer_release_v1_add_listener(buffer_release1, |
| 299 | &buffer_release_listener, |
| 300 | &buf_released1); |
| 301 | wl_surface_attach(surface, buf1->proxy, 0, 0); |
| 302 | frame_callback_set(surface, &frame); |
| 303 | wl_surface_commit(surface); |
| 304 | frame_callback_wait(client, &frame); |
| 305 | /* Check that exactly one buffer_release event was emitted. */ |
| 306 | assert(buf_released1 == 1); |
| 307 | |
| 308 | buffer_release2 = |
| 309 | zwp_linux_surface_synchronization_v1_get_release(surface_sync); |
| 310 | zwp_linux_buffer_release_v1_add_listener(buffer_release2, |
| 311 | &buffer_release_listener, |
| 312 | &buf_released2); |
| 313 | wl_surface_attach(surface, buf2->proxy, 0, 0); |
| 314 | frame_callback_set(surface, &frame); |
| 315 | wl_surface_commit(surface); |
| 316 | frame_callback_wait(client, &frame); |
| 317 | /* Check that we didn't get any new events on the inactive |
| 318 | * buffer_release. */ |
| 319 | assert(buf_released1 == 1); |
| 320 | /* Check that exactly one buffer_release event was emitted. */ |
| 321 | assert(buf_released2 == 1); |
| 322 | |
| 323 | buffer_destroy(buf2); |
| 324 | buffer_destroy(buf1); |
| 325 | zwp_linux_buffer_release_v1_destroy(buffer_release2); |
| 326 | zwp_linux_buffer_release_v1_destroy(buffer_release1); |
| 327 | zwp_linux_surface_synchronization_v1_destroy(surface_sync); |
| 328 | zwp_linux_explicit_synchronization_v1_destroy(sync); |
| 329 | } |