From 056e1ebed94379db41ebb2e40cc88a873cfb4411 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 8 Apr 2009 10:17:09 +0000 Subject: initial commit of netsurf framebuffer library svn path=/trunk/libnsfb/; revision=7060 --- include/frontend.h | 35 ++++++++++++++++++++ include/libnsfb.h | 75 ++++++++++++++++++++++++++++++++++++++++++ include/libnsfb_plot.h | 72 +++++++++++++++++++++++++++++++++++++++++ include/nsfb.h | 30 +++++++++++++++++ include/nsfb_plot.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/plot_util.h | 24 ++++++++++++++ include/plotters.h | 1 + 7 files changed, 325 insertions(+) create mode 100644 include/frontend.h create mode 100644 include/libnsfb.h create mode 100644 include/libnsfb_plot.h create mode 100644 include/nsfb.h create mode 100644 include/nsfb_plot.h create mode 100644 include/plot_util.h create mode 100644 include/plotters.h (limited to 'include') diff --git a/include/frontend.h b/include/frontend.h new file mode 100644 index 0000000..ee76632 --- /dev/null +++ b/include/frontend.h @@ -0,0 +1,35 @@ +#include "libnsfb.h" /* exported interface */ +#include "nsfb.h" + +/* frontend default options */ +typedef int (nsfb_fendfn_defaults_t)(nsfb_t *nsfb); +/* frontend init */ +typedef int (nsfb_fendfn_init_t)(nsfb_t *nsfb); +/* frontend finalise */ +typedef int (nsfb_fendfn_fini_t)(nsfb_t *nsfb); +/* frontend input */ +typedef int (nsfb_fendfn_input_t)(nsfb_t *nsfb); +/* frontend area claim */ +typedef int (nsfb_fendfn_claim_t)(nsfb_t *nsfb, nsfb_bbox_t *box); +/* frontend area release */ +typedef int (nsfb_fendfn_release_t)(nsfb_t *nsfb, nsfb_bbox_t *box); + +typedef struct nsfb_frontend_rtns_s { + nsfb_fendfn_defaults_t *defaults; + nsfb_fendfn_init_t *initialise; + nsfb_fendfn_fini_t *finalise; + nsfb_fendfn_input_t *input; + nsfb_fendfn_claim_t *claim; + nsfb_fendfn_release_t *release; +} nsfb_frontend_rtns_t; + +void _nsfb_register_frontend(const enum nsfb_frontend_e type, const nsfb_frontend_rtns_t *rtns, const char *name); + +nsfb_frontend_rtns_t *nsfb_frontend_get_rtns(enum nsfb_frontend_e type); + +/* macro which adds a builtin command with no argument limits */ +#define NSFB_FRONTEND_DEF(__name, __type, __rtns) \ + static void __name##_register_frontend(void) __attribute__((constructor)); \ + void __name##_register_frontend(void) { \ + _nsfb_register_frontend(__type, __rtns, #__name); \ + } diff --git a/include/libnsfb.h b/include/libnsfb.h new file mode 100644 index 0000000..0af8ab3 --- /dev/null +++ b/include/libnsfb.h @@ -0,0 +1,75 @@ +#ifndef _LIBNSFB_H +#define _LIBNSFB_H 1 + +#include + +typedef struct nsfb_cursor_s nsfb_cursor_t; +typedef struct nsfb_s nsfb_t; +typedef uint32_t nsfb_colour_t; + +/* bounding box */ +typedef struct nsfb_bbox_s { + int x0; + int y0; + int x1; + int y1; +} nsfb_bbox_t; + +/** The type of frontend for a framebuffer context. */ +enum nsfb_frontend_e { + NSFB_FRONTEND_NONE = 0, /* Empty frontend. */ + NSFB_FRONTEND_SDL, + NSFB_FRONTEND_LINUX, + NSFB_FRONTEND_VNC, + NSFB_FRONTEND_ABLE, + NSFB_FRONTEND_RAM, +}; + +/** Initialise nsfb context. + * + * This initialises a framebuffer context. + * + * @param frontend The type of frontend to create a context for. + */ +nsfb_t *nsfb_init(enum nsfb_frontend_e frontend); + +/** Initialise selected frontend. + * + * @param nsfb The context frturned from ::nsfb_init + */ +int nsfb_init_frontend(nsfb_t *nsfb); + +/** Process input from a frontend. + */ +int nsfb_input(nsfb_t *nsfb); + +/** Claim an area of screen to be redrawn. + * + * Informs the nsfb library that an area of screen will be updated by the user + * program, used for soft cursor plotting. + * + * @param box The bounding box of the area which might be altered. + */ +int nsfb_claim(nsfb_t *nsfb, nsfb_bbox_t *box); + +/** Release an area of screen which has been redrawn. + * + * Informs the nsfb library that an area of screen has been updated by the user + * program. Some frontends only update on area release. + * + * @param box The bounding box of the area which has been altered. + */ +int nsfb_release(nsfb_t *nsfb, nsfb_bbox_t *box); + +/** Obtain the geometry of a nsfb context. + * + * @param width a variable to store the framebuffer width in or NULL + * @param height a variable to store the framebuffer height in or NULL + * @param bpp a variable to store the framebuffer bpp in or NULL + */ +int nsfb_get_geometry(nsfb_t *nsfb, int *width, int *height, int *bpp); + +/** Obtain the framebuffer memory base and stride. */ +int nsfb_get_framebuffer(nsfb_t *nsfb, uint8_t **ptr, int *linelen); + +#endif diff --git a/include/libnsfb_plot.h b/include/libnsfb_plot.h new file mode 100644 index 0000000..64aa729 --- /dev/null +++ b/include/libnsfb_plot.h @@ -0,0 +1,72 @@ +/** Sets a clip rectangle for subsequent plots. + * + * Sets a clipping area which constrains all subsequent plotting operations. + * The clipping area must lie within the framebuffer visible screen or false + * will be returned and the new clipping area not set. + */ +bool nsfb_plot_set_clip(nsfb_t *nsfb, nsfb_bbox_t *clip); + +/** Clears plotting area to a flat colour. + */ +bool nsfb_plot_clg(nsfb_t *nsfb, nsfb_colour_t c); + +/** Plots a rectangle outline. + * + * The line can be solid, dotted or dashed. Top left corner at (x0,y0) and + * rectangle has given width and height. + */ +bool nsfb_plot_rectangle(nsfb_t *nsfb, nsfb_bbox_t *rect, int line_width, nsfb_colour_t c, bool dotted, bool dashed); + +/** Plots a filled rectangle. Top left corner at (x0,y0), bottom + * right corner at (x1,y1). Note: (x0,y0) is inside filled area, + * but (x1,y1) is below and to the right. See diagram below. + */ +bool nsfb_plot_rectangle_fill(nsfb_t *nsfb, nsfb_bbox_t *rect, nsfb_colour_t c); + +/** Plots a line. + * + * Draw a line from (x0,y0) to (x1,y1). Coordinates are at centre of line + * width/thickness. + */ +bool nsfb_plot_line(nsfb_t *nsfb, nsfb_bbox_t *line, int line_width, nsfb_colour_t c, bool dotted, bool dashed); + +/** Plots a filled polygon. + * + * Plots a filled polygon with straight lines between points. The lines around + * the edge of the ploygon are not plotted. The polygon is filled with a + * non-zero winding rule. + * + * + */ +bool nsfb_plot_polygon(nsfb_t *nsfb, const int *p, unsigned int n, nsfb_colour_t fill); + +/** Plot an ellipse. + */ +bool nsfb_plot_ellipse(nsfb_t *nsfb, nsfb_bbox_t *ellipse, nsfb_colour_t c); + +/** Plot a filled ellipse. + */ +bool nsfb_plot_ellipse_fill(nsfb_t *nsfb, nsfb_bbox_t *ellipse, nsfb_colour_t c); + +/** Plots an arc. + * + * around (x,y), from anticlockwise from angle1 to angle2. Angles are measured + * anticlockwise from horizontal, in degrees. + */ +bool nsfb_plot_arc(nsfb_t *nsfb, int x, int y, int radius, int angle1, int angle2, nsfb_colour_t c); + +/** Plots an alpha blended pixel. + * + * plots an alpha blended pixel. + */ +bool nsfb_plot_point(nsfb_t *nsfb, int x, int y, nsfb_colour_t c); + +/** copy an area of screen + * + * Copy an area of the display. + */ +bool nsfb_plot_copy(nsfb_t *nsfb, int srcx, int srcy, int width, int height, int dstx, int dsty); + +/** Plot bitmap. + */ +bool nsfb_plot_bitmap(nsfb_t *nsfb, nsfb_bbox_t *loc, nsfb_colour_t *pixel, int bmp_width, int bmp_height, int bmp_stride, bool alpha); diff --git a/include/nsfb.h b/include/nsfb.h new file mode 100644 index 0000000..cec4a63 --- /dev/null +++ b/include/nsfb.h @@ -0,0 +1,30 @@ +#ifndef _NSFB_H +#define _NSFB_H 1 + +#include + +/** NS Framebuffer context + */ +struct nsfb_s { + int width; /**< Visible width. */ + int height; /**< Visible height. */ + int bpp; /**< Bits per pixel. */ + + int refresh; /**< Desired refresh rate for physical displays. */ + char *output_dev; /**> Path to output device for frontends that require it. */ + + uint8_t *ptr; /**< Base of video memory. */ + int linelen; /**< length of a video line. */ + + nsfb_colour_t palette[256]; /**< palette for index modes */ + nsfb_cursor_t *cursor; + + struct nsfb_frontend_rtns_s *frontend_rtns; /**< frontend routines. */ + void *frontend_priv; + + nsfb_bbox_t clip; /**< current clipping rectangle for plotters */ + struct nsfb_plotter_fns_s *plotter_fns; /**< Plotter methods */ +}; + + +#endif diff --git a/include/nsfb_plot.h b/include/nsfb_plot.h new file mode 100644 index 0000000..2dbd22a --- /dev/null +++ b/include/nsfb_plot.h @@ -0,0 +1,88 @@ + + +/** Clears plotting area to a flat colour (if needed) + */ +typedef bool (nsfb_plotfn_clg_t)(nsfb_t *nsfb, nsfb_colour_t c); + +/** Plots a rectangle outline. The line can be solid, dotted or + * dashed. Top left corner at (x0,y0) and rectangle has given + * width and height. + */ +typedef bool (nsfb_plotfn_rectangle_t)(nsfb_t *nsfb, nsfb_bbox_t *rect, int line_width, nsfb_colour_t c, bool dotted, bool dashed); + +/** Plots a line from (x0,y0) to (x1,y1). Coordinates are at + * centre of line width/thickness. + */ +typedef bool (nsfb_plotfn_line_t)(nsfb_t *nsfb, nsfb_bbox_t *line, int line_width, nsfb_colour_t c, bool dotted, bool dashed); + +/** Plots a filled polygon with straight lines between points. + * The lines around the edge of the ploygon are not plotted. The + * polygon is filled with the non-zero winding rule. + */ +typedef bool (nsfb_plotfn_polygon_t)(nsfb_t *nsfb, const int *p, unsigned int n, nsfb_colour_t fill); + +/** Plots a filled rectangle. Top left corner at (x0,y0), bottom + * right corner at (x1,y1). Note: (x0,y0) is inside filled area, + * but (x1,y1) is below and to the right. See diagram below. + */ +typedef bool (nsfb_plotfn_fill_t)(nsfb_t *nsfb, nsfb_bbox_t *rect, nsfb_colour_t c); + +/** Sets a clip rectangle for subsequent plots. + */ +typedef bool (nsfb_plotfn_clip_t)(nsfb_t *nsfb, nsfb_bbox_t *clip); + +/** Plots an arc, around (x,y), from anticlockwise from angle1 to + * angle2. Angles are measured anticlockwise from horizontal, in + * degrees. + */ +typedef bool (nsfb_plotfn_arc_t)(nsfb_t *nsfb, int x, int y, int radius, int angle1, int angle2, nsfb_colour_t c); + +/** Plots a point. + * + * Plot a single alpha blended pixel. + */ +typedef bool (nsfb_plotfn_point_t)(nsfb_t *nsfb, int x, int y, nsfb_colour_t c); + +/** Plot an ellipse. + * + * plot an ellipse outline, note if teh bounding box is square this will plot a + * circle. + */ +typedef bool (nsfb_plotfn_ellipse_t)(nsfb_t *nsfb, nsfb_bbox_t *ellipse, nsfb_colour_t c); + +/** Plot a filled ellipse. + * + * plot a filled ellipse, note if the bounding box is square this will plot a + * circle. + */ +typedef bool (nsfb_plotfn_ellipse_fill_t)(nsfb_t *nsfb, nsfb_bbox_t *ellipse, nsfb_colour_t c); + + +/** Plot bitmap + */ +typedef bool (nsfb_plotfn_bitmap_t)(nsfb_t *nsfb, nsfb_bbox_t *loc, nsfb_colour_t *pixel, int bmp_width, int bmp_height, int bmp_stride, bool alpha); + + +/** Copy an area of screen + * + * Copy an area of the display. + */ +typedef bool (nsfb_plotfn_copy_t)(nsfb_t *nsfb, int srcx, int srcy, int width, int height, int dstx, int dsty); + +/** plotter function table. */ +typedef struct nsfb_plotter_fns_s { + nsfb_plotfn_clg_t *clg; + nsfb_plotfn_rectangle_t *rectangle; + nsfb_plotfn_line_t *line; + nsfb_plotfn_polygon_t *polygon; + nsfb_plotfn_fill_t *fill; + nsfb_plotfn_clip_t *clip; + nsfb_plotfn_ellipse_t *ellipse; + nsfb_plotfn_ellipse_fill_t *ellipse_fill; + nsfb_plotfn_arc_t *arc; + nsfb_plotfn_bitmap_t *bitmap; + nsfb_plotfn_point_t *point; + nsfb_plotfn_copy_t *copy; +} nsfb_plotter_fns_t; + + diff --git a/include/plot_util.h b/include/plot_util.h new file mode 100644 index 0000000..6565cb1 --- /dev/null +++ b/include/plot_util.h @@ -0,0 +1,24 @@ +/* alpha blend two pixels together */ +static inline nsfb_colour_t +nsfb_plot_ablend(nsfb_colour_t pixel, nsfb_colour_t scrpixel) +{ + int opacity = pixel >> 24; + int transp = 0x100 - opacity; + uint32_t rb, g; + + rb = ((pixel & 0xFF00FF) * opacity + + (scrpixel & 0xFF00FF) * transp) >> 8; + g = ((pixel & 0x00FF00) * opacity + + (scrpixel & 0x00FF00) * transp) >> 8; + + return (rb & 0xFF00FF) | (g & 0xFF00); +} + + +bool nsfb_plot_clip(const nsfb_bbox_t * restrict clip, nsfb_bbox_t * restrict rect); + +bool nsfb_plot_clip_ctx(nsfb_t *nsfb, nsfb_bbox_t * restrict rect); + +bool nsfb_plot_clip_line(const nsfb_bbox_t * restrict clip, nsfb_bbox_t * restrict line); + +bool nsfb_plot_clip_line_ctx(nsfb_t *nsfb, nsfb_bbox_t * restrict line); diff --git a/include/plotters.h b/include/plotters.h new file mode 100644 index 0000000..dc4648b --- /dev/null +++ b/include/plotters.h @@ -0,0 +1 @@ +bool select_plotters(nsfb_t *nsfb); -- cgit v1.2.3