2 * Copyright © 2014, 2016 Collabora, Ltd.
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:
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.
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
34 #include "shared/helpers.h"
35 #include "shared/xalloc.h"
36 #include "weston-test-client-helper.h"
37 #include "viewporter-client-protocol.h"
39 static struct wp_viewporter *
40 get_viewporter(struct client *client)
43 struct global *global_wpr = NULL;
44 static struct wp_viewporter *wpr;
49 wl_list_for_each(g, &client->global_list, link) {
50 if (strcmp(g->interface, wp_viewporter_interface.name))
54 assert(0 && "multiple wp_viewporter objects");
59 assert(global_wpr && "no wp_viewporter found");
61 assert(global_wpr->version == 1);
63 wpr = wl_registry_bind(client->wl_registry, global_wpr->name,
64 &wp_viewporter_interface, 1);
70 static struct wp_viewport *
71 create_viewport(struct client *client)
73 struct wp_viewporter *viewporter;
74 struct wp_viewport *viewport;
76 viewporter = get_viewporter(client);
77 viewport = wp_viewporter_get_viewport(viewporter,
78 client->surface->wl_surface);
85 set_source(struct wp_viewport *vp, int x, int y, int w, int h)
87 wp_viewport_set_source(vp, wl_fixed_from_int(x), wl_fixed_from_int(y),
88 wl_fixed_from_int(w), wl_fixed_from_int(h));
91 TEST(test_viewporter_double_create)
93 struct wp_viewporter *viewporter;
94 struct client *client;
96 client = create_client_and_test_surface(100, 50, 123, 77);
98 viewporter = get_viewporter(client);
99 wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);
100 wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);
102 expect_protocol_error(client, &wp_viewporter_interface,
103 WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS);
106 struct bad_source_rect_args {
110 static const struct bad_source_rect_args bad_source_rect_args[] = {
121 TEST_P(test_viewporter_bad_source_rect, bad_source_rect_args)
123 const struct bad_source_rect_args *args = data;
124 struct client *client;
125 struct wp_viewport *vp;
127 client = create_client_and_test_surface(100, 50, 123, 77);
129 vp = create_viewport(client);
131 fprintf(stderr, "wp_viewport.set_source x=%d, y=%d, w=%d, h=%d\n",
132 args->x, args->y, args->w, args->h);
133 set_source(vp, args->x, args->y, args->w, args->h);
135 expect_protocol_error(client, &wp_viewport_interface,
136 WP_VIEWPORT_ERROR_BAD_VALUE);
139 TEST(test_viewporter_unset_source_rect)
141 struct client *client;
142 struct wp_viewport *vp;
144 client = create_client_and_test_surface(100, 50, 123, 77);
146 vp = create_viewport(client);
147 set_source(vp, -1, -1, -1, -1);
148 wl_surface_commit(client->surface->wl_surface);
150 client_roundtrip(client);
153 struct bad_destination_args {
157 static const struct bad_destination_args bad_destination_args[] = {
166 TEST_P(test_viewporter_bad_destination_size, bad_destination_args)
168 const struct bad_destination_args *args = data;
169 struct client *client;
170 struct wp_viewport *vp;
172 client = create_client_and_test_surface(100, 50, 123, 77);
174 vp = create_viewport(client);
176 fprintf(stderr, "wp_viewport.set_destination w=%d, h=%d\n",
178 wp_viewport_set_destination(vp, args->w, args->h);
180 expect_protocol_error(client, &wp_viewport_interface,
181 WP_VIEWPORT_ERROR_BAD_VALUE);
184 TEST(test_viewporter_unset_destination_size)
186 struct client *client;
187 struct wp_viewport *vp;
189 client = create_client_and_test_surface(100, 50, 123, 77);
191 vp = create_viewport(client);
192 wp_viewport_set_destination(vp, -1, -1);
193 wl_surface_commit(client->surface->wl_surface);
195 client_roundtrip(client);
198 struct nonint_destination_args {
202 static const struct nonint_destination_args nonint_destination_args[] = {
203 #define F(i,f) ((i) * 256 + (f))
204 { F(20, 0), F(10, 1) },
205 { F(20, 0), F(10, -1) },
206 { F(20, 1), F(10, 0) },
207 { F(20, -1), F(10, 0) },
208 { F(20, 128), F(10, 128) },
212 TEST_P(test_viewporter_non_integer_destination_size, nonint_destination_args)
214 const struct nonint_destination_args *args = data;
215 struct client *client;
216 struct wp_viewport *vp;
218 client = create_client_and_test_surface(100, 50, 123, 77);
220 vp = create_viewport(client);
222 fprintf(stderr, "non-integer size w=%f, h=%f\n",
223 wl_fixed_to_double(args->w), wl_fixed_to_double(args->h));
224 wp_viewport_set_source(vp, 5, 6, args->w, args->h);
225 wp_viewport_set_destination(vp, -1, -1);
226 wl_surface_commit(client->surface->wl_surface);
228 expect_protocol_error(client, &wp_viewport_interface,
229 WP_VIEWPORT_ERROR_BAD_SIZE);
232 struct source_buffer_args {
236 enum wl_output_transform buffer_transform;
240 get_surface_width(struct surface *surface,
242 enum wl_output_transform buffer_transform)
244 switch (buffer_transform) {
245 case WL_OUTPUT_TRANSFORM_NORMAL:
246 case WL_OUTPUT_TRANSFORM_180:
247 case WL_OUTPUT_TRANSFORM_FLIPPED:
248 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
249 return surface->width / buffer_scale;
250 case WL_OUTPUT_TRANSFORM_90:
251 case WL_OUTPUT_TRANSFORM_270:
252 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
253 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
254 return surface->height / buffer_scale;
261 get_surface_height(struct surface *surface,
263 enum wl_output_transform buffer_transform)
265 switch (buffer_transform) {
266 case WL_OUTPUT_TRANSFORM_NORMAL:
267 case WL_OUTPUT_TRANSFORM_180:
268 case WL_OUTPUT_TRANSFORM_FLIPPED:
269 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
270 return surface->height / buffer_scale;
271 case WL_OUTPUT_TRANSFORM_90:
272 case WL_OUTPUT_TRANSFORM_270:
273 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
274 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
275 return surface->width / buffer_scale;
282 setup_source_vs_buffer(struct client *client,
283 const struct source_buffer_args *args)
285 struct wl_surface *surf;
286 struct wp_viewport *vp;
288 surf = client->surface->wl_surface;
289 vp = create_viewport(client);
291 fprintf(stderr, "surface %dx%d\n",
292 get_surface_width(client->surface,
293 args->buffer_scale, args->buffer_transform),
294 get_surface_height(client->surface,
295 args->buffer_scale, args->buffer_transform));
296 fprintf(stderr, "source x=%f, y=%f, w=%f, h=%f; "
297 "buffer scale=%d, transform=%d\n",
298 wl_fixed_to_double(args->x), wl_fixed_to_double(args->y),
299 wl_fixed_to_double(args->w), wl_fixed_to_double(args->h),
300 args->buffer_scale, args->buffer_transform);
302 wl_surface_set_buffer_scale(surf, args->buffer_scale);
303 wl_surface_set_buffer_transform(surf, args->buffer_transform);
304 wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
305 wp_viewport_set_source(vp, args->x, args->y, args->w, args->h);
306 wp_viewport_set_destination(vp, 99, 99);
307 wl_surface_commit(surf);
310 /* buffer dimensions */
314 /* source rect base size */
320 /* epsilon of wl_fixed_t */
323 TEST(test_viewporter_source_buffer_params)
325 const int max_scale = 2;
327 /* buffer_scale requirement */
328 assert(WIN_W % max_scale == 0);
329 assert(WIN_H % max_scale == 0);
331 /* source rect must fit inside regardless of scale and transform */
332 assert(SRC_W < WIN_W / max_scale);
333 assert(SRC_H < WIN_H / max_scale);
334 assert(SRC_W < WIN_H / max_scale);
335 assert(SRC_H < WIN_W / max_scale);
337 /* If buffer scale was ignored, source rect should be inside instead */
338 assert(WIN_W / max_scale + SRC_W + MRG < WIN_W);
339 assert(WIN_H / max_scale + SRC_H + MRG < WIN_H);
340 assert(WIN_W / max_scale + SRC_H + MRG < WIN_W);
341 assert(WIN_H / max_scale + SRC_W + MRG < WIN_H);
344 static const struct source_buffer_args bad_source_buffer_args[] = {
345 #define F(i) ((i) * 256)
347 /* Flush right-top, but epsilon too far right. */
348 { F(WIN_W - SRC_W) + EPS, F(0), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
349 { F(WIN_W - SRC_W), F(0), F(SRC_W) + EPS, F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
350 /* Flush left-bottom, but epsilon too far down. */
351 { F(0), F(WIN_H - SRC_H) + EPS, F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
352 { F(0), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_NORMAL },
353 /* Completely outside on the right. */
354 { F(WIN_W + MRG), F(0), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
355 /* Competely outside on the bottom. */
356 { F(0), F(WIN_H + MRG), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
359 * buffer_scale=2, so the surface size will be halved.
360 * If buffer_scale was not taken into account, these would all be inside.
361 * These are the same as above, but adapted to buffer_scale=2.
363 { F(WIN_W / 2 - SRC_W) + EPS, F(0), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
364 { F(WIN_W / 2 - SRC_W), F(0), F(SRC_W) + EPS, F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
366 { F(0), F(WIN_H / 2 - SRC_H) + EPS, F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
367 { F(0), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H) + EPS, 2, WL_OUTPUT_TRANSFORM_NORMAL },
369 { F(WIN_W / 2 + MRG), F(0), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
371 { F(0), F(WIN_H / 2 + MRG), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
373 /* Exceeding bottom-right corner by epsilon: */
374 /* non-dimension-swapping transforms */
375 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
376 { F(WIN_W - SRC_W), F(WIN_H - SRC_H) + EPS, F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED },
377 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W) + EPS, F(SRC_H), 1, WL_OUTPUT_TRANSFORM_180 },
379 /* dimension-swapping transforms */
380 { F(WIN_H - SRC_W) + EPS, F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_90 },
381 { F(WIN_H - SRC_W), F(WIN_W - SRC_H) + EPS, F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_270 },
382 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W) + EPS, F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
383 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_FLIPPED_270 },
385 /* non-dimension-swapping transforms, buffer_scale=2 */
386 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H) + EPS, F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
387 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W) + EPS, F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED },
388 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H) + EPS, 2, WL_OUTPUT_TRANSFORM_180 },
390 /* dimension-swapping transforms, buffer_scale=2 */
391 { F(WIN_H / 2 - SRC_W) + EPS, F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_90 },
392 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H) + EPS, F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_270 },
393 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W) + EPS, F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
394 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H) + EPS, 2, WL_OUTPUT_TRANSFORM_FLIPPED_270 },
399 TEST_P(test_viewporter_source_outside_buffer, bad_source_buffer_args)
401 const struct source_buffer_args *args = data;
402 struct client *client;
404 client = create_client_and_test_surface(100, 50, WIN_W, WIN_H);
405 setup_source_vs_buffer(client, args);
407 expect_protocol_error(client, &wp_viewport_interface,
408 WP_VIEWPORT_ERROR_OUT_OF_BUFFER);
411 static const struct source_buffer_args good_source_buffer_args[] = {
412 #define F(i) ((i) * 256)
414 /* top-left, top-right, bottom-left, and bottom-right corner */
415 { F(0), F(0), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
416 { F(WIN_W - SRC_W), F(0), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
417 { F(0), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
418 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
420 /* buffer_scale=2, so the surface size will be halved */
421 { F(0), F(0), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
422 { F(WIN_W / 2 - SRC_W), F(0), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
423 { F(0), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
424 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
426 /* with half pixel offset */
427 { F(WIN_W / 2 - SRC_W) + 128, F(WIN_H / 2 - SRC_H) + 128, F(SRC_W) - 128, F(SRC_H) - 128, 2, WL_OUTPUT_TRANSFORM_NORMAL },
429 /* Flushed to bottom-right corner: */
430 /* non-dimension-swapping transforms */
431 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
432 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED },
433 { F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_180 },
435 /* dimension-swapping transforms */
436 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_90 },
437 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_270 },
438 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
439 { F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_270 },
441 /* non-dimension-swapping transforms, buffer_scale=2 */
442 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
443 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED },
444 { F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_180 },
446 /* dimension-swapping transforms, buffer_scale=2 */
447 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_90 },
448 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_270 },
449 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
450 { F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_270 },
455 TEST_P(test_viewporter_source_inside_buffer, good_source_buffer_args)
457 const struct source_buffer_args *args = data;
458 struct client *client;
460 client = create_client_and_test_surface(100, 50, WIN_W, WIN_H);
461 setup_source_vs_buffer(client, args);
462 client_roundtrip(client);
472 TEST(test_viewporter_outside_null_buffer)
474 struct client *client;
475 struct wp_viewport *vp;
476 struct wl_surface *surf;
478 client = create_client_and_test_surface(100, 50, 123, 77);
479 surf = client->surface->wl_surface;
481 /* If buffer is NULL, does not matter what the source rect is. */
482 vp = create_viewport(client);
483 wl_surface_attach(surf, NULL, 0, 0);
484 set_source(vp, 1000, 1000, 20, 10);
485 wp_viewport_set_destination(vp, 99, 99);
486 wl_surface_commit(surf);
487 client_roundtrip(client);
489 /* Try again, with all old values. */
490 wl_surface_commit(surf);
491 client_roundtrip(client);
493 /* Try once more with old NULL buffer. */
494 set_source(vp, 1200, 1200, 20, 10);
495 wl_surface_commit(surf);
496 client_roundtrip(client);
498 /* When buffer comes back, source rect matters again. */
499 wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
500 wl_surface_commit(surf);
501 expect_protocol_error(client, &wp_viewport_interface,
502 WP_VIEWPORT_ERROR_OUT_OF_BUFFER);
505 TEST(test_viewporter_no_surface_set_source)
507 struct client *client;
508 struct wp_viewport *vp;
510 client = create_client_and_test_surface(100, 50, 123, 77);
511 vp = create_viewport(client);
512 wl_surface_destroy(client->surface->wl_surface);
513 client->surface->wl_surface = NULL;
515 /* But the wl_surface does not exist anymore. */
516 set_source(vp, 1000, 1000, 20, 10);
518 expect_protocol_error(client, &wp_viewport_interface,
519 WP_VIEWPORT_ERROR_NO_SURFACE);
522 TEST(test_viewporter_no_surface_set_destination)
524 struct client *client;
525 struct wp_viewport *vp;
527 client = create_client_and_test_surface(100, 50, 123, 77);
528 vp = create_viewport(client);
529 wl_surface_destroy(client->surface->wl_surface);
530 client->surface->wl_surface = NULL;
532 /* But the wl_surface does not exist anymore. */
533 wp_viewport_set_destination(vp, 99, 99);
535 expect_protocol_error(client, &wp_viewport_interface,
536 WP_VIEWPORT_ERROR_NO_SURFACE);
539 TEST(test_viewporter_no_surface_destroy)
541 struct client *client;
542 struct wp_viewport *vp;
544 client = create_client_and_test_surface(100, 50, 123, 77);
545 vp = create_viewport(client);
546 wl_surface_destroy(client->surface->wl_surface);
547 client->surface->wl_surface = NULL;
549 /* But the wl_surface does not exist anymore. */
550 wp_viewport_destroy(vp);
552 client_roundtrip(client);