The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
gfx.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file gfx.h
5 * @brief libgfx: drawing into pixel buffers in memory.
6 *
7 * every buffer has one pixel format: 32 bits per pixel, premultiplied
8 * ARGB (0xAARRGGBB in a uint32_t, each colour already multiplied by the
9 * alpha). premultiplied pixels make blending one multiply per channel,
10 * and scaling and blending give no dark edges. only gfx_present changes
11 * the format, when it copies a buffer to the framebuffer.
12 *
13 * coordinates are pixels, x to the right and y down, with (0, 0) at the
14 * top left. a rectangle includes its left and top edges and excludes its
15 * right and bottom edges. every drawing call is clipped to the buffer's
16 * clip rectangle, so drawing outside the buffer is safe and does nothing.
17 *
18 * the drawing is integer only, so the host and ginnOS make the same
19 * pixels (the host tests compare golden images).
20 */
21
22#include <stdbool.h>
23#include <stddef.h>
24#include <stdint.h>
25
26/**
27 * a premultiplied ARGB colour, 0xAARRGGBB. make one with gfx_rgb or
28 * gfx_rgba.
29 */
31
32typedef struct
33{
36 int32_t w; // 0 or less: the rectangle is empty
39
40typedef struct
41{
45 int32_t stride; // pixels from one row to the next (at least width)
46 gfx_rect_t clip; // drawing stays inside this (and inside the buffer)
47 bool owned; // gfx_buffer_free frees the pixels
49
50/**
51 * the pixel format of a framebuffer, for gfx_present. the sizes and
52 * shifts are those of the framebuffer's FBIOGET_VSCREENINFO.
53 */
64
65#define GFX_TRANSPARENT ((gfx_color_t)0)
66
67static inline gfx_color_t gfx_rgb(uint8_t r, uint8_t g, uint8_t b)
68{
69 return 0xFF000000u | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
70}
71
72/**
73 * a colour with alpha, from straight (not premultiplied) channels.
74 */
76
77static inline uint8_t gfx_alpha(gfx_color_t c)
78{
79 return (uint8_t)(c >> 24);
80}
81
82/**
83 * a colour with its alpha (and so its premultiplied channels) scaled by
84 * opacity / 255.
85 */
87
88/**
89 * a source pixel over a destination pixel (Porter-Duff source over).
90 */
92
93/**
94 * a mix of two colours, as CSS color-mix in sRGB: amount of b (0 to 255)
95 * and the rest of a.
96 */
98
99static inline gfx_rect_t gfx_rect(int32_t x, int32_t y, int32_t w, int32_t h)
100{
101 gfx_rect_t r = {x, y, w, h};
102 return r;
103}
104
105static inline bool gfx_rect_empty(gfx_rect_t r)
106{
107 return r.w <= 0 || r.h <= 0;
108}
109
110/**
111 * the part two rectangles share (empty if none).
112 */
114
115/**
116 * the smallest rectangle that holds both (an empty one counts as none).
117 */
119
121
122/**
123 * a new buffer of transparent pixels, from malloc.
124 * @return the buffer, or NULL (size 0 or out of memory).
125 */
127
128/**
129 * free a buffer from gfx_buffer_new (and its pixels, if it owns them).
130 */
132
133/**
134 * make a buffer over memory the caller owns (a shared memory mapping, for
135 * example). the clip rectangle is the whole buffer.
136 * @param stride pixels from one row to the next.
137 */
138void gfx_buffer_init(gfx_buffer_t *buf, uint32_t *pixels, int32_t width, int32_t height,
139 int32_t stride);
140
141/**
142 * a buffer for a part of another buffer: the same pixels, its own clip.
143 * the rectangle is clipped to the parent. it is not freed on its own.
144 */
146
147/**
148 * set the clip rectangle (it is clipped to the buffer).
149 */
150void gfx_set_clip(gfx_buffer_t *buf, gfx_rect_t clip);
151
152/**
153 * make the whole buffer the clip rectangle.
154 */
156
157static inline uint32_t *gfx_pixel_at(const gfx_buffer_t *buf, int32_t x, int32_t y)
158{
159 return buf->pixels + (ptrdiff_t)y * buf->stride + x;
160}
161
162/**
163 * set every pixel in the clip rectangle to a colour (no blending).
164 */
165void gfx_clear(gfx_buffer_t *buf, gfx_color_t color);
166
167/**
168 * fill a rectangle, blended over what is there.
169 */
171
172/**
173 * the edges of a rectangle, thickness pixels wide, on the inside.
174 */
175void gfx_stroke_rect(gfx_buffer_t *buf, gfx_rect_t r, int32_t thickness, gfx_color_t color);
176
177/**
178 * fill a rectangle with round corners, with anti-aliased corners (as CSS
179 * border-radius).
180 * @param radius the corner radius (made smaller if the rectangle is small).
181 */
183
184/**
185 * fill a round rectangle with a vertical gradient from top to bottom,
186 * sampled at the pixel centres (as a CSS linear-gradient).
187 */
189 gfx_color_t top, gfx_color_t bottom);
190
191/**
192 * the border of a round rectangle: width pixels wide, inside r (as a CSS
193 * border with box-sizing: border-box). the inside is not touched.
194 */
196 gfx_color_t color);
197
198/**
199 * a CSS box-shadow: "x y blur spread color", and "inset".
200 */
201typedef struct
202{
203 int32_t x; // the offset
205 int32_t blur; // the CSS blur radius: a Gaussian blur with sigma = blur / 2
206 int32_t spread; // grows the shape (shrinks it for an inset shadow)
208 bool inset; // inside the box instead of outside it
210
211/**
212 * draw a box-shadow of a round box. an outer shadow is drawn only outside
213 * the box and an inset one only inside it, as in CSS: draw outer shadows
214 * before the box, inset ones after its background.
215 * @param box the border box (an inset shadow: the box inside the border).
216 */
217void gfx_box_shadow(gfx_buffer_t *buf, gfx_rect_t box, int32_t radius, const gfx_shadow_t *shadow);
218
219/**
220 * a line one pixel wide from (x0, y0) to (x1, y1), both ends included,
221 * blended over what is there.
222 */
223void gfx_line(gfx_buffer_t *buf, int32_t x0, int32_t y0, int32_t x1, int32_t y1,
224 gfx_color_t color);
225
226/**
227 * copy a part of src to dst at (x, y), replacing the pixels (no blending).
228 * the buffers may be the same one, and the parts may overlap.
229 */
230void gfx_copy(gfx_buffer_t *dst, int32_t x, int32_t y, const gfx_buffer_t *src,
231 gfx_rect_t src_rect);
232
233/**
234 * blend a part of src over dst at (x, y), with an opacity (255: as it is).
235 */
236void gfx_blend(gfx_buffer_t *dst, int32_t x, int32_t y, const gfx_buffer_t *src,
237 gfx_rect_t src_rect, uint8_t opacity);
238
239/**
240 * draw a part of src scaled to fill dst_rect, blended over dst, with
241 * bilinear filtering.
242 */
243void gfx_blend_scaled(gfx_buffer_t *dst, gfx_rect_t dst_rect, const gfx_buffer_t *src,
244 gfx_rect_t src_rect, uint8_t opacity);
245
246/**
247 * a coverage mask (one byte per pixel, 0 to 255) in a colour, blended
248 * over dst with its top left at (x, y). text and line icons are drawn
249 * this way.
250 * @param stride bytes from one mask row to the next.
251 */
252void gfx_draw_mask(gfx_buffer_t *dst, int32_t x, int32_t y, const uint8_t *mask, int32_t width,
253 int32_t height, int32_t stride, gfx_color_t color);
254
255/**
256 * a vertical gradient from top to bottom colour over a rectangle
257 * (replacing the pixels). for backgrounds.
258 */
260
261/**
262 * blur the whole buffer like a Gaussian with this sigma (in pixels); the
263 * edge pixels are used again past the edges.
264 */
265void gfx_blur(gfx_buffer_t *buf, double sigma);
266
267/**
268 * copy a part of a buffer to a framebuffer, in its pixel format. the
269 * pixels are taken as over black (alpha is dropped).
270 * @param fb the framebuffer's first pixel.
271 * @param fb_pitch bytes from one framebuffer row to the next.
272 * @param fb_width the framebuffer's size, to clip to.
273 * @param src_rect the part of src; it goes to the same place on the
274 * framebuffer.
275 */
276void gfx_present(void *fb, uint32_t fb_pitch, int32_t fb_width, int32_t fb_height,
277 const gfx_format_t *format, const gfx_buffer_t *src, gfx_rect_t src_rect);
gfx_color_t gfx_over(gfx_color_t dst, gfx_color_t src)
a source pixel over a destination pixel (Porter-Duff source over).
Definition gfx.c:21
void gfx_stroke_rect(gfx_buffer_t *buf, gfx_rect_t r, int32_t thickness, gfx_color_t color)
the edges of a rectangle, thickness pixels wide, on the inside.
Definition gfx.c:149
void gfx_clear(gfx_buffer_t *buf, gfx_color_t color)
set every pixel in the clip rectangle to a colour (no blending).
Definition gfx.c:131
gfx_rect_t gfx_rect_union(gfx_rect_t a, gfx_rect_t b)
the smallest rectangle that holds both (an empty one counts as none).
Definition gfx.c:42
gfx_rect_t gfx_rect_intersect(gfx_rect_t a, gfx_rect_t b)
the part two rectangles share (empty if none).
Definition gfx.c:31
void gfx_fill_rect(gfx_buffer_t *buf, gfx_rect_t r, gfx_color_t color)
fill a rectangle, blended over what is there.
Definition gfx.c:142
void gfx_set_clip(gfx_buffer_t *buf, gfx_rect_t clip)
set the clip rectangle (it is clipped to the buffer).
Definition gfx.c:106
void gfx_reset_clip(gfx_buffer_t *buf)
make the whole buffer the clip rectangle.
Definition gfx.c:111
void gfx_buffer_free(gfx_buffer_t *buf)
free a buffer from gfx_buffer_new (and its pixels, if it owns them).
Definition gfx.c:77
gfx_color_t gfx_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
a colour with alpha, from straight (not premultiplied) channels.
Definition gfx.c:11
void gfx_present(void *fb, uint32_t fb_pitch, int32_t fb_width, int32_t fb_height, const gfx_format_t *format, const gfx_buffer_t *src, gfx_rect_t src_rect)
copy a part of a buffer to a framebuffer, in its pixel format.
Definition gfx.c:261
bool gfx_rect_contains(gfx_rect_t r, int32_t x, int32_t y)
Definition gfx.c:55
void gfx_buffer_init(gfx_buffer_t *buf, uint32_t *pixels, int32_t width, int32_t height, int32_t stride)
make a buffer over memory the caller owns (a shared memory mapping, for example).
Definition gfx.c:86
gfx_buffer_t gfx_buffer_sub(const gfx_buffer_t *parent, gfx_rect_t r)
a buffer for a part of another buffer: the same pixels, its own clip.
Definition gfx.c:97
gfx_color_t gfx_fade(gfx_color_t c, uint8_t opacity)
a colour with its alpha (and so its premultiplied channels) scaled by opacity / 255.
Definition gfx.c:16
gfx_buffer_t * gfx_buffer_new(int32_t width, int32_t height)
a new buffer of transparent pixels, from malloc.
Definition gfx.c:60
void gfx_draw_mask(gfx_buffer_t *dst, int32_t x, int32_t y, const uint8_t *mask, int32_t width, int32_t height, int32_t stride, gfx_color_t color)
a coverage mask (one byte per pixel, 0 to 255) in a colour, blended over dst with its top left at (x,...
Definition gfx.c:239
void gfx_copy(gfx_buffer_t *dst, int32_t x, int32_t y, const gfx_buffer_t *src, gfx_rect_t src_rect)
copy a part of src to dst at (x, y), replacing the pixels (no blending).
Definition gfx.c:199
gfx_color_t gfx_mix(gfx_color_t a, gfx_color_t b, uint8_t amount)
a mix of two colours, as CSS color-mix in sRGB: amount of b (0 to 255) and the rest of a.
Definition gfx.c:26
void gfx_fill_gradient(gfx_buffer_t *buf, gfx_rect_t r, gfx_color_t top, gfx_color_t bottom)
a vertical gradient from top to bottom colour over a rectangle (replacing the pixels).
Definition gfx.c:167
void gfx_blend(gfx_buffer_t *dst, int32_t x, int32_t y, const gfx_buffer_t *src, gfx_rect_t src_rect, uint8_t opacity)
blend a part of src over dst at (x, y), with an opacity (255: as it is).
Definition gfx.c:218
uint32_t gfx_color_t
a premultiplied ARGB colour, 0xAARRGGBB.
Definition gfx.h:30
void gfx_blur(gfx_buffer_t *buf, double sigma)
blur the whole buffer like a Gaussian with this sigma (in pixels); the edge pixels are used again pas...
Definition gfx_blur.c:47
void gfx_border_round_rect(gfx_buffer_t *buf, gfx_rect_t r, int32_t radius, int32_t width, gfx_color_t color)
the border of a round rectangle: width pixels wide, inside r (as a CSS border with box-sizing: border...
Definition gfx_round.c:135
void gfx_box_shadow(gfx_buffer_t *buf, gfx_rect_t box, int32_t radius, const gfx_shadow_t *s)
draw a box-shadow of a round box.
Definition gfx_round.c:273
void gfx_fill_round_rect(gfx_buffer_t *buf, gfx_rect_t r, int32_t radius, gfx_color_t color)
fill a rectangle with round corners, with anti-aliased corners (as CSS border-radius).
Definition gfx_round.c:130
void gfx_fill_round_rect_gradient(gfx_buffer_t *buf, gfx_rect_t r, int32_t radius, gfx_color_t top, gfx_color_t bottom)
fill a round rectangle with a vertical gradient from top to bottom, sampled at the pixel centres (as ...
Definition gfx_round.c:116
void gfx_blend_scaled(gfx_buffer_t *dst, gfx_rect_t dst_rect, const gfx_buffer_t *src, gfx_rect_t src_rect, uint8_t opacity)
draw a part of src scaled to fill dst_rect, blended over dst, with bilinear filtering.
Definition gfx_scale.c:47
void gfx_line(gfx_buffer_t *buf, int32_t x0, int32_t y0, int32_t x1, int32_t y1, gfx_color_t color)
a line one pixel wide from (x0, y0) to (x1, y1), both ends included, blended over what is there.
Definition gfx_shapes.c:8
unsigned int uint32_t
Definition fb.h:17
unsigned char uint8_t
Definition fb.h:18
NULL, size_t, ptrdiff_t, offsetof.
__PTRDIFF_TYPE__ ptrdiff_t
Definition stddef.h:14
integer types of fixed width.
int int32_t
Definition stdint.h:14
bool, true and false.
Definition gfx.h:41
bool owned
Definition gfx.h:47
int32_t stride
Definition gfx.h:45
uint32_t * pixels
Definition gfx.h:42
int32_t width
Definition gfx.h:43
int32_t height
Definition gfx.h:44
gfx_rect_t clip
Definition gfx.h:46
the pixel format of a framebuffer, for gfx_present.
Definition gfx.h:55
uint8_t green_shift
Definition gfx.h:60
uint8_t bytes_per_pixel
Definition gfx.h:56
uint8_t red_size
Definition gfx.h:57
uint8_t red_shift
Definition gfx.h:58
uint8_t green_size
Definition gfx.h:59
uint8_t blue_size
Definition gfx.h:61
uint8_t blue_shift
Definition gfx.h:62
Definition gfx.h:33
int32_t y
Definition gfx.h:35
int32_t x
Definition gfx.h:34
int32_t h
Definition gfx.h:37
int32_t w
Definition gfx.h:36
a CSS box-shadow: "x y blur spread color", and "inset".
Definition gfx.h:202
int32_t x
Definition gfx.h:203
bool inset
Definition gfx.h:208
int32_t blur
Definition gfx.h:205
int32_t spread
Definition gfx.h:206
gfx_color_t color
Definition gfx.h:207
int32_t y
Definition gfx.h:204