1 // Copyright 2016 The Fuchsia Authors
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 <inttypes.h>
11 #include <stdbool.h>
12 #include <sys/types.h>
13 #include <zircon/compiler.h>
14 #include <zircon/pixelformat.h>
15 #include <zircon/types.h>
16
17 __BEGIN_CDECLS
18
19 // gfx library
20
21 typedef zx_pixel_format_t gfx_format;
22
23 #define MAX_ALPHA 255
24
25 // surface flags
26 #define GFX_FLAG_FREE_ON_DESTROY (1 << 0) // free the ptr at destroy
27 #define GFX_FLAG_FLUSH_CPU_CACHE (1 << 1) // do a cache flush during gfx_flush
28
29 typedef struct gfx_font {
30 const uint16_t* data;
31 unsigned width;
32 unsigned height;
33 } gfx_font;
34
35 /**
36 * @brief Describe a graphics drawing surface
37 *
38 * The gfx_surface object represents a framebuffer that can be rendered
39 * to. Elements include a pointer to the actual pixel memory, its size, its
40 * layout, and pointers to basic drawing functions.
41 *
42 * @ingroup graphics
43 */
44 typedef struct gfx_surface {
45 void* ptr;
46 uint32_t flags;
47 gfx_format format;
48 uint width;
49 uint height;
50 uint stride;
51 uint pixelsize;
52 size_t len;
53 uint alpha;
54
55 // function pointers
56 uint32_t (*translate_color)(uint32_t input);
57 void (*copyrect)(struct gfx_surface*, uint x, uint y, uint width, uint height, uint x2, uint y2);
58 void (*fillrect)(struct gfx_surface*, uint x, uint y, uint width, uint height, uint color);
59 void (*putpixel)(struct gfx_surface*, uint x, uint y, uint color);
60 void (*putchar)(struct gfx_surface*, const struct gfx_font*,
61 uint ch, uint x, uint y, uint fg, uint bg);
62 void (*flush)(uint starty, uint endy);
63 } gfx_surface;
64
65 extern const struct gfx_font font_9x16;
66 extern const struct gfx_font font_18x32;
67
68 // copy a rect from x,y with width x height to x2, y2
69 void gfx_copyrect(gfx_surface* surface, uint x, uint y, uint width, uint height, uint x2, uint y2);
70
71 // fill a rect within the surface with a color
72 void gfx_fillrect(gfx_surface* surface, uint x, uint y, uint width, uint height, uint color);
73
74 // draw a pixel at x, y in the surface
75 void gfx_putpixel(gfx_surface* surface, uint x, uint y, uint color);
76
77 // draw a single pixel line between x1,y1 and x2,y1
78 void gfx_line(gfx_surface* surface, uint x1, uint y1, uint x2, uint y2, uint color);
79
80 // blend between two surfaces
81 void gfx_surface_blend(struct gfx_surface* target, struct gfx_surface* source, uint destx, uint desty);
82
83 // ensure the surface is written back to memory and optionally backing store
84 void gfx_flush(struct gfx_surface* surface);
85
86 // flush a subset of the surface
87 void gfx_flush_rows(struct gfx_surface* surface, uint start, uint end);
88
89 void gfx_putchar(struct gfx_surface* surface, const struct gfx_font* font,
90 uint ch, uint x, uint y, uint fg, uint bg);
91
92 // clear the entire surface with a color
gfx_clear(gfx_surface * surface,uint color)93 static inline void gfx_clear(gfx_surface* surface, uint color) {
94 surface->fillrect(surface, 0, 0, surface->width, surface->height, color);
95 gfx_flush(surface);
96 }
97
98 // surface setup
99 gfx_surface* gfx_create_surface(void* ptr, uint width, uint height, uint stride, gfx_format format, uint32_t flags);
100 zx_status_t gfx_init_surface(gfx_surface* surface, void* ptr, uint width, uint height, uint stride, gfx_format format, uint32_t flags);
101
102 // utility routine to make a surface out of a display info
103 struct display_info;
104 gfx_surface* gfx_create_surface_from_display(struct display_info*);
105 zx_status_t gfx_init_surface_from_display(gfx_surface* surface, struct display_info*);
106
107 // free the surface
108 // optionally frees the buffer if the free bit is set
109 void gfx_surface_destroy(struct gfx_surface* surface);
110
111 // utility routine to fill the display with a little moire pattern
112 void gfx_draw_pattern(void);
113
114 __END_CDECLS
115