viewporter-test.c 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright © 2014, 2016 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>

#include "shared/helpers.h"
#include "shared/xalloc.h"
#include "weston-test-client-helper.h"
#include "viewporter-client-protocol.h"
Pekka Paalanen's avatar
Pekka Paalanen committed
38 39 40 41 42 43 44 45 46 47 48 49
#include "weston-test-fixture-compositor.h"

static enum test_result_code
fixture_setup(struct weston_test_harness *harness)
{
	struct compositor_setup setup;

	compositor_setup_defaults(&setup);

	return weston_test_harness_execute_as_client(harness, &setup);
}
DECLARE_FIXTURE_SETUP(fixture_setup);
50 51 52 53 54 55

static struct wp_viewporter *
get_viewporter(struct client *client)
{
	struct global *g;
	struct global *global_wpr = NULL;
56
	struct wp_viewporter *wpr;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	wl_list_for_each(g, &client->global_list, link) {
		if (strcmp(g->interface, wp_viewporter_interface.name))
			continue;

		if (global_wpr)
			assert(0 && "multiple wp_viewporter objects");

		global_wpr = g;
	}

	assert(global_wpr && "no wp_viewporter found");

	assert(global_wpr->version == 1);

	wpr = wl_registry_bind(client->wl_registry, global_wpr->name,
			       &wp_viewporter_interface, 1);
	assert(wpr);

	return wpr;
}

static struct wp_viewport *
create_viewport(struct client *client)
{
	struct wp_viewporter *viewporter;
	struct wp_viewport *viewport;

	viewporter = get_viewporter(client);
	viewport = wp_viewporter_get_viewport(viewporter,
					      client->surface->wl_surface);
	assert(viewport);

	return viewport;
}

static void
set_source(struct wp_viewport *vp, int x, int y, int w, int h)
{
	wp_viewport_set_source(vp, wl_fixed_from_int(x), wl_fixed_from_int(y),
				   wl_fixed_from_int(w), wl_fixed_from_int(h));
}

TEST(test_viewporter_double_create)
{
	struct wp_viewporter *viewporter;
	struct client *client;

	client = create_client_and_test_surface(100, 50, 123, 77);

	viewporter = get_viewporter(client);
	wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);
	wp_viewporter_get_viewport(viewporter, client->surface->wl_surface);

	expect_protocol_error(client, &wp_viewporter_interface,
			      WP_VIEWPORTER_ERROR_VIEWPORT_EXISTS);
}

struct bad_source_rect_args {
	int x, y, w, h;
};

static const struct bad_source_rect_args bad_source_rect_args[] = {
	{ -5,  0,  20,  10 },
	{  0, -5,  20,  10 },
	{  5,  6,   0,  10 },
	{  5,  6,  20,   0 },
	{  5,  6, -20,  10 },
	{  5,  6,  20, -10 },
	{ -1, -1,  20,  10 },
	{  5,  6,  -1,  -1 },
};

TEST_P(test_viewporter_bad_source_rect, bad_source_rect_args)
{
	const struct bad_source_rect_args *args = data;
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);

	vp = create_viewport(client);

140
	testlog("wp_viewport.set_source x=%d, y=%d, w=%d, h=%d\n",
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		args->x, args->y, args->w, args->h);
	set_source(vp, args->x, args->y, args->w, args->h);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_BAD_VALUE);
}

TEST(test_viewporter_unset_source_rect)
{
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);

	vp = create_viewport(client);
	set_source(vp, -1, -1, -1, -1);
	wl_surface_commit(client->surface->wl_surface);

	client_roundtrip(client);
}

struct bad_destination_args {
	int w, h;
};

static const struct bad_destination_args bad_destination_args[] = {
	{   0,  10 },
	{  20,   0 },
	{ -20,  10 },
	{  -1,  10 },
	{  20, -10 },
	{  20,  -1 },
};

TEST_P(test_viewporter_bad_destination_size, bad_destination_args)
{
	const struct bad_destination_args *args = data;
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);

	vp = create_viewport(client);

185
	testlog("wp_viewport.set_destination w=%d, h=%d\n", args->w, args->h);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	wp_viewport_set_destination(vp, args->w, args->h);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_BAD_VALUE);
}

TEST(test_viewporter_unset_destination_size)
{
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);

	vp = create_viewport(client);
	wp_viewport_set_destination(vp, -1, -1);
	wl_surface_commit(client->surface->wl_surface);

	client_roundtrip(client);
}

struct nonint_destination_args {
	wl_fixed_t w, h;
};

static const struct nonint_destination_args nonint_destination_args[] = {
#define F(i,f) ((i) * 256 + (f))
	{ F(20, 0),   F(10, 1) },
	{ F(20, 0),   F(10, -1) },
	{ F(20, 1),   F(10, 0) },
	{ F(20, -1),  F(10, 0) },
	{ F(20, 128), F(10, 128) },
#undef F
};

TEST_P(test_viewporter_non_integer_destination_size, nonint_destination_args)
{
	const struct nonint_destination_args *args = data;
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);

	vp = create_viewport(client);

230
	testlog("non-integer size w=%f, h=%f\n",
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		wl_fixed_to_double(args->w), wl_fixed_to_double(args->h));
	wp_viewport_set_source(vp, 5, 6, args->w, args->h);
	wp_viewport_set_destination(vp, -1, -1);
	wl_surface_commit(client->surface->wl_surface);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_BAD_SIZE);
}

struct source_buffer_args {
	wl_fixed_t x, y;
	wl_fixed_t w, h;
	int buffer_scale;
	enum wl_output_transform buffer_transform;
};

static int
get_surface_width(struct surface *surface,
		  int buffer_scale,
		  enum wl_output_transform buffer_transform)
{
	switch (buffer_transform) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
	case WL_OUTPUT_TRANSFORM_180:
	case WL_OUTPUT_TRANSFORM_FLIPPED:
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
		return surface->width / buffer_scale;
	case WL_OUTPUT_TRANSFORM_90:
	case WL_OUTPUT_TRANSFORM_270:
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		return surface->height / buffer_scale;
	}

	return -1;
}

static int
get_surface_height(struct surface *surface,
		   int buffer_scale,
		   enum wl_output_transform buffer_transform)
{
	switch (buffer_transform) {
	case WL_OUTPUT_TRANSFORM_NORMAL:
	case WL_OUTPUT_TRANSFORM_180:
	case WL_OUTPUT_TRANSFORM_FLIPPED:
	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
		return surface->height / buffer_scale;
	case WL_OUTPUT_TRANSFORM_90:
	case WL_OUTPUT_TRANSFORM_270:
	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
		return surface->width / buffer_scale;
	}

	return -1;
}

static void
setup_source_vs_buffer(struct client *client,
		       const struct source_buffer_args *args)
{
	struct wl_surface *surf;
	struct wp_viewport *vp;

	surf = client->surface->wl_surface;
	vp = create_viewport(client);

299
	testlog("surface %dx%d\n",
300 301 302 303
		get_surface_width(client->surface,
				  args->buffer_scale, args->buffer_transform),
		get_surface_height(client->surface,
				   args->buffer_scale, args->buffer_transform));
304
	testlog("source x=%f, y=%f, w=%f, h=%f; "
305 306 307 308 309 310 311
		"buffer scale=%d, transform=%d\n",
		wl_fixed_to_double(args->x), wl_fixed_to_double(args->y),
		wl_fixed_to_double(args->w), wl_fixed_to_double(args->h),
		args->buffer_scale, args->buffer_transform);

	wl_surface_set_buffer_scale(surf, args->buffer_scale);
	wl_surface_set_buffer_transform(surf, args->buffer_transform);
312
	wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
	wp_viewport_set_source(vp, args->x, args->y, args->w, args->h);
	wp_viewport_set_destination(vp, 99, 99);
	wl_surface_commit(surf);
}

/* buffer dimensions */
#define WIN_W 124
#define WIN_H 78

/* source rect base size */
#define SRC_W 20
#define SRC_H 10

/* margin */
#define MRG 10
/* epsilon of wl_fixed_t */
#define EPS 1

TEST(test_viewporter_source_buffer_params)
{
	const int max_scale = 2;

	/* buffer_scale requirement */
	assert(WIN_W % max_scale == 0);
	assert(WIN_H % max_scale == 0);

	/* source rect must fit inside regardless of scale and transform */
	assert(SRC_W < WIN_W / max_scale);
	assert(SRC_H < WIN_H / max_scale);
	assert(SRC_W < WIN_H / max_scale);
	assert(SRC_H < WIN_W / max_scale);

	/* If buffer scale was ignored, source rect should be inside instead */
	assert(WIN_W / max_scale + SRC_W + MRG < WIN_W);
	assert(WIN_H / max_scale + SRC_H + MRG < WIN_H);
	assert(WIN_W / max_scale + SRC_H + MRG < WIN_W);
	assert(WIN_H / max_scale + SRC_W + MRG < WIN_H);
}

static const struct source_buffer_args bad_source_buffer_args[] = {
#define F(i) ((i) * 256)

/* Flush right-top, but epsilon too far right. */
	{ F(WIN_W - SRC_W) + EPS, F(0), F(SRC_W),       F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W - SRC_W),       F(0), F(SRC_W) + EPS, F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
/* Flush left-bottom, but epsilon too far down. */
	{ F(0), F(WIN_H - SRC_H) + EPS, F(SRC_W), F(SRC_H),       1, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(0), F(WIN_H - SRC_H),       F(SRC_W), F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_NORMAL },
/* Completely outside on the right. */
	{ F(WIN_W + MRG), F(0), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
/* Competely outside on the bottom. */
	{ F(0), F(WIN_H + MRG), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },

/*
 * buffer_scale=2, so the surface size will be halved.
 * If buffer_scale was not taken into account, these would all be inside.
 * These are the same as above, but adapted to buffer_scale=2.
 */
	{ F(WIN_W / 2 - SRC_W) + EPS, F(0),                       F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W / 2 - SRC_W),       F(0),                       F(SRC_W) + EPS, F(SRC_H),       2, WL_OUTPUT_TRANSFORM_NORMAL },

	{ F(0),                       F(WIN_H / 2 - SRC_H) + EPS, F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(0),                       F(WIN_H / 2 - SRC_H),       F(SRC_W),       F(SRC_H) + EPS, 2, WL_OUTPUT_TRANSFORM_NORMAL },

	{ F(WIN_W / 2 + MRG),         F(0),                       F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_NORMAL },

	{ F(0),                       F(WIN_H / 2 + MRG),         F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_NORMAL },

/* Exceeding bottom-right corner by epsilon: */
/* non-dimension-swapping transforms */
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H),       F(SRC_W),       F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H) + EPS, F(SRC_W),       F(SRC_H),       1, WL_OUTPUT_TRANSFORM_FLIPPED },
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H),       F(SRC_W) + EPS, F(SRC_H),       1, WL_OUTPUT_TRANSFORM_180 },

/* dimension-swapping transforms */
	{ F(WIN_H - SRC_W) + EPS, F(WIN_W - SRC_H),       F(SRC_W),       F(SRC_H),       1, WL_OUTPUT_TRANSFORM_90 },
	{ F(WIN_H - SRC_W),       F(WIN_W - SRC_H) + EPS, F(SRC_W),       F(SRC_H),       1, WL_OUTPUT_TRANSFORM_270 },
	{ F(WIN_H - SRC_W),       F(WIN_W - SRC_H),       F(SRC_W) + EPS, F(SRC_H),       1, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
	{ F(WIN_H - SRC_W),       F(WIN_W - SRC_H),       F(SRC_W),       F(SRC_H) + EPS, 1, WL_OUTPUT_TRANSFORM_FLIPPED_270 },

/* non-dimension-swapping transforms, buffer_scale=2 */
	{ 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 },
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H),       F(SRC_W) + EPS, F(SRC_H),       2, WL_OUTPUT_TRANSFORM_FLIPPED },
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H),       F(SRC_W),       F(SRC_H) + EPS, 2, WL_OUTPUT_TRANSFORM_180 },

/* dimension-swapping transforms, buffer_scale=2 */
	{ F(WIN_H / 2 - SRC_W) + EPS, F(WIN_W / 2 - SRC_H),       F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_90 },
	{ F(WIN_H / 2 - SRC_W),       F(WIN_W / 2 - SRC_H) + EPS, F(SRC_W),       F(SRC_H),       2, WL_OUTPUT_TRANSFORM_270 },
	{ 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 },
	{ 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 },

#undef F
};

TEST_P(test_viewporter_source_outside_buffer, bad_source_buffer_args)
{
	const struct source_buffer_args *args = data;
	struct client *client;

	client = create_client_and_test_surface(100, 50, WIN_W, WIN_H);
	setup_source_vs_buffer(client, args);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_OUT_OF_BUFFER);
}

static const struct source_buffer_args good_source_buffer_args[] = {
#define F(i) ((i) * 256)

/* top-left, top-right, bottom-left, and bottom-right corner */
	{ F(0),             F(0),             F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W - SRC_W), F(0),             F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(0),             F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_NORMAL },

/* buffer_scale=2, so the surface size will be halved */
	{ F(0),                 F(0),                 F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W / 2 - SRC_W), F(0),                 F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(0),                 F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_NORMAL },

/* with half pixel offset */
	{ 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 },

/* Flushed to bottom-right corner: */
/* non-dimension-swapping transforms */
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED },
	{ F(WIN_W - SRC_W), F(WIN_H - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_180 },

/* dimension-swapping transforms */
	{ F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_90 },
	{ F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_270 },
	{ F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
	{ F(WIN_H - SRC_W), F(WIN_W - SRC_H), F(SRC_W), F(SRC_H), 1, WL_OUTPUT_TRANSFORM_FLIPPED_270 },

/* non-dimension-swapping transforms, buffer_scale=2 */
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_180 },
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED },
	{ F(WIN_W / 2 - SRC_W), F(WIN_H / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_180 },

/* dimension-swapping transforms, buffer_scale=2 */
	{ F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_90 },
	{ F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_270 },
	{ F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_90 },
	{ F(WIN_H / 2 - SRC_W), F(WIN_W / 2 - SRC_H), F(SRC_W), F(SRC_H), 2, WL_OUTPUT_TRANSFORM_FLIPPED_270 },

#undef F
};

TEST_P(test_viewporter_source_inside_buffer, good_source_buffer_args)
{
	const struct source_buffer_args *args = data;
	struct client *client;

	client = create_client_and_test_surface(100, 50, WIN_W, WIN_H);
	setup_source_vs_buffer(client, args);
	client_roundtrip(client);
}

#undef WIN_W
#undef WIN_H
#undef SRC_W
#undef SRC_H
#undef MRG
#undef EPS

TEST(test_viewporter_outside_null_buffer)
{
	struct client *client;
	struct wp_viewport *vp;
	struct wl_surface *surf;

	client = create_client_and_test_surface(100, 50, 123, 77);
	surf = client->surface->wl_surface;

	/* If buffer is NULL, does not matter what the source rect is. */
	vp = create_viewport(client);
	wl_surface_attach(surf, NULL, 0, 0);
	set_source(vp, 1000, 1000, 20, 10);
	wp_viewport_set_destination(vp, 99, 99);
	wl_surface_commit(surf);
	client_roundtrip(client);

	/* Try again, with all old values. */
	wl_surface_commit(surf);
	client_roundtrip(client);

	/* Try once more with old NULL buffer. */
	set_source(vp, 1200, 1200, 20, 10);
	wl_surface_commit(surf);
	client_roundtrip(client);

	/* When buffer comes back, source rect matters again. */
507
	wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	wl_surface_commit(surf);
	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_OUT_OF_BUFFER);
}

TEST(test_viewporter_no_surface_set_source)
{
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);
	vp = create_viewport(client);
	wl_surface_destroy(client->surface->wl_surface);
	client->surface->wl_surface = NULL;

	/* But the wl_surface does not exist anymore. */
	set_source(vp, 1000, 1000, 20, 10);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_NO_SURFACE);
}

TEST(test_viewporter_no_surface_set_destination)
{
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);
	vp = create_viewport(client);
	wl_surface_destroy(client->surface->wl_surface);
	client->surface->wl_surface = NULL;

	/* But the wl_surface does not exist anymore. */
	wp_viewport_set_destination(vp, 99, 99);

	expect_protocol_error(client, &wp_viewport_interface,
			      WP_VIEWPORT_ERROR_NO_SURFACE);
}

TEST(test_viewporter_no_surface_destroy)
{
	struct client *client;
	struct wp_viewport *vp;

	client = create_client_and_test_surface(100, 50, 123, 77);
	vp = create_viewport(client);
	wl_surface_destroy(client->surface->wl_surface);
	client->surface->wl_surface = NULL;

	/* But the wl_surface does not exist anymore. */
	wp_viewport_destroy(vp);

	client_roundtrip(client);
}