interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / ui / paint.c

276 lines · 9790 bytes Raw Blame History
1/* paint.c -- the drawing kit; see paint.h. */
2#include "paint.h"
3
4/* ---- tables, filled on first use ---- */
5
6static int16_t sin_t[4097]; /* a full turn in 4096 steps, Q14; one extra for interpolation */
7static uint32_t exp_t[4096]; /* exp(-k/256), Q16 */
8static uint32_t log_t[257]; /* log2(1 + k/256), Q16 */
9static int ready;
10
11static void setup(void)
12{
13 /* sine: a rotation by a 4096th of a turn, applied over and over (Q30) */
14 const int64_t C = 1073740561, S = 1647099;
15 int64_t c = 1ll << 30, s = 0;
16 for (unsigned k = 0; k <= 1024; k++) {
17 int16_t v = k == 1024 ? 16384 : (int16_t)((s + (1 << 15)) >> 16);
18 sin_t[k] = v;
19 sin_t[2048 - k] = v;
20 sin_t[(2048 + k) & 4095] = (int16_t)-v;
21 sin_t[(4096 - k) & 4095] = (int16_t)-v;
22 int64_t nc = (c * C - s * S) >> 30, ns = (s * C + c * S) >> 30;
23 c = nc, s = ns;
24 }
25 sin_t[4096] = sin_t[0];
26 /* exp(-k/256): one factor of exp(-1/256) (Q30) per step */
27 uint64_t e = 1ull << 30;
28 for (unsigned k = 0; k < 4096; k++) {
29 exp_t[k] = (uint32_t)((e + (1u << 13)) >> 14);
30 e = e * 1069555701ull >> 30;
31 }
32 /* log2(1 + k/256): square the value 16 times, taking a bit each time it passes 2 */
33 for (unsigned k = 0; k <= 256; k++) {
34 uint64_t y = (256u + k) << 8; /* Q16, in [1, 2] */
35 uint32_t r = 0;
36 if (k == 256) {
37 log_t[k] = 65536;
38 continue;
39 }
40 for (int b = 15; b >= 0; b--) {
41 y = y * y >> 16;
42 if (y >= 2u << 16) {
43 y >>= 1;
44 r |= 1u << b;
45 }
46 }
47 log_t[k] = r;
48 }
49 ready = 1;
50}
51
52int32_t ck_sin(uint32_t a)
53{
54 if (!ready)
55 setup();
56 a &= 65535u;
57 uint32_t i = a >> 4, f = a & 15u;
58 return sin_t[i] + (((int32_t)sin_t[i + 1] - sin_t[i]) * (int32_t)f >> 4);
59}
60
61int32_t ck_cos(uint32_t a) { return ck_sin(a + 16384u); }
62
63uint32_t ck_atan2(int32_t y, int32_t x)
64{
65 static const uint16_t step[14] = {8192, 4836, 2555, 1297, 651, 326, 163, 81, 41, 20, 10, 5, 3, 1};
66 int64_t X = x, Y = y;
67 uint32_t ang = 0;
68 if (X == 0 && Y == 0)
69 return 0;
70 if (X < 0) { /* turn half a turn into the right half-plane */
71 X = -X, Y = -Y;
72 ang = 32768;
73 }
74 int64_t m = X > (Y < 0 ? -Y : Y) ? X : (Y < 0 ? -Y : Y);
75 while (m < (1 << 20))
76 X *= 2, Y *= 2, m *= 2; /* not a shift: Y may be negative */
77 while (m >= (1 << 22))
78 X >>= 1, Y >>= 1, m >>= 1;
79 int32_t z = 0;
80 for (int i = 0; i < 14; i++) {
81 int64_t nx;
82 if (Y > 0) {
83 nx = X + (Y >> i);
84 Y -= X >> i;
85 z += step[i];
86 } else {
87 nx = X - (Y >> i);
88 Y += X >> i;
89 z -= step[i];
90 }
91 X = nx;
92 }
93 return (ang + (uint32_t)z) & 65535u;
94}
95
96uint32_t ck_exp_neg(uint32_t x)
97{
98 if (!ready)
99 setup();
100 return x < 4096 ? exp_t[x] : 0;
101}
102
103uint32_t ck_log2_q16(uint64_t v)
104{
105 if (!ready)
106 setup();
107 if (!v)
108 return 0;
109 int n = 63 - __builtin_clzll(v);
110 uint64_t m = n >= 16 ? v >> (n - 16) : v << (16 - n); /* Q16 in [1, 2) */
111 uint32_t k = (uint32_t)(m >> 8 & 255u), f = (uint32_t)(m & 255u);
112 return (uint32_t)n * 65536u + log_t[k] + ((log_t[k + 1] - log_t[k]) * f >> 8);
113}
114
115uint64_t ck_isqrt(uint64_t v) { return ck_af_isqrt(v); }
116
117uint32_t ck_hash(int32_t x, int32_t y, uint32_t seed)
118{
119 uint32_t h = (uint32_t)x * 0x8da6b343u ^ (uint32_t)y * 0xd8163841u ^ seed * 0xcb1ab31fu;
120 h ^= h >> 13;
121 h *= 0x5bd1e995u;
122 h ^= h >> 15;
123 h *= 0x27d4eb2du;
124 h ^= h >> 16;
125 return h;
126}
127
128uint32_t ck_noise(int32_t x, int32_t y, uint32_t seed)
129{
130 int32_t ix = x >> 8, iy = y >> 8;
131 uint32_t fx = ck_smooth(x & 255), fy = ck_smooth(y & 255);
132 uint64_t v00 = ck_hash(ix, iy, seed) >> 16, v10 = ck_hash(ix + 1, iy, seed) >> 16;
133 uint64_t v01 = ck_hash(ix, iy + 1, seed) >> 16, v11 = ck_hash(ix + 1, iy + 1, seed) >> 16;
134 uint64_t a = v00 * (256 - fx) + v10 * fx, b = v01 * (256 - fx) + v11 * fx;
135 return (uint32_t)((a * (256 - fy) + b * fy) >> 16);
136}
137
138uint32_t ck_fbm(int32_t x, int32_t y, uint32_t seed, int octaves)
139{
140 uint64_t sum = 0, total = 0;
141 for (int o = 0; o < octaves; o++) {
142 uint32_t w = 1u << (octaves - 1 - o);
143 sum += (uint64_t)ck_noise(x << o, y << o, seed + (uint32_t)o * 7919u) * w;
144 total += w;
145 }
146 return (uint32_t)(sum / total);
147}
148
149/* ---- shapes ---- */
150
151/* d becomes the part of d inside both the clip and the canvas */
152static int fit(const ck_canvas *cv, const ck_rect *clip, ck_rect *d)
153{
154 ck_rect all = {0, 0, cv->w, cv->h};
155 return ck_clip(d, clip) && ck_clip(d, &all);
156}
157
158void ck_glow(ck_canvas *cv, const ck_rect *clip, int32_t cx, int32_t cy, int32_t r, uint32_t color, uint32_t a)
159{
160 if (r <= 0 || !a)
161 return;
162 ck_rect d = {(cx - r) >> 4, (cy - r) >> 4, 0, 0};
163 d.w = ((cx + r) >> 4) - d.x + 1;
164 d.h = ((cy + r) >> 4) - d.y + 1;
165 if (!fit(cv, clip, &d))
166 return;
167 int64_t r2 = (int64_t)r * r;
168 uint64_t inv = (1ull << 40) / (uint64_t)r2;
169 for (int32_t y = d.y; y < d.y + d.h; y++) {
170 int64_t dy = (int64_t)y * 16 + 8 - cy, dy2 = dy * dy;
171 if (dy2 >= r2)
172 continue;
173 int32_t half = (int32_t)ck_isqrt((uint64_t)(r2 - dy2));
174 int32_t x0 = ck_max(d.x, (cx - half) >> 4), x1 = ck_min(d.x + d.w - 1, (cx + half) >> 4);
175 uint32_t *row = cv->px + (size_t)y * (size_t)cv->w;
176 for (int32_t x = x0; x <= x1; x++) {
177 int64_t dx = (int64_t)x * 16 + 8 - cx, d2 = dx * dx + dy2;
178 if (d2 >= r2)
179 continue;
180 uint32_t f = 256u - (uint32_t)((uint64_t)d2 * inv >> 32);
181 uint32_t w = (f * f >> 8) * f >> 8; /* 0..256 */
182 row[x] = ck_add(row[x], color, w * a >> 8);
183 }
184 }
185}
186
187static void plot(ck_canvas *cv, const ck_rect *clip, int32_t x, int32_t y, uint32_t color, uint32_t a)
188{
189 if (!a || x < clip->x || y < clip->y || x >= clip->x + clip->w || y >= clip->y + clip->h || x < 0 || y < 0 ||
190 x >= cv->w || y >= cv->h)
191 return;
192 uint32_t *p = cv->px + (size_t)y * (size_t)cv->w + x;
193 *p = ck_add(*p, color, a);
194}
195
196void ck_thread(ck_canvas *cv, const ck_rect *clip, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t width,
197 uint32_t color, uint32_t a0, uint32_t a1)
198{
199 int32_t pad = (width >> 4) + 1;
200 ck_rect box = {(ck_min(x0, x1) >> 4) - pad, (ck_min(y0, y1) >> 4) - pad, 0, 0};
201 box.w = (ck_max(x0, x1) >> 4) - box.x + pad + 1;
202 box.h = (ck_max(y0, y1) >> 4) - box.y + pad + 1;
203 if (width <= 0 || !ck_meets(box, clip) || (!a0 && !a1))
204 return;
205 int steep = ck_abs(y1 - y0) > ck_abs(x1 - x0);
206 if (steep) {
207 int32_t t = x0;
208 x0 = y0, y0 = t;
209 t = x1;
210 x1 = y1, y1 = t;
211 }
212 if (x0 > x1) {
213 int32_t t = x0;
214 x0 = x1, x1 = t;
215 t = y0;
216 y0 = y1, y1 = t;
217 uint32_t u = a0;
218 a0 = a1, a1 = u;
219 }
220 int32_t len = x1 - x0;
221 if (len < 1)
222 len = 1;
223 int64_t grad = (int64_t)(y1 - y0) * 65536 / len;
224 /* Project the native-scaled width onto the minor axis, not its alpha.
225 * This keeps horizontal and vertical threads equally weighted at 4K. */
226 uint32_t slant = (uint32_t)(ck_isqrt((uint64_t)(65536ll * 65536ll + grad * grad)) >> 8); /* 256 = flat */
227 int32_t radius = (int32_t)((int64_t)width * slant >> 9);
228 int32_t first = (x0 + 7) >> 4, last = (x1 - 8) >> 4;
229 first = ck_max(first, steep ? clip->y : clip->x);
230 last = ck_min(last, (steep ? clip->y + clip->h : clip->x + clip->w) - 1);
231 for (int32_t X = first; X <= last; X++) {
232 int32_t along = X * 16 + 8 - x0;
233 int32_t yc = y0 + (int32_t)(along * grad >> 16);
234 uint32_t a = (uint32_t)((int32_t)a0 + ((int32_t)a1 - (int32_t)a0) * along / len);
235 for (int32_t Y = (yc - radius - 8) >> 4; Y <= (yc + radius + 8) >> 4; Y++) {
236 uint32_t coverage = ck_cover(ck_abs(Y * 16 + 8 - yc) - radius);
237 if (steep)
238 plot(cv, clip, Y, X, color, a * coverage >> 8);
239 else
240 plot(cv, clip, X, Y, color, a * coverage >> 8);
241 }
242 }
243}
244
245int32_t ck_sd_rrect(int32_t x, int32_t y, const ck_rect *r, int32_t rad)
246{
247 int64_t px = (int64_t)x * 16 + 8, py = (int64_t)y * 16 + 8;
248 int64_t cx = (int64_t)r->x * 16 + (int64_t)r->w * 8, cy = (int64_t)r->y * 16 + (int64_t)r->h * 8;
249 int64_t qx = (px > cx ? px - cx : cx - px) - ((int64_t)r->w * 8 - rad * 16);
250 int64_t qy = (py > cy ? py - cy : cy - py) - ((int64_t)r->h * 8 - rad * 16);
251 int64_t out = qx > 0 && qy > 0 ? (int64_t)ck_isqrt((uint64_t)(qx * qx + qy * qy)) : qx > qy ? qx : qy;
252 return (int32_t)(out - rad * 16);
253}
254
255uint64_t ck_letter(ck_canvas *cv, const ck_rect *clip, ck_af_face *f, uint64_t pen, int32_t y, char c,
256 uint32_t color, uint32_t a, int add)
257{
258 const ck_af_glyph *g = a ? ck_af_glyph_get(f, c) : 0;
259 if (g && g->alpha) {
260 int32_t gx = (int32_t)((pen + 128) / 256) + g->ox, gy = y + g->oy;
261 ck_rect d = {gx, gy, g->w, g->h};
262 if (fit(cv, clip, &d))
263 for (int32_t yy = d.y; yy < d.y + d.h; yy++) {
264 uint32_t *row = cv->px + (size_t)yy * (size_t)cv->w;
265 const uint8_t *m = g->alpha + (size_t)(yy - gy) * g->w;
266 for (int32_t xx = d.x; xx < d.x + d.w; xx++) {
267 uint32_t cov = m[xx - gx];
268 if (cov) {
269 uint32_t w = (cov + (cov >> 7)) * a >> 8;
270 row[xx] = add ? ck_add(row[xx], color, w) : ck_mix(row[xx], color, w);
271 }
272 }
273 }
274 }
275 return pen + ck_af_advance(f, c);
276}