ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_gfx.h File Reference

Software 2D graphics primitives layered on top of a caller-ownedframebuffer (DRW / D/AVE 2D / GLCDC ready). More...

#include <stdint.h>
#include "ra8_err.h"
#include "ra8_gfx_font.h"
Include dependency graph for ra8_gfx.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  ra8_gfx_format_t : uint8_t {
  k_ra8_gfx_format_rgb565 = 2 ,
  k_ra8_gfx_format_rgb888 = 3 ,
  k_ra8_gfx_format_argb8888 = 4
}
 Pixel format of the framebuffer bound by ra8_gfx_init(). More...
enum  ra8_gfx_dim_limits_t : uint16_t {
  k_ra8_gfx_min_dim = 1 ,
  k_ra8_gfx_max_dim = 4096
}
 Bounds on framebuffer dimensions accepted by ra8_gfx_init(). More...

Functions

ra8_err_t ra8_gfx_init (void *fb, uint16_t width, uint16_t height, ra8_gfx_format_t format)
 Bind ra8_gfx to a caller-owned framebuffer.
ra8_err_t ra8_gfx_clear (uint32_t color)
 Fill the bound framebuffer's clip region with a single colour.
ra8_err_t ra8_gfx_set_clip (int32_t x, int32_t y, int32_t w, int32_t h)
 Restrict all subsequent drawing to a rectangle (dirty-region updates).
ra8_err_t ra8_gfx_reset_clip (void)
 Reset the clip rectangle to the whole framebuffer.
ra8_err_t ra8_gfx_pixel (int32_t x, int32_t y, uint32_t color)
 Set a single pixel.
ra8_err_t ra8_gfx_blit_gray8 (const uint8_t *src, int32_t w, int32_t h, int32_t dst_x, int32_t dst_y)
 Blit an 8-bit grayscale image to a framebuffer rectangle.
ra8_err_t ra8_gfx_blit_gray4_zoom (const uint8_t *src, int32_t src_w, int32_t src_h, int32_t sx, int32_t sy, int32_t sw, int32_t sh, int32_t zoom, int32_t dst_x, int32_t dst_y)
 Nearest-neighbour integer-zoom blit of a sub-rectangle of a packed 4-bit grayscale image into the framebuffer (no scale-to-fit).
ra8_err_t ra8_gfx_line (int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color)
 Draw a line from (x0,y0) to (x1,y1) with Bresenham's algorithm.
ra8_err_t ra8_gfx_rect (int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color, bool filled)
 Draw an axis-aligned rectangle.
ra8_err_t ra8_gfx_circle (int32_t cx, int32_t cy, int32_t r, uint32_t color, bool filled)
 Draw a circle using midpoint algorithm.
ra8_err_t ra8_gfx_text_out (int32_t x, int32_t y, const char *str, const ra8_gfx_font_t *font, uint32_t fg_color, uint32_t bg_color)
 Render a NUL-terminated ASCII string.
ra8_err_t ra8_gfx_text_size (const char *str, const ra8_gfx_font_t *font, uint32_t *out_w, uint32_t *out_h)
 Compute rendered pixel dimensions of a string.
ra8_err_t ra8_gfx_blit (const void *src_buf, uint16_t src_w, uint16_t src_h, ra8_gfx_format_t src_format, int32_t dst_x, int32_t dst_y)
 Copy a sub-image from src_buf into the framebuffer at (dst_x,dst_y).

Detailed Description

Software 2D graphics primitives layered on top of a caller-owned

framebuffer (DRW / D/AVE 2D / GLCDC ready).

This module is a small, dependency-free 2D graphics library aimed at the RA8D2's GLCDC + DRW (D/AVE 2D) accelerator. The current implementation is a portable C software pixel pusher; if RA8_GFX_USE_DRW is defined a future revision can route ra8_gfx_rect / ra8_gfx_blit through ra8_drw's hardware blitter without changing call sites.

The framebuffer memory itself is owned by the caller – ra8_gfx_init only remembers a pointer and metadata, so the same library can be used over the GLCDC display plane, an off-screen scratch buffer, or a host-side test buffer.

Since
0.1.0

Definition in file ra8_gfx.h.

Enumeration Type Documentation

◆ ra8_gfx_dim_limits_t

enum ra8_gfx_dim_limits_t : uint16_t

Bounds on framebuffer dimensions accepted by ra8_gfx_init().

Enumerator
k_ra8_gfx_min_dim 

Minimum width or height in pixels.

k_ra8_gfx_max_dim 

Maximum supported edge length.

Definition at line 49 of file ra8_gfx.h.

◆ ra8_gfx_format_t

enum ra8_gfx_format_t : uint8_t

Pixel format of the framebuffer bound by ra8_gfx_init().

Values double as the bytes-per-pixel - the low byte is the byte stride for one pixel; the high byte is just a unique tag.

Enumerator
k_ra8_gfx_format_rgb565 

16-bit RGB565, little-endian in memory.

k_ra8_gfx_format_rgb888 

24-bit packed R,G,B bytes.

k_ra8_gfx_format_argb8888 

32-bit ARGB, A in MSB.

Definition at line 39 of file ra8_gfx.h.

Function Documentation

◆ ra8_gfx_blit()

ra8_err_t ra8_gfx_blit ( const void * src_buf,
uint16_t src_w,
uint16_t src_h,
ra8_gfx_format_t src_format,
int32_t dst_x,
int32_t dst_y )
nodiscard

Copy a sub-image from src_buf into the framebuffer at (dst_x,dst_y).

Parameters
[in]src_bufSource image bytes.
[in]src_w,src_hSource size.
[in]src_formatPixel format of the source image.
[in]dst_x,dst_yDestination top-left in the framebuffer.
Returns
Error code.
Return values
k_ra8_okBlitted (clipped if needed).
k_ra8_err_null_ptrsrc_buf was NULL.
k_ra8_err_not_initializedra8_gfx_init() was not called.
k_ra8_err_invalid_argsrc_w or src_h was zero or src_format invalid.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Destination rectangle (clipped to FB) holds the converted pixels.
Since
0.1.0

Definition at line 928 of file ra8_gfx_text.c.

References g_gfx_text_state, internal_bpp(), internal_format_ok(), internal_get_pixel(), k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_err_null_ptr, k_ra8_ok, and priv_gfx_text_plot().

Referenced by ls_blit().

◆ ra8_gfx_blit_gray4_zoom()

ra8_err_t ra8_gfx_blit_gray4_zoom ( const uint8_t * src,
int32_t src_w,
int32_t src_h,
int32_t sx,
int32_t sy,
int32_t sw,
int32_t sh,
int32_t zoom,
int32_t dst_x,
int32_t dst_y )
nodiscard

Nearest-neighbour integer-zoom blit of a sub-rectangle of a packed 4-bit grayscale image into the framebuffer (no scale-to-fit).

Samples the source sub-rectangle [sx, sx + sw) x [sy, sy + sh) of a packed gray4 image and writes every sampled source pixel as a zoom x zoom nearest-neighbour block, so the destination spans sw * zoom by sh * zoom pixels (before clipping). Unlike ra8_gfx_blit() or the reflow engine's scale-to-fit path, no source pixels are decimated – this is the 1:1 (zoom == 1) or magnified (zoom >= 2) view a reader loupe uses to inspect a full-resolution page window without resolution loss.

The source is packed two pixels per byte at flat nibble index y * src_w + x: an even flat index occupies the high nibble, an odd flat index the low nibble. Each 4-bit sample n is expanded to the 8-bit gray (n << 4) | n, then to (g << 16) | (g << 8) | g before down-conversion to the bound pixel format – byte-identical to ra8_gfx_blit_gray8()'s expansion of the same gray level.

The sampled window is clamped to the source image bounds [0, src_w) x [0, src_h): a sub-rectangle that runs off an image edge draws only the in-image portion at its natural destination offset (the off-image remainder is left untouched). Destination pixels are additionally confined to the active clip rectangle, so a lens window near a panel edge is clipped, never wrapped.

Parameters
[in]srcPacked gray4 source image, >= (src_w * src_h + 1) / 2 bytes.
[in]src_wSource image width in pixels (> 0); also the nibble stride.
[in]src_hSource image height in pixels (> 0).
[in]sxSub-rectangle left in source pixels (may be negative).
[in]sySub-rectangle top in source pixels (may be negative).
[in]swSub-rectangle width in source pixels (<= 0 draws nothing).
[in]shSub-rectangle height in source pixels (<= 0 draws nothing).
[in]zoomInteger magnification factor (>= 1; <= 0 rejected).
[in]dst_xDestination column of the sub-rectangle's top-left.
[in]dst_yDestination row of the sub-rectangle's top-left.
Returns
Error code.
Return values
k_ra8_okVisible pixels written (or fully clipped out).
k_ra8_err_not_initializedra8_gfx_init() was not called.
k_ra8_err_invalid_argsrc is NULL, src_w / src_h <= 0, or zoom <= 0.
Precondition
ra8_gfx_init() returned k_ra8_ok.
src holds at least (src_w * src_h + 1) / 2 readable bytes.
Postcondition
Each in-clip, in-image destination pixel equals its zoomed gray sample.
Pixels outside the clip rectangle or off the source image are unchanged.
Note
Not thread-safe; shares the single ra8_gfx bind state.
See also
ra8_gfx_blit_gray8 1:1 gray8 blit with no zoom or sub-rect.
ra8_gfx_set_clip Confine the lens blit to its window.
Since
0.1.0

Definition at line 104 of file ra8_gfx_blit_gray4.c.

References g_gfx_text_state, internal_gray4_block(), internal_gray4_color(), k_ra8_err_invalid_arg, k_ra8_err_not_initialized, and k_ra8_ok.

Referenced by internal_emit_strip(), and sh_image_loupe().

◆ ra8_gfx_blit_gray8()

ra8_err_t ra8_gfx_blit_gray8 ( const uint8_t * src,
int32_t w,
int32_t h,
int32_t dst_x,
int32_t dst_y )
nodiscard

Blit an 8-bit grayscale image to a framebuffer rectangle.

Writes the w x h block of 8-bit gray samples at src into the bound framebuffer with its top-left at (dst_x, dst_y), expanding each sample g to the colour (g<<16)|(g<<8)|g and down-converting to the panel format. The clip rectangle and framebuffer bounds are resolved ONCE for the whole block (not per pixel), so a full image is a tight row loop – the same pixels a per-pixel ra8_gfx_pixel() loop would write, far fewer instructions. Rows are sampled left-to-right, top-to-bottom; src is row-major with stride w.

Parameters
[in]srcRow-major 8-bit gray buffer of at least w * h bytes.
[in]wSource width in pixels (> 0).
[in]hSource height in pixels (> 0).
[in]dst_xDestination column of the block's left edge.
[in]dst_yDestination row of the block's top edge.
Returns
Error code.
Return values
k_ra8_okVisible pixels written (or fully clipped out).
k_ra8_err_not_initializedra8_gfx_init() was not called.
k_ra8_err_invalid_argsrc is NULL, or w / h <= 0.
Precondition
ra8_gfx_init() returned k_ra8_ok.
src holds at least w * h bytes.
Postcondition
Each in-clip destination pixel equals its down-converted gray sample.
Pixels outside the clip rectangle are left unchanged.
Note
Not thread-safe; shares the single ra8_gfx bind state.
Since
0.1.0

Definition at line 758 of file ra8_gfx_text.c.

References g_gfx_text_state, internal_blit_gray8_565(), internal_blit_gray8_slow(), k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_gfx_format_rgb565, and k_ra8_ok.

Referenced by erb_render_image(), sh_image_blit_cover(), and sh_image_blit_gray8().

◆ ra8_gfx_circle()

ra8_err_t ra8_gfx_circle ( int32_t cx,
int32_t cy,
int32_t r,
uint32_t color,
bool filled )
nodiscard

Draw a circle using midpoint algorithm.

Parameters
[in]cx,cyCentre.
[in]rRadius in pixels (>= 0).
[in]color32-bit colour.
[in]filledtrue for solid disc, false for 1-pixel outline.
Returns
Error code.
Return values
k_ra8_okDrawn.
k_ra8_err_not_initializedra8_gfx_init() was not called.
k_ra8_err_invalid_argr < 0.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Pixels on / inside the circle within bounds are updated.
Since
0.1.0

Definition at line 894 of file ra8_gfx_text.c.

References g_gfx_text_state, internal_circle_filled_step(), internal_circle_outline_step(), k_ra8_err_invalid_arg, k_ra8_err_not_initialized, k_ra8_gfx_max_dim, and k_ra8_ok.

Referenced by er_round_rect(), and priv_ra8_svgp_draw_circle().

◆ ra8_gfx_clear()

ra8_err_t ra8_gfx_clear ( uint32_t color)
nodiscard

Fill the bound framebuffer's clip region with a single colour.

Parameters
[in]color32-bit colour in 0x00RRGGBB or 0xAARRGGBB form.
Returns
Error code.
Return values
k_ra8_okCleared.
k_ra8_err_not_initializedra8_gfx_init() was not called.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Every pixel of the active clip region equals color (down-converted as needed to the active pixel format). With the default (full-framebuffer) clip this is the whole framebuffer.
Note
Honours the clip rectangle set by ra8_gfx_set_clip(); the default clip is the whole framebuffer, so unclipped callers see no change.
See also
ra8_gfx_set_clip
Since
0.1.0

Definition at line 549 of file ra8_gfx_text.c.

References g_gfx_text_state, internal_fill_rect_565(), internal_put_pixel(), k_ra8_err_not_initialized, k_ra8_gfx_format_rgb565, and k_ra8_ok.

Referenced by ch_render(), cm_render(), ef_decode_and_hash(), er_render_keyboard(), er_render_library(), er_render_reading(), erb_render_all(), erb_render_image(), internal_decode(), internal_rc_render_all(), ls_redraw(), main(), mg_reader_render(), render_page(), sfr_bringup_panel(), sfr_panic_halt(), sh_comic_probe_blob(), sh_comic_render(), sh_cover_render(), sh_epub_thumb(), sh_loading_overlay(), sh_reader_render(), sh_shelf_render(), sh_toc_render(), wa_app_render(), wd_app_render(), and wk_check_full().

◆ ra8_gfx_init()

ra8_err_t ra8_gfx_init ( void * fb,
uint16_t width,
uint16_t height,
ra8_gfx_format_t format )
nodiscard

Bind ra8_gfx to a caller-owned framebuffer.

Parameters
[in]fbPointer to framebuffer memory.
[in]widthFramebuffer width in pixels.
[in]heightFramebuffer height in pixels.
[in]formatPixel format (see ra8_gfx_format_t).
Returns
Error code.
Return values
k_ra8_okBound successfully.
k_ra8_err_null_ptrfb was NULL.
k_ra8_err_invalid_argDimensions out of range or unsupported format.
Precondition
fb points to at least width * height * bytes_per_pixel(format) bytes.
width, height in [1, 4096].
Postcondition
Subsequent draw calls operate on the bound buffer.
On error, no global state is changed.
Note
Not thread-safe; bind once during init.
Since
0.1.0

Definition at line 522 of file ra8_gfx_text.c.

References g_gfx_text_state, internal_bpp(), internal_format_ok(), k_ra8_err_invalid_arg, k_ra8_err_null_ptr, k_ra8_gfx_max_dim, k_ra8_gfx_min_dim, and k_ra8_ok.

Referenced by app_bringup_gfx(), cm_bringup_panel(), ez_bringup_panel(), main(), mg_bringup_panel(), priv_viewer_render_comic(), priv_viewer_tile_comic(), render_page(), sfr_bringup_panel(), sh_comic_probe_blob(), sh_comic_selfcheck(), sh_epub_thumb(), sh_panel_or_halt(), wc_panel_up(), wd_panel_up(), and wk_panel_up().

◆ ra8_gfx_line()

ra8_err_t ra8_gfx_line ( int32_t x0,
int32_t y0,
int32_t x1,
int32_t y1,
uint32_t color )
nodiscard

Draw a line from (x0,y0) to (x1,y1) with Bresenham's algorithm.

Parameters
[in]x0,y0,x1,y1Endpoints (clipped to framebuffer).
[in]color32-bit colour.
Returns
Error code.
Return values
k_ra8_okLine drawn (after clipping).
k_ra8_err_not_initializedra8_gfx_init() was not called.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Pixels on the rasterised line within bounds equal color.
Since
0.1.0

Definition at line 785 of file ra8_gfx_text.c.

References g_gfx_text_state, k_ra8_err_not_initialized, k_ra8_gfx_max_dim, k_ra8_ok, and priv_gfx_text_plot().

Referenced by ch_border(), ch_fill(), er_icon_delete(), er_icon_return(), er_tri_up(), priv_ra8_svgp_draw_line(), priv_ra8_svgp_draw_polyline(), and priv_ra8_svgp_fill_poly().

◆ ra8_gfx_pixel()

ra8_err_t ra8_gfx_pixel ( int32_t x,
int32_t y,
uint32_t color )
nodiscard

Set a single pixel.

Parameters
[in]xColumn, 0 = left.
[in]yRow, 0 = top.
[in]color32-bit colour.
Returns
Error code.
Return values
k_ra8_okPixel written.
k_ra8_err_not_initializedra8_gfx_init() was not called.
k_ra8_err_range_check_failed(x,y) outside framebuffer.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
On success the addressed pixel equals the down-converted colour.
Since
0.1.0

Definition at line 628 of file ra8_gfx_text.c.

References g_gfx_text_state, k_ra8_err_not_initialized, k_ra8_err_range_check_failed, k_ra8_ok, and priv_gfx_text_plot().

Referenced by internal_blit_alpha_mask(), internal_blit_scaled(), internal_draw_underline(), and priv_ra8_svgp_fill_poly_grad().

◆ ra8_gfx_rect()

ra8_err_t ra8_gfx_rect ( int32_t x,
int32_t y,
int32_t w,
int32_t h,
uint32_t color,
bool filled )
nodiscard

◆ ra8_gfx_reset_clip()

ra8_err_t ra8_gfx_reset_clip ( void )
nodiscard

Reset the clip rectangle to the whole framebuffer.

Returns
Error code.
Return values
k_ra8_okClip reset to the full framebuffer.
k_ra8_err_not_initializedra8_gfx_init() was not called.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Subsequent draws may touch any framebuffer pixel.
Note
Not thread-safe; the clip is module-global state.
See also
ra8_gfx_set_clip
Since
0.1.0

Definition at line 616 of file ra8_gfx_text.c.

References g_gfx_text_state, k_ra8_err_not_initialized, and k_ra8_ok.

Referenced by er_render_nag_region(), ls_redraw(), and sh_image_loupe().

◆ ra8_gfx_set_clip()

ra8_err_t ra8_gfx_set_clip ( int32_t x,
int32_t y,
int32_t w,
int32_t h )
nodiscard

Restrict all subsequent drawing to a rectangle (dirty-region updates).

Every draw call (clear, rect, line, pixel, text, blit) writes only pixels inside the intersection of this rectangle and the framebuffer; pixels outside are left untouched. This is the primitive for incremental / dirty-region repaints – set the clip to the damaged area, redraw, then ra8_gfx_reset_clip() – so a small change (e.g. an overlay banner) does not repaint the whole panel. The clip persists until changed or reset; ra8_gfx_init() and ra8_gfx_reset_clip() set it to the full framebuffer.

Parameters
[in]xClip left in pixels (clamped to the framebuffer).
[in]yClip top in pixels (clamped to the framebuffer).
[in]wClip width in pixels (a non-positive width yields an empty clip).
[in]hClip height in pixels (a non-positive height yields an empty clip).
Returns
Error code.
Return values
k_ra8_okClip set (possibly empty).
k_ra8_err_not_initializedra8_gfx_init() was not called.
Precondition
ra8_gfx_init() returned k_ra8_ok.
Postcondition
Subsequent draws are confined to the clamped rectangle.
An off-screen or zero-area request leaves an empty clip (draws are no-ops).
Note
Not thread-safe; the clip is module-global state.
See also
ra8_gfx_reset_clip
Since
0.1.0

Definition at line 580 of file ra8_gfx_text.c.

References g_gfx_text_state, k_ra8_err_not_initialized, and k_ra8_ok.

Referenced by er_render_nag_region(), and sh_image_loupe().

◆ ra8_gfx_text_out()

ra8_err_t ra8_gfx_text_out ( int32_t x,
int32_t y,
const char * str,
const ra8_gfx_font_t * font,
uint32_t fg_color,
uint32_t bg_color )
nodiscard

Render a NUL-terminated ASCII string.

Parameters
[in]x,yTop-left of the first glyph.
[in]strNUL-terminated ASCII string (NULL not allowed).
[in]fontFont descriptor (NULL not allowed).
[in]fg_colorForeground colour.
[in]bg_colorBackground colour.
Returns
Error code.
Return values
k_ra8_okRendered.
k_ra8_err_null_ptrstr or font was NULL.
k_ra8_err_not_initializedra8_gfx_init() was not called.
Precondition
ra8_gfx_init() returned k_ra8_ok.
font->glyph_data covers at least last-first+1 glyphs.
Postcondition
Glyph cells within bounds are filled with fg/bg colour pairs.
Since
0.1.0

Definition at line 182 of file ra8_gfx_text_glyph.c.

References g_gfx_text_state, ra8_gfx_font_t::glyph_width, internal_render_glyph(), k_ra8_err_not_initialized, k_ra8_err_null_ptr, k_ra8_gfx_max_dim, and k_ra8_ok.

Referenced by ch_render(), cm_draw_status_bar(), er_draw_key_label(), er_nag_render(), er_text_left(), ls_text(), mg_render_status(), render_page(), sh_comic_render(), sh_cover_button(), sh_cover_render(), sh_draw_card(), sh_loading_overlay(), sh_reader_render(), sh_titlebar(), sh_toc_render(), wc_footer_render(), wc_status_render(), wc_tile_render(), wd_content_render(), wd_status_render(), wd_tabbar_render(), wd_text(), and wk_paint_text().

◆ ra8_gfx_text_size()

ra8_err_t ra8_gfx_text_size ( const char * str,
const ra8_gfx_font_t * font,
uint32_t * out_w,
uint32_t * out_h )
nodiscard

Compute rendered pixel dimensions of a string.

Parameters
[in]strASCII string.
[in]fontFont descriptor.
[out]out_wReceives total pixel width.
[out]out_hReceives total pixel height (font glyph height).
Returns
Error code.
Return values
k_ra8_okMeasured.
k_ra8_err_null_ptrAny argument was NULL.
Precondition
All pointer arguments are non-NULL.
Postcondition
*out_w and *out_h are written.
Since
0.1.0

Definition at line 211 of file ra8_gfx_text_glyph.c.

References ra8_gfx_font_t::glyph_height, ra8_gfx_font_t::glyph_width, k_ra8_err_null_ptr, k_ra8_gfx_max_dim, and k_ra8_ok.

Referenced by er_text_right(), ls_text(), wd_size(), and wk_paint_size().