drm/vkms: Add support for YUV and DRM_FORMAT_R*
Louis Chauvet
maintainerv15 cover drm/vkms: Add support for YUV and DRM_FORMAT_R*
This patchset is extracted from [1]. The goal is to introduce the YUV support, thanks to Arthur's work.
- PATCH 1: Add the support of YUV formats
- PATCH 2: Add some drm properties to expose more YUV features
- PATCH 3: Cleanup the todo
- PATCH 4..6: Add some kunit tests
- PATCH 7: Add the support of DRM_FORMAT_R1/2/4/8
[1]: https://lore.kernel.org/r/[email protected]
---
Changes in v15:
- Export drm_get_color_encoding_name only for kunit tests
- Link to v14: https://lore.kernel.org/r/[email protected]
Changes in v14:
- Rebased on drm-misc-next
- Link to v13: https://lore.kernel.org/r/[email protected]
Changes since previous series:
- Fix build test as modules issue: https://lore.kernel.org/all/[email protected]/
- Export required symbols in DRM core to use them in kunit
- Update the kunit comments according to Maxime's feedback
- Link to original series: https://lore.kernel.org/r/[email protected]
---
Arthur Grillo (5):
drm/vkms: Add YUV support
drm/vkms: Add range and encoding properties to the plane drm/vkms: Drop YUV formats TODO
drm/vkms: Create KUnit tests for YUV conversions drm/vkms: Add how to run the Kunit tests
Louis Chauvet (2):
drm: Export symbols to use in tests
drm/vkms: Add support for DRM_FORMAT_R*
Documentation/gpu/vkms.rst | 14 +- drivers/gpu/drm/drm_color_mgmt.c | 3 + drivers/gpu/drm/vkms/Kconfig | 15 + drivers/gpu/drm/vkms/Makefile | 1 + drivers/gpu/drm/vkms/tests/.kunitconfig | 4 + drivers/gpu/drm/vkms/tests/Makefile | 3 + drivers/gpu/drm/vkms/tests/vkms_format_test.c | 270 +++++++++++++++ drivers/gpu/drm/vkms/vkms_drv.h | 18 + drivers/gpu/drm/vkms/vkms_formats.c | 467 +++++++++++++++++++++++++- drivers/gpu/drm/vkms/vkms_formats.h | 9 + drivers/gpu/drm/vkms/vkms_plane.c | 29 +- 11 files changed, 829 insertions(+), 4 deletions(-)
---
base-commit: f8a2397baf041a5cee408b082334bb09c7e161df
change-id: 20240201-yuv-1337d90d9576
Best regards,
Signature · 1 line
Louis Chauvet <[email protected]>
Louis Chauvet
maintainerPATCH v15 1/7 drm/vkms: Add YUV support
From: Arthur Grillo <[email protected]>
Add support to the YUV formats bellow:
- NV12/NV16/NV24
- NV21/NV61/NV42
- YUV420/YUV422/YUV444
- YVU420/YVU422/YVU444
The conversion from yuv to rgb is done with fixed-point arithmetic, using 32.32 fixed-point numbers and the drm_fixed helpers.
To do the conversion, a specific matrix must be used for each color range (DRM_COLOR_*_RANGE) and encoding (DRM_COLOR_*). This matrix is stored in the conversion_matrix struct, along with the specific y_offset needed. This matrix is queried only once, in vkms_plane_atomic_update and stored in a vkms_plane_state. Those conversion matrices of each encoding and range were obtained by rounding the values of the original conversion matrices multiplied by 2^32. This is done to avoid the use of floating point operations.
The same reading function is used for YUV and YVU formats. As the only difference between those two category of formats is the order of field, a simple swap in conversion matrix columns allows using the same function.
[Louis Chauvet:
- Adapted Arthur's work
- Implemented the read_line_t callbacks for yuv
- add struct conversion_matrix
- store the whole conversion_matrix in the plane state
- remove struct pixel_yuv_u8
- update the commit message
- Merge the modifications from Arthur]
--- drivers/gpu/drm/vkms/vkms_drv.h | 18 ++ drivers/gpu/drm/vkms/vkms_formats.c | 354 ++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/vkms/vkms_formats.h | 4 + drivers/gpu/drm/vkms/vkms_plane.c | 16 +- 4 files changed, 391 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_drv.h +18 -0
@@ -100,17 +100,35 @@ typedef void (*pixel_read_line_t)(const struct vkms_plane_state *plane, int x_st int y_start, enum pixel_read_direction direction, int count, struct pixel_argb_u16 out_pixel[]); +/** +* struct conversion_matrix - Matrix to use for a specific encoding and range +* +* @matrix: Conversion matrix from yuv to rgb. The matrix is stored in a row-major manner and is +* used to compute rgb values from yuv values: +* [[r],[g],[b]] = @matrix * [[y],[u],[v]] +* OR for yvu formats: +* [[r],[g],[b]] = @matrix * [[y],[v],[u]] +* The values of the matrix are signed fixed-point values with 32 bits fractional part. +* @y_offset: Offset to apply on the y value. +*/ +struct conversion_matrix { +s64 matrix[3 ][3 ]; +int y_offset; +}; +/** * struct vkms_plane_state - Driver specific plane state * @base: base plane state * @frame_info: data required for composing computation * @pixel_read_line: function to read a pixel line in this plane. The creator of a * struct vkms_plane_state must ensure that this pointer is valid +* @conversion_matrix: matrix used for yuv formats to convert to rgb */ struct vkms_plane_state {struct drm_shadow_plane_state base;struct vkms_frame_info *frame_info;pixel_read_line_t pixel_read_line; +struct conversion_matrix conversion_matrix; };struct vkms_plane {
drivers/gpu/drm/vkms/vkms_formats.c +354 -0
384 lines not on this page · raw message
drivers/gpu/drm/vkms/vkms_formats.h +4 -0
@@ -9,4 +9,8 @@ pixel_read_line_t get_pixel_read_line_function(u32 format);pixel_write_t get_pixel_write_function (u32 format); +void get_conversion_matrix_to_argb_u16 (u32 format,enum drm_color_encoding encoding, +enum drm_color_range range, +struct conversion_matrix *matrix); +#endif /* _VKMS_FORMATS_H_ */
drivers/gpu/drm/vkms/vkms_plane.c +15 -1
@@ -17,7 +17,19 @@ static const u32 vkms_formats[] = {DRM_FORMAT_XRGB8888 ,DRM_FORMAT_XRGB16161616 ,DRM_FORMAT_ARGB16161616 , -DRM_FORMAT_RGB565 +DRM_FORMAT_RGB565 , +DRM_FORMAT_NV12 , +DRM_FORMAT_NV16 , +DRM_FORMAT_NV24 , +DRM_FORMAT_NV21 , +DRM_FORMAT_NV61 , +DRM_FORMAT_NV42 , +DRM_FORMAT_YUV420 , +DRM_FORMAT_YUV422 , +DRM_FORMAT_YUV444 , +DRM_FORMAT_YVU420 , +DRM_FORMAT_YVU422 , + DRM_FORMAT_YVU444, }; static struct drm_plane_state *
@@ -118,6 +130,8 @@ static void vkms_plane_atomic_update(struct drm_plane *plane, frame_info->rotation = new_state->rotation; vkms_plane_state->pixel_read_line =get_pixel_read_line_function (fmt); +get_conversion_matrix_to_argb_u16 (fmt, new_state->color_encoding, new_state->color_range, + &vkms_plane_state->conversion_matrix); } static int vkms_plane_atomic_check(struct drm_plane *plane,
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 2/7 drm/vkms: Add range and encoding properties to the plane
In reply to Louis ChauvetFrom: Arthur Grillo <[email protected]>
Now that the driver internally handles these quantization ranges and YUV encoding matrices, expose the UAPI for setting them.
--- drivers/gpu/drm/vkms/vkms_plane.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/gpu/drm/vkms/vkms_plane.c +9 -0
@@ -218,5 +218,14 @@ struct vkms_plane *vkms_plane_init(struct vkms_device *vkmsdev,drm_plane_create_rotation_property (&plane->base,DRM_MODE_ROTATE_0 ,DRM_MODE_ROTATE_MASK |DRM_MODE_REFLECT_MASK ); +drm_plane_create_color_properties (&plane->base, +BIT (DRM_COLOR_YCBCR_BT601 ) | +BIT (DRM_COLOR_YCBCR_BT709 ) | +BIT (DRM_COLOR_YCBCR_BT2020 ), +BIT (DRM_COLOR_YCBCR_LIMITED_RANGE ) | +BIT (DRM_COLOR_YCBCR_FULL_RANGE ), +DRM_COLOR_YCBCR_BT601 , +DRM_COLOR_YCBCR_FULL_RANGE ); +return plane; }
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 3/7 drm/vkms: Drop YUV formats TODO
In reply to Louis ChauvetFrom: Arthur Grillo <[email protected]>
VKMS has support for YUV formats now. Remove the task from the TODO list.
--- Documentation/gpu/vkms.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Documentation/gpu/vkms.rst +1 -2
@@ -122,8 +122,7 @@ There's lots of plane features we could add support for: - Scaling. -- Additional buffer formats, especially YUV formats for video like NV12. - Low/high bpp RGB formats would also be interesting. +- Additional buffer formats. Low/high bpp RGB formats would be interesting. - Async updates (currently only possible on cursor plane using the legacy cursor api).
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 4/7 drm: Export symbols to use in tests
In reply to Louis ChauvetThe functions drm_get_color_encoding_name and drm_get_color_range_name are useful for clarifying test results. Therefore, export them so they can be used in tests built as modules.
--- drivers/gpu/drm/drm_color_mgmt.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/gpu/drm/drm_color_mgmt.c +3 -0
@@ -28,6 +28,7 @@#include <drm/drm_device.h> #include <drm/drm_drv.h> #include <drm/drm_print.h> +#include <kunit/visibility.h> #include "drm_crtc_internal.h"
@@ -494,6 +495,7 @@ const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)return color_encoding_name[encoding]; } +EXPORT_SYMBOL_IF_KUNIT (drm_get_color_encoding_name);/** * drm_get_color_range_name - return a string for color range
@@ -509,6 +511,7 @@ const char *drm_get_color_range_name(enum drm_color_range range)return color_range_name[range]; } +EXPORT_SYMBOL_IF_KUNIT (drm_get_color_range_name);/** * drm_plane_create_color_properties - color encoding related plane properties
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 5/7 drm/vkms: Create KUnit tests for YUV conversions
In reply to Louis ChauvetFrom: Arthur Grillo <[email protected]>
Create KUnit tests to test the conversion between YUV and RGB. Test each conversion and range combination with some common colors.
The code used to compute the expected result can be found in comment.
[Louis Chauvet:
- fix minor formating issues (whitespace, double line)
- change expected alpha from 0x0000 to 0xffff
- adapt to the new get_conversion_matrix usage
- apply the changes from Arthur
- move struct pixel_yuv_u8 to the test itself]
--- drivers/gpu/drm/vkms/Kconfig | 15 ++ drivers/gpu/drm/vkms/Makefile | 1 + drivers/gpu/drm/vkms/tests/.kunitconfig | 4 + drivers/gpu/drm/vkms/tests/Makefile | 3 + drivers/gpu/drm/vkms/tests/vkms_format_test.c | 270 ++++++++++++++++++++++++++ drivers/gpu/drm/vkms/vkms_formats.c | 7 +- drivers/gpu/drm/vkms/vkms_formats.h | 5 + 7 files changed, 303 insertions(+), 2 deletions(-)
drivers/gpu/drm/vkms/Kconfig +15 -0
@@ -14,3 +14,18 @@ config DRM_VKMS a VKMS. If M is selected the module will be called vkms. + +config DRM_VKMS_KUNIT_TESTS + tristate "KUnit tests for VKMS." if !KUNIT_ALL_TESTS + depends on DRM_VKMS && KUNIT + default KUNIT_ALL_TESTS + help + This builds unit tests for VKMS. This option is not useful for + distributions or general kernels, but only for kernel + developers working on VKMS. + + For more information on KUnit and unit tests in general, + please refer to the KUnit documentation in + Documentation/dev-tools/kunit/. + + If in doubt, say "N".
drivers/gpu/drm/vkms/Makefile +1 -0
@@ -9,3 +9,4 @@ vkms-y := \ vkms_writeback.o obj-$(CONFIG_DRM_VKMS) += vkms.o +obj-$(CONFIG_DRM_VKMS_KUNIT_TESTS) += tests/
drivers/gpu/drm/vkms/tests/.kunitconfig new file +4 -0
@@ -0,0 +1,4 @@ +CONFIG_KUNIT=y +CONFIG_DRM=y +CONFIG_DRM_VKMS=y +CONFIG_DRM_VKMS_KUNIT_TESTS=y
drivers/gpu/drm/vkms/tests/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only + +obj-$(CONFIG_DRM_VKMS_KUNIT_TESTS) += vkms_format_test.o
drivers/gpu/drm/vkms/tests/vkms_format_test.c new file +270 -0
270 lines not on this page · raw message
drivers/gpu/drm/vkms/vkms_formats.c +5 -2
@@ -7,6 +7,8 @@#include <drm/drm_rect.h> #include <drm/drm_fixed.h> +#include <kunit/visibility.h> +#include "vkms_formats.h" /**
@@ -247,8 +249,8 @@ static struct pixel_argb_u16 argb_u16_from_RGB565(const __le16 *pixel)return out_pixel; } -static struct pixel_argb_u16 argb_u16_from_yuv888 (u8 y,u8 channel_1,u8 channel_2, -const struct conversion_matrix *matrix) +VISIBLE_IF_KUNIT struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1, u8 channel_2, +const struct conversion_matrix *matrix) {u16 r, g, b;s64 fp_y, fp_channel_1, fp_channel_2;
@@ -278,6 +280,7 @@ static struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1, u8 channelreturn argb_u16_from_u16161616 (0xffff , r, g, b); } +EXPORT_SYMBOL_IF_KUNIT (argb_u16_from_yuv888);/* * The following functions are read_line function for each pixel format supported by VKMS.
drivers/gpu/drm/vkms/vkms_formats.h +5 -0
@@ -13,4 +13,9 @@ void get_conversion_matrix_to_argb_u16(u32 format, enum drm_color_encoding encodenum drm_color_range range, struct conversion_matrix *matrix); +#if IS_ENABLED (CONFIG_KUNIT ) +struct pixel_argb_u16 argb_u16_from_yuv888 (u8 y,u8 channel_1,u8 channel_2, +const struct conversion_matrix *matrix); +#endif +#endif /* _VKMS_FORMATS_H_ */
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 6/7 drm/vkms: Add how to run the Kunit tests
In reply to Louis ChauvetFrom: Arthur Grillo <[email protected]>
Now that we have KUnit tests, add instructions on how to run them.
--- Documentation/gpu/vkms.rst | 11 +++++++++++ 1 file changed, 11 insertions(+)
Documentation/gpu/vkms.rst +11 -0
@@ -89,6 +89,17 @@ You can also run subtests if you do not want to run the entire test:: sudo ./build/tests/kms_flip --run-subtest basic-plain-flip --device "sys:/sys/devices/platform/vkms" sudo IGT_DEVICE="sys:/sys/devices/platform/vkms" ./build/tests/kms_flip --run-subtest basic-plain-flip +Testing With KUnit +================== + +KUnit (Kernel unit testing framework) provides a common framework for unit tests +within the Linux kernel. +More information in ../dev-tools/kunit/index.rst . + +To run the VKMS KUnit tests:: + + tools/testing/kunit/kunit.py run --kunitconfig=drivers/gpu/drm/vkms/tests + TODO ====
Signature · 1 line
2.47.1
Louis Chauvet
maintainerPATCH v15 7/7 drm/vkms: Add support for DRM_FORMAT_R*
In reply to Louis ChauvetThis add the support for:
- R1/R2/R4/R8
R1 format was tested with [1] and [2].
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/igt-dev/[email protected]/
--- drivers/gpu/drm/vkms/vkms_formats.c | 110 +++++++++++++++++++++++++++++++++++- drivers/gpu/drm/vkms/vkms_plane.c | 4 ++ 2 files changed, 113 insertions(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_formats.c +109 -1
@@ -249,6 +249,16 @@ static struct pixel_argb_u16 argb_u16_from_RGB565(const __le16 *pixel)return out_pixel; } +static struct pixel_argb_u16 argb_u16_from_gray8 (u8 gray) +{ +return argb_u16_from_u8888 (255 , gray, gray, gray); +} + +static struct pixel_argb_u16 argb_u16_from_grayu16 (u16 gray) +{ +return argb_u16_from_u16161616 (0xFFFF , gray, gray, gray); +} + VISIBLE_IF_KUNIT struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1, u8 channel_2,const struct conversion_matrix *matrix) {
@@ -286,7 +296,7 @@ EXPORT_SYMBOL_IF_KUNIT(argb_u16_from_yuv888);* The following functions are read_line function for each pixel format supported by VKMS. * * They read a line starting at the point @x_start,@y_start following the @direction. The result -* is stored in @out_pixel and in the format ARGB16161616. +* is stored in @out_pixel and in a 64 bits format, see struct pixel_argb_u16. * * These functions are very repetitive, but the innermost pixel loops must be kept inside these * functions for performance reasons. Some benchmarking was done in [1] where having the innermost
@@ -295,6 +305,96 @@ EXPORT_SYMBOL_IF_KUNIT(argb_u16_from_yuv888);* [1]: https://lore.kernel.org/dri-devel/[email protected]/ */ +static void Rx_read_line (const struct vkms_plane_state *plane,int x_start, +int y_start,enum pixel_read_direction direction,int count, +struct pixel_argb_u16 out_pixel[]) +{ +struct pixel_argb_u16 *end = out_pixel + count; +int bits_per_pixel =drm_format_info_bpp (plane->frame_info->fb->format,0 ); +u8 *src_pixels; +int rem_x, rem_y; + +WARN_ONCE (drm_format_info_block_height (plane->frame_info->fb->format,0 ) !=1 , +"%s() only support formats with block_h == 1" , __func__); + +packed_pixels_addr (plane->frame_info, x_start, y_start,0 , &src_pixels, &rem_x, &rem_y); +int bit_offset = (8 - bits_per_pixel) - rem_x * bits_per_pixel; +int step =get_block_step_bytes (plane->frame_info->fb, direction,0 ); +int mask = (0x1 << bits_per_pixel) -1 ; +int lum_per_level =0xFFFF / mask; + +if (direction ==READ_LEFT_TO_RIGHT || direction ==READ_RIGHT_TO_LEFT ) { +int restart_bit_offset; +int step_bit_offset; + +if (direction ==READ_LEFT_TO_RIGHT ) { + restart_bit_offset =8 - bits_per_pixel; + step_bit_offset = -bits_per_pixel; + }else { + restart_bit_offset =0 ; + step_bit_offset = bits_per_pixel; + } + +while (out_pixel < end) { +u8 val = ((*src_pixels) >> bit_offset) & mask; + + *out_pixel =argb_u16_from_grayu16 ((int )val * lum_per_level); + + bit_offset += step_bit_offset; +if (bit_offset <0 ||8 <= bit_offset) { + bit_offset = restart_bit_offset; + src_pixels += step; + } + out_pixel +=1 ; + } + }else if (direction ==READ_TOP_TO_BOTTOM || direction ==READ_BOTTOM_TO_TOP ) { +while (out_pixel < end) { +u8 val = (*src_pixels >> bit_offset) & mask; + *out_pixel =argb_u16_from_grayu16 ((int )val * lum_per_level); + src_pixels += step; + out_pixel +=1 ; + } + } +} + +static void R1_read_line (const struct vkms_plane_state *plane,int x_start, +int y_start,enum pixel_read_direction direction,int count, +struct pixel_argb_u16 out_pixel[]) +{ +Rx_read_line (plane, x_start, y_start, direction, count, out_pixel); +} + +static void R2_read_line (const struct vkms_plane_state *plane,int x_start, +int y_start,enum pixel_read_direction direction,int count, +struct pixel_argb_u16 out_pixel[]) +{ +Rx_read_line (plane, x_start, y_start, direction, count, out_pixel); +} + +static void R4_read_line (const struct vkms_plane_state *plane,int x_start, +int y_start,enum pixel_read_direction direction,int count, +struct pixel_argb_u16 out_pixel[]) +{ +Rx_read_line (plane, x_start, y_start, direction, count, out_pixel); +} + +static void R8_read_line (const struct vkms_plane_state *plane,int x_start, +int y_start,enum pixel_read_direction direction,int count, +struct pixel_argb_u16 out_pixel[]) +{ +struct pixel_argb_u16 *end = out_pixel + count; +u8 *src_pixels; +int step =get_block_step_bytes (plane->frame_info->fb, direction,0 ); + +packed_pixels_addr_1x1 (plane->frame_info, x_start, y_start,0 , &src_pixels); + +while (out_pixel < end) { + *out_pixel =argb_u16_from_gray8 (*src_pixels); + src_pixels += step; + out_pixel +=1 ; + } +} +static void ARGB8888_read_line (const struct vkms_plane_state *plane,int x_start,int y_start,enum pixel_read_direction direction,int count,struct pixel_argb_u16 out_pixel[])
@@ -606,6 +706,14 @@ pixel_read_line_t get_pixel_read_line_function(u32 format)case DRM_FORMAT_YVU422 :case DRM_FORMAT_YVU444 :return &planar_yuv_read_line; +case DRM_FORMAT_R1 : +return &R1_read_line; +case DRM_FORMAT_R2 : +return &R2_read_line; +case DRM_FORMAT_R4 : +return &R4_read_line; +case DRM_FORMAT_R8 : +return &R8_read_line;default :/* * This is a bug in vkms_plane_atomic_check(). All the supported
drivers/gpu/drm/vkms/vkms_plane.c +4 -0
@@ -30,6 +30,10 @@ static const u32 vkms_formats[] = {DRM_FORMAT_YVU420 ,DRM_FORMAT_YVU422 ,DRM_FORMAT_YVU444 , +DRM_FORMAT_R1 , +DRM_FORMAT_R2 , +DRM_FORMAT_R4 , + DRM_FORMAT_R8, }; static struct drm_plane_state *
Signature · 1 line
2.47.1