altos/telelco-v3.0: Add some real drawing stuff
[fw/altos] / src / draw / ao_draw.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #ifndef _AO_DRAW_H_
16 #define _AO_DRAW_H_
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include "ao_font.h"
21 #include "ao_box.h"
22
23 struct ao_bitmap {
24         uint32_t        *base;
25         int16_t         stride; /* in units */
26         int16_t         width;  /* in pixels */
27         int16_t         height; /* in pixels */
28         struct ao_box   damage;
29 };
30
31 #define AO_BITMAP_STRIDE(width) (((width) + 31) >> 5)
32
33 struct ao_coord {
34         float   x, y;
35 };
36
37 struct ao_transform {
38         float   x_scale, x_off;
39         float   y_scale, y_off;
40 };
41
42 static inline float ao_t_x(float x, float y, const struct ao_transform *t)
43 {
44         (void) y;
45         return x * t->x_scale + t->x_off;
46 }
47
48 static inline int16_t ao_t_xi(float x, float y, const struct ao_transform *t)
49 {
50         return (int16_t) (ao_t_x(x, y, t) + 0.5f);
51 }
52
53 static inline float ao_t_y(float x, float y, const struct ao_transform *t)
54 {
55         (void) x;
56         return y * t->y_scale + t->y_off;
57 }
58
59 static inline int16_t ao_t_yi(float x, float y, const struct ao_transform *t)
60 {
61         return (int16_t) (ao_t_y(x, y, t) + 0.5f);
62 }
63
64 static inline float ao_t_x_c(const struct ao_coord      *c,
65                              const struct ao_transform  *t)
66 {
67         return ao_t_x(c->x, c->y, t);
68 }
69
70 static inline float ao_t_y_c(const struct ao_coord      *c,
71                              const struct ao_transform  *t)
72 {
73         return ao_t_y(c->x, c->y, t);
74 }
75
76 static inline int16_t
77 ao_stride(int16_t width)
78 {
79         return (int16_t) ((width + 31) >> 5);
80 }
81
82 static inline int16_t
83 ao_stride_bytes(int16_t width)
84 {
85         return (int16_t) ((width + 7) >> 3);
86 }
87
88 static inline void
89 ao_damage_set_empty(struct ao_bitmap *dst)
90 {
91         ao_box_set_empty(&dst->damage);
92 }
93
94 struct ao_pattern {
95         uint8_t         pattern[8];
96 };
97
98 struct ao_glyph_metrics {
99         int8_t  width;
100         int8_t  height;
101         int8_t  x_off;
102         int8_t  y_off;
103         int8_t  advance;
104 };
105
106 struct ao_font {
107         const uint8_t   *bytes;
108         const uint16_t  *pos;
109         const struct ao_glyph_metrics *metrics;
110         int16_t max_width;
111         int16_t max_height;
112         int16_t ascent;
113 };
114
115 void
116 ao_copy(struct ao_bitmap        *dst,
117         int16_t                 dst_x,
118         int16_t                 dst_y,
119         int16_t                 width,
120         int16_t                 height,
121         const struct ao_bitmap  *src,
122         int16_t                 src_x,
123         int16_t                 src_y,
124         uint8_t                 rop);
125
126 void
127 ao_rect(struct ao_bitmap        *dst,
128         int16_t                 x,
129         int16_t                 y,
130         int16_t                 width,
131         int16_t                 height,
132         uint32_t                fill,
133         uint8_t                 rop);
134
135 void
136 ao_pattern(struct ao_bitmap             *dst,
137            int16_t                      x,
138            int16_t                      y,
139            int16_t                      width,
140            int16_t                      height,
141            const struct ao_pattern      *pattern,
142            int16_t                      pat_x,
143            int16_t                      pat_y,
144            uint8_t                      rop);
145
146 void
147 ao_line(struct ao_bitmap        *dst,
148         int16_t                 x1,
149         int16_t                 y1,
150         int16_t                 x2,
151         int16_t                 y2,
152         uint32_t                fill,
153         uint8_t                 rop);
154
155 void
156 ao_poly(struct ao_bitmap                *dst,
157         const struct ao_coord           *coords,
158         uint16_t                        ncoords,
159         const struct ao_transform       *transform,
160         uint32_t                        fill,
161         uint8_t                         rop);
162
163 void
164 ao_text(struct ao_bitmap        *dst,
165         const struct ao_font    *font,
166         int16_t                 x,
167         int16_t                 y,
168         const char              *string,
169         uint32_t                fill,
170         uint8_t                 rop);
171
172 void
173 ao_logo(struct ao_bitmap                *dst,
174         const struct ao_transform       *transform,
175         const struct ao_font            *font,
176         uint32_t                        fill,
177         uint8_t                         rop);
178
179 extern const struct ao_transform ao_identity;
180
181 #define AO_SHIFT        5
182 #define AO_UNIT         (1 << AO_SHIFT)
183 #define AO_MASK         (AO_UNIT - 1)
184 #define AO_ALLONES      ((uint32_t) -1)
185
186 /*
187  *          dst
188  *         0   1
189  *
190  *      0  a   b
191  *  src
192  *      1  c   d
193  *
194  *      ROP = abcd
195  */
196
197 #define AO_CLEAR         0x0    /* 0 */
198 #define AO_AND           0x1    /* src AND dst */
199 #define AO_AND_REVERSE   0x2    /* src AND NOT dst */
200 #define AO_COPY          0x3    /* src */
201 #define AO_AND_INVERTED  0x4    /* NOT src AND dst */
202 #define AO_NOOP          0x5    /* dst */
203 #define AO_XOR           0x6    /* src XOR dst */
204 #define AO_OR            0x7    /* src OR dst */
205 #define AO_NOR           0x8    /* NOT src AND NOT dst */
206 #define AO_EQUIV         0x9    /* NOT src XOR dst */
207 #define AO_INVERT        0xa    /* NOT dst */
208 #define AO_OR_REVERSE    0xb    /* src OR NOT dst */
209 #define AO_COPY_INVERTED 0xc    /* NOT src */
210 #define AO_OR_INVERTED   0xd    /* NOT src OR dst */
211 #define AO_NAND          0xe    /* NOT src OR NOT dst */
212 #define AO_SET           0xf    /* 1 */
213
214 #define AO_WHITE        0xffffffff
215 #define AO_BLACK        0x00000000
216
217 #endif /* _AO_DRAW_H_ */