Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)

Merge pull request #2 from iProgramMC/trunk

backends/fb: Make the canvas feature optional

Changed files
+42
backends
+38
backends/fb.c
···
if (x >= _ctx->cols || y >= _ctx->rows) {
return;
}
+
+
#ifdef FLANTERM_FB_DISABLE_CANVAS
+
uint32_t default_bg = ctx->default_bg;
+
#endif
x = ctx->offset_x + x * ctx->glyph_width;
y = ctx->offset_y + y * ctx->glyph_height;
···
for (size_t gy = 0; gy < ctx->glyph_height; gy++) {
uint8_t fy = gy / ctx->font_scale_y;
volatile uint32_t *fb_line = ctx->framebuffer + x + (y + gy) * (ctx->pitch / 4);
+
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas_line = ctx->canvas + x + (y + gy) * ctx->width;
+
#endif
+
for (size_t fx = 0; fx < ctx->font_width; fx++) {
bool draw = glyph[fy * ctx->font_width + fx];
for (size_t i = 0; i < ctx->font_scale_x; i++) {
size_t gx = ctx->font_scale_x * fx + i;
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t bg = c->bg == 0xffffffff ? canvas_line[gx] : c->bg;
uint32_t fg = c->fg == 0xffffffff ? canvas_line[gx] : c->fg;
+
#else
+
uint32_t bg = c->bg == 0xffffffff ? default_bg : c->bg;
+
uint32_t fg = c->fg == 0xffffffff ? default_bg : c->fg;
+
#endif
fb_line[gx] = draw ? fg : bg;
}
}
···
x = ctx->offset_x + x * ctx->glyph_width;
y = ctx->offset_y + y * ctx->glyph_height;
+
+
#ifdef FLANTERM_FB_DISABLE_CANVAS
+
uint32_t default_bg = ctx->default_bg;
+
#endif
bool *new_glyph = &ctx->font_bool[c->c * ctx->font_height * ctx->font_width];
bool *old_glyph = &ctx->font_bool[old->c * ctx->font_height * ctx->font_width];
···
continue;
for (size_t i = 0; i < ctx->font_scale_x; i++) {
size_t gx = ctx->font_scale_x * fx + i;
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t bg = c->bg == 0xffffffff ? canvas_line[gx] : c->bg;
uint32_t fg = c->fg == 0xffffffff ? canvas_line[gx] : c->fg;
+
#else
+
uint32_t bg = c->bg == 0xffffffff ? default_bg : c->bg;
+
uint32_t fg = c->fg == 0xffffffff ? default_bg : c->fg;
+
#endif
fb_line[gx] = new_draw ? fg : bg;
}
}
···
static void flanterm_fb_full_refresh(struct flanterm_context *_ctx) {
struct flanterm_fb_context *ctx = (void *)_ctx;
+
+
#ifdef FLANTERM_FB_DISABLE_CANVAS
+
uint32_t default_bg = ctx->default_bg;
+
#endif
for (size_t y = 0; y < ctx->height; y++) {
for (size_t x = 0; x < ctx->width; x++) {
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
ctx->framebuffer[y * (ctx->pitch / sizeof(uint32_t)) + x] = ctx->canvas[y * ctx->width + x];
+
#else
+
ctx->framebuffer[y * (ctx->pitch / sizeof(uint32_t)) + x] = default_bg;
+
#endif
}
}
···
_free(ctx->grid, ctx->grid_size);
_free(ctx->queue, ctx->queue_size);
_free(ctx->map, ctx->map_size);
+
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
_free(ctx->canvas, ctx->canvas_size);
+
#endif
+
_free(ctx, sizeof(struct flanterm_fb_context));
}
struct flanterm_context *flanterm_fb_init(
void *(*_malloc)(size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
+
#endif
uint32_t *ansi_colours, uint32_t *ansi_bright_colours,
uint32_t *default_bg, uint32_t *default_fg,
uint32_t *default_bg_bright, uint32_t *default_fg_bright,
···
ctx->map = _malloc(ctx->map_size);
memset(ctx->map, 0, ctx->map_size);
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
ctx->canvas_size = ctx->width * ctx->height * sizeof(uint32_t);
ctx->canvas = _malloc(ctx->canvas_size);
if (canvas != NULL) {
···
ctx->canvas[i] = ctx->default_bg;
}
}
+
#endif
_ctx->raw_putchar = flanterm_fb_raw_putchar;
_ctx->clear = flanterm_fb_clear;
+4
backends/fb.h
···
uint32_t default_fg, default_bg;
uint32_t default_fg_bright, default_bg_bright;
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
size_t canvas_size;
uint32_t *canvas;
+
#endif
size_t grid_size;
size_t queue_size;
···
struct flanterm_context *flanterm_fb_init(
void *(*_malloc)(size_t),
uint32_t *framebuffer, size_t width, size_t height, size_t pitch,
+
#ifndef FLANTERM_FB_DISABLE_CANVAS
uint32_t *canvas,
+
#endif
uint32_t *ansi_colours, uint32_t *ansi_bright_colours,
uint32_t *default_bg, uint32_t *default_fg,
uint32_t *default_bg_bright, uint32_t *default_fg_bright,