AtlatestRepositorysigil-ui

sigil-ui / tree / nativeraster.c

1/*
2 * Pixel loops for (sigil ui raster).
3 *
4 * A canvas is a bytevector of width * height 32-bit pixels, XRGB8888 in
5 * native (little-endian) order: the same layout as a DRM XRGB8888 buffer and
6 * a 32 bpp fbdev framebuffer, so presenting is a row copy.
7 *
8 * Every drawing call takes a clip rectangle in device pixels, [x0, x1) by
9 * [y0, y1), which is intersected with the canvas. Damage repaints use it to
10 * redraw only part of an item.
11 */
13#include <sigil/sigil.h>
14#include <stdint.h>
15#include <string.h>
17#define REGISTER_AND_EXPORT(name, func, arity, doc) \
18 sigil_module_register_native(vm, name, func, arity, doc); \
19 sigil_module_export(vm, name)
21typedef struct {
22 uint32_t *px;
23 int64_t w, h;
24} Canvas;
26typedef struct {
27 int64_t x0, y0, x1, y1;
28} Clip;
30static int arg_int(SigilVM *vm, Value v, int64_t *out, const char *who, const char *what)
32 if (!sigil_is_fixnum(v)) {
33 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: %s must be an exact integer", who, what);
34 return 0;
35 }
36 *out = sigil_as_fixnum(v);
37 return 1;
40static int arg_canvas(SigilVM *vm, Value bv, Value wv, Canvas *c, const char *who)
42 int64_t w;
43 if (!sigil_is_bytevector(bv)) {
44 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: canvas pixels must be a bytevector", who);
45 return 0;
46 }
47 if (!arg_int(vm, wv, &w, who, "canvas width")) return 0;
48 size_t len = sigil_bytevector_length(bv);
49 if (w <= 0 || len % (size_t)(4 * w) != 0) {
50 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: %zu bytes is not a canvas %lld pixels wide",
51 who, len, (long long)w);
52 return 0;
53 }
54 c->px = (uint32_t *)sigil_bytevector_data(bv);
55 c->w = w;
56 c->h = (int64_t)(len / (size_t)(4 * w));
57 return 1;
60/* Clip from four argument slots, intersected with the canvas. */
61static int arg_clip(SigilVM *vm, Value *args, const Canvas *c, Clip *k, const char *who)
63 if (!arg_int(vm, args[0], &k->x0, who, "clip x0") ||
64 !arg_int(vm, args[1], &k->y0, who, "clip y0") ||
65 !arg_int(vm, args[2], &k->x1, who, "clip x1") ||
66 !arg_int(vm, args[3], &k->y1, who, "clip y1"))
67 return 0;
68 if (k->x0 < 0) k->x0 = 0;
69 if (k->y0 < 0) k->y0 = 0;
70 if (k->x1 > c->w) k->x1 = c->w;
71 if (k->y1 > c->h) k->y1 = c->h;
72 return 1;
75static int arg_rgb(SigilVM *vm, Value v, uint32_t *out, const char *who)
77 int64_t n;
78 if (!arg_int(vm, v, &n, who, "color")) return 0;
79 if (n < 0 || n > 0xFFFFFF) {
80 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: color %lld is not 0xRRGGBB", who, (long long)n);
81 return 0;
82 }
83 *out = (uint32_t)n;
84 return 1;
87static inline uint32_t blend(uint32_t bg, uint32_t fg, uint32_t a)
89 uint32_t na = 255 - a;
90 uint32_t r = (((bg >> 16) & 255) * na + ((fg >> 16) & 255) * a + 127) / 255;
91 uint32_t g = (((bg >> 8) & 255) * na + ((fg >> 8) & 255) * a + 127) / 255;
92 uint32_t b = ((bg & 255) * na + (fg & 255) * a + 127) / 255;
93 return (r << 16) | (g << 8) | b;
96/* (%canvas-fill! px width x y w h rgb cx0 cy0 cx1 cy1) */
97static Value native_fill(SigilVM *vm, int argc, Value *args)
99 Canvas c; Clip k; int64_t x, y, w, h; uint32_t rgb;
100 const char *who = "canvas-fill!";
101 if (!arg_canvas(vm, args[0], args[1], &c, who) ||
102 !arg_int(vm, args[2], &x, who, "x") || !arg_int(vm, args[3], &y, who, "y") ||
103 !arg_int(vm, args[4], &w, who, "width") || !arg_int(vm, args[5], &h, who, "height") ||
104 !arg_rgb(vm, args[6], &rgb, who) || !arg_clip(vm, args + 7, &c, &k, who))
105 return SIGIL_UNDEFINED;
106 int64_t x0 = x > k.x0 ? x : k.x0, y0 = y > k.y0 ? y : k.y0;
107 int64_t x1 = x + w < k.x1 ? x + w : k.x1, y1 = y + h < k.y1 ? y + h : k.y1;
108 if (x0 >= x1 || y0 >= y1) return SIGIL_UNDEFINED;
109 uint32_t *row = c.px + y0 * c.w;
110 for (int64_t i = x0; i < x1; i++) row[i] = rgb;
111 for (int64_t j = y0 + 1; j < y1; j++)
112 memcpy(c.px + j * c.w + x0, row + x0, (size_t)(x1 - x0) * 4);
113 return SIGIL_UNDEFINED;
116/* (%canvas-blend-fill! px width x y w h rgb alpha cx0 cy0 cx1 cy1), alpha 0..255 */
117static Value native_blend_fill(SigilVM *vm, int argc, Value *args)
119 Canvas c; Clip k; int64_t x, y, w, h, a; uint32_t rgb;
120 const char *who = "canvas-blend-fill!";
121 if (!arg_canvas(vm, args[0], args[1], &c, who) ||
122 !arg_int(vm, args[2], &x, who, "x") || !arg_int(vm, args[3], &y, who, "y") ||
123 !arg_int(vm, args[4], &w, who, "width") || !arg_int(vm, args[5], &h, who, "height") ||
124 !arg_rgb(vm, args[6], &rgb, who) || !arg_int(vm, args[7], &a, who, "alpha") ||
125 !arg_clip(vm, args + 8, &c, &k, who))
126 return SIGIL_UNDEFINED;
127 if (a < 0 || a > 255) {
128 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: alpha %lld is not 0..255", who, (long long)a);
129 return SIGIL_UNDEFINED;
130 }
131 int64_t x0 = x > k.x0 ? x : k.x0, y0 = y > k.y0 ? y : k.y0;
132 int64_t x1 = x + w < k.x1 ? x + w : k.x1, y1 = y + h < k.y1 ? y + h : k.y1;
133 if (a == 0) return SIGIL_UNDEFINED;
134 for (int64_t j = y0; j < y1; j++) {
135 uint32_t *row = c.px + j * c.w;
136 for (int64_t i = x0; i < x1; i++)
137 row[i] = a == 255 ? rgb : blend(row[i], rgb, (uint32_t)a);
138 }
139 return SIGIL_UNDEFINED;
142/* (%canvas-mask! px width x y alpha mask-width rgb cx0 cy0 cx1 cy1)
143 * alpha: bytevector of mask-width * mask-height levels 0..255. Level 255
144 * writes the color exactly; level 0 leaves the pixel alone. */
145static Value native_mask(SigilVM *vm, int argc, Value *args)
147 Canvas c; Clip k; int64_t x, y, mw; uint32_t rgb;
148 const char *who = "canvas-mask!";
149 if (!arg_canvas(vm, args[0], args[1], &c, who) ||
150 !arg_int(vm, args[2], &x, who, "x") || !arg_int(vm, args[3], &y, who, "y"))
151 return SIGIL_UNDEFINED;
152 if (!sigil_is_bytevector(args[4])) {
153 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: alpha must be a bytevector", who);
154 return SIGIL_UNDEFINED;
155 }
156 if (!arg_int(vm, args[5], &mw, who, "mask width") || !arg_rgb(vm, args[6], &rgb, who) ||
157 !arg_clip(vm, args + 7, &c, &k, who))
158 return SIGIL_UNDEFINED;
159 size_t len = sigil_bytevector_length(args[4]);
160 if (mw <= 0 || len % (size_t)mw != 0) {
161 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: %zu alpha bytes is not a mask %lld wide",
162 who, len, (long long)mw);
163 return SIGIL_UNDEFINED;
164 }
165 int64_t mh = (int64_t)(len / (size_t)mw);
166 const uint8_t *alpha = sigil_bytevector_data(args[4]);
167 int64_t x0 = x > k.x0 ? x : k.x0, y0 = y > k.y0 ? y : k.y0;
168 int64_t x1 = x + mw < k.x1 ? x + mw : k.x1, y1 = y + mh < k.y1 ? y + mh : k.y1;
169 for (int64_t j = y0; j < y1; j++) {
170 uint32_t *row = c.px + j * c.w;
171 const uint8_t *arow = alpha + (j - y) * mw - x;
172 for (int64_t i = x0; i < x1; i++) {
173 uint32_t a = arow[i];
174 if (a == 0) continue;
175 row[i] = a == 255 ? rgb : blend(row[i], rgb, a);
176 }
177 }
178 return SIGIL_UNDEFINED;
181/* (%canvas-bits! px width x y bits offset cell-w cell-h rgb cx0 cy0 cx1 cy1)
182 * A 1-bit glyph cell: cell-h rows of ceil(cell-w / 8) bytes starting at
183 * offset, most significant bit leftmost. Set bits write the color; clear
184 * bits leave the pixel alone. */
185static Value native_bits(SigilVM *vm, int argc, Value *args)
187 Canvas c; Clip k; int64_t x, y, off, cw, ch; uint32_t rgb;
188 const char *who = "canvas-bits!";
189 if (!arg_canvas(vm, args[0], args[1], &c, who) ||
190 !arg_int(vm, args[2], &x, who, "x") || !arg_int(vm, args[3], &y, who, "y"))
191 return SIGIL_UNDEFINED;
192 if (!sigil_is_bytevector(args[4])) {
193 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: glyph bits must be a bytevector", who);
194 return SIGIL_UNDEFINED;
195 }
196 if (!arg_int(vm, args[5], &off, who, "offset") || !arg_int(vm, args[6], &cw, who, "cell width") ||
197 !arg_int(vm, args[7], &ch, who, "cell height") || !arg_rgb(vm, args[8], &rgb, who) ||
198 !arg_clip(vm, args + 9, &c, &k, who))
199 return SIGIL_UNDEFINED;
200 int64_t bpr = (cw + 7) / 8;
201 size_t len = sigil_bytevector_length(args[4]);
202 if (cw <= 0 || ch <= 0 || off < 0 || (size_t)(off + bpr * ch) > len) {
203 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: glyph at %lld (%lldx%lld) is outside %zu bytes",
204 who, (long long)off, (long long)cw, (long long)ch, len);
205 return SIGIL_UNDEFINED;
206 }
207 const uint8_t *bits = (const uint8_t *)sigil_bytevector_data(args[4]) + off;
208 int64_t x0 = x > k.x0 ? x : k.x0, y0 = y > k.y0 ? y : k.y0;
209 int64_t x1 = x + cw < k.x1 ? x + cw : k.x1, y1 = y + ch < k.y1 ? y + ch : k.y1;
210 for (int64_t j = y0; j < y1; j++) {
211 const uint8_t *brow = bits + (j - y) * bpr;
212 uint32_t *row = c.px + j * c.w;
213 for (int64_t i = x0; i < x1; i++) {
214 int64_t b = i - x;
215 if (brow[b >> 3] & (0x80 >> (b & 7))) row[i] = rgb;
216 }
217 }
218 return SIGIL_UNDEFINED;
221/* (%canvas-copy! dst dst-width dx dy src src-width sx sy w h) */
222static Value native_copy(SigilVM *vm, int argc, Value *args)
224 Canvas d, s; int64_t dx, dy, sx, sy, w, h;
225 const char *who = "canvas-copy!";
226 if (!arg_canvas(vm, args[0], args[1], &d, who) ||
227 !arg_int(vm, args[2], &dx, who, "dx") || !arg_int(vm, args[3], &dy, who, "dy") ||
228 !arg_canvas(vm, args[4], args[5], &s, who) ||
229 !arg_int(vm, args[6], &sx, who, "sx") || !arg_int(vm, args[7], &sy, who, "sy") ||
230 !arg_int(vm, args[8], &w, who, "width") || !arg_int(vm, args[9], &h, who, "height"))
231 return SIGIL_UNDEFINED;
232 if (sx < 0 || sy < 0 || w < 0 || h < 0 || sx + w > s.w || sy + h > s.h ||
233 dx < 0 || dy < 0 || dx + w > d.w || dy + h > d.h) {
234 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: rectangle outside a canvas", who);
235 return SIGIL_UNDEFINED;
236 }
237 for (int64_t j = 0; j < h; j++)
238 memmove(d.px + (dy + j) * d.w + dx, s.px + (sy + j) * s.w + sx, (size_t)w * 4);
239 return SIGIL_UNDEFINED;
242/* (%canvas->rgb px width) => bytevector of R G B bytes, row-major */
243static Value native_to_rgb(SigilVM *vm, int argc, Value *args)
245 Canvas c;
246 if (!arg_canvas(vm, args[0], args[1], &c, "canvas->rgb")) return SIGIL_UNDEFINED;
247 size_t n = (size_t)(c.w * c.h);
248 Value out = sigil_make_bytevector(vm, n * 3);
249 /* the canvas may have moved if allocation collected; re-read it */
250 if (!arg_canvas(vm, args[0], args[1], &c, "canvas->rgb")) return SIGIL_UNDEFINED;
251 uint8_t *o = sigil_bytevector_data(out);
252 for (size_t i = 0; i < n; i++) {
253 uint32_t p = c.px[i];
254 o[3 * i] = (uint8_t)(p >> 16);
255 o[3 * i + 1] = (uint8_t)(p >> 8);
256 o[3 * i + 2] = (uint8_t)p;
257 }
258 return out;
261/* (%canvas-count px width x y w h rgb) => pixels in the rectangle that are
262 * not rgb. Used by layout gates (a band must stay background). */
263static Value native_count_other(SigilVM *vm, int argc, Value *args)
265 Canvas c; int64_t x, y, w, h; uint32_t rgb;
266 const char *who = "canvas-count-other";
267 if (!arg_canvas(vm, args[0], args[1], &c, who) ||
268 !arg_int(vm, args[2], &x, who, "x") || !arg_int(vm, args[3], &y, who, "y") ||
269 !arg_int(vm, args[4], &w, who, "width") || !arg_int(vm, args[5], &h, who, "height") ||
270 !arg_rgb(vm, args[6], &rgb, who))
271 return SIGIL_UNDEFINED;
272 if (x < 0 || y < 0 || w < 0 || h < 0 || x + w > c.w || y + h > c.h) {
273 sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: rectangle outside the canvas", who);
274 return SIGIL_UNDEFINED;
275 }
276 int64_t n = 0;
277 for (int64_t j = y; j < y + h; j++)
278 for (int64_t i = x; i < x + w; i++)
279 if ((c.px[j * c.w + i] & 0xFFFFFF) != rgb) n++;
280 return sigil_fixnum(n);
283void sigil__register_sigil_ui_surface(SigilVM *vm);
284void sigil__register_sigil_ui_png(SigilVM *vm);
285#if !defined(__APPLE__) && !defined(_WIN32)
286void sigil__register_sigil_ui_wayland(SigilVM *vm);
287#endif
289void sigil__init_sigil_ui_native(SigilVM *vm)
291 SigilModule *module = sigil_begin_module(vm, "(sigil ui raster)");
292 if (module) {
293 REGISTER_AND_EXPORT("%canvas-fill!", native_fill, SIGIL_ARITY_EXACT(11),
294 "Fill a rectangle with a color");
295 REGISTER_AND_EXPORT("%canvas-blend-fill!", native_blend_fill, SIGIL_ARITY_EXACT(12),
296 "Blend a color over a rectangle at constant alpha");
297 REGISTER_AND_EXPORT("%canvas-mask!", native_mask, SIGIL_ARITY_EXACT(11),
298 "Blend a color through an 8-bit alpha mask");
299 REGISTER_AND_EXPORT("%canvas-bits!", native_bits, SIGIL_ARITY_EXACT(13),
300 "Draw a 1-bit glyph cell in a color");
301 REGISTER_AND_EXPORT("%canvas-copy!", native_copy, SIGIL_ARITY_EXACT(10),
302 "Copy a rectangle between canvases");
303 REGISTER_AND_EXPORT("%canvas->rgb", native_to_rgb, SIGIL_ARITY_EXACT(2),
304 "Canvas pixels as packed RGB bytes");
305 REGISTER_AND_EXPORT("%canvas-count-other", native_count_other, SIGIL_ARITY_EXACT(7),
306 "Count pixels in a rectangle that differ from a color");
307 sigil_end_module(vm);
308 }
309 sigil__register_sigil_ui_surface(vm);
310 sigil__register_sigil_ui_png(vm);
311#if !defined(__APPLE__) && !defined(_WIN32)
312 sigil__register_sigil_ui_wayland(vm);
313#endif