altos/draw: Add damage tracking
[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 struct ao_coord {
32         float   x, y;
33 };
34
35 struct ao_transform {
36         float   x_scale, x_off;
37         float   y_scale, y_off;
38 };
39
40 static inline float ao_t_x(float x, float y, const struct ao_transform *t)
41 {
42         (void) y;
43         return x * t->x_scale + t->x_off;
44 }
45
46 static inline int16_t ao_t_xi(float x, float y, const struct ao_transform *t)
47 {
48         return (int16_t) (ao_t_x(x, y, t) + 0.5f);
49 }
50
51 static inline float ao_t_y(float x, float y, const struct ao_transform *t)
52 {
53         (void) x;
54         return y * t->y_scale + t->y_off;
55 }
56
57 static inline int16_t ao_t_yi(float x, float y, const struct ao_transform *t)
58 {
59         return (int16_t) (ao_t_y(x, y, t) + 0.5f);
60 }
61
62 static inline float ao_t_x_c(const struct ao_coord      *c,
63                              const struct ao_transform  *t)
64 {
65         return ao_t_x(c->x, c->y, t);
66 }
67
68 static inline float ao_t_y_c(const struct ao_coord      *c,
69                              const struct ao_transform  *t)
70 {
71         return ao_t_y(c->x, c->y, t);
72 }
73
74 static inline int16_t
75 ao_stride(int16_t width)
76 {
77         return (int16_t) ((width + 31) >> 5);
78 }
79
80 static inline int16_t
81 ao_stride_bytes(int16_t width)
82 {
83         return (int16_t) ((width + 7) >> 3);
84 }
85
86 static inline void
87 ao_damage_set_empty(struct ao_bitmap *dst)
88 {
89         ao_box_set_empty(&dst->damage);
90 }
91
92 struct ao_pattern {
93         uint8_t         pattern[8];
94 };
95
96 struct ao_glyph_metrics {
97         int8_t  width;
98         int8_t  height;
99         int8_t  x_off;
100         int8_t  y_off;
101         int8_t  advance;
102 };
103
104 struct ao_font {
105         const uint8_t   *bytes;
106         const uint16_t  *pos;
107         const struct ao_glyph_metrics *metrics;
108         int16_t max_width;
109         int16_t max_height;
110         int16_t ascent;
111 };
112
113 void
114 ao_copy(struct ao_bitmap        *dst,
115         int16_t                 dst_x,
116         int16_t                 dst_y,
117         int16_t                 width,
118         int16_t                 height,
119         const struct ao_bitmap  *src,
120         int16_t                 src_x,
121         int16_t                 src_y,
122         uint8_t                 rop);
123
124 void
125 ao_rect(struct ao_bitmap        *dst,
126         int16_t                 x,
127         int16_t                 y,
128         int16_t                 width,
129         int16_t                 height,
130         uint32_t                fill,
131         uint8_t                 rop);
132
133 void
134 ao_pattern(struct ao_bitmap             *dst,
135            int16_t                      x,
136            int16_t                      y,
137            int16_t                      width,
138            int16_t                      height,
139            const struct ao_pattern      *pattern,
140            int16_t                      pat_x,
141            int16_t                      pat_y,
142            uint8_t                      rop);
143
144 void
145 ao_line(struct ao_bitmap        *dst,
146         int16_t                 x1,
147         int16_t                 y1,
148         int16_t                 x2,
149         int16_t                 y2,
150         uint32_t                fill,
151         uint8_t                 rop);
152
153 void
154 ao_poly(struct ao_bitmap                *dst,
155         const struct ao_coord           *coords,
156         uint16_t                        ncoords,
157         const struct ao_transform       *transform,
158         uint32_t                        fill,
159         uint8_t                         rop);
160
161 void
162 ao_text(struct ao_bitmap        *dst,
163         const struct ao_font    *font,
164         int16_t                 x,
165         int16_t                 y,
166         char                    *string,
167         uint32_t                fill,
168         uint8_t                 rop);
169
170 void
171 ao_logo(struct ao_bitmap                *dst,
172         const struct ao_transform       *transform,
173         const struct ao_font            *font,
174         uint32_t                        fill,
175         uint8_t                         rop);
176
177 extern const struct ao_transform ao_identity;
178
179 #define AO_SHIFT        5
180 #define AO_UNIT         (1 << AO_SHIFT)
181 #define AO_MASK         (AO_UNIT - 1)
182 #define AO_ALLONES      ((uint32_t) -1)
183
184 /*
185  *          dst
186  *         0   1
187  *
188  *      0  a   b
189  *  src
190  *      1  c   d
191  *
192  *      ROP = abcd
193  */
194
195 #define AO_CLEAR         0x0    /* 0 */
196 #define AO_AND           0x1    /* src AND dst */
197 #define AO_AND_REVERSE   0x2    /* src AND NOT dst */
198 #define AO_COPY          0x3    /* src */
199 #define AO_AND_INVERTED  0x4    /* NOT src AND dst */
200 #define AO_NOOP          0x5    /* dst */
201 #define AO_XOR           0x6    /* src XOR dst */
202 #define AO_OR            0x7    /* src OR dst */
203 #define AO_NOR           0x8    /* NOT src AND NOT dst */
204 #define AO_EQUIV         0x9    /* NOT src XOR dst */
205 #define AO_INVERT        0xa    /* NOT dst */
206 #define AO_OR_REVERSE    0xb    /* src OR NOT dst */
207 #define AO_COPY_INVERTED 0xc    /* NOT src */
208 #define AO_OR_INVERTED   0xd    /* NOT src OR dst */
209 #define AO_NAND          0xe    /* NOT src OR NOT dst */
210 #define AO_SET           0xf    /* 1 */
211
212 #endif /* _AO_DRAW_H_ */