From 73ea756a08dfaeabda30158cb8a64dc2bcbcd8dc Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Mon, 4 Aug 2025 02:08:03 -0400 Subject: libgfx: draw: Add gfx_copy_region() This commit introduces a new gfx_copy_region() function that allows a rectangular region of the screen to be defined and its contents copied to a different part of the screen. Signed-off-by: Ian Moffett --- lib/libgfx/include/libgfx/draw.h | 16 ++++++++++++++++ lib/libgfx/src/draw.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib/libgfx/include/libgfx/draw.h b/lib/libgfx/include/libgfx/draw.h index 96fe35a..8efd986 100644 --- a/lib/libgfx/include/libgfx/draw.h +++ b/lib/libgfx/include/libgfx/draw.h @@ -101,8 +101,24 @@ struct gfx_point { color_t rgb; }; +/* + * Represents a rectangular region on + * the screen. + * + * @x,y: Position of this region on the screen + * @width: Region width + * @heght: Region height + */ +struct gfx_region { + scrpos_t x, y; + dimm_t width; + dimm_t height; +}; + int gfx_draw_shape(struct gfx_ctx *ctx, const struct gfx_shape *shape); int gfx_plot_point(struct gfx_ctx *ctx, const struct gfx_point *point); + +int gfx_copy_region(struct gfx_ctx *ctx, struct gfx_region *r, scrpos_t x, scrpos_t y); color_t gfx_get_pix(struct gfx_ctx *ctx, uint32_t x, uint32_t y); __always_inline static inline size_t diff --git a/lib/libgfx/src/draw.c b/lib/libgfx/src/draw.c index 784110a..43e0f08 100644 --- a/lib/libgfx/src/draw.c +++ b/lib/libgfx/src/draw.c @@ -182,3 +182,43 @@ gfx_draw_shape(struct gfx_ctx *ctx, const struct gfx_shape *shape) return -1; } + +/* + * Copy a region on one part of a screen to + * another part of a screen. + * + * @ctx: Graphics context pointer + * @r: Region to copy + * @x: X position for copy dest + * @y: Y position for copy dest + */ +int +gfx_copy_region(struct gfx_ctx *ctx, struct gfx_region *r, scrpos_t x, scrpos_t y) +{ + struct gfx_point point; + color_t pixel; + scrpos_t src_cx, src_cy; + dimm_t w, h; + + if (ctx == NULL || r == NULL) { + return -EINVAL; + } + + w = r->width; + h = r->height; + + for (int xoff = 0; xoff < w; ++xoff) { + for (int yoff = 0; yoff < h; ++yoff) { + /* Source position */ + src_cx = r->x + xoff; + src_cy = r->y + yoff; + + /* Plot the new pixel */ + pixel = gfx_get_pix(ctx, src_cx, src_cy); + point.x = x + xoff; + point.y = y + yoff; + point.rgb = pixel; + gfx_plot_point(ctx, &point); + } + } +} -- cgit v1.2.3