18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Test case for drm_damage_helper functions
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "drm_damage_helper: " fmt
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <drm/drm_damage_helper.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include "test-drm_modeset_common.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic void set_plane_src(struct drm_plane_state *state, int x1, int y1, int x2,
138c2ecf20Sopenharmony_ci			  int y2)
148c2ecf20Sopenharmony_ci{
158c2ecf20Sopenharmony_ci	state->src.x1 = x1;
168c2ecf20Sopenharmony_ci	state->src.y1 = y1;
178c2ecf20Sopenharmony_ci	state->src.x2 = x2;
188c2ecf20Sopenharmony_ci	state->src.y2 = y2;
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic void set_damage_clip(struct drm_mode_rect *r, int x1, int y1, int x2,
228c2ecf20Sopenharmony_ci			    int y2)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	r->x1 = x1;
258c2ecf20Sopenharmony_ci	r->y1 = y1;
268c2ecf20Sopenharmony_ci	r->x2 = x2;
278c2ecf20Sopenharmony_ci	r->y2 = y2;
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic void set_damage_blob(struct drm_property_blob *damage_blob,
318c2ecf20Sopenharmony_ci			    struct drm_mode_rect *r, uint32_t size)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	damage_blob->length = size;
348c2ecf20Sopenharmony_ci	damage_blob->data = r;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic void set_plane_damage(struct drm_plane_state *state,
388c2ecf20Sopenharmony_ci			     struct drm_property_blob *damage_blob)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	state->fb_damage_clips = damage_blob;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic bool check_damage_clip(struct drm_plane_state *state, struct drm_rect *r,
448c2ecf20Sopenharmony_ci			      int x1, int y1, int x2, int y2)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	/*
478c2ecf20Sopenharmony_ci	 * Round down x1/y1 and round up x2/y2. This is because damage is not in
488c2ecf20Sopenharmony_ci	 * 16.16 fixed point so to catch all pixels.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci	int src_x1 = state->src.x1 >> 16;
518c2ecf20Sopenharmony_ci	int src_y1 = state->src.y1 >> 16;
528c2ecf20Sopenharmony_ci	int src_x2 = (state->src.x2 >> 16) + !!(state->src.x2 & 0xFFFF);
538c2ecf20Sopenharmony_ci	int src_y2 = (state->src.y2 >> 16) + !!(state->src.y2 & 0xFFFF);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (x1 >= x2 || y1 >= y2) {
568c2ecf20Sopenharmony_ci		pr_err("Cannot have damage clip with no dimension.\n");
578c2ecf20Sopenharmony_ci		return false;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (x1 < src_x1 || y1 < src_y1 || x2 > src_x2 || y2 > src_y2) {
618c2ecf20Sopenharmony_ci		pr_err("Damage cannot be outside rounded plane src.\n");
628c2ecf20Sopenharmony_ci		return false;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (r->x1 != x1 || r->y1 != y1 || r->x2 != x2 || r->y2 != y2) {
668c2ecf20Sopenharmony_ci		pr_err("Damage = %d %d %d %d\n", r->x1, r->y1, r->x2, r->y2);
678c2ecf20Sopenharmony_ci		return false;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return true;
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage(void *ignored)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
768c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
778c2ecf20Sopenharmony_ci	struct drm_rect clip;
788c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
818c2ecf20Sopenharmony_ci		.width = 2048,
828c2ecf20Sopenharmony_ci		.height = 2048
838c2ecf20Sopenharmony_ci	};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
868c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
878c2ecf20Sopenharmony_ci		.fb = &fb,
888c2ecf20Sopenharmony_ci		.visible = true,
898c2ecf20Sopenharmony_ci	};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* Plane src same as fb size. */
928c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, fb.width << 16, fb.height << 16);
938c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, fb.width << 16, fb.height << 16);
948c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
958c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
968c2ecf20Sopenharmony_ci		num_hits++;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return plane src as damage.");
998c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 0, 0, 2048, 2048));
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_fractional_src(void *ignored)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
1078c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
1088c2ecf20Sopenharmony_ci	struct drm_rect clip;
1098c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
1128c2ecf20Sopenharmony_ci		.width = 2048,
1138c2ecf20Sopenharmony_ci		.height = 2048
1148c2ecf20Sopenharmony_ci	};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
1178c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
1188c2ecf20Sopenharmony_ci		.fb = &fb,
1198c2ecf20Sopenharmony_ci		.visible = true,
1208c2ecf20Sopenharmony_ci	};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Plane src has fractional part. */
1238c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x3fffe, 0x3fffe,
1248c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
1258c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x3fffe, 0x3fffe,
1268c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
1278c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
1288c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
1298c2ecf20Sopenharmony_ci		num_hits++;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return rounded off plane src as damage.");
1328c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 3, 3, 1028, 772));
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_src_moved(void *ignored)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
1408c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
1418c2ecf20Sopenharmony_ci	struct drm_rect clip;
1428c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
1458c2ecf20Sopenharmony_ci		.width = 2048,
1468c2ecf20Sopenharmony_ci		.height = 2048
1478c2ecf20Sopenharmony_ci	};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
1508c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
1518c2ecf20Sopenharmony_ci		.fb = &fb,
1528c2ecf20Sopenharmony_ci		.visible = true,
1538c2ecf20Sopenharmony_ci	};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Plane src moved since old plane state. */
1568c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
1578c2ecf20Sopenharmony_ci	set_plane_src(&state, 10 << 16, 10 << 16,
1588c2ecf20Sopenharmony_ci		      (10 + 1024) << 16, (10 + 768) << 16);
1598c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
1608c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
1618c2ecf20Sopenharmony_ci		num_hits++;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return plane src as damage.");
1648c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 10, 10, 1034, 778));
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return 0;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_fractional_src_moved(void *ignored)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
1728c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
1738c2ecf20Sopenharmony_ci	struct drm_rect clip;
1748c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
1778c2ecf20Sopenharmony_ci		.width = 2048,
1788c2ecf20Sopenharmony_ci		.height = 2048
1798c2ecf20Sopenharmony_ci	};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
1828c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
1838c2ecf20Sopenharmony_ci		.fb = &fb,
1848c2ecf20Sopenharmony_ci		.visible = true,
1858c2ecf20Sopenharmony_ci	};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* Plane src has fractional part and it moved since old plane state. */
1888c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x3fffe, 0x3fffe,
1898c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
1908c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
1918c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
1928c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
1938c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
1948c2ecf20Sopenharmony_ci		num_hits++;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return plane src as damage.");
1978c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 4, 4, 1029, 773));
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	return 0;
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_not_visible(void *ignored)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
2058c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
2068c2ecf20Sopenharmony_ci	struct drm_rect clip;
2078c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
2108c2ecf20Sopenharmony_ci		.width = 2048,
2118c2ecf20Sopenharmony_ci		.height = 2048
2128c2ecf20Sopenharmony_ci	};
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
2158c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
2168c2ecf20Sopenharmony_ci		.fb = &fb,
2178c2ecf20Sopenharmony_ci		.visible = false,
2188c2ecf20Sopenharmony_ci	};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
2218c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
2228c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
2238c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
2248c2ecf20Sopenharmony_ci		num_hits++;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should have no damage.");
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return 0;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_no_crtc(void *ignored)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
2348c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
2358c2ecf20Sopenharmony_ci	struct drm_rect clip;
2368c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
2398c2ecf20Sopenharmony_ci		.width = 2048,
2408c2ecf20Sopenharmony_ci		.height = 2048
2418c2ecf20Sopenharmony_ci	};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
2448c2ecf20Sopenharmony_ci		.crtc = 0,
2458c2ecf20Sopenharmony_ci		.fb = &fb,
2468c2ecf20Sopenharmony_ci	};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
2498c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
2508c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
2518c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
2528c2ecf20Sopenharmony_ci		num_hits++;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should have no damage.");
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	return 0;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ciint igt_damage_iter_no_damage_no_fb(void *ignored)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
2628c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
2638c2ecf20Sopenharmony_ci	struct drm_rect clip;
2648c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
2678c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
2688c2ecf20Sopenharmony_ci		.fb = 0,
2698c2ecf20Sopenharmony_ci	};
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
2728c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
2738c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
2748c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
2758c2ecf20Sopenharmony_ci		num_hits++;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should have no damage.");
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciint igt_damage_iter_simple_damage(void *ignored)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
2858c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
2868c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
2878c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
2888c2ecf20Sopenharmony_ci	struct drm_rect clip;
2898c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
2928c2ecf20Sopenharmony_ci		.width = 2048,
2938c2ecf20Sopenharmony_ci		.height = 2048
2948c2ecf20Sopenharmony_ci	};
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
2978c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
2988c2ecf20Sopenharmony_ci		.fb = &fb,
2998c2ecf20Sopenharmony_ci		.visible = true,
3008c2ecf20Sopenharmony_ci	};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
3038c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
3048c2ecf20Sopenharmony_ci	/* Damage set to plane src */
3058c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 0, 0, 1024, 768);
3068c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
3078c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
3088c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
3098c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
3108c2ecf20Sopenharmony_ci		num_hits++;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage when set.");
3138c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 0, 0, 1024, 768));
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	return 0;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage(void *ignored)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
3218c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
3228c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
3238c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
3248c2ecf20Sopenharmony_ci	struct drm_rect clip;
3258c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
3288c2ecf20Sopenharmony_ci		.width = 2048,
3298c2ecf20Sopenharmony_ci		.height = 2048
3308c2ecf20Sopenharmony_ci	};
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
3338c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
3348c2ecf20Sopenharmony_ci		.fb = &fb,
3358c2ecf20Sopenharmony_ci		.visible = true,
3368c2ecf20Sopenharmony_ci	};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
3398c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
3408c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 256, 192, 768, 576);
3418c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
3428c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
3438c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
3448c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
3458c2ecf20Sopenharmony_ci		num_hits++;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage when set.");
3488c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 256, 192, 768, 576));
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	return 0;
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_intersect_src(void *ignored)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
3568c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
3578c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
3588c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
3598c2ecf20Sopenharmony_ci	struct drm_rect clip;
3608c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
3638c2ecf20Sopenharmony_ci		.width = 2048,
3648c2ecf20Sopenharmony_ci		.height = 2048
3658c2ecf20Sopenharmony_ci	};
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
3688c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
3698c2ecf20Sopenharmony_ci		.fb = &fb,
3708c2ecf20Sopenharmony_ci		.visible = true,
3718c2ecf20Sopenharmony_ci	};
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
3748c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
3758c2ecf20Sopenharmony_ci	/* Damage intersect with plane src. */
3768c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 256, 192, 1360, 768);
3778c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
3788c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
3798c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
3808c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
3818c2ecf20Sopenharmony_ci		num_hits++;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage clipped to src.");
3848c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 256, 192, 1024, 768));
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_outside_src(void *ignored)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
3928c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
3938c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
3948c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
3958c2ecf20Sopenharmony_ci	struct drm_rect clip;
3968c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
3998c2ecf20Sopenharmony_ci		.width = 2048,
4008c2ecf20Sopenharmony_ci		.height = 2048
4018c2ecf20Sopenharmony_ci	};
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
4048c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
4058c2ecf20Sopenharmony_ci		.fb = &fb,
4068c2ecf20Sopenharmony_ci		.visible = true,
4078c2ecf20Sopenharmony_ci	};
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
4108c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
4118c2ecf20Sopenharmony_ci	/* Damage clip outside plane src */
4128c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 1360, 1360, 1380, 1380);
4138c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
4148c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
4158c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
4168c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
4178c2ecf20Sopenharmony_ci		num_hits++;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should have no damage.");
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	return 0;
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_fractional_src(void *ignored)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
4278c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
4288c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
4298c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
4308c2ecf20Sopenharmony_ci	struct drm_rect clip;
4318c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
4348c2ecf20Sopenharmony_ci		.width = 2048,
4358c2ecf20Sopenharmony_ci		.height = 2048
4368c2ecf20Sopenharmony_ci	};
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
4398c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
4408c2ecf20Sopenharmony_ci		.fb = &fb,
4418c2ecf20Sopenharmony_ci		.visible = true,
4428c2ecf20Sopenharmony_ci	};
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	/* Plane src has fractional part. */
4458c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
4468c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
4478c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
4488c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
4498c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 10, 10, 256, 330);
4508c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
4518c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
4528c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
4538c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
4548c2ecf20Sopenharmony_ci		num_hits++;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage when set.");
4578c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 10, 10, 256, 330));
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	return 0;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_intersect_fractional_src(void *ignored)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
4658c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
4668c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
4678c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
4688c2ecf20Sopenharmony_ci	struct drm_rect clip;
4698c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
4728c2ecf20Sopenharmony_ci		.width = 2048,
4738c2ecf20Sopenharmony_ci		.height = 2048
4748c2ecf20Sopenharmony_ci	};
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
4778c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
4788c2ecf20Sopenharmony_ci		.fb = &fb,
4798c2ecf20Sopenharmony_ci		.visible = true,
4808c2ecf20Sopenharmony_ci	};
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	/* Plane src has fractional part. */
4838c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
4848c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
4858c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
4868c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
4878c2ecf20Sopenharmony_ci	/* Damage intersect with plane src. */
4888c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 10, 1, 1360, 330);
4898c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
4908c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
4918c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
4928c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
4938c2ecf20Sopenharmony_ci		num_hits++;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage clipped to rounded off src.");
4968c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 10, 4, 1029, 330));
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	return 0;
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_outside_fractional_src(void *ignored)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
5048c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
5058c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
5068c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
5078c2ecf20Sopenharmony_ci	struct drm_rect clip;
5088c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
5118c2ecf20Sopenharmony_ci		.width = 2048,
5128c2ecf20Sopenharmony_ci		.height = 2048
5138c2ecf20Sopenharmony_ci	};
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
5168c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
5178c2ecf20Sopenharmony_ci		.fb = &fb,
5188c2ecf20Sopenharmony_ci		.visible = true,
5198c2ecf20Sopenharmony_ci	};
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Plane src has fractional part. */
5228c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
5238c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
5248c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
5258c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
5268c2ecf20Sopenharmony_ci	/* Damage clip outside plane src */
5278c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 1360, 1360, 1380, 1380);
5288c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
5298c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
5308c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
5318c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
5328c2ecf20Sopenharmony_ci		num_hits++;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should have no damage.");
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	return 0;
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_src_moved(void *ignored)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
5428c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
5438c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
5448c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
5458c2ecf20Sopenharmony_ci	struct drm_rect clip;
5468c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
5498c2ecf20Sopenharmony_ci		.width = 2048,
5508c2ecf20Sopenharmony_ci		.height = 2048
5518c2ecf20Sopenharmony_ci	};
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
5548c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
5558c2ecf20Sopenharmony_ci		.fb = &fb,
5568c2ecf20Sopenharmony_ci		.visible = true,
5578c2ecf20Sopenharmony_ci	};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	/* Plane src moved since old plane state. */
5608c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
5618c2ecf20Sopenharmony_ci	set_plane_src(&state, 10 << 16, 10 << 16,
5628c2ecf20Sopenharmony_ci		      (10 + 1024) << 16, (10 + 768) << 16);
5638c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 20, 30, 256, 256);
5648c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
5658c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
5668c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
5678c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
5688c2ecf20Sopenharmony_ci		num_hits++;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return plane src as damage.");
5718c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 10, 10, 1034, 778));
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	return 0;
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ciint igt_damage_iter_single_damage_fractional_src_moved(void *ignored)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
5798c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
5808c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
5818c2ecf20Sopenharmony_ci	struct drm_mode_rect damage;
5828c2ecf20Sopenharmony_ci	struct drm_rect clip;
5838c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
5868c2ecf20Sopenharmony_ci		.width = 2048,
5878c2ecf20Sopenharmony_ci		.height = 2048
5888c2ecf20Sopenharmony_ci	};
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
5918c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
5928c2ecf20Sopenharmony_ci		.fb = &fb,
5938c2ecf20Sopenharmony_ci		.visible = true,
5948c2ecf20Sopenharmony_ci	};
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* Plane src with fractional part moved since old plane state. */
5978c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x3fffe, 0x3fffe,
5988c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
5998c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
6008c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
6018c2ecf20Sopenharmony_ci	/* Damage intersect with plane src. */
6028c2ecf20Sopenharmony_ci	set_damage_clip(&damage, 20, 30, 1360, 256);
6038c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage, sizeof(damage));
6048c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
6058c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
6068c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
6078c2ecf20Sopenharmony_ci		num_hits++;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return rounded off plane src as damage.");
6108c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 4, 4, 1029, 773));
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	return 0;
6138c2ecf20Sopenharmony_ci}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ciint igt_damage_iter_damage(void *ignored)
6168c2ecf20Sopenharmony_ci{
6178c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
6188c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
6198c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
6208c2ecf20Sopenharmony_ci	struct drm_mode_rect damage[2];
6218c2ecf20Sopenharmony_ci	struct drm_rect clip;
6228c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
6258c2ecf20Sopenharmony_ci		.width = 2048,
6268c2ecf20Sopenharmony_ci		.height = 2048
6278c2ecf20Sopenharmony_ci	};
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
6308c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
6318c2ecf20Sopenharmony_ci		.fb = &fb,
6328c2ecf20Sopenharmony_ci		.visible = true,
6338c2ecf20Sopenharmony_ci	};
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
6368c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
6378c2ecf20Sopenharmony_ci	/* 2 damage clips. */
6388c2ecf20Sopenharmony_ci	set_damage_clip(&damage[0], 20, 30, 200, 180);
6398c2ecf20Sopenharmony_ci	set_damage_clip(&damage[1], 240, 200, 280, 250);
6408c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage[0], sizeof(damage));
6418c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
6428c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
6438c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip) {
6448c2ecf20Sopenharmony_ci		if (num_hits == 0)
6458c2ecf20Sopenharmony_ci			FAIL_ON(!check_damage_clip(&state, &clip, 20, 30, 200, 180));
6468c2ecf20Sopenharmony_ci		if (num_hits == 1)
6478c2ecf20Sopenharmony_ci			FAIL_ON(!check_damage_clip(&state, &clip, 240, 200, 280, 250));
6488c2ecf20Sopenharmony_ci		num_hits++;
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	FAIL(num_hits != 2, "Should return damage when set.");
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	return 0;
6548c2ecf20Sopenharmony_ci}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ciint igt_damage_iter_damage_one_intersect(void *ignored)
6578c2ecf20Sopenharmony_ci{
6588c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
6598c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
6608c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
6618c2ecf20Sopenharmony_ci	struct drm_mode_rect damage[2];
6628c2ecf20Sopenharmony_ci	struct drm_rect clip;
6638c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
6668c2ecf20Sopenharmony_ci		.width = 2048,
6678c2ecf20Sopenharmony_ci		.height = 2048
6688c2ecf20Sopenharmony_ci	};
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
6718c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
6728c2ecf20Sopenharmony_ci		.fb = &fb,
6738c2ecf20Sopenharmony_ci		.visible = true,
6748c2ecf20Sopenharmony_ci	};
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
6778c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
6788c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x40002, 0x40002,
6798c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
6808c2ecf20Sopenharmony_ci	/* 2 damage clips, one intersect plane src. */
6818c2ecf20Sopenharmony_ci	set_damage_clip(&damage[0], 20, 30, 200, 180);
6828c2ecf20Sopenharmony_ci	set_damage_clip(&damage[1], 2, 2, 1360, 1360);
6838c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage[0], sizeof(damage));
6848c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
6858c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
6868c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip) {
6878c2ecf20Sopenharmony_ci		if (num_hits == 0)
6888c2ecf20Sopenharmony_ci			FAIL_ON(!check_damage_clip(&state, &clip, 20, 30, 200, 180));
6898c2ecf20Sopenharmony_ci		if (num_hits == 1)
6908c2ecf20Sopenharmony_ci			FAIL_ON(!check_damage_clip(&state, &clip, 4, 4, 1029, 773));
6918c2ecf20Sopenharmony_ci		num_hits++;
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	FAIL(num_hits != 2, "Should return damage when set.");
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	return 0;
6978c2ecf20Sopenharmony_ci}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ciint igt_damage_iter_damage_one_outside(void *ignored)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
7028c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
7038c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
7048c2ecf20Sopenharmony_ci	struct drm_mode_rect damage[2];
7058c2ecf20Sopenharmony_ci	struct drm_rect clip;
7068c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
7098c2ecf20Sopenharmony_ci		.width = 2048,
7108c2ecf20Sopenharmony_ci		.height = 2048
7118c2ecf20Sopenharmony_ci	};
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
7148c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
7158c2ecf20Sopenharmony_ci		.fb = &fb,
7168c2ecf20Sopenharmony_ci		.visible = true,
7178c2ecf20Sopenharmony_ci	};
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0, 0, 1024 << 16, 768 << 16);
7208c2ecf20Sopenharmony_ci	set_plane_src(&state, 0, 0, 1024 << 16, 768 << 16);
7218c2ecf20Sopenharmony_ci	/* 2 damage clips, one outside plane src. */
7228c2ecf20Sopenharmony_ci	set_damage_clip(&damage[0], 1360, 1360, 1380, 1380);
7238c2ecf20Sopenharmony_ci	set_damage_clip(&damage[1], 240, 200, 280, 250);
7248c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage[0], sizeof(damage));
7258c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
7268c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
7278c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
7288c2ecf20Sopenharmony_ci		num_hits++;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return damage when set.");
7318c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 240, 200, 280, 250));
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	return 0;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ciint igt_damage_iter_damage_src_moved(void *ignored)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
7398c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
7408c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
7418c2ecf20Sopenharmony_ci	struct drm_mode_rect damage[2];
7428c2ecf20Sopenharmony_ci	struct drm_rect clip;
7438c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
7468c2ecf20Sopenharmony_ci		.width = 2048,
7478c2ecf20Sopenharmony_ci		.height = 2048
7488c2ecf20Sopenharmony_ci	};
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
7518c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
7528c2ecf20Sopenharmony_ci		.fb = &fb,
7538c2ecf20Sopenharmony_ci		.visible = true,
7548c2ecf20Sopenharmony_ci	};
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
7578c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
7588c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x3fffe, 0x3fffe,
7598c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
7608c2ecf20Sopenharmony_ci	/* 2 damage clips, one outside plane src. */
7618c2ecf20Sopenharmony_ci	set_damage_clip(&damage[0], 1360, 1360, 1380, 1380);
7628c2ecf20Sopenharmony_ci	set_damage_clip(&damage[1], 240, 200, 280, 250);
7638c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage[0], sizeof(damage));
7648c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
7658c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
7668c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
7678c2ecf20Sopenharmony_ci		num_hits++;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	FAIL(num_hits != 1, "Should return round off plane src as damage.");
7708c2ecf20Sopenharmony_ci	FAIL_ON(!check_damage_clip(&state, &clip, 3, 3, 1028, 772));
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	return 0;
7738c2ecf20Sopenharmony_ci}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ciint igt_damage_iter_damage_not_visible(void *ignored)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	struct drm_atomic_helper_damage_iter iter;
7788c2ecf20Sopenharmony_ci	struct drm_plane_state old_state;
7798c2ecf20Sopenharmony_ci	struct drm_property_blob damage_blob;
7808c2ecf20Sopenharmony_ci	struct drm_mode_rect damage[2];
7818c2ecf20Sopenharmony_ci	struct drm_rect clip;
7828c2ecf20Sopenharmony_ci	uint32_t num_hits = 0;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	struct drm_framebuffer fb = {
7858c2ecf20Sopenharmony_ci		.width = 2048,
7868c2ecf20Sopenharmony_ci		.height = 2048
7878c2ecf20Sopenharmony_ci	};
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	struct drm_plane_state state = {
7908c2ecf20Sopenharmony_ci		.crtc = ZERO_SIZE_PTR,
7918c2ecf20Sopenharmony_ci		.fb = &fb,
7928c2ecf20Sopenharmony_ci		.visible = false,
7938c2ecf20Sopenharmony_ci	};
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	set_plane_src(&old_state, 0x40002, 0x40002,
7968c2ecf20Sopenharmony_ci		      0x40002 + (1024 << 16), 0x40002 + (768 << 16));
7978c2ecf20Sopenharmony_ci	set_plane_src(&state, 0x3fffe, 0x3fffe,
7988c2ecf20Sopenharmony_ci		      0x3fffe + (1024 << 16), 0x3fffe + (768 << 16));
7998c2ecf20Sopenharmony_ci	/* 2 damage clips, one outside plane src. */
8008c2ecf20Sopenharmony_ci	set_damage_clip(&damage[0], 1360, 1360, 1380, 1380);
8018c2ecf20Sopenharmony_ci	set_damage_clip(&damage[1], 240, 200, 280, 250);
8028c2ecf20Sopenharmony_ci	set_damage_blob(&damage_blob, &damage[0], sizeof(damage));
8038c2ecf20Sopenharmony_ci	set_plane_damage(&state, &damage_blob);
8048c2ecf20Sopenharmony_ci	drm_atomic_helper_damage_iter_init(&iter, &old_state, &state);
8058c2ecf20Sopenharmony_ci	drm_atomic_for_each_plane_damage(&iter, &clip)
8068c2ecf20Sopenharmony_ci		num_hits++;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	FAIL(num_hits != 0, "Should not return any damage.");
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	return 0;
8118c2ecf20Sopenharmony_ci}
812