[Concept,12/27] sandbox: Allow grid size to be specified in 'sb grid' command

Message ID 20260119204130.3972647-13-sjg@u-boot.org
State New
Headers
Series Expo debugging and textedit improvements (part E) |

Commit Message

Simon Glass Jan. 19, 2026, 8:41 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add an optional second parameter to 'sb grid' to specify the grid size
in pixels. For example, 'sb grid 1 14' enables a 20-pixel (0x14) grid.
The default remains 10 pixels when no size is specified.

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

 arch/sandbox/cpu/sdl.c              | 11 +++++++----
 arch/sandbox/include/asm/sdl_sync.h |  2 ++
 arch/sandbox/include/asm/state.h    |  1 +
 cmd/sb.c                            |  6 ++++--
 doc/develop/expo.rst                |  4 ++++
 doc/usage/cmd/sb.rst                |  7 ++++---
 drivers/video/sandbox_sdl.c         |  1 +
 test/cmd/sb.c                       | 11 +++++++++++
 8 files changed, 34 insertions(+), 9 deletions(-)
  

Patch

diff --git a/arch/sandbox/cpu/sdl.c b/arch/sandbox/cpu/sdl.c
index 4a98d920c9f..f2cc4bbd041 100644
--- a/arch/sandbox/cpu/sdl.c
+++ b/arch/sandbox/cpu/sdl.c
@@ -311,16 +311,19 @@  static int copy_to_texture(void *lcd_base, const struct vid_bbox *damage)
 	return 0;
 }
 
-static void draw_grid(void)
+static void draw_grid(int size)
 {
 	int x, y;
 
+	if (!size)
+		size = 0x20;
+
 	SDL_SetRenderDrawColor(sdl.renderer, 192, 192, 192, SDL_ALPHA_OPAQUE);
 
-	for (x = 0; x < sdl.vis_width; x += 10)
+	for (x = 0; x < sdl.vis_width; x += size)
 		SDL_RenderDrawLine(sdl.renderer, x, 0, x, sdl.vis_height - 1);
 
-	for (y = 0; y < sdl.vis_height; y += 10)
+	for (y = 0; y < sdl.vis_height; y += size)
 		SDL_RenderDrawLine(sdl.renderer, 0, y, sdl.vis_width - 1, y);
 }
 
@@ -345,7 +348,7 @@  int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage,
 	}
 
 	if (opts && opts->draw_grid)
-		draw_grid();
+		draw_grid(opts->grid_size);
 
 	/*
 	 * On some machines this does not appear. Draw an empty rectangle which
diff --git a/arch/sandbox/include/asm/sdl_sync.h b/arch/sandbox/include/asm/sdl_sync.h
index 78f4233e056..81b28ff61e5 100644
--- a/arch/sandbox/include/asm/sdl_sync.h
+++ b/arch/sandbox/include/asm/sdl_sync.h
@@ -13,9 +13,11 @@ 
  * struct sandbox_sdl_sync_opts - Options for sandbox_sdl_sync()
  *
  * @draw_grid: Draw a grid overlay on the display
+ * @grid_size: Grid size in pixels (0 for default of 0x20)
  */
 struct sandbox_sdl_sync_opts {
 	bool draw_grid;
+	int grid_size;
 };
 
 #endif
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 31766a6e7ea..001d780aec8 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -180,6 +180,7 @@  struct sandbox_state {
 	bool quiet_vidconsole;		/* Don't use vidconsole for stdout */
 	bool disable_mcheck;		/* Disable mcheck heap protection */
 	bool show_grid;			/* Show grid overlay on video */
+	int grid_size;			/* Grid size in pixels (0 for default) */
 	int video_test;			/* ms to wait before next assert */
 	const char *video_frames_dir;	/* Directory to write video frames */
 	int video_frame_count;		/* Number of frames written */
diff --git a/cmd/sb.c b/cmd/sb.c
index 55a4b5b447f..dcf7ec9d0b4 100644
--- a/cmd/sb.c
+++ b/cmd/sb.c
@@ -106,6 +106,8 @@  static int do_sb_grid(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	state->show_grid = hextoul(argv[1], NULL);
+	if (argc >= 3)
+		state->grid_size = hextoul(argv[2], NULL);
 
 	return 0;
 }
@@ -160,7 +162,7 @@  static int do_sb_devoff(struct cmd_tbl *cmdtp, int flag, int argc,
 U_BOOT_LONGHELP(sb,
 	"devoff <node>  - Disable device from device tree node\n"
 	"sb devon <node>   - Enable device from device tree node\n"
-	"sb grid <0|1>     - Enable/disable grid overlay on video\n"
+	"sb grid <0|1> [<size>] - Enable/disable grid overlay on video\n"
 	"sb handoff        - Show handoff data received from SPL\n"
 	"sb map            - Show mapped memory\n"
 	"sb state          - Show sandbox state");
@@ -168,7 +170,7 @@  U_BOOT_LONGHELP(sb,
 U_BOOT_CMD_WITH_SUBCMDS(sb, "Sandbox status commands", sb_help_text,
 	U_BOOT_SUBCMD_MKENT(devoff, 2, 1, do_sb_devoff),
 	U_BOOT_SUBCMD_MKENT(devon, 2, 1, do_sb_devon),
-	U_BOOT_SUBCMD_MKENT(grid, 2, 1, do_sb_grid),
+	U_BOOT_SUBCMD_MKENT(grid, 3, 1, do_sb_grid),
 	U_BOOT_SUBCMD_MKENT(handoff, 1, 1, do_sb_handoff),
 	U_BOOT_SUBCMD_MKENT(map, 1, 1, do_sb_map),
 	U_BOOT_SUBCMD_MKENT(state, 1, 1, do_sb_state));
diff --git a/doc/develop/expo.rst b/doc/develop/expo.rst
index ca83b403621..fc642ed3696 100644
--- a/doc/develop/expo.rst
+++ b/doc/develop/expo.rst
@@ -797,6 +797,10 @@  For example, to watch an expo test render with a visible display::
 
     ./u-boot -T -l -V 500 --video_frames /tmp/good -c "ut bootstd expo_render_image"
 
+The :doc:`../usage/cmd/sb` ``grid`` subcommand can be used to overlay a grid on
+the display, to help with checking alignment of objects. The grid size defaults
+to 0x20 pixels but can be specified as a parameter.
+
 This will write each asserted expo frame to ``/tmp/good/frame0.bmp``,
 ``/tmp/good/frame1.bmp``, etc.
 
diff --git a/doc/usage/cmd/sb.rst b/doc/usage/cmd/sb.rst
index ee72ecd0db9..08f8f87f88f 100644
--- a/doc/usage/cmd/sb.rst
+++ b/doc/usage/cmd/sb.rst
@@ -13,7 +13,7 @@  Synopsis
 
     sb devoff <node>
     sb devon <node>
-    sb grid <0|1>
+    sb grid <0|1> [<size>]
     sb handoff
     sb map
     sb state
@@ -45,8 +45,9 @@  sb grid
 ~~~~~~~
 
 This enables or disables a grid overlay on the video display. When enabled,
-a 10-pixel grid is drawn over the display, which is useful for debugging UI
-layout and alignment. Use ``sb grid 1`` to enable and ``sb grid 0`` to disable.
+a grid is drawn over the display, which is useful for debugging UI layout and
+alignment. Use ``sb grid 1`` to enable and ``sb grid 0`` to disable. An
+optional second parameter specifies the grid size in pixels (default 0x20).
 
 sb handoff
 ~~~~~~~~~~
diff --git a/drivers/video/sandbox_sdl.c b/drivers/video/sandbox_sdl.c
index 4785185dd70..6fc71bc9cb9 100644
--- a/drivers/video/sandbox_sdl.c
+++ b/drivers/video/sandbox_sdl.c
@@ -149,6 +149,7 @@  static int sandbox_sdl_video_sync(struct udevice *vid, uint flags)
 		       sizeof(plat->last_sync_damage));
 
 	opts.draw_grid = state->show_grid;
+	opts.grid_size = state->grid_size;
 
 	return sandbox_sdl_sync(uc_priv->fb, damage, &opts);
 }
diff --git a/test/cmd/sb.c b/test/cmd/sb.c
index 4ce8a8c4215..b1fbddac449 100644
--- a/test/cmd/sb.c
+++ b/test/cmd/sb.c
@@ -130,6 +130,7 @@  static int dm_test_sb_grid(struct unit_test_state *uts)
 
 	/* Ensure grid is initially off */
 	state->show_grid = false;
+	state->grid_size = 0;
 
 	/* Enable grid */
 	ut_assertok(run_command("sb grid 1", 0));
@@ -141,6 +142,16 @@  static int dm_test_sb_grid(struct unit_test_state *uts)
 	ut_assert_console_end();
 	ut_asserteq(false, state->show_grid);
 
+	/* Enable grid with custom size (0x14 = 20 decimal) */
+	ut_assertok(run_command("sb grid 1 14", 0));
+	ut_assert_console_end();
+	ut_asserteq(true, state->show_grid);
+	ut_asserteq(0x14, state->grid_size);
+
+	/* Clean up */
+	state->show_grid = false;
+	state->grid_size = 0;
+
 	return 0;
 }
 DM_TEST(dm_test_sb_grid, UTF_CONSOLE);