ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_glyph_atlas.c
Go to the documentation of this file.
1
20
21#include "ra8_glyph_atlas.h"
22
23#include <stddef.h>
24#include <stdint.h>
25#include <string.h>
26
27#include "ra8_attributes.h"
28#include "ra8_check.h"
29#include "ra8_err.h"
30#include "ra8_keycache.h"
31
33static const char* const s_tag = "ra8_glyph_atlas";
34
62internal_glyph_render(void* ctx, const void* key, uint8_t* cell, uint32_t cell_bytes, void* user)
63{
64 const ra8_glyph_atlas_t* atlas = (const ra8_glyph_atlas_t*)ctx;
65 const ra8_glyph_key_t* gk = (const ra8_glyph_key_t*)key;
67 uint16_t w = 0;
68 uint16_t h = 0;
69 const ra8_err_t rerr = atlas->render(atlas->render_ctx, gk, cell, cell_bytes, &w, &h);
70 if (rerr != k_ra8_ok) {
71 return rerr;
72 }
73 dims->w = w;
74 dims->h = h;
75 return k_ra8_ok;
76}
77
79{
80 RA8_CHECK_NULL_PTR(atlas, s_tag, "atlas must not be nullptr");
81 RA8_CHECK_NULL_PTR(cfg, s_tag, "cfg must not be nullptr");
82 RA8_CHECK_NULL_PTR(cfg->render, s_tag, "render must not be nullptr");
83 (void)memset(atlas, 0, sizeof(*atlas));
84 atlas->render = cfg->render;
85 atlas->render_ctx = cfg->render_ctx;
86 ra8_keycache_cfg_t kcfg = {};
87 kcfg.cell_mem = cfg->cell_mem;
88 kcfg.cell_bytes = cfg->cell_bytes;
89 kcfg.cell_count = cfg->cell_count;
90 kcfg.key_mem = (uint8_t*)cfg->keys;
91 kcfg.key_bytes = (uint32_t)sizeof(ra8_glyph_key_t);
92 kcfg.user_mem = (uint8_t*)cfg->dims;
93 kcfg.user_bytes = (uint32_t)sizeof(ra8_glyph_dims_t);
94 kcfg.meta = cfg->meta;
95 kcfg.buckets = cfg->buckets;
96 kcfg.bucket_count = cfg->bucket_count;
98 kcfg.render_ctx = atlas;
99 return ra8_keycache_init(&atlas->kc, &kcfg);
100}
101
104{
105 RA8_CHECK_NULL_PTR(atlas, s_tag, "atlas must not be nullptr");
106 RA8_CHECK_NULL_PTR(key, s_tag, "key must not be nullptr");
107 RA8_CHECK_NULL_PTR(out_glyph, s_tag, "out_glyph must not be nullptr");
108 ra8_keycache_view_t v = {};
109 const ra8_err_t err = ra8_keycache_get(&atlas->kc, key, &v);
110 if (err != k_ra8_ok) {
111 return err;
112 }
113 const ra8_glyph_dims_t* dims = (const ra8_glyph_dims_t*)v.user;
114 *out_glyph = (ra8_glyph_t){.bitmap = v.data, .width = dims->w, .height = dims->h};
115 return k_ra8_ok;
116}
117
118ra8_err_t ra8_glyph_atlas_put(ra8_glyph_atlas_t* atlas, const uint8_t* bitmap)
119{
120 RA8_CHECK_NULL_PTR(atlas, s_tag, "atlas must not be nullptr");
121 RA8_CHECK_NULL_PTR(bitmap, s_tag, "bitmap must not be nullptr");
122 return ra8_keycache_put(&atlas->kc, bitmap);
123}
124
126 uint32_t* out_hits,
127 uint32_t* out_misses,
128 uint32_t* out_evictions)
129{
130 RA8_CHECK_NULL_PTR(atlas, s_tag, "atlas must not be nullptr");
131 if (atlas->kc.cfg.cell_mem == nullptr) {
133 }
134 return ra8_keycache_stats(&atlas->kc, out_hits, out_misses, out_evictions);
135}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
ra8_err_t ra8_glyph_atlas_put(ra8_glyph_atlas_t *atlas, const uint8_t *bitmap)
Release one pin on a glyph previously returned by ra8_glyph_atlas_get.
ra8_err_t ra8_glyph_atlas_stats(const ra8_glyph_atlas_t *atlas, uint32_t *out_hits, uint32_t *out_misses, uint32_t *out_evictions)
Report the atlas hit / miss / eviction counters.
static ra8_err_t internal_glyph_render(void *ctx, const void *key, uint8_t *cell, uint32_t cell_bytes, void *user)
Render trampoline: adapt ra8_glyph_render_fn to the keycache seam.
ra8_err_t ra8_glyph_atlas_get(ra8_glyph_atlas_t *atlas, const ra8_glyph_key_t *key, ra8_glyph_t *out_glyph)
Get (and pin) the rendered glyph for key.
ra8_err_t ra8_glyph_atlas_init(ra8_glyph_atlas_t *atlas, const ra8_glyph_atlas_cfg_t *cfg)
Initialise a glyph atlas over caller-supplied storage.
Fixed-RAM-budget glyph cache with LRU eviction (Layer 3, #147).
The one reusable hash + pin + evict cache engine (#147, #345).
ra8_err_t ra8_keycache_get(ra8_keycache_t *kc, const void *key, ra8_keycache_view_t *out_view)
Get (and pin) the cell for key, rendering it on a miss.
ra8_err_t ra8_keycache_stats(const ra8_keycache_t *kc, uint32_t *out_hits, uint32_t *out_misses, uint32_t *out_evictions)
Report the cache hit / miss / eviction counters.
ra8_err_t ra8_keycache_init(ra8_keycache_t *kc, const ra8_keycache_cfg_t *cfg)
Initialise a cache engine over caller-supplied storage.
ra8_err_t ra8_keycache_put(ra8_keycache_t *kc, const uint8_t *data)
Release one pin on a cell previously returned by ra8_keycache_get.
Caller-supplied storage + renderer for ra8_glyph_atlas_init.
ra8_glyph_key_t * keys
cell_count key-storage entries.
uint32_t bucket_count
Number of hash buckets (>= 1).
ra8_glyph_render_fn render
Render-on-miss callback.
ra8_glyph_dims_t * dims
cell_count dimension descriptors.
uint32_t cell_count
Number of cells.
int32_t * buckets
bucket_count hash-bucket heads.
uint32_t cell_bytes
Bytes per cell (max glyph bitmap).
void * render_ctx
Opaque context passed to render.
uint8_t * cell_mem
cell_count * cell_bytes of bitmap storage.
ra8_keycache_cell_t * meta
cell_count link-metadata entries.
Glyph-cache state (caller-owned; treat as private).
ra8_glyph_render_fn render
Caller's glyph renderer.
void * render_ctx
Caller's renderer context.
ra8_keycache_t kc
Underlying keyed-LRU cache.
Per-cell user descriptor: the rendered glyph dimensions.
uint16_t w
Rendered glyph width in pixels.
uint16_t h
Rendered glyph height in pixels.
Identifies one rendered glyph.
A pinned view of a cached glyph bitmap returned by ra8_glyph_atlas_get.
Caller-supplied storage + policy + renderer for ra8_keycache_init.
uint32_t bucket_count
Number of hash buckets (>= 1).
uint32_t user_bytes
Bytes per user descriptor (may be 0).
uint8_t * cell_mem
cell_count * cell_bytes of cell storage.
uint32_t cell_bytes
Bytes per cell (the rendered payload).
uint8_t * key_mem
cell_count * key_bytes of key storage.
int32_t * buckets
bucket_count hash-bucket heads.
uint32_t key_bytes
Bytes per key (>= 1).
ra8_keycache_cell_t * meta
cell_count link-metadata entries.
uint32_t cell_count
Number of cells.
void * render_ctx
Opaque context passed to render.
ra8_keycache_render_fn render
Render-on-miss callback.
uint8_t * user_mem
cell_count * user_bytes, or NULL if none.
ra8_keycache_cfg_t cfg
Configuration (copied at init).
A pinned view of a cached cell returned by ra8_keycache_get.
uint8_t * data
Cell payload (cell_bytes wide).
void * user
Per-cell user descriptor, or NULL.