Re: [PATCH v5 1/1] video console: refactoring and optimization

2023-02-21 Thread Simon Glass
On Mon, 20 Feb 2023 at 03:49, Dzmitry Sankouski  wrote:
>
> - move common code to vidconsole_internal.h and console_core.c
> - unite probe functions
> - get rid of code duplications in switch across bpp values
> - extract common pixel fill logic in two functions one per
> horizontal and vertical filling
> - rearrange statements in put_xy* methods in unified way
> - replace types - uint*_t to u*
>
> Signed-off-by: Dzmitry Sankouski 
> ---
> Changes for v2: none
> Changes for v3: none
> Changes for v4:
> - move common code to vidconsole_internal.h
> - unite probe functions
> Changes for v5:
> - move common functions to console-core.c file
> - remove static keyword from shared functions
>
>  drivers/video/Makefile  |   6 +
>  drivers/video/console_core.c| 141 +
>  drivers/video/console_normal.c  | 150 --
>  drivers/video/console_rotate.c  | 308 
>  drivers/video/vidconsole_internal.h |  95 +
>  5 files changed, 324 insertions(+), 376 deletions(-)
>  create mode 100644 drivers/video/console_core.c
>  create mode 100644 drivers/video/vidconsole_internal.h

Reviewed-by: Simon Glass 


[PATCH v5 1/1] video console: refactoring and optimization

2023-02-20 Thread Dzmitry Sankouski
- move common code to vidconsole_internal.h and console_core.c
- unite probe functions
- get rid of code duplications in switch across bpp values
- extract common pixel fill logic in two functions one per
horizontal and vertical filling
- rearrange statements in put_xy* methods in unified way
- replace types - uint*_t to u*

Signed-off-by: Dzmitry Sankouski 
---
Changes for v2: none
Changes for v3: none
Changes for v4:
- move common code to vidconsole_internal.h
- unite probe functions
Changes for v5:
- move common functions to console-core.c file
- remove static keyword from shared functions

 drivers/video/Makefile  |   6 +
 drivers/video/console_core.c| 141 +
 drivers/video/console_normal.c  | 150 --
 drivers/video/console_rotate.c  | 308 
 drivers/video/vidconsole_internal.h |  95 +
 5 files changed, 324 insertions(+), 376 deletions(-)
 create mode 100644 drivers/video/console_core.c
 create mode 100644 drivers/video/vidconsole_internal.h

diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index cdb7d9a54d..cb3f373645 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -9,6 +9,12 @@ obj-$(CONFIG_BACKLIGHT_GPIO) += backlight_gpio.o
 obj-$(CONFIG_BACKLIGHT_PWM) += pwm_backlight.o
 obj-$(CONFIG_CONSOLE_NORMAL) += console_normal.o
 obj-$(CONFIG_CONSOLE_ROTATION) += console_rotate.o
+ifdef CONFIG_CONSOLE_NORMAL
+obj-y += console_core.o
+else ifdef CONFIG_CONSOLE_ROTATION
+obj-y += console_core.o
+endif
+obj-$(CONFIG_CONSOLE_ROTATION) += console_core.o
 obj-$(CONFIG_CONSOLE_TRUETYPE) += console_truetype.o fonts/
 obj-$(CONFIG_DISPLAY) += display-uclass.o
 obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi-host-uclass.o
diff --git a/drivers/video/console_core.c b/drivers/video/console_core.c
new file mode 100644
index 00..9c2e4cb4ea
--- /dev/null
+++ b/drivers/video/console_core.c
@@ -0,0 +1,141 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2015 Google, Inc
+ * (C) Copyright 2015
+ * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
+ * (C) Copyright 2023 Dzmitry Sankouski 
+ */
+
+#include 
+#include 
+#include 
+#include "vidconsole_internal.h"
+
+int check_bpix_support(int bpix)
+{
+   if (bpix == VIDEO_BPP8 && IS_ENABLED(CONFIG_VIDEO_BPP8))
+   return 0;
+   else if (bpix == VIDEO_BPP16 && IS_ENABLED(CONFIG_VIDEO_BPP16))
+   return 0;
+   else if (bpix == VIDEO_BPP32 && IS_ENABLED(CONFIG_VIDEO_BPP32))
+   return 0;
+   else
+   return -ENOSYS;
+}
+
+inline void fill_pixel_and_goto_next(void **dstp, u32 value, int pbytes, int 
step)
+{
+   u8 *dst_byte = *dstp;
+
+   if (pbytes == 4) {
+   u32 *dst = *dstp;
+   *dst = value;
+   }
+   if (pbytes == 2) {
+   u16 *dst = *dstp;
+   *dst = value;
+   }
+   if (pbytes == 1) {
+   u8 *dst = *dstp;
+   *dst = value;
+   }
+   *dstp = dst_byte + step;
+}
+
+int fill_char_vertically(uchar *pfont, void **line, struct video_priv 
*vid_priv,
+bool direction)
+{
+   int step, line_step, pbytes, ret;
+   void *dst;
+
+   ret = check_bpix_support(vid_priv->bpix);
+   if (ret)
+   return ret;
+
+   pbytes = VNBYTES(vid_priv->bpix);
+   if (direction) {
+   step = -pbytes;
+   line_step = -vid_priv->line_length;
+   } else {
+   step = pbytes;
+   line_step = vid_priv->line_length;
+   }
+
+   for (int row = 0; row < VIDEO_FONT_HEIGHT; row++) {
+   dst = *line;
+   uchar bits = pfont[row];
+
+   for (int i = 0; i < VIDEO_FONT_WIDTH; i++) {
+   u32 value = (bits & 0x80) ?
+   vid_priv->colour_fg :
+   vid_priv->colour_bg;
+
+   fill_pixel_and_goto_next(,
+value,
+pbytes,
+step
+   );
+   bits <<= 1;
+   }
+   *line += line_step;
+   }
+   return ret;
+}
+
+int fill_char_horizontally(uchar *pfont, void **line, struct video_priv 
*vid_priv,
+  bool direction)
+{
+   int step, line_step, pbytes, ret;
+   void *dst;
+   u8 mask = 0x80;
+
+   ret = check_bpix_support(vid_priv->bpix);
+   if (ret)
+   return ret;
+
+   pbytes = VNBYTES(vid_priv->bpix);
+   if (direction) {
+   step = -pbytes;
+   line_step = vid_priv->line_length;
+   } else {
+   step = pbytes;
+   line_step = -vid_priv->line_length;
+   }
+   for (int col = 0; col < VIDEO_FONT_WIDTH; col++) {
+   dst =