1 // SPDX-License-Identifier: GPL-2.0
3 * Test cases for the drm_plane_helper functions
6 #define pr_fmt(fmt) "drm_plane_helper: " fmt
8 #include <drm/drm_atomic_helper.h>
9 #include <drm/drm_framebuffer.h>
10 #include <drm/drm_plane_helper.h>
11 #include <drm/drm_modes.h>
13 #include "test-drm_modeset_common.h"
15 static void set_src(struct drm_plane_state *plane_state,
16 unsigned src_x, unsigned src_y,
17 unsigned src_w, unsigned src_h)
19 plane_state->src_x = src_x;
20 plane_state->src_y = src_y;
21 plane_state->src_w = src_w;
22 plane_state->src_h = src_h;
25 static bool check_src_eq(struct drm_plane_state *plane_state,
26 unsigned src_x, unsigned src_y,
27 unsigned src_w, unsigned src_h)
29 if (plane_state->src.x1 < 0) {
30 pr_err("src x coordinate %x should never be below 0.\n", plane_state->src.x1);
31 drm_rect_debug_print("src: ", &plane_state->src, true);
34 if (plane_state->src.y1 < 0) {
35 pr_err("src y coordinate %x should never be below 0.\n", plane_state->src.y1);
36 drm_rect_debug_print("src: ", &plane_state->src, true);
40 if (plane_state->src.x1 != src_x ||
41 plane_state->src.y1 != src_y ||
42 drm_rect_width(&plane_state->src) != src_w ||
43 drm_rect_height(&plane_state->src) != src_h) {
44 drm_rect_debug_print("src: ", &plane_state->src, true);
51 static void set_crtc(struct drm_plane_state *plane_state,
52 int crtc_x, int crtc_y,
53 unsigned crtc_w, unsigned crtc_h)
55 plane_state->crtc_x = crtc_x;
56 plane_state->crtc_y = crtc_y;
57 plane_state->crtc_w = crtc_w;
58 plane_state->crtc_h = crtc_h;
61 static bool check_crtc_eq(struct drm_plane_state *plane_state,
62 int crtc_x, int crtc_y,
63 unsigned crtc_w, unsigned crtc_h)
65 if (plane_state->dst.x1 != crtc_x ||
66 plane_state->dst.y1 != crtc_y ||
67 drm_rect_width(&plane_state->dst) != crtc_w ||
68 drm_rect_height(&plane_state->dst) != crtc_h) {
69 drm_rect_debug_print("dst: ", &plane_state->dst, false);
77 int igt_check_plane_state(void *ignored)
81 static const struct drm_crtc_state crtc_state = {
82 .crtc = ZERO_SIZE_PTR,
86 DRM_MODE("1024x768", 0, 65000, 1024, 1048,
87 1184, 1344, 0, 768, 771, 777, 806, 0,
88 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
91 static struct drm_plane plane = {
94 static struct drm_framebuffer fb = {
98 static struct drm_plane_state plane_state = {
100 .crtc = ZERO_SIZE_PTR,
102 .rotation = DRM_MODE_ROTATE_0
105 /* Simple clipping, no scaling. */
106 set_src(&plane_state, 0, 0, fb.width << 16, fb.height << 16);
107 set_crtc(&plane_state, 0, 0, fb.width, fb.height);
108 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
109 DRM_PLANE_HELPER_NO_SCALING,
110 DRM_PLANE_HELPER_NO_SCALING,
112 FAIL(ret < 0, "Simple clipping check should pass\n");
113 FAIL_ON(!plane_state.visible);
114 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 1024 << 16, 768 << 16));
115 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));
117 /* Rotated clipping + reflection, no scaling. */
118 plane_state.rotation = DRM_MODE_ROTATE_90 | DRM_MODE_REFLECT_X;
119 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
120 DRM_PLANE_HELPER_NO_SCALING,
121 DRM_PLANE_HELPER_NO_SCALING,
123 FAIL(ret < 0, "Rotated clipping check should pass\n");
124 FAIL_ON(!plane_state.visible);
125 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 768 << 16, 1024 << 16));
126 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));
127 plane_state.rotation = DRM_MODE_ROTATE_0;
129 /* Check whether positioning works correctly. */
130 set_src(&plane_state, 0, 0, 1023 << 16, 767 << 16);
131 set_crtc(&plane_state, 0, 0, 1023, 767);
132 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
133 DRM_PLANE_HELPER_NO_SCALING,
134 DRM_PLANE_HELPER_NO_SCALING,
136 FAIL(!ret, "Should not be able to position on the crtc with can_position=false\n");
138 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
139 DRM_PLANE_HELPER_NO_SCALING,
140 DRM_PLANE_HELPER_NO_SCALING,
142 FAIL(ret < 0, "Simple positioning should work\n");
143 FAIL_ON(!plane_state.visible);
144 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 1023 << 16, 767 << 16));
145 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1023, 767));
147 /* Simple scaling tests. */
148 set_src(&plane_state, 0, 0, 512 << 16, 384 << 16);
149 set_crtc(&plane_state, 0, 0, 1024, 768);
150 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
152 DRM_PLANE_HELPER_NO_SCALING,
154 FAIL(!ret, "Upscaling out of range should fail.\n");
155 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
157 DRM_PLANE_HELPER_NO_SCALING,
159 FAIL(ret < 0, "Upscaling exactly 2x should work\n");
160 FAIL_ON(!plane_state.visible);
161 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 512 << 16, 384 << 16));
162 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));
164 set_src(&plane_state, 0, 0, 2048 << 16, 1536 << 16);
165 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
166 DRM_PLANE_HELPER_NO_SCALING,
167 0x1ffff, false, false);
168 FAIL(!ret, "Downscaling out of range should fail.\n");
169 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
170 DRM_PLANE_HELPER_NO_SCALING,
171 0x20000, false, false);
172 FAIL(ret < 0, "Should succeed with exact scaling limit\n");
173 FAIL_ON(!plane_state.visible);
174 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 2048 << 16, 1536 << 16));
175 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));
177 /* Testing rounding errors. */
178 set_src(&plane_state, 0, 0, 0x40001, 0x40001);
179 set_crtc(&plane_state, 1022, 766, 4, 4);
180 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
181 DRM_PLANE_HELPER_NO_SCALING,
184 FAIL(ret < 0, "Should succeed by clipping to exact multiple");
185 FAIL_ON(!plane_state.visible);
186 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 2 << 16, 2 << 16));
187 FAIL_ON(!check_crtc_eq(&plane_state, 1022, 766, 2, 2));
189 set_src(&plane_state, 0x20001, 0x20001, 0x4040001, 0x3040001);
190 set_crtc(&plane_state, -2, -2, 1028, 772);
191 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
192 DRM_PLANE_HELPER_NO_SCALING,
195 FAIL(ret < 0, "Should succeed by clipping to exact multiple");
196 FAIL_ON(!plane_state.visible);
197 FAIL_ON(!check_src_eq(&plane_state, 0x40002, 0x40002, 1024 << 16, 768 << 16));
198 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));
200 set_src(&plane_state, 0, 0, 0x3ffff, 0x3ffff);
201 set_crtc(&plane_state, 1022, 766, 4, 4);
202 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
204 DRM_PLANE_HELPER_NO_SCALING,
206 FAIL(ret < 0, "Should succeed by clipping to exact multiple");
207 FAIL_ON(!plane_state.visible);
208 /* Should not be rounded to 0x20001, which would be upscaling. */
209 FAIL_ON(!check_src_eq(&plane_state, 0, 0, 2 << 16, 2 << 16));
210 FAIL_ON(!check_crtc_eq(&plane_state, 1022, 766, 2, 2));
212 set_src(&plane_state, 0x1ffff, 0x1ffff, 0x403ffff, 0x303ffff);
213 set_crtc(&plane_state, -2, -2, 1028, 772);
214 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
216 DRM_PLANE_HELPER_NO_SCALING,
218 FAIL(ret < 0, "Should succeed by clipping to exact multiple");
219 FAIL_ON(!plane_state.visible);
220 FAIL_ON(!check_src_eq(&plane_state, 0x3fffe, 0x3fffe, 1024 << 16, 768 << 16));
221 FAIL_ON(!check_crtc_eq(&plane_state, 0, 0, 1024, 768));