diff mbox series

drm/vkms: add alpha-premultiplied color blending

Message ID 20200819205336.fce24lioz34vbcd2@smtp.gmail.com (mailing list archive)
State New, archived
Headers show
Series drm/vkms: add alpha-premultiplied color blending | expand

Commit Message

Melissa Wen Aug. 19, 2020, 8:53 p.m. UTC
The current VKMS blend function ignores alpha channel and just overwrites
vaddr_src with vaddr_dst. This XRGB approach triggers a warning when
running the kms_cursor_crc/cursor-alpha-transparent test case. In IGT
tests, cairo_format_argb32 uses premultiplied alpha (according to
documentation), so this patch considers premultiplied alpha colors to
compose vaddr_src with vaddr_dst.

This change removes the following cursor-alpha-transparent warning:
Suspicious CRC: All values are 0.

Cc: Daniel Vetter <daniel@ffwll.ch>
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>

Signed-off-by: Melissa Wen <melissa.srw@gmail.com>
---
 drivers/gpu/drm/vkms/vkms_composer.c | 43 +++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 10 deletions(-)

Comments

Pekka Paalanen Aug. 20, 2020, 7:27 a.m. UTC | #1
On Wed, 19 Aug 2020 17:53:36 -0300
Melissa Wen <melissa.srw@gmail.com> wrote:

> The current VKMS blend function ignores alpha channel and just overwrites
> vaddr_src with vaddr_dst. This XRGB approach triggers a warning when
> running the kms_cursor_crc/cursor-alpha-transparent test case. In IGT
> tests, cairo_format_argb32 uses premultiplied alpha (according to
> documentation), so this patch considers premultiplied alpha colors to
> compose vaddr_src with vaddr_dst.
> 
> This change removes the following cursor-alpha-transparent warning:
> Suspicious CRC: All values are 0.
> 
> Cc: Daniel Vetter <daniel@ffwll.ch>
> Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
> Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
> 
> Signed-off-by: Melissa Wen <melissa.srw@gmail.com>
> ---
>  drivers/gpu/drm/vkms/vkms_composer.c | 43 +++++++++++++++++++++-------
>  1 file changed, 33 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
> index 4f3b07a32b60..6aac962d3e2e 100644
> --- a/drivers/gpu/drm/vkms/vkms_composer.c
> +++ b/drivers/gpu/drm/vkms/vkms_composer.c
> @@ -32,8 +32,6 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>  			src_offset = composer->offset
>  				     + (i * composer->pitch)
>  				     + (j * composer->cpp);
> -			/* XRGB format ignores Alpha channel */
> -			bitmap_clear(vaddr_out + src_offset, 24, 8);
>  			crc = crc32_le(crc, vaddr_out + src_offset,
>  				       sizeof(u32));
>  		}
> @@ -42,6 +40,32 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>  	return crc;
>  }
>  
> +u8 blend_channel(u8 c_src, u8 c_dst, u8 a_src)
> +{
> +	u32 pre_blend;
> +	u8 new_color;
> +
> +	/* Premultiplied alpha blending - IGT + cairo context */
> +	pre_blend = (c_src * 255 + c_dst * (255 - a_src));
> +
> +	/* Faster div by 255 */
> +	new_color = ((pre_blend + ((pre_blend + 257) >> 8)) >> 8);
> +
> +	return new_color;
> +}
> +
> +void alpha_blending(u8 *argb_src, u8 *argb_dst)
> +{
> +	u8 a_src;
> +
> +	a_src = argb_src[3];

Hi,

DRM pixel formats are often defined as "bits in a 32-bit word", but
here you are accessing it as an array of bytes. To me that looks
suspicious wrt. big-endian architectures.

Unfortunately I have again forgot how DRM pixel formats should be
interpreted on a big-endian machine, if I ever even understood it, so I
can't say if this is right or not.


Thanks,
pq

> +	argb_dst[0] = blend_channel(argb_src[0], argb_dst[0], a_src);
> +	argb_dst[1] = blend_channel(argb_src[1], argb_dst[1], a_src);
> +	argb_dst[2] = blend_channel(argb_src[2], argb_dst[2], a_src);
> +	/* Opaque primary */
> +	argb_dst[3] = 0xFF;
> +}
> +
>  /**
>   * blend - blend value at vaddr_src with value at vaddr_dst
>   * @vaddr_dst: destination address
> @@ -50,12 +74,9 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>   * @src_composer: source framebuffer's metadata
>   *
>   * Blend value at vaddr_src with value at vaddr_dst.
> - * Currently, this function write value of vaddr_src on value
> - * at vaddr_dst using buffer's metadata to locate the new values
> - * from vaddr_src and their destination at vaddr_dst.
> - *
> - * TODO: Use the alpha value to blend vaddr_src with vaddr_dst
> - *	 instead of overwriting it.
> + * Currently, this function considers premultiplied alpha for blending, as used
> + * by Cairo. It uses buffer's metadata to locate the new composite values at
> + * vaddr_dst.
>   */
>  static void blend(void *vaddr_dst, void *vaddr_src,
>  		  struct vkms_composer *dest_composer,
> @@ -63,6 +84,7 @@ static void blend(void *vaddr_dst, void *vaddr_src,
>  {
>  	int i, j, j_dst, i_dst;
>  	int offset_src, offset_dst;
> +	u8 *p_dst, *p_src;
>  
>  	int x_src = src_composer->src.x1 >> 16;
>  	int y_src = src_composer->src.y1 >> 16;
> @@ -84,8 +106,9 @@ static void blend(void *vaddr_dst, void *vaddr_src,
>  				     + (i * src_composer->pitch)
>  				     + (j * src_composer->cpp);
>  
> -			memcpy(vaddr_dst + offset_dst,
> -			       vaddr_src + offset_src, sizeof(u32));
> +			p_src = (u8 *)(vaddr_src + offset_src);
> +			p_dst = (u8 *)(vaddr_dst + offset_dst);
> +			alpha_blending(p_src, p_dst);
>  		}
>  		i_dst++;
>  	}
Rodrigo Siqueira Aug. 25, 2020, 3:04 a.m. UTC | #2
Hi Melissa,

First of all, thanks a lot for your patch!

Follows my inline comments.

On 08/19, Melissa Wen wrote:
> The current VKMS blend function ignores alpha channel and just overwrites
> vaddr_src with vaddr_dst. This XRGB approach triggers a warning when
> running the kms_cursor_crc/cursor-alpha-transparent test case. In IGT
> tests, cairo_format_argb32 uses premultiplied alpha (according to
> documentation), so this patch considers premultiplied alpha colors to
> compose vaddr_src with vaddr_dst.
> 
> This change removes the following cursor-alpha-transparent warning:
> Suspicious CRC: All values are 0.
> 
> Cc: Daniel Vetter <daniel@ffwll.ch>
> Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
> Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
> 
> Signed-off-by: Melissa Wen <melissa.srw@gmail.com>
> ---
>  drivers/gpu/drm/vkms/vkms_composer.c | 43 +++++++++++++++++++++-------
>  1 file changed, 33 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
> index 4f3b07a32b60..6aac962d3e2e 100644
> --- a/drivers/gpu/drm/vkms/vkms_composer.c
> +++ b/drivers/gpu/drm/vkms/vkms_composer.c
> @@ -32,8 +32,6 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>  			src_offset = composer->offset
>  				     + (i * composer->pitch)
>  				     + (j * composer->cpp);
> -			/* XRGB format ignores Alpha channel */
> -			bitmap_clear(vaddr_out + src_offset, 24, 8);
>  			crc = crc32_le(crc, vaddr_out + src_offset,
>  				       sizeof(u32));
>  		}
> @@ -42,6 +40,32 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>  	return crc;
>  }
>  
> +u8 blend_channel(u8 c_src, u8 c_dst, u8 a_src)

Use static here.

Also, replace c_src to src, c_dst to dst, and a_src to alpha.

> +{
> +	u32 pre_blend;
> +	u8 new_color;
> +
> +	/* Premultiplied alpha blending - IGT + cairo context */

You can drop the part that says "IGT + cairo context", this explanation
better suit the commit message.

> +	pre_blend = (c_src * 255 + c_dst * (255 - a_src));
> +
> +	/* Faster div by 255 */
> +	new_color = ((pre_blend + ((pre_blend + 257) >> 8)) >> 8);
> +
> +	return new_color;
> +}
> +
> +void alpha_blending(u8 *argb_src, u8 *argb_dst)

Use static.

Looks like that argb_src is a read-only variable, in this sense add
const.

> +{
> +	u8 a_src;
> +
> +	a_src = argb_src[3];

change a_src to alpha.

> +	argb_dst[0] = blend_channel(argb_src[0], argb_dst[0], a_src);
> +	argb_dst[1] = blend_channel(argb_src[1], argb_dst[1], a_src);
> +	argb_dst[2] = blend_channel(argb_src[2], argb_dst[2], a_src);
> +	/* Opaque primary */
> +	argb_dst[3] = 0xFF;
> +}
> +
>  /**
>   * blend - blend value at vaddr_src with value at vaddr_dst
>   * @vaddr_dst: destination address
> @@ -50,12 +74,9 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
>   * @src_composer: source framebuffer's metadata
>   *
>   * Blend value at vaddr_src with value at vaddr_dst.
> - * Currently, this function write value of vaddr_src on value
> - * at vaddr_dst using buffer's metadata to locate the new values
> - * from vaddr_src and their destination at vaddr_dst.
> - *
> - * TODO: Use the alpha value to blend vaddr_src with vaddr_dst
> - *	 instead of overwriting it.
> + * Currently, this function considers premultiplied alpha for blending, as used
> + * by Cairo. It uses buffer's metadata to locate the new composite values at
> + * vaddr_dst.
>   */
>  static void blend(void *vaddr_dst, void *vaddr_src,
>  		  struct vkms_composer *dest_composer,
> @@ -63,6 +84,7 @@ static void blend(void *vaddr_dst, void *vaddr_src,
>  {
>  	int i, j, j_dst, i_dst;
>  	int offset_src, offset_dst;
> +	u8 *p_dst, *p_src;

I suppose that p stands for "pixel", right? In this case how about use
pixel?

Best Regards
Rodrigo Siqueira

>  
>  	int x_src = src_composer->src.x1 >> 16;
>  	int y_src = src_composer->src.y1 >> 16;
> @@ -84,8 +106,9 @@ static void blend(void *vaddr_dst, void *vaddr_src,
>  				     + (i * src_composer->pitch)
>  				     + (j * src_composer->cpp);
>  
> -			memcpy(vaddr_dst + offset_dst,
> -			       vaddr_src + offset_src, sizeof(u32));
> +			p_src = (u8 *)(vaddr_src + offset_src);
> +			p_dst = (u8 *)(vaddr_dst + offset_dst);
> +			alpha_blending(p_src, p_dst);
>  		}
>  		i_dst++;
>  	}
> -- 
> 2.28.0
>
Melissa Wen Aug. 25, 2020, 11:47 a.m. UTC | #3
Hi Rodrigo,

Thanks for the review!
I just sent a v2 applying your suggestions.

Best Regards,

Melissa Wen

Melissa Wen


On Tue, Aug 25, 2020 at 12:04 AM Rodrigo Siqueira
<rodrigosiqueiramelo@gmail.com> wrote:
>
> Hi Melissa,
>
> First of all, thanks a lot for your patch!
>
> Follows my inline comments.
>
> On 08/19, Melissa Wen wrote:
> > The current VKMS blend function ignores alpha channel and just overwrites
> > vaddr_src with vaddr_dst. This XRGB approach triggers a warning when
> > running the kms_cursor_crc/cursor-alpha-transparent test case. In IGT
> > tests, cairo_format_argb32 uses premultiplied alpha (according to
> > documentation), so this patch considers premultiplied alpha colors to
> > compose vaddr_src with vaddr_dst.
> >
> > This change removes the following cursor-alpha-transparent warning:
> > Suspicious CRC: All values are 0.
> >
> > Cc: Daniel Vetter <daniel@ffwll.ch>
> > Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
> > Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
> >
> > Signed-off-by: Melissa Wen <melissa.srw@gmail.com>
> > ---
> >  drivers/gpu/drm/vkms/vkms_composer.c | 43 +++++++++++++++++++++-------
> >  1 file changed, 33 insertions(+), 10 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
> > index 4f3b07a32b60..6aac962d3e2e 100644
> > --- a/drivers/gpu/drm/vkms/vkms_composer.c
> > +++ b/drivers/gpu/drm/vkms/vkms_composer.c
> > @@ -32,8 +32,6 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
> >                       src_offset = composer->offset
> >                                    + (i * composer->pitch)
> >                                    + (j * composer->cpp);
> > -                     /* XRGB format ignores Alpha channel */
> > -                     bitmap_clear(vaddr_out + src_offset, 24, 8);
> >                       crc = crc32_le(crc, vaddr_out + src_offset,
> >                                      sizeof(u32));
> >               }
> > @@ -42,6 +40,32 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
> >       return crc;
> >  }
> >
> > +u8 blend_channel(u8 c_src, u8 c_dst, u8 a_src)
>
> Use static here.
>
> Also, replace c_src to src, c_dst to dst, and a_src to alpha.
>
> > +{
> > +     u32 pre_blend;
> > +     u8 new_color;
> > +
> > +     /* Premultiplied alpha blending - IGT + cairo context */
>
> You can drop the part that says "IGT + cairo context", this explanation
> better suit the commit message.
>
> > +     pre_blend = (c_src * 255 + c_dst * (255 - a_src));
> > +
> > +     /* Faster div by 255 */
> > +     new_color = ((pre_blend + ((pre_blend + 257) >> 8)) >> 8);
> > +
> > +     return new_color;
> > +}
> > +
> > +void alpha_blending(u8 *argb_src, u8 *argb_dst)
>
> Use static.
>
> Looks like that argb_src is a read-only variable, in this sense add
> const.
>
> > +{
> > +     u8 a_src;
> > +
> > +     a_src = argb_src[3];
>
> change a_src to alpha.
>
> > +     argb_dst[0] = blend_channel(argb_src[0], argb_dst[0], a_src);
> > +     argb_dst[1] = blend_channel(argb_src[1], argb_dst[1], a_src);
> > +     argb_dst[2] = blend_channel(argb_src[2], argb_dst[2], a_src);
> > +     /* Opaque primary */
> > +     argb_dst[3] = 0xFF;
> > +}
> > +
> >  /**
> >   * blend - blend value at vaddr_src with value at vaddr_dst
> >   * @vaddr_dst: destination address
> > @@ -50,12 +74,9 @@ static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
> >   * @src_composer: source framebuffer's metadata
> >   *
> >   * Blend value at vaddr_src with value at vaddr_dst.
> > - * Currently, this function write value of vaddr_src on value
> > - * at vaddr_dst using buffer's metadata to locate the new values
> > - * from vaddr_src and their destination at vaddr_dst.
> > - *
> > - * TODO: Use the alpha value to blend vaddr_src with vaddr_dst
> > - *    instead of overwriting it.
> > + * Currently, this function considers premultiplied alpha for blending, as used
> > + * by Cairo. It uses buffer's metadata to locate the new composite values at
> > + * vaddr_dst.
> >   */
> >  static void blend(void *vaddr_dst, void *vaddr_src,
> >                 struct vkms_composer *dest_composer,
> > @@ -63,6 +84,7 @@ static void blend(void *vaddr_dst, void *vaddr_src,
> >  {
> >       int i, j, j_dst, i_dst;
> >       int offset_src, offset_dst;
> > +     u8 *p_dst, *p_src;
>
> I suppose that p stands for "pixel", right? In this case how about use
> pixel?
>
> Best Regards
> Rodrigo Siqueira
>
> >
> >       int x_src = src_composer->src.x1 >> 16;
> >       int y_src = src_composer->src.y1 >> 16;
> > @@ -84,8 +106,9 @@ static void blend(void *vaddr_dst, void *vaddr_src,
> >                                    + (i * src_composer->pitch)
> >                                    + (j * src_composer->cpp);
> >
> > -                     memcpy(vaddr_dst + offset_dst,
> > -                            vaddr_src + offset_src, sizeof(u32));
> > +                     p_src = (u8 *)(vaddr_src + offset_src);
> > +                     p_dst = (u8 *)(vaddr_dst + offset_dst);
> > +                     alpha_blending(p_src, p_dst);
> >               }
> >               i_dst++;
> >       }
> > --
> > 2.28.0
> >
>
> --
> Rodrigo Siqueira
> https://siqueira.tech
diff mbox series

Patch

diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
index 4f3b07a32b60..6aac962d3e2e 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.c
+++ b/drivers/gpu/drm/vkms/vkms_composer.c
@@ -32,8 +32,6 @@  static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
 			src_offset = composer->offset
 				     + (i * composer->pitch)
 				     + (j * composer->cpp);
-			/* XRGB format ignores Alpha channel */
-			bitmap_clear(vaddr_out + src_offset, 24, 8);
 			crc = crc32_le(crc, vaddr_out + src_offset,
 				       sizeof(u32));
 		}
@@ -42,6 +40,32 @@  static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
 	return crc;
 }
 
+u8 blend_channel(u8 c_src, u8 c_dst, u8 a_src)
+{
+	u32 pre_blend;
+	u8 new_color;
+
+	/* Premultiplied alpha blending - IGT + cairo context */
+	pre_blend = (c_src * 255 + c_dst * (255 - a_src));
+
+	/* Faster div by 255 */
+	new_color = ((pre_blend + ((pre_blend + 257) >> 8)) >> 8);
+
+	return new_color;
+}
+
+void alpha_blending(u8 *argb_src, u8 *argb_dst)
+{
+	u8 a_src;
+
+	a_src = argb_src[3];
+	argb_dst[0] = blend_channel(argb_src[0], argb_dst[0], a_src);
+	argb_dst[1] = blend_channel(argb_src[1], argb_dst[1], a_src);
+	argb_dst[2] = blend_channel(argb_src[2], argb_dst[2], a_src);
+	/* Opaque primary */
+	argb_dst[3] = 0xFF;
+}
+
 /**
  * blend - blend value at vaddr_src with value at vaddr_dst
  * @vaddr_dst: destination address
@@ -50,12 +74,9 @@  static uint32_t compute_crc(void *vaddr_out, struct vkms_composer *composer)
  * @src_composer: source framebuffer's metadata
  *
  * Blend value at vaddr_src with value at vaddr_dst.
- * Currently, this function write value of vaddr_src on value
- * at vaddr_dst using buffer's metadata to locate the new values
- * from vaddr_src and their destination at vaddr_dst.
- *
- * TODO: Use the alpha value to blend vaddr_src with vaddr_dst
- *	 instead of overwriting it.
+ * Currently, this function considers premultiplied alpha for blending, as used
+ * by Cairo. It uses buffer's metadata to locate the new composite values at
+ * vaddr_dst.
  */
 static void blend(void *vaddr_dst, void *vaddr_src,
 		  struct vkms_composer *dest_composer,
@@ -63,6 +84,7 @@  static void blend(void *vaddr_dst, void *vaddr_src,
 {
 	int i, j, j_dst, i_dst;
 	int offset_src, offset_dst;
+	u8 *p_dst, *p_src;
 
 	int x_src = src_composer->src.x1 >> 16;
 	int y_src = src_composer->src.y1 >> 16;
@@ -84,8 +106,9 @@  static void blend(void *vaddr_dst, void *vaddr_src,
 				     + (i * src_composer->pitch)
 				     + (j * src_composer->cpp);
 
-			memcpy(vaddr_dst + offset_dst,
-			       vaddr_src + offset_src, sizeof(u32));
+			p_src = (u8 *)(vaddr_src + offset_src);
+			p_dst = (u8 *)(vaddr_dst + offset_dst);
+			alpha_blending(p_src, p_dst);
 		}
 		i_dst++;
 	}