Skip to content

Commit 544f161

Browse files
committed
drm/vkms: Create helpers macro to avoid code duplication in format callbacks
The callback functions for line conversion are almost identical for some format. The generic READ_LINE macro generate all the required boilerplate to process a line. Two overrides of this macro have been added to avoid duplication of the same arguments every time. Reviewed-by: Maíra Canal <mcanal@igalia.com> Acked-by: Daniel Stone <daniels@collabora.com> Link: https://lore.kernel.org/r/20250703-b4-new-color-formats-v7-1-15fd8fd2e15c@bootlin.com Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
1 parent 0d2902d commit 544f161

1 file changed

Lines changed: 65 additions & 127 deletions

File tree

drivers/gpu/drm/vkms/vkms_formats.c

Lines changed: 65 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,64 @@ VISIBLE_IF_KUNIT struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1,
292292
}
293293
EXPORT_SYMBOL_IF_KUNIT(argb_u16_from_yuv888);
294294

295+
/**
296+
* READ_LINE() - Generic generator for a read_line function which can be used for format with one
297+
* plane and a block_h == block_w == 1.
298+
*
299+
* @function_name: Function name to generate
300+
* @pixel_name: Temporary pixel name used in the @__VA_ARGS__ parameters
301+
* @pixel_type: Used to specify the type you want to cast the pixel pointer
302+
* @callback: Callback to call for each pixels. This fonction should take @__VA_ARGS__ as parameter
303+
* and return a pixel_argb_u16
304+
* __VA_ARGS__: Argument to pass inside the callback. You can use @pixel_name to access current
305+
* pixel.
306+
*/
307+
#define READ_LINE(function_name, pixel_name, pixel_type, callback, ...) \
308+
static void function_name(const struct vkms_plane_state *plane, int x_start, \
309+
int y_start, enum pixel_read_direction direction, int count, \
310+
struct pixel_argb_u16 out_pixel[]) \
311+
{ \
312+
struct pixel_argb_u16 *end = out_pixel + count; \
313+
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0); \
314+
u8 *src_pixels; \
315+
\
316+
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels); \
317+
\
318+
while (out_pixel < end) { \
319+
pixel_type *(pixel_name) = (pixel_type *)src_pixels; \
320+
*out_pixel = (callback)(__VA_ARGS__); \
321+
out_pixel += 1; \
322+
src_pixels += step; \
323+
} \
324+
}
325+
326+
/**
327+
* READ_LINE_ARGB8888() - Generic generator for ARGB8888 formats.
328+
* The pixel type used is u8, so pixel_name[0]..pixel_name[n] are the n components of the pixel.
329+
*
330+
* @function_name: Function name to generate
331+
* @pixel_name: temporary pixel to use in @a, @r, @g and @b parameters
332+
* @a: alpha value
333+
* @r: red value
334+
* @g: green value
335+
* @b: blue value
336+
*/
337+
#define READ_LINE_ARGB8888(function_name, pixel_name, a, r, g, b) \
338+
READ_LINE(function_name, pixel_name, u8, argb_u16_from_u8888, a, r, g, b)
339+
/**
340+
* READ_LINE_le16161616() - Generic generator for ARGB16161616 formats.
341+
* The pixel type used is u16, so pixel_name[0]..pixel_name[n] are the n components of the pixel.
342+
*
343+
* @function_name: Function name to generate
344+
* @pixel_name: temporary pixel to use in @a, @r, @g and @b parameters
345+
* @a: alpha value
346+
* @r: red value
347+
* @g: green value
348+
* @b: blue value
349+
*/
350+
#define READ_LINE_le16161616(function_name, pixel_name, a, r, g, b) \
351+
READ_LINE(function_name, pixel_name, __le16, argb_u16_from_le16161616, a, r, g, b)
352+
295353
/*
296354
* The following functions are read_line function for each pixel format supported by VKMS.
297355
*
@@ -378,138 +436,18 @@ static void R4_read_line(const struct vkms_plane_state *plane, int x_start,
378436
Rx_read_line(plane, x_start, y_start, direction, count, out_pixel);
379437
}
380438

381-
static void R8_read_line(const struct vkms_plane_state *plane, int x_start,
382-
int y_start, enum pixel_read_direction direction, int count,
383-
struct pixel_argb_u16 out_pixel[])
384-
{
385-
struct pixel_argb_u16 *end = out_pixel + count;
386-
u8 *src_pixels;
387-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
388439

389-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
440+
READ_LINE_ARGB8888(XRGB8888_read_line, px, 0xFF, px[2], px[1], px[0])
390441

391-
while (out_pixel < end) {
392-
*out_pixel = argb_u16_from_gray8(*src_pixels);
393-
src_pixels += step;
394-
out_pixel += 1;
395-
}
396-
}
442+
READ_LINE_ARGB8888(ARGB8888_read_line, px, px[3], px[2], px[1], px[0])
443+
READ_LINE_ARGB8888(ABGR8888_read_line, px, px[3], px[0], px[1], px[2])
397444

398-
static void ARGB8888_read_line(const struct vkms_plane_state *plane, int x_start, int y_start,
399-
enum pixel_read_direction direction, int count,
400-
struct pixel_argb_u16 out_pixel[])
401-
{
402-
struct pixel_argb_u16 *end = out_pixel + count;
403-
u8 *src_pixels;
404-
405-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
445+
READ_LINE_le16161616(ARGB16161616_read_line, px, px[3], px[2], px[1], px[0])
446+
READ_LINE_le16161616(XRGB16161616_read_line, px, cpu_to_le16(0xFFFF), px[2], px[1], px[0])
406447

407-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
448+
READ_LINE(RGB565_read_line, px, __le16, argb_u16_from_RGB565, px)
408449

409-
while (out_pixel < end) {
410-
u8 *px = (u8 *)src_pixels;
411-
*out_pixel = argb_u16_from_u8888(px[3], px[2], px[1], px[0]);
412-
out_pixel += 1;
413-
src_pixels += step;
414-
}
415-
}
416-
417-
static void XRGB8888_read_line(const struct vkms_plane_state *plane, int x_start, int y_start,
418-
enum pixel_read_direction direction, int count,
419-
struct pixel_argb_u16 out_pixel[])
420-
{
421-
struct pixel_argb_u16 *end = out_pixel + count;
422-
u8 *src_pixels;
423-
424-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
425-
426-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
427-
428-
while (out_pixel < end) {
429-
u8 *px = (u8 *)src_pixels;
430-
*out_pixel = argb_u16_from_u8888(255, px[2], px[1], px[0]);
431-
out_pixel += 1;
432-
src_pixels += step;
433-
}
434-
}
435-
436-
static void ABGR8888_read_line(const struct vkms_plane_state *plane, int x_start, int y_start,
437-
enum pixel_read_direction direction, int count,
438-
struct pixel_argb_u16 out_pixel[])
439-
{
440-
struct pixel_argb_u16 *end = out_pixel + count;
441-
u8 *src_pixels;
442-
443-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
444-
445-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
446-
447-
while (out_pixel < end) {
448-
u8 *px = (u8 *)src_pixels;
449-
/* Switch blue and red pixels. */
450-
*out_pixel = argb_u16_from_u8888(px[3], px[0], px[1], px[2]);
451-
out_pixel += 1;
452-
src_pixels += step;
453-
}
454-
}
455-
456-
static void ARGB16161616_read_line(const struct vkms_plane_state *plane, int x_start,
457-
int y_start, enum pixel_read_direction direction, int count,
458-
struct pixel_argb_u16 out_pixel[])
459-
{
460-
struct pixel_argb_u16 *end = out_pixel + count;
461-
u8 *src_pixels;
462-
463-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
464-
465-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
466-
467-
while (out_pixel < end) {
468-
u16 *px = (u16 *)src_pixels;
469-
*out_pixel = argb_u16_from_u16161616(px[3], px[2], px[1], px[0]);
470-
out_pixel += 1;
471-
src_pixels += step;
472-
}
473-
}
474-
475-
static void XRGB16161616_read_line(const struct vkms_plane_state *plane, int x_start,
476-
int y_start, enum pixel_read_direction direction, int count,
477-
struct pixel_argb_u16 out_pixel[])
478-
{
479-
struct pixel_argb_u16 *end = out_pixel + count;
480-
u8 *src_pixels;
481-
482-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
483-
484-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
485-
486-
while (out_pixel < end) {
487-
__le16 *px = (__le16 *)src_pixels;
488-
*out_pixel = argb_u16_from_le16161616(cpu_to_le16(0xFFFF), px[2], px[1], px[0]);
489-
out_pixel += 1;
490-
src_pixels += step;
491-
}
492-
}
493-
494-
static void RGB565_read_line(const struct vkms_plane_state *plane, int x_start,
495-
int y_start, enum pixel_read_direction direction, int count,
496-
struct pixel_argb_u16 out_pixel[])
497-
{
498-
struct pixel_argb_u16 *end = out_pixel + count;
499-
u8 *src_pixels;
500-
501-
packed_pixels_addr_1x1(plane->frame_info, x_start, y_start, 0, &src_pixels);
502-
503-
int step = get_block_step_bytes(plane->frame_info->fb, direction, 0);
504-
505-
while (out_pixel < end) {
506-
__le16 *px = (__le16 *)src_pixels;
507-
508-
*out_pixel = argb_u16_from_RGB565(px);
509-
out_pixel += 1;
510-
src_pixels += step;
511-
}
512-
}
450+
READ_LINE(R8_read_line, px, u8, argb_u16_from_gray8, *px)
513451

514452
/*
515453
* This callback can be used for YUV formats where U and V values are

0 commit comments

Comments
 (0)