[Concept,10/27] sandbox: Add grid-drawing option to sandbox_sdl_sync()

Message ID 20260119204130.3972647-11-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 struct parameter to sandbox_sdl_sync() to control sync
behaviour. Initially this supports drawing a 10-pixel grid overlay on the
display, useful for debugging UI layout.

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

 arch/sandbox/cpu/sdl.c              | 20 +++++++++++++++++++-
 arch/sandbox/include/asm/sdl.h      |  8 ++++++--
 arch/sandbox/include/asm/sdl_sync.h | 21 +++++++++++++++++++++
 drivers/video/sandbox_sdl.c         |  2 +-
 4 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 arch/sandbox/include/asm/sdl_sync.h
  

Patch

diff --git a/arch/sandbox/cpu/sdl.c b/arch/sandbox/cpu/sdl.c
index dd2589c64e2..4a98d920c9f 100644
--- a/arch/sandbox/cpu/sdl.c
+++ b/arch/sandbox/cpu/sdl.c
@@ -11,6 +11,7 @@ 
 #include <sysreset.h>
 #include <linux/input.h>
 #include <SDL2/SDL.h>
+#include <asm/sdl_sync.h>
 #include <asm/state.h>
 #include <video_defs.h>
 
@@ -310,7 +311,21 @@  static int copy_to_texture(void *lcd_base, const struct vid_bbox *damage)
 	return 0;
 }
 
-int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage)
+static void draw_grid(void)
+{
+	int x, y;
+
+	SDL_SetRenderDrawColor(sdl.renderer, 192, 192, 192, SDL_ALPHA_OPAQUE);
+
+	for (x = 0; x < sdl.vis_width; x += 10)
+		SDL_RenderDrawLine(sdl.renderer, x, 0, x, sdl.vis_height - 1);
+
+	for (y = 0; y < sdl.vis_height; y += 10)
+		SDL_RenderDrawLine(sdl.renderer, 0, y, sdl.vis_width - 1, y);
+}
+
+int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage,
+		     const struct sandbox_sdl_sync_opts *opts)
 {
 	struct SDL_Rect rect;
 	int ret;
@@ -329,6 +344,9 @@  int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage)
 		return -EIO;
 	}
 
+	if (opts && opts->draw_grid)
+		draw_grid();
+
 	/*
 	 * On some machines this does not appear. Draw an empty rectangle which
 	 * seems to fix that.
diff --git a/arch/sandbox/include/asm/sdl.h b/arch/sandbox/include/asm/sdl.h
index a80db51ad19..b2965a0a700 100644
--- a/arch/sandbox/include/asm/sdl.h
+++ b/arch/sandbox/include/asm/sdl.h
@@ -8,6 +8,7 @@ 
 
 #include <errno.h>
 #include <video.h>
+#include <asm/sdl_sync.h>
 
 struct mouse_event;
 struct vid_bbox;
@@ -44,9 +45,11 @@  int sandbox_sdl_remove_display(void);
  *
  * @lcd_base: Base of frame buffer
  * @damage: Optional damage rectangle to limit the update region (may be NULL)
+ * @opts: Optional sync options (may be NULL)
  * Return: 0 if screen was updated, -ENODEV is there is no screen.
  */
-int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage);
+int sandbox_sdl_sync(void *lcd_base, const struct vid_bbox *damage,
+		     const struct sandbox_sdl_sync_opts *opts);
 
 /**
  * sandbox_sdl_scan_keys() - scan for pressed keys
@@ -139,7 +142,8 @@  static inline int sandbox_sdl_remove_display(void)
 }
 
 static inline int sandbox_sdl_sync(void *lcd_base,
-				   const struct vid_bbox *damage)
+				   const struct vid_bbox *damage,
+				   const struct sandbox_sdl_sync_opts *opts)
 {
 	return -ENODEV;
 }
diff --git a/arch/sandbox/include/asm/sdl_sync.h b/arch/sandbox/include/asm/sdl_sync.h
new file mode 100644
index 00000000000..78f4233e056
--- /dev/null
+++ b/arch/sandbox/include/asm/sdl_sync.h
@@ -0,0 +1,21 @@ 
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright 2026 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#ifndef __SANDBOX_SDL_SYNC_H
+#define __SANDBOX_SDL_SYNC_H
+
+#include <stdbool.h>
+
+/**
+ * struct sandbox_sdl_sync_opts - Options for sandbox_sdl_sync()
+ *
+ * @draw_grid: Draw a grid overlay on the display
+ */
+struct sandbox_sdl_sync_opts {
+	bool draw_grid;
+};
+
+#endif
diff --git a/drivers/video/sandbox_sdl.c b/drivers/video/sandbox_sdl.c
index 7954e266d98..2e2ebe14f84 100644
--- a/drivers/video/sandbox_sdl.c
+++ b/drivers/video/sandbox_sdl.c
@@ -146,7 +146,7 @@  static int sandbox_sdl_video_sync(struct udevice *vid, uint flags)
 		memset(&plat->last_sync_damage, '\0',
 		       sizeof(plat->last_sync_damage));
 
-	return sandbox_sdl_sync(uc_priv->fb, damage);
+	return sandbox_sdl_sync(uc_priv->fb, damage, NULL);
 }
 
 static const struct video_ops sandbox_sdl_ops = {