summaryrefslogtreecommitdiff
path: root/src/user/lib
diff options
context:
space:
mode:
authordzwdz2022-08-08 20:18:56 +0200
committerdzwdz2022-08-08 20:18:56 +0200
commitcb04eada02bedc5b748e01b09a899dc139c4970f (patch)
tree41b679ec42e1d2aa6c4350e4534030419cc33cf6 /src/user/lib
parent06415b58cdba12cd6a1a5b06f567d11d584364cc (diff)
user/lib: shared libdraw for framebuffer handling
Diffstat (limited to 'src/user/lib')
-rw-r--r--src/user/lib/draw/draw.c50
-rw-r--r--src/user/lib/draw/draw.h19
-rw-r--r--src/user/lib/draw/flush.c43
3 files changed, 112 insertions, 0 deletions
diff --git a/src/user/lib/draw/draw.c b/src/user/lib/draw/draw.c
new file mode 100644
index 0000000..c4508da
--- /dev/null
+++ b/src/user/lib/draw/draw.c
@@ -0,0 +1,50 @@
+#include <camellia/syscalls.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <user/lib/draw/draw.h>
+
+void dirty_reset(struct rect *d) {
+ d->x1 = ~0; d->y1 = ~0;
+ d->x2 = 0; d->y2 = 0;
+}
+
+void dirty_mark(struct rect *d, uint32_t x, uint32_t y) {
+ if (d->x1 > x) d->x1 = x;
+ if (d->x2 < x) d->x2 = x;
+ if (d->y1 > y) d->y1 = y;
+ if (d->y2 < y) d->y2 = y;
+}
+
+int fb_setup(struct framebuf *fb, const char *base) {
+ char path[64], *spec;
+ size_t pos;
+ FILE *f;
+
+ f = fopen(base, "r");
+ if (!f) return -errno;
+
+ pos = strlen(base);
+ memcpy(path, base, pos);
+ spec = path + pos;
+ fread(spec, 1, sizeof(path) - pos, f);
+ /* assumes the read went correctly */
+ fclose(f);
+
+ fb->fd = _syscall_open(path, strlen(path), 0);
+ if (fb->fd < 0) return fb->fd;
+
+ fb->width = strtol(spec, &spec, 0);
+ if (*spec++ != 'x') return -EINVAL;
+ fb->height = strtol(spec, &spec, 0);
+ if (*spec++ != 'x') return -EINVAL;
+ fb->bpp = strtol(spec, &spec, 0);
+ fb->len = _syscall_getsize(fb->fd);
+ fb->pitch = fb->len / fb->height;
+ fb->b = malloc(fb->len);
+
+ if (fb->bpp != 32) return -EINVAL;
+
+ return 0;
+}
diff --git a/src/user/lib/draw/draw.h b/src/user/lib/draw/draw.h
new file mode 100644
index 0000000..ba15840
--- /dev/null
+++ b/src/user/lib/draw/draw.h
@@ -0,0 +1,19 @@
+#pragma once
+#include <camellia/types.h>
+#include <stddef.h>
+#include <stdint.h>
+
+struct framebuf {
+ size_t len, width, height, pitch;
+ uint8_t bpp;
+ char *b;
+
+ handle_t fd;
+};
+
+struct rect { uint32_t x1, y1, x2, y2; };
+void dirty_reset(struct rect *d);
+void dirty_mark(struct rect *d, uint32_t x, uint32_t y);
+void dirty_flush(struct rect *d, struct framebuf *fb);
+
+int fb_setup(struct framebuf *fb, const char *base);
diff --git a/src/user/lib/draw/flush.c b/src/user/lib/draw/flush.c
new file mode 100644
index 0000000..060fdaf
--- /dev/null
+++ b/src/user/lib/draw/flush.c
@@ -0,0 +1,43 @@
+#include <camellia/execbuf.h>
+#include <camellia/syscalls.h>
+#include <user/lib/draw/draw.h>
+
+static void flush_combined(struct rect pix, struct framebuf *fb) {
+ size_t low = fb->pitch * pix.y1 + 4 * pix.x1;
+ size_t high = fb->pitch * pix.y2 + 4 * pix.y2 + 4;
+ _syscall_write(fb->fd, fb->b + low, high - low, low, 0);
+}
+
+static void flush_split(struct rect pix, struct framebuf *fb) {
+ static uint64_t execbuf[EXECBUF_MAX_LEN / sizeof(uint64_t)];
+ size_t epos = 0;
+ if (7 * (pix.y2 - pix.y1) * sizeof(uint64_t) >= sizeof execbuf) {
+ flush_combined(pix, fb);
+ return;
+ }
+
+ for (uint32_t y = pix.y1; y < pix.y2; y++) {
+ size_t low = fb->pitch * y + 4 * pix.x1;
+ size_t high = fb->pitch * y + 4 * pix.x2 + 4;
+
+ execbuf[epos++] = EXECBUF_SYSCALL;
+ execbuf[epos++] = _SYSCALL_WRITE;
+ execbuf[epos++] = fb->fd;
+ execbuf[epos++] = (uintptr_t)fb->b + low;
+ execbuf[epos++] = high - low;
+ execbuf[epos++] = low;
+ execbuf[epos++] = 0;
+ }
+ _syscall_execbuf(execbuf, epos * sizeof(uint64_t));
+}
+
+void dirty_flush(struct rect *d, struct framebuf *fb) {
+ if (~d->x1 == 0) return;
+ if (d->x2 >= fb->width) d->x2 = fb->width - 1;
+ if (d->y2 >= fb->height) d->y2 = fb->height - 1;
+
+ /* the threshold is mostly arbitrary, wasn't based on any real benchmarks */
+ if (d->x2 - d->x1 > fb->width - 600) flush_combined(*d, fb);
+ else flush_split(*d, fb);
+ dirty_reset(d);
+}