Skip to content

Commit 7a48da0

Browse files
committed
drm/tests: Add helper to create mock plane
We're going to need a full-blown, functional, KMS device to test more components of the atomic modesetting infrastructure. Let's add a new helper to create a dumb, mocked, primary plane. By default, it will create a linear XRGB8888 plane, using the default helpers. Reviewed-by: Maíra Canal <mcanal@igalia.com> Signed-off-by: Maxime Ripard <mripard@kernel.org> Link: https://patchwork.freedesktop.org/patch/msgid/20240222-kms-hdmi-connector-state-v7-3-8f4af575fce2@kernel.org
1 parent 6667194 commit 7a48da0

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

drivers/gpu/drm/tests/drm_kunit_helpers.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <drm/drm_atomic.h>
44
#include <drm/drm_atomic_helper.h>
55
#include <drm/drm_drv.h>
6+
#include <drm/drm_fourcc.h>
67
#include <drm/drm_kunit_helpers.h>
78
#include <drm/drm_managed.h>
89

@@ -164,5 +165,89 @@ drm_kunit_helper_atomic_state_alloc(struct kunit *test,
164165
}
165166
EXPORT_SYMBOL_GPL(drm_kunit_helper_atomic_state_alloc);
166167

168+
static const uint32_t default_plane_formats[] = {
169+
DRM_FORMAT_XRGB8888,
170+
};
171+
172+
static const uint64_t default_plane_modifiers[] = {
173+
DRM_FORMAT_MOD_LINEAR,
174+
DRM_FORMAT_MOD_INVALID
175+
};
176+
177+
static const struct drm_plane_helper_funcs default_plane_helper_funcs = {
178+
};
179+
180+
static const struct drm_plane_funcs default_plane_funcs = {
181+
.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
182+
.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
183+
.reset = drm_atomic_helper_plane_reset,
184+
};
185+
186+
/**
187+
* drm_kunit_helper_create_primary_plane - Creates a mock primary plane for a KUnit test
188+
* @test: The test context object
189+
* @drm: The device to alloc the plane for
190+
* @funcs: Callbacks for the new plane. Optional.
191+
* @helper_funcs: Helpers callbacks for the new plane. Optional.
192+
* @formats: array of supported formats (DRM_FORMAT\_\*). Optional.
193+
* @num_formats: number of elements in @formats
194+
* @modifiers: array of struct drm_format modifiers terminated by
195+
* DRM_FORMAT_MOD_INVALID. Optional.
196+
*
197+
* This allocates and initializes a mock struct &drm_plane meant to be
198+
* part of a mock device for a KUnit test.
199+
*
200+
* Resources will be cleaned up automatically.
201+
*
202+
* @funcs will default to the default helpers implementations.
203+
* @helper_funcs will default to an empty implementation. @formats will
204+
* default to XRGB8888 only. @modifiers will default to a linear
205+
* modifier only.
206+
*
207+
* Returns:
208+
* A pointer to the new plane, or an ERR_PTR() otherwise.
209+
*/
210+
struct drm_plane *
211+
drm_kunit_helper_create_primary_plane(struct kunit *test,
212+
struct drm_device *drm,
213+
const struct drm_plane_funcs *funcs,
214+
const struct drm_plane_helper_funcs *helper_funcs,
215+
const uint32_t *formats,
216+
unsigned int num_formats,
217+
const uint64_t *modifiers)
218+
{
219+
struct drm_plane *plane;
220+
221+
if (!funcs)
222+
funcs = &default_plane_funcs;
223+
224+
if (!helper_funcs)
225+
helper_funcs = &default_plane_helper_funcs;
226+
227+
if (!formats || !num_formats) {
228+
formats = default_plane_formats;
229+
num_formats = ARRAY_SIZE(default_plane_formats);
230+
}
231+
232+
if (!modifiers)
233+
modifiers = default_plane_modifiers;
234+
235+
plane = __drmm_universal_plane_alloc(drm,
236+
sizeof(struct drm_plane), 0,
237+
0,
238+
funcs,
239+
formats,
240+
num_formats,
241+
default_plane_modifiers,
242+
DRM_PLANE_TYPE_PRIMARY,
243+
NULL);
244+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, plane);
245+
246+
drm_plane_helper_add(plane, helper_funcs);
247+
248+
return plane;
249+
}
250+
EXPORT_SYMBOL_GPL(drm_kunit_helper_create_primary_plane);
251+
167252
MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
168253
MODULE_LICENSE("GPL");

include/drm/drm_kunit_helpers.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <kunit/test.h>
1111

1212
struct drm_device;
13+
struct drm_plane_funcs;
14+
struct drm_plane_helper_funcs;
1315
struct kunit;
1416

1517
struct device *drm_kunit_helper_alloc_device(struct kunit *test);
@@ -99,4 +101,13 @@ drm_kunit_helper_atomic_state_alloc(struct kunit *test,
99101
struct drm_device *drm,
100102
struct drm_modeset_acquire_ctx *ctx);
101103

104+
struct drm_plane *
105+
drm_kunit_helper_create_primary_plane(struct kunit *test,
106+
struct drm_device *drm,
107+
const struct drm_plane_funcs *funcs,
108+
const struct drm_plane_helper_funcs *helper_funcs,
109+
const uint32_t *formats,
110+
unsigned int num_formats,
111+
const uint64_t *modifiers);
112+
102113
#endif // DRM_KUNIT_HELPERS_H_

0 commit comments

Comments
 (0)