[U-Boot] [PATCH v2 04/19] video: Use fractional units for X coordinates

Anatolij Gustschin agust at denx.de
Sat Jan 23 01:19:34 CET 2016


From: Simon Glass <sjg at chromium.org>

With anti-aliased fonts we need a more fine-grained horizontal position
than a single pixel. Characters can be positioned to start part-way through
a pixel, with anti-aliasing (greyscale edges) taking care of the visual
effect.

To cope with this, use fractional units (1/256 pixel) for horizontal
positions in the text console.

Signed-off-by: Simon Glass <sjg at chromium.org>
Signed-off-by: Anatolij Gustschin <agust at denx.de>
---
Changes in v2: rebased

 drivers/video/console_normal.c    |   24 ++++++++++++--
 drivers/video/console_rotate.c    |   65 ++++++++++++++++++++++++++++---------
 drivers/video/vidconsole-uclass.c |   56 ++++++++++++++++++--------------
 include/video_console.h           |   40 ++++++++++++++++++-----
 test/dm/video.c                   |    4 +--
 5 files changed, 136 insertions(+), 53 deletions(-)

diff --git a/drivers/video/console_normal.c b/drivers/video/console_normal.c
index d1031c8..89a55dd 100644
--- a/drivers/video/console_normal.c
+++ b/drivers/video/console_normal.c
@@ -71,13 +71,18 @@ static int console_normal_move_rows(struct udevice *dev, uint rowdst,
 	return 0;
 }
 
-static int console_normal_putc_xy(struct udevice *dev, uint x, uint y, char ch)
+static int console_normal_putc_xy(struct udevice *dev, uint x_frac, uint y,
+				  char ch)
 {
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
 	struct udevice *vid = dev->parent;
 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
 	int i, row;
 	void *line = vid_priv->fb + y * vid_priv->line_length +
-		x * VNBYTES(vid_priv->bpix);
+		VID_TO_PIXEL(x_frac) * VNBYTES(vid_priv->bpix);
+
+	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
+		return -EAGAIN;
 
 	for (row = 0; row < VIDEO_FONT_HEIGHT; row++) {
 		uchar bits = video_fontdata[ch * VIDEO_FONT_HEIGHT + row];
@@ -125,6 +130,20 @@ static int console_normal_putc_xy(struct udevice *dev, uint x, uint y, char ch)
 		line += vid_priv->line_length;
 	}
 
+	return VID_TO_POS(VIDEO_FONT_WIDTH);
+}
+
+static int console_normal_probe(struct udevice *dev)
+{
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
+	struct udevice *vid_dev = dev->parent;
+	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
+
+	vc_priv->x_charsize = VIDEO_FONT_WIDTH;
+	vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
+	vc_priv->cols = vid_priv->xsize / VIDEO_FONT_WIDTH;
+	vc_priv->rows = vid_priv->ysize / VIDEO_FONT_HEIGHT;
+
 	return 0;
 }
 
@@ -138,4 +157,5 @@ U_BOOT_DRIVER(vidconsole_normal) = {
 	.name	= "vidconsole0",
 	.id	= UCLASS_VIDEO_CONSOLE,
 	.ops	= &console_normal_ops,
+	.probe	= console_normal_probe,
 };
diff --git a/drivers/video/console_rotate.c b/drivers/video/console_rotate.c
index ebb31d8..227141d 100644
--- a/drivers/video/console_rotate.c
+++ b/drivers/video/console_rotate.c
@@ -82,17 +82,22 @@ static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
 	return 0;
 }
 
-static int console_putc_xy_1(struct udevice *dev, uint x, uint y, char ch)
+static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, char ch)
 {
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
 	struct udevice *vid = dev->parent;
 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
 	int pbytes = VNBYTES(vid_priv->bpix);
 	int i, col;
 	int mask = 0x80;
-	void *line = vid_priv->fb + (x + 1) * vid_priv->line_length -
-		(y + 1) * pbytes;
+	void *line;
 	uchar *pfont = video_fontdata + ch * VIDEO_FONT_HEIGHT;
 
+	line = vid_priv->fb + (VID_TO_PIXEL(x_frac) + 1) *
+			vid_priv->line_length - (y + 1) * pbytes;
+	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
+		return -EAGAIN;
+
 	for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
 		switch (vid_priv->bpix) {
 #ifdef CONFIG_VIDEO_BPP8
@@ -135,7 +140,7 @@ static int console_putc_xy_1(struct udevice *dev, uint x, uint y, char ch)
 		mask >>= 1;
 	}
 
-	return 0;
+	return VID_TO_POS(VIDEO_FONT_WIDTH);
 }
 
 
@@ -201,17 +206,21 @@ static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
 	return 0;
 }
 
-static int console_putc_xy_2(struct udevice *dev, uint x, uint y, char ch)
+static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, char ch)
 {
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
 	struct udevice *vid = dev->parent;
 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
 	int i, row;
 	void *line;
 
+	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
+		return -EAGAIN;
+
 	line = vid_priv->fb + (vid_priv->ysize - y - 1) *
-		vid_priv->line_length +
-		(vid_priv->xsize - x - VIDEO_FONT_WIDTH - 1) *
-		VNBYTES(vid_priv->bpix);
+			vid_priv->line_length +
+			(vid_priv->xsize - VID_TO_PIXEL(x_frac) -
+			VIDEO_FONT_WIDTH - 1) * VNBYTES(vid_priv->bpix);
 
 	for (row = 0; row < VIDEO_FONT_HEIGHT; row++) {
 		uchar bits = video_fontdata[ch * VIDEO_FONT_HEIGHT + row];
@@ -259,7 +268,7 @@ static int console_putc_xy_2(struct udevice *dev, uint x, uint y, char ch)
 		line -= vid_priv->line_length;
 	}
 
-	return 0;
+	return VID_TO_POS(VIDEO_FONT_WIDTH);
 }
 
 static int console_set_row_3(struct udevice *dev, uint row, int clr)
@@ -329,17 +338,22 @@ static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
 	return 0;
 }
 
-static int console_putc_xy_3(struct udevice *dev, uint x, uint y, char ch)
+static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, char ch)
 {
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
 	struct udevice *vid = dev->parent;
 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
 	int pbytes = VNBYTES(vid_priv->bpix);
 	int i, col;
 	int mask = 0x80;
-	void *line = vid_priv->fb + (vid_priv->ysize - x - 1) *
+	void *line = vid_priv->fb +
+		(vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1) *
 		vid_priv->line_length + y * pbytes;
 	uchar *pfont = video_fontdata + ch * VIDEO_FONT_HEIGHT;
 
+	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
+		return -EAGAIN;
+
 	for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
 		switch (vid_priv->bpix) {
 #ifdef CONFIG_VIDEO_BPP8
@@ -382,17 +396,35 @@ static int console_putc_xy_3(struct udevice *dev, uint x, uint y, char ch)
 		mask >>= 1;
 	}
 
-	return 0;
+	return VID_TO_POS(VIDEO_FONT_WIDTH);
 }
 
 
+static int console_probe_2(struct udevice *dev)
+{
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
+	struct udevice *vid_dev = dev->parent;
+	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
+
+	vc_priv->x_charsize = VIDEO_FONT_WIDTH;
+	vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
+	vc_priv->cols = vid_priv->xsize / VIDEO_FONT_WIDTH;
+	vc_priv->rows = vid_priv->ysize / VIDEO_FONT_HEIGHT;
+
+	return 0;
+}
+
 static int console_probe_1_3(struct udevice *dev)
 {
-	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
-	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
+	struct udevice *vid_dev = dev->parent;
+	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
 
-	priv->cols = vid_priv->ysize / VIDEO_FONT_WIDTH;
-	priv->rows = vid_priv->xsize / VIDEO_FONT_HEIGHT;
+	vc_priv->x_charsize = VIDEO_FONT_WIDTH;
+	vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
+	vc_priv->cols = vid_priv->ysize / VIDEO_FONT_WIDTH;
+	vc_priv->rows = vid_priv->xsize / VIDEO_FONT_HEIGHT;
+	vc_priv->xsize_frac = VID_TO_POS(vid_priv->ysize);
 
 	return 0;
 }
@@ -426,6 +458,7 @@ U_BOOT_DRIVER(vidconsole_2) = {
 	.name	= "vidconsole2",
 	.id	= UCLASS_VIDEO_CONSOLE,
 	.ops	= &console_ops_2,
+	.probe	= console_probe_2,
 };
 
 U_BOOT_DRIVER(vidconsole_3) = {
diff --git a/drivers/video/vidconsole-uclass.c b/drivers/video/vidconsole-uclass.c
index ea10189..d997186 100644
--- a/drivers/video/vidconsole-uclass.c
+++ b/drivers/video/vidconsole-uclass.c
@@ -52,14 +52,14 @@ static void vidconsole_back(struct udevice *dev)
 {
 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
 
-	if (--priv->curr_col < 0) {
-		priv->curr_col = priv->cols - 1;
-		if (--priv->curr_row < 0)
-			priv->curr_row = 0;
+	priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
+	if (priv->xcur_frac < 0) {
+		priv->xcur_frac = (priv->cols - 1) *
+			VID_TO_POS(priv->x_charsize);
+		priv->ycur -= priv->y_charsize;
+		if (priv->ycur < 0)
+			priv->ycur = 0;
 	}
-
-	vidconsole_putc_xy(dev, priv->curr_col * VIDEO_FONT_WIDTH,
-			   priv->curr_row * VIDEO_FONT_HEIGHT, ' ');
 }
 
 /* Move to a newline, scrolling the display if necessary */
@@ -71,15 +71,16 @@ static void vidconsole_newline(struct udevice *dev)
 	const int rows = CONFIG_CONSOLE_SCROLL_LINES;
 	int i;
 
-	priv->curr_col = 0;
+	priv->xcur_frac = 0;
+	priv->ycur += priv->y_charsize;
 
 	/* Check if we need to scroll the terminal */
-	if (++priv->curr_row >= priv->rows) {
+	if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
 		vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
 		for (i = 0; i < rows; i++)
 			vidconsole_set_row(dev, priv->rows - i - 1,
 					   vid_priv->colour_bg);
-		priv->curr_row -= rows;
+		priv->ycur -= rows * priv->y_charsize;
 	}
 	video_sync(dev->parent);
 }
@@ -91,16 +92,16 @@ int vidconsole_put_char(struct udevice *dev, char ch)
 
 	switch (ch) {
 	case '\r':
-		priv->curr_col = 0;
+		priv->xcur_frac = 0;
 		break;
 	case '\n':
 		vidconsole_newline(dev);
 		break;
 	case '\t':	/* Tab (8 chars alignment) */
-		priv->curr_col +=  8;
-		priv->curr_col &= ~7;
+		priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
+				+ 1) * priv->tab_width_frac;
 
-		if (priv->curr_col >= priv->cols)
+		if (priv->xcur_frac >= priv->xsize_frac)
 			vidconsole_newline(dev);
 		break;
 	case '\b':
@@ -112,13 +113,16 @@ int vidconsole_put_char(struct udevice *dev, char ch)
 		 * colour depth. Check this and return an error to help with
 		 * diagnosis.
 		 */
-		ret = vidconsole_putc_xy(dev,
-					 priv->curr_col * VIDEO_FONT_WIDTH,
-					 priv->curr_row * VIDEO_FONT_HEIGHT,
-					 ch);
-		if (ret)
+		ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
+		if (ret == -EAGAIN) {
+			vidconsole_newline(dev);
+			ret = vidconsole_putc_xy(dev, priv->xcur_frac,
+						 priv->ycur, ch);
+		}
+		if (ret < 0)
 			return ret;
-		if (++priv->curr_col >= priv->cols)
+		priv->xcur_frac += ret;
+		if (priv->xcur_frac >= priv->xsize_frac)
 			vidconsole_newline(dev);
 		break;
 	}
@@ -148,8 +152,7 @@ static int vidconsole_pre_probe(struct udevice *dev)
 	struct udevice *vid = dev->parent;
 	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
 
-	priv->rows = vid_priv->ysize / VIDEO_FONT_HEIGHT;
-	priv->cols = vid_priv->xsize / VIDEO_FONT_WIDTH;
+	priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
 
 	return 0;
 }
@@ -161,6 +164,9 @@ static int vidconsole_post_probe(struct udevice *dev)
 	struct stdio_dev *sdev = &priv->sdev;
 	int ret;
 
+	if (!priv->tab_width_frac)
+		priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
+
 	strlcpy(sdev->name, dev->name, sizeof(sdev->name));
 	sdev->flags = DEV_FLAGS_OUTPUT;
 	sdev->putc = vidconsole_putc;
@@ -184,9 +190,11 @@ UCLASS_DRIVER(vidconsole) = {
 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
 {
 	struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
+	struct udevice *vid_dev = dev->parent;
+	struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
 
-	priv->curr_col = min_t(short, col, priv->cols - 1);
-	priv->curr_row = min_t(short, row, priv->rows - 1);
+	priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
+	priv->ycur = min_t(short, row, vid_priv->ysize - 1);
 }
 
 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
diff --git a/include/video_console.h b/include/video_console.h
index c0fc792..eeba368 100644
--- a/include/video_console.h
+++ b/include/video_console.h
@@ -7,21 +7,37 @@
 #ifndef __video_console_h
 #define __video_console_h
 
+#define VID_FRAC_DIV	256
+
+#define VID_TO_PIXEL(x)	((x) / VID_FRAC_DIV)
+#define VID_TO_POS(x)	((x) * VID_FRAC_DIV)
+
 /**
  * struct vidconsole_priv - uclass-private data about a console device
  *
+ * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
+ * method. Drivers may set up @xstart_frac if desired.
+ *
  * @sdev:	stdio device, acting as an output sink
- * @curr_col:	Current text column (0=left)
- * @curr_row:	Current row (0=top)
+ * @xcur_frac:	Current X position, in fractional units (VID_TO_POS(x))
+ * @curr_row:	Current Y position in pixels (0=top)
  * @rows:	Number of text rows
  * @cols:	Number of text columns
+ * @x_charsize:	Character width in pixels
+ * @y_charsize:	Character height in pixels
+ * @tab_width_frac:	Tab width in fractional units
+ * @xsize_frac:	Width of the display in fractional units
  */
 struct vidconsole_priv {
 	struct stdio_dev sdev;
-	int curr_col;
-	int curr_row;
+	int xcur_frac;
+	int ycur;
 	int rows;
 	int cols;
+	int x_charsize;
+	int y_charsize;
+	int tab_width_frac;
+	int xsize_frac;
 };
 
 /**
@@ -36,12 +52,15 @@ struct vidconsole_ops {
 	 * putc_xy() - write a single character to a position
 	 *
 	 * @dev:	Device to write to
-	 * @x:		Pixel X position (0=left-most pixel)
+	 * @x_frac:	Fractional pixel X position (0=left-most pixel) which
+	 *		is the X position multipled by VID_FRAC_DIV.
 	 * @y:		Pixel Y position (0=top-most pixel)
 	 * @ch:		Character to write
-	 * @return 0 if OK, -ve on error
+	 * @return number of fractional pixels that the cursor should move,
+	 * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
+	 * on error
 	 */
-	int (*putc_xy)(struct udevice *dev, uint x, uint y, char ch);
+	int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
 
 	/**
 	 * move_rows() - Move text rows from one place to another
@@ -75,10 +94,13 @@ struct vidconsole_ops {
  * vidconsole_putc_xy() - write a single character to a position
  *
  * @dev:	Device to write to
- * @x:		Pixel X position (0=left-most pixel)
+ * @x_frac:	Fractional pixel X position (0=left-most pixel) which
+ *		is the X position multipled by VID_FRAC_DIV.
  * @y:		Pixel Y position (0=top-most pixel)
  * @ch:		Character to write
- * @return 0 if OK, -ve on error
+ * @return number of fractional pixels that the cursor should move,
+ * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
+ * on error
  */
 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
 
diff --git a/test/dm/video.c b/test/dm/video.c
index 9f5e7fc..be94633 100644
--- a/test/dm/video.c
+++ b/test/dm/video.c
@@ -106,14 +106,14 @@ static int dm_test_video_text(struct unit_test_state *uts)
 	ut_asserteq(46, compress_frame_buffer(dev));
 
 	for (i = 0; i < 20; i++)
-		vidconsole_putc_xy(con, i * 8, 0, ' ' + i);
+		vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
 	ut_asserteq(273, compress_frame_buffer(dev));
 
 	vidconsole_set_row(con, 0, WHITE);
 	ut_asserteq(46, compress_frame_buffer(dev));
 
 	for (i = 0; i < 20; i++)
-		vidconsole_putc_xy(con, i * 8, 0, ' ' + i);
+		vidconsole_putc_xy(con, VID_TO_POS(i * 8), 0, ' ' + i);
 	ut_asserteq(273, compress_frame_buffer(dev));
 
 	return 0;
-- 
1.7.9.5



More information about the U-Boot mailing list