Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/ui/paint.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / ui / paint.h` 176 lines ยท 6960 bytes Raw Blame History 1 /* paint.h -- the drawing kit shared by the night sky (ui/night.c), Allen's 2 * presence (ui/mind.c) and his home (ui/allen_ui.c): rectangles, colour 3 * arithmetic, soft round lights, anti-aliased threads, rounded shapes and 4 * text, all into a RAM canvas of 0x00RRGGBB pixels; plus the integer 5 * mathematics they share (sine, atan2, exp, log2, hashed noise). Integer 6 * only (the kernel builds -mgeneral-regs-only), freestanding, no memory of 7 * its own beyond fixed tables. Host tested through ui/tests/allen_ui_test.c. 8 * 9 * Angles are in 65536ths of a turn. Positions handed to the soft shapes are 10 * in 16ths of a pixel, so slow movement is smooth rather than stepping a 11 * whole pixel at a time. */ 12 #ifndef AIENOS_PAINT_H 13 #define AIENOS_PAINT_H 14 #include 15 #include 16 17 #include "allen_font.h" 18 #include "sky_data.h" 19 20 typedef struct { 21 int32_t x, y, w, h; 22 } ck_rect; 23 24 typedef struct { 25 uint32_t *px; /* w * h pixels, rows w apart */ 26 int32_t w, h; 27 } ck_canvas; 28 29 #define CK_RGB(r, g, b) ((uint32_t)(r) << 16 | (uint32_t)(g) << 8 | (uint32_t)(b)) 30 31 static inline int32_t ck_min(int32_t a, int32_t b) { return a < b ? a : b; } 32 static inline int32_t ck_max(int32_t a, int32_t b) { return a > b ? a : b; } 33 static inline int32_t ck_abs(int32_t a) { return a < 0 ? -a : a; } 34 static inline int32_t ck_clamp(int32_t v, int32_t lo, int32_t hi) { return v < lo ? lo : v > hi ? hi : v; } 35 36 /* r becomes its overlap with c; 0 when they do not meet. */ 37 static inline int ck_clip(ck_rect *r, const ck_rect *c) 38 { 39 int32_t x0 = ck_max(r->x, c->x), y0 = ck_max(r->y, c->y); 40 int32_t x1 = ck_min(r->x + r->w, c->x + c->w), y1 = ck_min(r->y + r->h, c->y + c->h); 41 if (x1 <= x0 || y1 <= y0) 42 return 0; 43 *r = (ck_rect){x0, y0, x1 - x0, y1 - y0}; 44 return 1; 45 } 46 47 static inline int ck_meets(ck_rect a, const ck_rect *b) { return ck_clip(&a, b); } 48 static inline ck_rect ck_grow(ck_rect r, int32_t m) { return (ck_rect){r.x - m, r.y - m, r.w + 2 * m, r.h + 2 * m}; } 49 50 /* The smallest rectangle holding both (an empty one adds nothing). */ 51 static inline ck_rect ck_union(ck_rect a, ck_rect b) 52 { 53 if (a.w <= 0 || a.h <= 0) 54 return b; 55 if (b.w <= 0 || b.h <= 0) 56 return a; 57 int32_t x0 = ck_min(a.x, b.x), y0 = ck_min(a.y, b.y); 58 return (ck_rect){x0, y0, ck_max(a.x + a.w, b.x + b.w) - x0, ck_max(a.y + a.h, b.y + b.h) - y0}; 59 } 60 61 /* ---- colour: channels 0..255, weights 0..256 ---- */ 62 63 static inline uint32_t ck_ch(uint32_t c, int s) { return c >> s & 255u; } 64 65 /* Canvas and palette values are sRGB, but coverage and fading weigh light, 66 * not encoded bytes. Reuse the sky's fixed Q15 transfer tables; no pow, 67 * allocation or per-pixel search. Keep the unweighted endpoints exact. */ 68 static inline uint32_t ck_light_encode(uint32_t light) 69 { 70 return (ck_sky_srgb16[light >= 32767u ? 4095u : light >> 3] + 8u) >> 4; 71 } 72 73 static inline uint32_t ck_scale(uint32_t c, uint32_t f) 74 { 75 if (!f) 76 return 0; 77 if (f >= 256) 78 return c; 79 return ck_light_encode(ck_sky_lin[ck_ch(c, 16)] * f >> 8) << 16 | 80 ck_light_encode(ck_sky_lin[ck_ch(c, 8)] * f >> 8) << 8 | 81 ck_light_encode(ck_sky_lin[ck_ch(c, 0)] * f >> 8); 82 } 83 84 static inline uint32_t ck_light_add_channel(uint32_t d, uint32_t c, uint32_t a) 85 { 86 uint32_t added = ck_sky_lin[c] * a >> 8; 87 return added ? ck_light_encode(ck_sky_lin[d] + added) : d; 88 } 89 90 static inline uint32_t ck_light_mix_channel(uint32_t d, uint32_t c, uint32_t a) 91 { 92 return d == c ? d : ck_light_encode((ck_sky_lin[d] * (256 - a) + ck_sky_lin[c] * a) >> 8); 93 } 94 95 /* Add c's light with weight a/256, saturating at display white. */ 96 static inline uint32_t ck_add(uint32_t d, uint32_t c, uint32_t a) 97 { 98 if (!a || !c) 99 return d; 100 if (!d && a == 256) 101 return c; 102 return ck_light_add_channel(ck_ch(d, 16), ck_ch(c, 16), a) << 16 | 103 ck_light_add_channel(ck_ch(d, 8), ck_ch(c, 8), a) << 8 | 104 ck_light_add_channel(ck_ch(d, 0), ck_ch(c, 0), a); 105 } 106 107 /* d toward c by light coverage a/256; combine before rounding. */ 108 static inline uint32_t ck_mix(uint32_t d, uint32_t c, uint32_t a) 109 { 110 if (!a || d == c) 111 return d; 112 if (a >= 256) 113 return c; 114 return ck_light_mix_channel(ck_ch(d, 16), ck_ch(c, 16), a) << 16 | 115 ck_light_mix_channel(ck_ch(d, 8), ck_ch(c, 8), a) << 8 | 116 ck_light_mix_channel(ck_ch(d, 0), ck_ch(c, 0), a); 117 } 118 119 /* ---- easing: t and the result in 0..256 ---- */ 120 121 static inline uint32_t ck_smooth(int32_t t) /* 3t^2 - 2t^3 */ 122 { 123 t = ck_clamp(t, 0, 256); 124 return (uint32_t)(t * t * (768 - 2 * t) >> 16); 125 } 126 127 static inline uint32_t ck_ease_out(int32_t t) /* 1 - (1 - t)^3 */ 128 { 129 t = 256 - ck_clamp(t, 0, 256); 130 return 256u - (uint32_t)(t * t * t >> 16); 131 } 132 133 /* 0..256 for how far `now` is from `from` to `to` (ms) */ 134 static inline int32_t ck_prog(uint32_t now, uint32_t from, uint32_t to) 135 { 136 if (now <= from) 137 return 0; 138 if (now >= to) 139 return 256; 140 return (int32_t)((uint64_t)(now - from) * 256u / (to - from)); 141 } 142 143 /* ---- mathematics (paint.c) ---- */ 144 145 int32_t ck_sin(uint32_t turn65536); /* scaled by 16384 */ 146 int32_t ck_cos(uint32_t turn65536); 147 uint32_t ck_atan2(int32_t y, int32_t x); /* the angle of (x, y), 0..65535 */ 148 uint32_t ck_exp_neg(uint32_t x256); /* exp(-x/256), scaled by 65536 */ 149 uint32_t ck_log2_q16(uint64_t v); /* log2(v) * 65536, v >= 1 */ 150 uint64_t ck_isqrt(uint64_t v); 151 uint32_t ck_hash(int32_t x, int32_t y, uint32_t seed); 152 uint32_t ck_noise(int32_t x256, int32_t y256, uint32_t seed); /* smooth value noise, 0..65535 */ 153 uint32_t ck_fbm(int32_t x256, int32_t y256, uint32_t seed, int octaves); /* 0..65535 */ 154 155 /* ---- shapes (paint.c); each draws only inside the clip rectangle ---- */ 156 157 /* A soft round light at (cx, cy) with radius r (16ths of a pixel): colour 158 * added with weight a * (1 - d^2/r^2)^3. */ 159 void ck_glow(ck_canvas *cv, const ck_rect *clip, int32_t cx, int32_t cy, int32_t r, uint32_t color, uint32_t a); 160 /* A thread from (x0, y0) to (x1, y1), width and positions in 16ths of 161 * a pixel; anti-aliased light whose weight runs from a0 to a1 along it. */ 162 void ck_thread(ck_canvas *cv, const ck_rect *clip, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t width, 163 uint32_t color, uint32_t a0, uint32_t a1); 164 /* Signed distance from a pixel's centre to a rounded rectangle, 16ths of a 165 * pixel, negative inside; and the coverage of a pixel at such a distance. */ 166 int32_t ck_sd_rrect(int32_t x, int32_t y, const ck_rect *r, int32_t radius); 167 static inline uint32_t ck_cover(int32_t d16) 168 { 169 int32_t v = 8 - d16; 170 return v <= 0 ? 0u : v >= 16 ? 256u : (uint32_t)(v * 16); 171 } 172 /* One letter at pen (1/256 px) on baseline y, mixed in with weight a 173 * (additive light when add is set); returns the pen after it. */ 174 uint64_t ck_letter(ck_canvas *cv, const ck_rect *clip, ck_af_face *f, uint64_t pen, int32_t y, char c, 175 uint32_t color, uint32_t a, int add); 176 #endif