Add software rasterizer library and demo.

The software rasterizer is to be used with raw framebuffer
devices, where no GPU or X11 is available.
The demo emulates a raw framebuffer on X11 using XShmImage / XImage.

Features implemented:
* Drawing primitives
* Drawing polygons (using Darel Rex Finley algorithm)
* Drawing arcs and circles (using Bresenham's elipses algorithm)
* Drawing images using nearest filtering
* Bounds check on every operation
* Fastpath for lines
* Font rendering using nearest filtering
* Window resize
* Thread safe implementation by using a context
* Fixed lower and upper scissors on fast-path
* Adapted coding style to nuklear's style
* Implemented text scissors

Color formats:
Define one of them at compile time.
* RAWFB_RGBX_8888 (32bpp)
* RAWFB_XRGB_8888 (32bpp)

Tested:
The library has been tested on Lenovo Thinkpad T500 and is able to render
more than 30fps on a single core with no further optimizations and VSNYC enabled.

TODO:
* Improve font rendering by using filters.
* Account font foreground color.

Usage:
The raw framebuffer library needs a "texture" that holds the prerendered
font data. The texture is used at runtime to blit the letters onto screen.
You have to provide the framebuffer address, dimension and pitch.

Signed-off-by: Patrick Rudolph <siro@das-labor.org>
This commit is contained in:
Patrick Rudolph 2016-12-06 07:28:54 +01:00
parent e888338498
commit 2eb72b26e4
4 changed files with 1682 additions and 0 deletions

13
demo/x11_rawfb/Makefile Normal file
View File

@ -0,0 +1,13 @@
# Install
BIN = zahnrad
# Flags
CFLAGS = -std=c89 -pedantic -O2 -Wunused -DRAWFB_XRGB_8888
SRC = main.c
OBJ = $(SRC:.c=.o)
$(BIN):
@mkdir -p bin
rm -f bin/$(BIN) $(OBJS)
$(CC) $(SRC) $(CFLAGS) -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -o bin/$(BIN) -lX11 -lXext -lm

221
demo/x11_rawfb/main.c Normal file
View File

@ -0,0 +1,221 @@
/* nuklear - v1.17 - public domain */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION
#define NK_XLIBSHM_IMPLEMENTATION
#define NK_RAWFB_IMPLEMENTATION
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h"
#include "nuklear_rawfb.h"
#include "nuklear_xlib.h"
#define DTIME 20
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define UNUSED(a) (void)a
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a)/sizeof(a)[0])
typedef struct XWindow XWindow;
struct XWindow {
Display *dpy;
Window root;
Visual *vis;
Colormap cmap;
XWindowAttributes attr;
XSetWindowAttributes swa;
Window win;
int screen;
unsigned int width;
unsigned int height;
};
static void
die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputs("\n", stderr);
exit(EXIT_FAILURE);
}
static void*
xcalloc(size_t siz, size_t n)
{
void *ptr = calloc(siz, n);
if (!ptr) die("Out of memory\n");
return ptr;
}
static long
timestamp(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) return 0;
return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
}
static void
sleep_for(long t)
{
struct timespec req;
const time_t sec = (int)(t/1000);
const long ms = t - (sec * 1000);
req.tv_sec = sec;
req.tv_nsec = ms * 1000000L;
while(-1 == nanosleep(&req, &req));
}
/* ===============================================================
*
* EXAMPLE
*
* ===============================================================*/
/* This are some code examples to provide a small overview of what can be
* done with this library. To try out an example uncomment the include
* and the corresponding function. */
/*#include "../style.c"*/
/*#include "../calculator.c"*/
#include "../overview.c"
#include "../node_editor.c"
/* ===============================================================
*
* DEMO
*
* ===============================================================*/
int
main(void)
{
long dt;
long started;
int running = 1;
int status;
XWindow xw;
struct rawfb_context *rawfb;
void *fb = NULL;
unsigned char tex_scratch[512 * 512];
XSizeHints hints;
/* X11 */
memset(&xw, 0, sizeof xw);
xw.dpy = XOpenDisplay(NULL);
if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
xw.root = DefaultRootWindow(xw.dpy);
xw.screen = XDefaultScreen(xw.dpy);
xw.vis = XDefaultVisual(xw.dpy, xw.screen);
xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
xw.swa.colormap = xw.cmap;
xw.swa.event_mask =
ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPress | ButtonReleaseMask| ButtonMotionMask |
Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
PointerMotionMask | KeymapStateMask | EnterWindowMask | LeaveWindowMask;
xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
XDefaultDepth(xw.dpy, xw.screen), InputOutput,
xw.vis, CWEventMask | CWColormap, &xw.swa);
XStoreName(xw.dpy, xw.win, "X11");
XMapWindow(xw.dpy, xw.win);
XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
xw.width = (unsigned int)xw.attr.width;
xw.height = (unsigned int)xw.attr.height;
/* Framebuffer emulator */
status = nk_xlib_init(xw.dpy, xw.vis, xw.screen, xw.win, xw.width, xw.height, &fb);
if (!status || !fb)
return 0;
/* GUI */
rawfb = nk_rawfb_init(fb, tex_scratch, xw.width, xw.height, xw.width * 4);
if (!rawfb)
running = 0;
/* style.c */
/*set_style(ctx, THEME_WHITE);*/
/*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
while (running) {
/* Input */
XEvent evt;
started = timestamp();
nk_input_begin(&rawfb->ctx);
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
if (XFilterEvent(&evt, xw.win)) continue;
nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt, rawfb);
}
nk_input_end(&rawfb->ctx);
/* GUI */
if (nk_begin(&rawfb->ctx, "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
enum {EASY, HARD};
static int op = EASY;
static int property = 20;
nk_layout_row_static(&rawfb->ctx, 30, 80, 1);
if (nk_button_label(&rawfb->ctx, "button"))
fprintf(stdout, "button pressed\n");
nk_layout_row_dynamic(&rawfb->ctx, 30, 2);
if (nk_option_label(&rawfb->ctx, "easy", op == EASY)) op = EASY;
if (nk_option_label(&rawfb->ctx, "hard", op == HARD)) op = HARD;
nk_layout_row_dynamic(&rawfb->ctx, 25, 1);
nk_property_int(&rawfb->ctx, "Compression:", 0, &property, 100, 10, 1);
}
nk_end(&rawfb->ctx);
if (nk_window_is_closed(&rawfb->ctx, "Demo")) break;
/* -------------- EXAMPLES ---------------- */
/*calculator(ctx);*/
overview(&rawfb->ctx);
node_editor(&rawfb->ctx);
/* ----------------------------------------- */
/* Draw framebuffer */
nk_rawfb_render(rawfb, nk_rgb(30,30,30), 1);
/* Emulate framebuffer */
XClearWindow(xw.dpy, xw.win);
nk_xlib_render(xw.win);
XFlush(xw.dpy);
/* Timing */
dt = timestamp() - started;
if (dt < DTIME)
sleep_for(DTIME - dt);
}
nk_rawfb_shutdown(rawfb);
nk_xlib_shutdown();
XUnmapWindow(xw.dpy, xw.win);
XFreeColormap(xw.dpy, xw.cmap);
XDestroyWindow(xw.dpy, xw.win);
XCloseDisplay(xw.dpy);
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,298 @@
/*
* Nuklear - v1.17 - public domain
* no warrenty implied; use at your own risk.
* authored from 2015-2016 by Micha Mettke
* Copyright 2016 Patrick Rudolph
*/
/*
* ==============================================================
*
* API
*
* ===============================================================
*/
#ifndef NK_XLIBSHM_H_
#define NK_XLIBSHM_H_
#include <X11/Xlib.h>
NK_API int nk_xlib_init(Display *dpy, Visual *vis, int screen, Window root, unsigned int w, unsigned int h, void **fb);
NK_API int nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt, struct rawfb_context *rawfb);
NK_API void nk_xlib_render(Drawable screen);
NK_API void nk_xlib_shutdown(void);
#endif
/*
* ==============================================================
*
* IMPLEMENTATION
*
* ===============================================================
*/
#ifdef NK_XLIBSHM_IMPLEMENTATION
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/Xlocale.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
static struct {
struct nk_context ctx;
struct XSurface *surf;
Cursor cursor;
Display *dpy;
Window root;
XImage *ximg;
XShmSegmentInfo xsi;
char fallback;
GC gc;
} xlib;
NK_API int
nk_xlib_init(Display *dpy, Visual *vis, int screen, Window root,
unsigned int w, unsigned int h, void **fb)
{
unsigned int depth = XDefaultDepth(dpy, screen);
xlib.dpy = dpy;
xlib.root = root;
if (!setlocale(LC_ALL,"")) return 0;
if (!XSupportsLocale()) return 0;
if (!XSetLocaleModifiers("@im=none")) return 0;
/* create invisible cursor */
{static XColor dummy; char data[1] = {0};
Pixmap blank = XCreateBitmapFromData(dpy, root, data, 1, 1);
if (blank == None) return 0;
xlib.cursor = XCreatePixmapCursor(dpy, blank, blank, &dummy, &dummy, 0, 0);
XFreePixmap(dpy, blank);}
xlib.fallback = False;
do
{
int status;
/* Initialize shared memory according to:
* https://www.x.org/archive/X11R7.5/doc/Xext/mit-shm.html */
if (!XShmQueryExtension(dpy))
{
printf("No XShm Extension available.\n");
xlib.fallback = True;
break;
}
xlib.ximg = XShmCreateImage(dpy, vis, depth, ZPixmap, NULL, &xlib.xsi, w, h);
if (!xlib.ximg)
{
xlib.fallback = True;
break;
}
xlib.xsi.shmid = shmget(IPC_PRIVATE, xlib.ximg->bytes_per_line * xlib.ximg->height, IPC_CREAT | 0777);
if (xlib.xsi.shmid < 0)
{
XDestroyImage(xlib.ximg);
xlib.fallback = True;
break;
}
xlib.xsi.shmaddr = xlib.ximg->data = shmat(xlib.xsi.shmid, NULL, 0);
if ((size_t)xlib.xsi.shmaddr < 0)
{
XDestroyImage(xlib.ximg);
xlib.fallback = True;
break;
}
xlib.xsi.readOnly = False;
status = XShmAttach(dpy, &xlib.xsi);
if (!status)
{
shmdt(xlib.xsi.shmaddr);
XDestroyImage(xlib.ximg);
xlib.fallback = True;
break;
}
XSync(dpy, False);
shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
} while(0);
if (xlib.fallback)
{
xlib.ximg = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL, w, h, 32, 0);
if (!xlib.ximg)
return 0;
xlib.ximg->data = malloc(h * xlib.ximg->bytes_per_line);
if (!xlib.ximg->data)
return 0;
}
xlib.gc = XDefaultGC(dpy, screen);
*fb = xlib.ximg->data;
return 1;
}
NK_API int
nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt, struct rawfb_context *rawfb)
{
/* optional grabbing behavior */
if (rawfb->ctx.input.mouse.grab) {
/* XDefineCursor(xlib.dpy, xlib.root, xlib.cursor); */
rawfb->ctx.input.mouse.grab = 0;
} else if (rawfb->ctx.input.mouse.ungrab) {
XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0,
(int)rawfb->ctx.input.mouse.prev.x, (int)rawfb->ctx.input.mouse.prev.y);
/* XUndefineCursor(xlib.dpy, xlib.root); */
rawfb->ctx.input.mouse.ungrab = 0;
}
if (evt->type == KeyPress || evt->type == KeyRelease)
{
/* Key handler */
int ret, down = (evt->type == KeyPress);
KeySym *code = XGetKeyboardMapping(xlib.dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
if (*code == XK_Shift_L || *code == XK_Shift_R) nk_input_key(&rawfb->ctx, NK_KEY_SHIFT, down);
else if (*code == XK_Delete) nk_input_key(&rawfb->ctx, NK_KEY_DEL, down);
else if (*code == XK_Return) nk_input_key(&rawfb->ctx, NK_KEY_ENTER, down);
else if (*code == XK_Tab) nk_input_key(&rawfb->ctx, NK_KEY_TAB, down);
else if (*code == XK_Left) nk_input_key(&rawfb->ctx, NK_KEY_LEFT, down);
else if (*code == XK_Right) nk_input_key(&rawfb->ctx, NK_KEY_RIGHT, down);
else if (*code == XK_Up) nk_input_key(&rawfb->ctx, NK_KEY_UP, down);
else if (*code == XK_Down) nk_input_key(&rawfb->ctx, NK_KEY_DOWN, down);
else if (*code == XK_BackSpace) nk_input_key(&rawfb->ctx, NK_KEY_BACKSPACE, down);
else if (*code == XK_Escape) nk_input_key(&rawfb->ctx, NK_KEY_TEXT_RESET_MODE, down);
else if (*code == XK_Page_Up) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_UP, down);
else if (*code == XK_Page_Down) nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_DOWN, down);
else if (*code == XK_Home) {
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_START, down);
nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_START, down);
} else if (*code == XK_End) {
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_END, down);
nk_input_key(&rawfb->ctx, NK_KEY_SCROLL_END, down);
} else {
if (*code == 'c' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_COPY, down);
else if (*code == 'v' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_PASTE, down);
else if (*code == 'x' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_CUT, down);
else if (*code == 'z' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_UNDO, down);
else if (*code == 'r' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REDO, down);
else if (*code == XK_Left && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_LEFT, down);
else if (*code == XK_Right && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_WORD_RIGHT, down);
else if (*code == 'b' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_START, down);
else if (*code == 'e' && (evt->xkey.state & ControlMask))
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_LINE_END, down);
else {
if (*code == 'i')
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_INSERT_MODE, down);
else if (*code == 'r')
nk_input_key(&rawfb->ctx, NK_KEY_TEXT_REPLACE_MODE, down);
if (down) {
char buf[32];
KeySym keysym = 0;
if (XLookupString((XKeyEvent*)evt, buf, 32, &keysym, NULL) != NoSymbol)
nk_input_glyph(&rawfb->ctx, buf);
}
}
}
XFree(code);
return 1;
} else if (evt->type == ButtonPress || evt->type == ButtonRelease) {
/* Button handler */
int down = (evt->type == ButtonPress);
const int x = evt->xbutton.x, y = evt->xbutton.y;
if (evt->xbutton.button == Button1)
nk_input_button(&rawfb->ctx, NK_BUTTON_LEFT, x, y, down);
if (evt->xbutton.button == Button2)
nk_input_button(&rawfb->ctx, NK_BUTTON_MIDDLE, x, y, down);
else if (evt->xbutton.button == Button3)
nk_input_button(&rawfb->ctx, NK_BUTTON_RIGHT, x, y, down);
else if (evt->xbutton.button == Button4)
nk_input_scroll(&rawfb->ctx, nk_vec2(0, 1.0f));
else if (evt->xbutton.button == Button5)
nk_input_scroll(&rawfb->ctx, nk_vec2(0, -1.0f));
else return 0;
return 1;
} else if (evt->type == MotionNotify) {
/* Mouse motion handler */
const int x = evt->xmotion.x, y = evt->xmotion.y;
nk_input_motion(&rawfb->ctx, x, y);
if (rawfb->ctx.input.mouse.grabbed) {
rawfb->ctx.input.mouse.pos.x = rawfb->ctx.input.mouse.prev.x;
rawfb->ctx.input.mouse.pos.y = rawfb->ctx.input.mouse.prev.y;
XWarpPointer(xlib.dpy, None, xlib.root, 0, 0, 0, 0, (int)rawfb->ctx.input.mouse.pos.x, (int)rawfb->ctx.input.mouse.pos.y);
}
return 1;
} else if (evt->type == Expose || evt->type == ConfigureNotify) {
/* Window resize handler */
XWindowAttributes attr;
XGetWindowAttributes(dpy, win, &attr);
unsigned int width, height;
void *fb;
width = (unsigned int)attr.width;
height = (unsigned int)attr.height;
nk_xlib_shutdown();
nk_xlib_init(dpy, XDefaultVisual(dpy, screen), screen, win,
width, height, &fb);
nk_rawfb_resize_fb(rawfb, fb, width, height, width * 4);
} else if (evt->type == KeymapNotify) {
XRefreshKeyboardMapping(&evt->xmapping);
return 1;
} else if (evt->type == LeaveNotify) {
XUndefineCursor(xlib.dpy, xlib.root);
} else if (evt->type == EnterNotify) {
XDefineCursor(xlib.dpy, xlib.root, xlib.cursor);
}
return 0;
}
NK_API void
nk_xlib_shutdown(void)
{
XFreeCursor(xlib.dpy, xlib.cursor);
if (xlib.fallback)
{
free(xlib.ximg->data);
XDestroyImage(xlib.ximg);
}
else
{
XShmDetach(xlib.dpy, &xlib.xsi);
XDestroyImage(xlib.ximg);
shmdt(xlib.xsi.shmaddr);
shmctl(xlib.xsi.shmid, IPC_RMID, NULL);
}
nk_memset(&xlib, 0, sizeof(xlib));
}
NK_API void
nk_xlib_render(Drawable screen)
{
if (xlib.fallback)
XPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height);
else
XShmPutImage(xlib.dpy, screen, xlib.gc, xlib.ximg,
0, 0, 0, 0, xlib.ximg->width, xlib.ximg->height, False);
}
#endif