<div dir="ltr"><div>Hi Pekka,<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Feb 10, 2022 at 6:50 AM Pekka Paalanen <<a href="mailto:ppaalanen@gmail.com">ppaalanen@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Fri, 21 Jan 2022 18:38:31 -0300<br>
Igor Torrente <<a href="mailto:igormtorrente@gmail.com" target="_blank">igormtorrente@gmail.com</a>> wrote:<br>
<br>
> Adds this common format to vkms.<br>
> <br>
> This commit also adds new helper macros to deal with fixed-point<br>
> arithmetic.<br>
> <br>
> It was done to improve the precision of the conversion to ARGB16161616<br>
> since the "conversion ratio" is not an integer.<br>
> <br>
> Signed-off-by: Igor Torrente <<a href="mailto:igormtorrente@gmail.com" target="_blank">igormtorrente@gmail.com</a>><br>
> ---<br>
> V3: Adapt the handlers to the new format introduced in patch 7 V3.<br>
> ---<br>
> drivers/gpu/drm/vkms/vkms_formats.c | 74 +++++++++++++++++++++++++++<br>
> drivers/gpu/drm/vkms/vkms_formats.h | 6 +++<br>
> drivers/gpu/drm/vkms/vkms_plane.c | 6 ++-<br>
> drivers/gpu/drm/vkms/vkms_writeback.c | 3 +-<br>
> 4 files changed, 86 insertions(+), 3 deletions(-)<br>
> <br>
> diff --git a/drivers/gpu/drm/vkms/vkms_formats.c b/drivers/gpu/drm/vkms/vkms_formats.c<br>
> index 661da39d1276..dc612882dd8c 100644<br>
> --- a/drivers/gpu/drm/vkms/vkms_formats.c<br>
> +++ b/drivers/gpu/drm/vkms/vkms_formats.c<br>
> @@ -11,6 +11,8 @@ format_transform_func get_fmt_transform_function(u32 format)<br>
> return &get_ARGB16161616;<br>
> else if (format == DRM_FORMAT_XRGB16161616)<br>
> return &XRGB16161616_to_ARGB16161616;<br>
> + else if (format == DRM_FORMAT_RGB565)<br>
> + return &RGB565_to_ARGB16161616;<br>
> else<br>
> return &XRGB8888_to_ARGB16161616;<br>
> }<br>
> @@ -23,6 +25,8 @@ format_transform_func get_wb_fmt_transform_function(u32 format)<br>
> return &convert_to_ARGB16161616;<br>
> else if (format == DRM_FORMAT_XRGB16161616)<br>
> return &convert_to_XRGB16161616;<br>
> + else if (format == DRM_FORMAT_RGB565)<br>
> + return &convert_to_RGB565;<br>
> else<br>
> return &convert_to_XRGB8888;<br>
> }<br>
> @@ -33,6 +37,26 @@ static int pixel_offset(struct vkms_frame_info *frame_info, int x, int y)<br>
> + (x * frame_info->cpp);<br>
> }<br>
> <br>
> +/*<br>
> + * FP stands for _Fixed Point_ and **not** _Float Point_<br>
<br>
Is it common in the kernel that FP always means fixed-point?<br></blockquote><div><br></div><div>I cannot say for sure, but I don't think so. I put it for people like me</div><div>that goes automatically to Floating-Point because never worked with <br></div><div>fixed-point before.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
If there is any doubt about that, I'd suggest using "fixed" and "float"<br>
to avoid misunderstandings.<br>
<br>
And, since you are not supposed to use floats in the kernel unless you<br>
really really must and you do all the preparations necessary (which you<br>
don't here), maybe replace the "float" with a fraction.<br>
<br>
In other words, write a macro that takes (65535, 31) as arguments<br>
instead of a float, when converting to fixed-point. Then you don't have<br>
to use those strange decimal constants either.<br></blockquote><div><br></div><div>It looks better, I will try to implement this. <br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> + * LF stands for Long Float (i.e. double)<br>
> + * The following macros help doing fixed point arithmetic.<br>
> + */<br>
> +/*<br>
> + * With FP scale 15 we have 17 and 15 bits of integer and fractional parts<br>
> + * respectively.<br>
> + * | 0000 0000 0000 0000 0.000 0000 0000 0000 |<br>
> + * 31 0<br>
> + */<br>
> +#define FP_SCALE 15<br>
> +<br>
> +#define LF_TO_FP(a) ((a) * (u64)(1 << FP_SCALE))<br>
> +#define INT_TO_FP(a) ((a) << FP_SCALE)<br>
> +#define FP_MUL(a, b) ((s32)(((s64)(a) * (b)) >> FP_SCALE))<br>
> +#define FP_DIV(a, b) ((s32)(((s64)(a) << FP_SCALE) / (b)))<br>
> +/* This macro converts a fixed point number to int, and round half up it */<br>
> +#define FP_TO_INT_ROUND_UP(a) (((a) + (1 << (FP_SCALE - 1))) >> FP_SCALE)<br>
> +<br>
> /*<br>
> * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates<br>
> *<br>
> @@ -125,6 +149,33 @@ void XRGB16161616_to_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> }<br>
> }<br>
> <br>
> +void RGB565_to_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> + struct line_buffer *stage_buffer)<br>
> +{<br>
> + u16 *src_pixels = get_packed_src_addr(frame_info, y);<br>
> + int x, x_limit = drm_rect_width(&frame_info->dst);<br>
> +<br>
> + for (x = 0; x < x_limit; x++, src_pixels++) {<br>
> + u16 rgb_565 = le16_to_cpu(*src_pixels);<br>
> + int fp_r = INT_TO_FP((rgb_565 >> 11) & 0x1f);<br>
> + int fp_g = INT_TO_FP((rgb_565 >> 5) & 0x3f);<br>
> + int fp_b = INT_TO_FP(rgb_565 & 0x1f);<br>
> +<br>
> + /*<br>
> + * The magic constants is the "conversion ratio" and is calculated<br>
> + * dividing 65535(2^16 - 1) by 31(2^5 -1) and 63(2^6 - 1)<br>
> + * respectively.<br>
> + */<br>
> + int fp_rb_ratio = LF_TO_FP(2114.032258065);<br>
> + int fp_g_ratio = LF_TO_FP(1040.238095238);<br>
> +<br>
> + stage_buffer[x].a = (u16)0xffff;<br>
> + stage_buffer[x].r = FP_TO_INT_ROUND_UP(FP_MUL(fp_r, fp_rb_ratio));<br>
> + stage_buffer[x].g = FP_TO_INT_ROUND_UP(FP_MUL(fp_g, fp_g_ratio));<br>
> + stage_buffer[x].b = FP_TO_INT_ROUND_UP(FP_MUL(fp_b, fp_rb_ratio));<br>
> + }<br>
> +}<br>
> +<br>
> <br>
> /*<br>
> * The following functions take an line of ARGB16161616 pixels from the<br>
> @@ -203,3 +254,26 @@ void convert_to_XRGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> dst_pixels[0] = src_buffer[x].b;<br>
> }<br>
> }<br>
> +<br>
> +void convert_to_RGB565(struct vkms_frame_info *frame_info, int y,<br>
> + struct line_buffer *src_buffer)<br>
> +{<br>
> + int x, x_dst = frame_info->dst.x1;<br>
> + u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);<br>
> + int x_limit = drm_rect_width(&frame_info->dst);<br>
> +<br>
> + for (x = 0; x < x_limit; x++, dst_pixels++) {<br>
> + int fp_r = INT_TO_FP(src_buffer[x].r);<br>
> + int fp_g = INT_TO_FP(src_buffer[x].g);<br>
> + int fp_b = INT_TO_FP(src_buffer[x].b);<br>
> +<br>
> + int fp_rb_ratio = LF_TO_FP(2114.032258065);<br>
> + int fp_g_ratio = LF_TO_FP(1040.238095238);<br>
<br>
Are there any guarantees that this will not result in floating-point<br>
CPU instructions being used? Like a compiler error if it did?<br>
<br>
Yes, it's a constant expression, but I think there were some funny<br>
rules in C that floating-point operations may not be evaluated at<br>
compile time. Maybe I'm just paranoid?<br>
<br></blockquote><div> </div><div>Well, I cannot guarantee anything, but every time that I intentionally/unintentionally</div><div>did anything related with floating-point it couldn't link the kernel. <br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Thanks,<br>
pq<br>
<br>
> +<br>
> + u16 r = FP_TO_INT_ROUND_UP(FP_DIV(fp_r, fp_rb_ratio));<br>
> + u16 g = FP_TO_INT_ROUND_UP(FP_DIV(fp_g, fp_g_ratio));<br>
> + u16 b = FP_TO_INT_ROUND_UP(FP_DIV(fp_b, fp_rb_ratio));<br>
> +<br>
> + *dst_pixels = cpu_to_le16(r << 11 | g << 5 | b);<br>
> + }<br>
> +}<br>
> diff --git a/drivers/gpu/drm/vkms/vkms_formats.h b/drivers/gpu/drm/vkms/vkms_formats.h<br>
> index 22358f3a33ab..836d6e43ea90 100644<br>
> --- a/drivers/gpu/drm/vkms/vkms_formats.h<br>
> +++ b/drivers/gpu/drm/vkms/vkms_formats.h<br>
> @@ -21,6 +21,9 @@ void get_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> void XRGB16161616_to_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> struct line_buffer *stage_buffer);<br>
> <br>
> +void RGB565_to_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> + struct line_buffer *stage_buffer);<br>
> +<br>
> void convert_to_ARGB8888(struct vkms_frame_info *frame_info, int y,<br>
> struct line_buffer *src_buffer);<br>
> <br>
> @@ -33,6 +36,9 @@ void convert_to_ARGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> void convert_to_XRGB16161616(struct vkms_frame_info *frame_info, int y,<br>
> struct line_buffer *src_buffer);<br>
> <br>
> +void convert_to_RGB565(struct vkms_frame_info *frame_info, int y,<br>
> + struct line_buffer *src_buffer);<br>
> +<br>
> typedef void (*format_transform_func)(struct vkms_frame_info *frame_info, int y,<br>
> struct line_buffer *buffer);<br>
> <br>
> diff --git a/drivers/gpu/drm/vkms/vkms_plane.c b/drivers/gpu/drm/vkms/vkms_plane.c<br>
> index 1d70c9e8f109..4643eefcdf29 100644<br>
> --- a/drivers/gpu/drm/vkms/vkms_plane.c<br>
> +++ b/drivers/gpu/drm/vkms/vkms_plane.c<br>
> @@ -13,14 +13,16 @@<br>
> <br>
> static const u32 vkms_formats[] = {<br>
> DRM_FORMAT_XRGB8888,<br>
> - DRM_FORMAT_XRGB16161616<br>
> + DRM_FORMAT_XRGB16161616,<br>
> + DRM_FORMAT_RGB565<br>
> };<br>
> <br>
> static const u32 vkms_plane_formats[] = {<br>
> DRM_FORMAT_ARGB8888,<br>
> DRM_FORMAT_XRGB8888,<br>
> DRM_FORMAT_XRGB16161616,<br>
> - DRM_FORMAT_ARGB16161616<br>
> + DRM_FORMAT_ARGB16161616,<br>
> + DRM_FORMAT_RGB565<br>
> };<br>
> <br>
> static struct drm_plane_state *<br>
> diff --git a/drivers/gpu/drm/vkms/vkms_writeback.c b/drivers/gpu/drm/vkms/vkms_writeback.c<br>
> index 393d3fc7966f..1aaa630090d3 100644<br>
> --- a/drivers/gpu/drm/vkms/vkms_writeback.c<br>
> +++ b/drivers/gpu/drm/vkms/vkms_writeback.c<br>
> @@ -15,7 +15,8 @@<br>
> static const u32 vkms_wb_formats[] = {<br>
> DRM_FORMAT_XRGB8888,<br>
> DRM_FORMAT_XRGB16161616,<br>
> - DRM_FORMAT_ARGB16161616<br>
> + DRM_FORMAT_ARGB16161616,<br>
> + DRM_FORMAT_RGB565<br>
> };<br>
> <br>
> static const struct drm_connector_funcs vkms_wb_connector_funcs = {<br>
<br>
</blockquote></div></div>