[Concept,21/36] video: Pass context to select_font() method

Message ID 20260120231814.2033069-22-sjg@u-boot.org
State New
Headers
Series video: Add multiple-context support to vidconsole (part F) |

Commit Message

Simon Glass Jan. 20, 2026, 11:17 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a context parameter to the select_font() method so that font
selection can be done for a specific context rather than always using
the default.

The vidconsole_select_font() wrapper handles NULL by using the default
context, so callers can pass NULL if they don't have a specific context.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 boot/expo_test.c                    |  2 +-
 boot/scene.c                        |  4 ++--
 cmd/font.c                          |  4 ++--
 drivers/video/console_core.c        |  3 ++-
 drivers/video/console_truetype.c    |  6 +++---
 drivers/video/vidconsole-uclass.c   |  7 +++++--
 drivers/video/vidconsole_internal.h |  3 ++-
 include/video_console.h             |  8 ++++++--
 test/dm/video.c                     | 26 +++++++++++++-------------
 9 files changed, 36 insertions(+), 27 deletions(-)
  

Patch

diff --git a/boot/expo_test.c b/boot/expo_test.c
index 2d5ba7c0f4c..1de597ea603 100644
--- a/boot/expo_test.c
+++ b/boot/expo_test.c
@@ -135,7 +135,7 @@  int expo_test_render(struct expo *exp)
 		test->render_delta_us = get_timer_us(test->base_time_us);
 
 	/* Select 8x16 font for test display */
-	ret = vidconsole_select_font(exp->cons, "8x16", 0);
+	ret = vidconsole_select_font(exp->cons, NULL, "8x16", 0);
 	if (ret && ret != -ENOSYS)
 		return log_msg_ret("font", ret);
 
diff --git a/boot/scene.c b/boot/scene.c
index e504cb29d51..58be46cb7ee 100644
--- a/boot/scene.c
+++ b/boot/scene.c
@@ -647,10 +647,10 @@  static int scene_txt_render(struct expo *exp, struct udevice *dev,
 		return -ENOTSUPP;
 
 	if (gen->font_name || gen->font_size) {
-		ret = vidconsole_select_font(cons, gen->font_name,
+		ret = vidconsole_select_font(cons, NULL, gen->font_name,
 					     gen->font_size);
 	} else {
-		ret = vidconsole_select_font(cons, NULL, 0);
+		ret = vidconsole_select_font(cons, NULL, NULL, 0);
 	}
 	if (ret && ret != -ENOSYS)
 		return log_msg_ret("font", ret);
diff --git a/cmd/font.c b/cmd/font.c
index 79218779a2d..ca759df79a3 100644
--- a/cmd/font.c
+++ b/cmd/font.c
@@ -46,7 +46,7 @@  static int do_font_select(struct cmd_tbl *cmdtp, int flag, int argc,
 	name = argv[1];
 	if (argc == 3)
 		size = dectoul(argv[2], NULL);
-	ret = vidconsole_select_font(dev, name, size);
+	ret = vidconsole_select_font(dev, NULL, name, size);
 	if (ret) {
 		printf("Failed (error %d)\n", ret);
 		return CMD_RET_FAILURE;
@@ -75,7 +75,7 @@  static int do_font_size(struct cmd_tbl *cmdtp, int flag, int argc,
 	} else {
 		size = dectoul(argv[1], NULL);
 
-		ret = vidconsole_select_font(dev, font_name, size);
+		ret = vidconsole_select_font(dev, NULL, font_name, size);
 		if (ret) {
 			printf("Failed (error %d)\n", ret);
 			return CMD_RET_FAILURE;
diff --git a/drivers/video/console_core.c b/drivers/video/console_core.c
index ecb0b9dab89..5cb1b3fb2b7 100644
--- a/drivers/video/console_core.c
+++ b/drivers/video/console_core.c
@@ -369,7 +369,8 @@  int console_fixed_putc_xy(struct udevice *dev, void *vctx, uint x_frac, uint y,
 	return VID_TO_POS(fontdata->width);
 }
 
-int console_simple_select_font(struct udevice *dev, const char *name, uint size)
+int console_simple_select_font(struct udevice *dev, void *ctx, const char *name,
+			       uint size)
 {
 	struct video_fontdata *font;
 
diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c
index b3884bddd72..af880ad5597 100644
--- a/drivers/video/console_truetype.c
+++ b/drivers/video/console_truetype.c
@@ -973,10 +973,10 @@  static int get_metrics(struct udevice *dev, const char *name, uint size,
 	return 0;
 }
 
-static int truetype_select_font(struct udevice *dev, const char *name,
-				uint size)
+static int truetype_select_font(struct udevice *dev, void *vctx,
+				const char *name, uint size)
 {
-	struct console_tt_ctx *ctx = vidconsole_ctx(dev);
+	struct console_tt_ctx *ctx = vctx;
 	struct console_tt_metrics *met;
 	struct video_fontdata *fontdata;
 	int ret;
diff --git a/drivers/video/vidconsole-uclass.c b/drivers/video/vidconsole-uclass.c
index 6ef86abc4ce..c0eee7d75fd 100644
--- a/drivers/video/vidconsole-uclass.c
+++ b/drivers/video/vidconsole-uclass.c
@@ -647,14 +647,17 @@  int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep
 	return 0;
 }
 
-int vidconsole_select_font(struct udevice *dev, const char *name, uint size)
+int vidconsole_select_font(struct udevice *dev, void *ctx, const char *name,
+			   uint size)
 {
 	struct vidconsole_ops *ops = vidconsole_get_ops(dev);
 
+	if (!ctx)
+		ctx = vidconsole_ctx(dev);
 	if (!ops->select_font)
 		return -ENOSYS;
 
-	return ops->select_font(dev, name, size);
+	return ops->select_font(dev, ctx, name, size);
 }
 
 int vidconsole_measure(struct udevice *dev, const char *name, uint size,
diff --git a/drivers/video/vidconsole_internal.h b/drivers/video/vidconsole_internal.h
index 8b598b26b13..e1e89f6524a 100644
--- a/drivers/video/vidconsole_internal.h
+++ b/drivers/video/vidconsole_internal.h
@@ -181,7 +181,8 @@  int console_simple_get_font(struct udevice *dev, int seq, struct vidfont_info *i
  * Internal function to be used in as ops.
  * See details in video_console.h select_font function
  **/
-int console_simple_select_font(struct udevice *dev, const char *name, uint size);
+int console_simple_select_font(struct udevice *dev, void *ctx, const char *name,
+			       uint size);
 
 /**
  * Normal console putc_xy function that can be called by other console drivers
diff --git a/include/video_console.h b/include/video_console.h
index 297049cb851..c5fc0577670 100644
--- a/include/video_console.h
+++ b/include/video_console.h
@@ -334,11 +334,13 @@  struct vidconsole_ops {
 	 * select_font() - Select a particular font by name / size
 	 *
 	 * @dev:	Device to adjust
+	 * @ctx:	Context to use
 	 * @name:	Font name to use (NULL to use default)
 	 * @size:	Font size to use (0 to use default)
 	 * Returns: 0 on success, -ENOENT if no such font
 	 */
-	int (*select_font)(struct udevice *dev, const char *name, uint size);
+	int (*select_font)(struct udevice *dev, void *ctx, const char *name,
+			   uint size);
 
 	/**
 	 * measure() - Measure the bounding box of some text
@@ -487,10 +489,12 @@  int vidconsole_get_font(struct udevice *dev, int seq,
  * vidconsole_select_font() - Select a particular font by name / size
  *
  * @dev:	Device to adjust
+ * @ctx:	Context to use (NULL to use default)
  * @name:	Font name to use (NULL to use default)
  * @size:	Font size to use (0 to use default)
  */
-int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
+int vidconsole_select_font(struct udevice *dev, void *ctx, const char *name,
+			   uint size);
 
 /**
  * vidconsole_measure() - Measure the bounding box of some text
diff --git a/test/dm/video.c b/test/dm/video.c
index 7a58a64c0f9..b4a1200e481 100644
--- a/test/dm/video.c
+++ b/test/dm/video.c
@@ -259,7 +259,7 @@  static int dm_test_video_text(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	ut_asserteq(46, video_compress_fb(uts, dev, false));
 	ut_assertok(video_check_copy_fb(uts, dev));
 
@@ -301,7 +301,7 @@  static int dm_test_video_text_12x22(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "12x22", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "12x22", 0));
 	ut_asserteq(46, video_compress_fb(uts, dev, false));
 	ut_assertok(video_check_copy_fb(uts, dev));
 
@@ -342,7 +342,7 @@  static int dm_test_video_chars(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	vidconsole_put_string(con, NULL, test_string);
 	ut_asserteq(466, video_compress_fb(uts, dev, false));
 	ut_assertok(video_check_copy_fb(uts, dev));
@@ -361,7 +361,7 @@  static int dm_test_video_ansi(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 
 	/* reference clear: */
 	video_clear(con->parent);
@@ -414,7 +414,7 @@  static int check_vidconsole_output(struct unit_test_state *uts, int rot,
 
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	ut_asserteq(46, video_compress_fb(uts, dev, false));
 	ut_assertok(video_check_copy_fb(uts, dev));
 
@@ -1165,11 +1165,11 @@  static int dm_test_video_font_switch(struct unit_test_state *uts)
 	vidconsole_put_string(con, NULL, truetype_text);
 
 	/* Switch to bitmap font */
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	vidconsole_put_string(con, NULL, bitmap_text);
 
 	/* Switch back to TrueType font */
-	ut_assertok(vidconsole_select_font(con, NULL, 0));
+	ut_assertok(vidconsole_select_font(con, NULL, NULL, 0));
 	vidconsole_put_string(con, NULL, final_truetype_text);
 
 	ut_asserteq(14892, video_compress_fb(uts, dev, false));
@@ -1242,7 +1242,7 @@  static int dm_test_video_backspace_normal(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	ut_assertok(check_cursor_backspace(uts, dev, con, 16));
 
 	return 0;
@@ -1256,7 +1256,7 @@  static int dm_test_video_backspace_truetype(struct unit_test_state *uts)
 
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, NULL, 30));
+	ut_assertok(vidconsole_select_font(con, NULL, NULL, 30));
 	ut_assertok(check_cursor_backspace(uts, dev, con, 30));
 
 	return 0;
@@ -1271,7 +1271,7 @@  static int dm_test_video_cmd(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 
 	ut_assertok(run_command("setcurs 10 5", 0));
 
@@ -1412,7 +1412,7 @@  static int dm_test_video_sync_damage(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 	priv = dev_get_uclass_priv(dev);
 
 	/* Use manual sync to prevent interference with the test */
@@ -1561,7 +1561,7 @@  static int dm_test_video_entry_save(struct unit_test_state *uts)
 	ut_assertok(select_vidconsole(uts, "vidconsole0"));
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, "8x16", 0));
+	ut_assertok(vidconsole_select_font(con, NULL, "8x16", 0));
 
 	ut_assertok(check_entry_save(uts, con));
 
@@ -1576,7 +1576,7 @@  static int dm_test_video_entry_save_tt(struct unit_test_state *uts)
 
 	ut_assertok(video_get_nologo(uts, &dev));
 	ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con));
-	ut_assertok(vidconsole_select_font(con, NULL, 30));
+	ut_assertok(vidconsole_select_font(con, NULL, NULL, 30));
 
 	ut_assertok(check_entry_save(uts, con));