Support alpha blending in MTK display driver
Subsystems (3)
v2 cover Support alpha blending in MTK display driver
Support "Pre-multiplied" and "None" blend mode on MediaTek's chips by adding correct blend mode property when the planes init. Before this patch, only the "Coverage" mode (default) is supported.
---
Changes in v2:
- Remove unnecessary codes
- Add more information to the commit message
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Hsiao Chien Sung (5):
drm/mediatek: Support "None" blending in OVL drm/mediatek: Support "None" blending in Mixer drm/mediatek: Support "Pre-multiplied" blending in OVL drm/mediatek: Support "Pre-multiplied" blending in Mixer drm/mediatek: Support alpha blending in display driver
drivers/gpu/drm/mediatek/mtk_disp_ovl.c | 36 +++++++++++++++++++++++++-------- drivers/gpu/drm/mediatek/mtk_ethdr.c | 13 +++++++++--- drivers/gpu/drm/mediatek/mtk_plane.c | 11 ++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-)
---
base-commit: 8ad49a92cff4bab13eb2f2725243f5f31eff3f3b
change-id: 20240710-alpha-blending-067295570863
Best regards,
Signature · 1 line
Hsiao Chien Sung <[email protected]>
PATCH v2 1/5 drm/mediatek: Support "None" blending in OVL
Support "None" alpha blending mode on MediaTek's chips.
--- drivers/gpu/drm/mediatek/mtk_disp_ovl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/mediatek/mtk_disp_ovl.c +3 -1
@@ -434,6 +434,7 @@ void mtk_ovl_layer_config(struct device *dev, unsigned int idx,unsigned int fmt = pending->format;unsigned int offset = (pending->y <<16 ) | pending->x;unsigned int src_size = (pending->height <<16 ) | pending->width; +unsigned int blend_mode = state->base.pixel_blend_mode;unsigned int ignore_pixel_alpha =0 ;unsigned int con;bool is_afbc = pending->modifier !=DRM_FORMAT_MOD_LINEAR ;
@@ -463,7 +464,8 @@ void mtk_ovl_layer_config(struct device *dev, unsigned int idx,* For RGB888 related formats, whether CONST_BLD is enabled or not won't * affect the result. Therefore we use !has_alpha as the condition. */ -if (state->base.fb && !state->base.fb->format->has_alpha) +if ((state->base.fb && !state->base.fb->format->has_alpha) || + blend_mode ==DRM_MODE_BLEND_PIXEL_NONE ) ignore_pixel_alpha =OVL_CONST_BLEND ;if (pending->rotation &DRM_MODE_REFLECT_Y ) {
Signature · 1 line
2.43.0
PATCH v2 2/5 drm/mediatek: Support "None" blending in Mixer
In reply to Hsiao Chien SungSupport "None" alpha blending mode on MediaTek's chips.
--- drivers/gpu/drm/mediatek/mtk_ethdr.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/mediatek/mtk_ethdr.c +3 -1
@@ -3,6 +3,7 @@* Copyright (c) 2021 MediaTek Inc. */ +#include <drm/drm_blend.h> #include <drm/drm_fourcc.h> #include <drm/drm_framebuffer.h> #include <linux/clk.h>
@@ -175,7 +176,8 @@ void mtk_ethdr_layer_config(struct device *dev, unsigned int idx, alpha_con |= state->base.alpha &MIXER_ALPHA ; } -if (state->base.fb && !state->base.fb->format->has_alpha) { +if ((state->base.fb && !state->base.fb->format->has_alpha) || + state->base.pixel_blend_mode ==DRM_MODE_BLEND_PIXEL_NONE ) {/* * Mixer doesn't support CONST_BLD mode, * use a trick to make the output equivalent
Signature · 1 line
2.43.0
PATCH v2 3/5 drm/mediatek: Support "Pre-multiplied" blending in OVL
In reply to Hsiao Chien SungSupport "Pre-multiplied" alpha blending mode on in OVL. Before this patch, only the "coverage" mode is supported.
--- drivers/gpu/drm/mediatek/mtk_disp_ovl.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-)
drivers/gpu/drm/mediatek/mtk_disp_ovl.c +25 -7
@@ -56,8 +56,12 @@#define GMC_THRESHOLD_HIGH ((1 << GMC_THRESHOLD_BITS) / 4)#define GMC_THRESHOLD_LOW ((1 << GMC_THRESHOLD_BITS) / 8) +#define OVL_CON_CLRFMT_MAN BIT(23)#define OVL_CON_BYTE_SWAP BIT(24) -#define OVL_CON_MTX_YUV_TO_RGB (6 << 16) + +/* OVL_CON_RGB_SWAP works only if OVL_CON_CLRFMT_MAN is enabled */ +#define OVL_CON_RGB_SWAP BIT(25) +#define OVL_CON_CLRFMT_RGB (1 << 12)#define OVL_CON_CLRFMT_ARGB8888 (2 << 12)#define OVL_CON_CLRFMT_RGBA8888 (3 << 12)
@@ -65,6 +69,11 @@#define OVL_CON_CLRFMT_BGRA8888 (OVL_CON_CLRFMT_ARGB8888 | OVL_CON_BYTE_SWAP)#define OVL_CON_CLRFMT_UYVY (4 << 12)#define OVL_CON_CLRFMT_YUYV (5 << 12) +#define OVL_CON_MTX_YUV_TO_RGB (6 << 16) +#define OVL_CON_CLRFMT_PARGB8888 ((3 << 12) | OVL_CON_CLRFMT_MAN) +#define OVL_CON_CLRFMT_PABGR8888 (OVL_CON_CLRFMT_PARGB8888 | OVL_CON_RGB_SWAP) +#define OVL_CON_CLRFMT_PBGRA8888 (OVL_CON_CLRFMT_PARGB8888 | OVL_CON_BYTE_SWAP) +#define OVL_CON_CLRFMT_PRGBA8888 (OVL_CON_CLRFMT_PABGR8888 | OVL_CON_BYTE_SWAP)#define OVL_CON_CLRFMT_RGB565 (ovl) ((ovl)->data->fmt_rgb565_is_0 ? \ 0 : OVL_CON_CLRFMT_RGB)#define OVL_CON_CLRFMT_RGB888 (ovl) ((ovl)->data->fmt_rgb565_is_0 ? \
@@ -377,7 +386,8 @@ void mtk_ovl_layer_off(struct device *dev, unsigned int idx, DISP_REG_OVL_RDMA_CTRL(idx)); } -static unsigned int ovl_fmt_convert (struct mtk_disp_ovl *ovl,unsigned int fmt) +static unsigned int ovl_fmt_convert (struct mtk_disp_ovl *ovl,unsigned int fmt, +unsigned int blend_mode) {/* The return value in switch "MEM_MODE_INPUT_FORMAT_XXX" * is defined in mediatek HW data sheet.
@@ -398,22 +408,30 @@ static unsigned int ovl_fmt_convert(struct mtk_disp_ovl *ovl, unsigned int fmt)case DRM_FORMAT_RGBA8888 :case DRM_FORMAT_RGBX1010102 :case DRM_FORMAT_RGBA1010102 : -return OVL_CON_CLRFMT_RGBA8888 ; +return blend_mode ==DRM_MODE_BLEND_COVERAGE ? +OVL_CON_CLRFMT_RGBA8888 : +OVL_CON_CLRFMT_PRGBA8888 ;case DRM_FORMAT_BGRX8888 :case DRM_FORMAT_BGRA8888 :case DRM_FORMAT_BGRX1010102 :case DRM_FORMAT_BGRA1010102 : -return OVL_CON_CLRFMT_BGRA8888 ; +return blend_mode ==DRM_MODE_BLEND_COVERAGE ? +OVL_CON_CLRFMT_BGRA8888 : +OVL_CON_CLRFMT_PBGRA8888 ;case DRM_FORMAT_XRGB8888 :case DRM_FORMAT_ARGB8888 :case DRM_FORMAT_XRGB2101010 :case DRM_FORMAT_ARGB2101010 : -return OVL_CON_CLRFMT_ARGB8888 ; +return blend_mode ==DRM_MODE_BLEND_COVERAGE ? +OVL_CON_CLRFMT_ARGB8888 : +OVL_CON_CLRFMT_PARGB8888 ;case DRM_FORMAT_XBGR8888 :case DRM_FORMAT_ABGR8888 :case DRM_FORMAT_XBGR2101010 :case DRM_FORMAT_ABGR2101010 : -return OVL_CON_CLRFMT_ABGR8888 ; +return blend_mode ==DRM_MODE_BLEND_COVERAGE ? +OVL_CON_CLRFMT_ABGR8888 : +OVL_CON_CLRFMT_PABGR8888 ;case DRM_FORMAT_UYVY :return OVL_CON_CLRFMT_UYVY |OVL_CON_MTX_YUV_TO_RGB ;case DRM_FORMAT_YUYV :
@@ -453,7 +471,7 @@ void mtk_ovl_layer_config(struct device *dev, unsigned int idx,return ; } - con =ovl_fmt_convert (ovl, fmt); + con =ovl_fmt_convert (ovl, fmt, blend_mode);if (state->base.fb) { con |=OVL_CON_AEN ; con |= state->base.alpha &OVL_CON_ALPHA ;
Signature · 1 line
2.43.0
PATCH v2 4/5 drm/mediatek: Support "Pre-multiplied" blending in Mixer
In reply to Hsiao Chien SungSupport "Pre-multiplied" alpha blending mode in Mixer. Before this patch, only the coverage mode is supported.
To replace the default setting that is set in mtk_ethdr_config(), we change mtk_ddp_write_mask() to mtk_ddp_write(), and this change will also reset the NON_PREMULTI_SOURCE bit that was assigned in mtk_ethdr_config(). Therefore, we must still set NON_PREMULTI_SOURCE bit if the blend mode is not DRM_MODE_BLEND_PREMULTI.
--- drivers/gpu/drm/mediatek/mtk_ethdr.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/mediatek/mtk_ethdr.c +7 -2
@@ -36,6 +36,7 @@#define MIX_SRC_L0_EN BIT(0)#define MIX_L_SRC_CON (n) (0x28 + 0x18 * (n))#define NON_PREMULTI_SOURCE (2 << 12) +#define PREMULTI_SOURCE (3 << 12)#define MIX_L_SRC_SIZE (n) (0x30 + 0x18 * (n))#define MIX_L_SRC_OFFSET (n) (0x34 + 0x18 * (n))#define MIX_FUNC_DCM0 0x120
@@ -176,6 +177,11 @@ void mtk_ethdr_layer_config(struct device *dev, unsigned int idx, alpha_con |= state->base.alpha &MIXER_ALPHA ; } +if (state->base.pixel_blend_mode ==DRM_MODE_BLEND_PREMULTI ) + alpha_con |=PREMULTI_SOURCE ; +else + alpha_con |=NON_PREMULTI_SOURCE ; +if ((state->base.fb && !state->base.fb->format->has_alpha) || state->base.pixel_blend_mode ==DRM_MODE_BLEND_PIXEL_NONE ) {/*
@@ -193,8 +199,7 @@ void mtk_ethdr_layer_config(struct device *dev, unsigned int idx,mtk_ddp_write (cmdq_pkt, pending->height <<16 | align_width, &mixer->cmdq_base, mixer->regs,MIX_L_SRC_SIZE (idx));mtk_ddp_write (cmdq_pkt, offset, &mixer->cmdq_base, mixer->regs,MIX_L_SRC_OFFSET (idx)); -mtk_ddp_write_mask (cmdq_pkt, alpha_con, &mixer->cmdq_base, mixer->regs,MIX_L_SRC_CON (idx), -0x1ff ); +mtk_ddp_write (cmdq_pkt, alpha_con, &mixer->cmdq_base, mixer->regs,MIX_L_SRC_CON (idx));mtk_ddp_write_mask (cmdq_pkt,BIT (idx), &mixer->cmdq_base, mixer->regs,MIX_SRC_CON ,BIT (idx)); }
Signature · 1 line
2.43.0
PATCH v2 5/5 drm/mediatek: Support alpha blending in display driver
In reply to Hsiao Chien SungSupport "Pre-multiplied" and "None" blend mode on MediaTek's chips by adding correct blend mode property when the planes init. Before this patch, only the "Coverage" mode (default) is supported.
For more information, there are three pixel blend modes in DRM driver:
"None", "Pre-multiplied", and "Coverage".
To understand the difference between these modes, let's take a look at the following two approaches to do alpha blending:
1. Straight:
dst.RGB = src.RGB * src.A + dst.RGB * (1 - src.A)
This is straightforward and easy to understand, when the source layer is compositing with the destination layer, it's alpha will affect the result. This is also known as "post-multiplied", or "Coverage" mode.
2. Pre-multiplied:
dst.RGB = src.RGB + dst.RGB * (1 - src.A)
Since the source RGB have already multiplied its alpha, only destination RGB need to multiply it. This is the "Pre-multiplied" mode in DRM.
For the "None" blend mode in DRM, it means the pixel alpha is ignored when compositing the layers, only the constant alpha for the composited layer will take effects.
--- drivers/gpu/drm/mediatek/mtk_plane.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/gpu/drm/mediatek/mtk_plane.c +11 -0
@@ -346,6 +346,17 @@ int mtk_plane_init(struct drm_device *dev, struct drm_plane *plane,DRM_INFO ("Create rotation property failed\n" ); } + err =drm_plane_create_alpha_property (plane); +if (err) +DRM_ERROR ("failed to create property: alpha\n" ); + + err =drm_plane_create_blend_mode_property (plane, +BIT (DRM_MODE_BLEND_PREMULTI ) | +BIT (DRM_MODE_BLEND_COVERAGE ) | +BIT (DRM_MODE_BLEND_PIXEL_NONE )); +if (err) +DRM_ERROR ("failed to create property: blend_mode\n" ); +drm_plane_helper_add (plane, &mtk_plane_helper_funcs);return 0 ;
Signature · 1 line
2.43.0