1 /*
2 * Copyright (c) 2010 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #pragma once
9
10 #include <stdbool.h>
11 #include <sys/types.h>
12 #include <inttypes.h>
13 #include <lk/compiler.h>
14
15 // gfx library
16
17 __BEGIN_CDECLS
18
19 // different graphics formats
20 typedef enum {
21 GFX_FORMAT_NONE,
22 GFX_FORMAT_RGB_565,
23 GFX_FORMAT_RGB_332,
24 GFX_FORMAT_RGB_2220,
25 GFX_FORMAT_ARGB_8888,
26 GFX_FORMAT_RGB_x888,
27 GFX_FORMAT_MONO,
28
29 GFX_FORMAT_MAX
30 } gfx_format;
31
32 #define MAX_ALPHA 255
33
34 /**
35 * @brief Describe a graphics drawing surface
36 *
37 * The gfx_surface object represents a framebuffer that can be rendered
38 * to. Elements include a pointer to the actual pixel memory, its size, its
39 * layout, and pointers to basic drawing functions.
40 *
41 * @ingroup graphics
42 */
43 typedef struct gfx_surface {
44 void *ptr;
45 bool free_on_destroy;
46 gfx_format format;
47 uint width;
48 uint height;
49 uint stride;
50 uint pixelsize;
51 size_t len;
52 uint alpha;
53
54 // function pointers
55 uint32_t (*translate_color)(uint32_t input);
56 void (*copyrect)(struct gfx_surface *, uint x, uint y, uint width, uint height, uint x2, uint y2);
57 void (*fillrect)(struct gfx_surface *, uint x, uint y, uint width, uint height, uint color);
58 void (*putpixel)(struct gfx_surface *, uint x, uint y, uint color);
59 void (*flush)(uint starty, uint endy);
60 } gfx_surface;
61
62 // copy a rect from x,y with width x height to x2, y2
63 void gfx_copyrect(gfx_surface *surface, uint x, uint y, uint width, uint height, uint x2, uint y2);
64
65 // fill a rect within the surface with a color
66 void gfx_fillrect(gfx_surface *surface, uint x, uint y, uint width, uint height, uint color);
67
68 // draw a pixel at x, y in the surface
69 void gfx_putpixel(gfx_surface *surface, uint x, uint y, uint color);
70
71 // draw a single pixel line between x1,y1 and x2,y1
72 void gfx_line(gfx_surface *surface, uint x1, uint y1, uint x2, uint y2, uint color);
73
74 // clear the entire surface with a color
gfx_clear(gfx_surface * surface,uint color)75 static inline void gfx_clear(gfx_surface *surface, uint color) {
76 surface->fillrect(surface, 0, 0, surface->width, surface->height, color);
77
78 if (surface->flush)
79 surface->flush(0, surface->height-1);
80 }
81
82 // blend between two surfaces
83 void gfx_surface_blend(struct gfx_surface *target, struct gfx_surface *source, uint destx, uint desty);
84
85 void gfx_flush(struct gfx_surface *surface);
86
87 void gfx_flush_rows(struct gfx_surface *surface, uint start, uint end);
88
89 // surface setup
90 gfx_surface *gfx_create_surface(void *ptr, uint width, uint height, uint stride, gfx_format format);
91
92 // utility routine to make a surface out of a display framebuffer
93 struct display_framebuffer;
94 gfx_surface *gfx_create_surface_from_display(struct display_framebuffer *) __NONNULL((1));
95
96 // free the surface
97 // optionally frees the buffer if the free bit is set
98 void gfx_surface_destroy(struct gfx_surface *surface);
99
100 // utility routine to fill the display with a little moire pattern
101 void gfx_draw_pattern(void);
102
103 // fill the screen with white
104 void gfx_draw_pattern_white(void);
105
106 __END_CDECLS
107
108