1ff3e748a923b529f17eb4578f9241d9bb587372
[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 void
19 ao_blt(uint32_t         *src_line,
20        int16_t          src_stride,
21        int16_t          src_x,
22        uint32_t         *dst_line,
23        int16_t          dst_stride,
24        int16_t          dst_x,
25        int16_t          width,
26        int16_t          height,
27        uint8_t          rop,
28        uint8_t          reverse,
29        uint8_t          upsidedown);
30
31 void
32 ao_solid(uint32_t       and,
33          uint32_t       xor,
34          uint32_t       *dst,
35          int16_t        dst_stride,
36          int16_t        dst_x,
37          int16_t        width,
38          int16_t        height);
39
40 void
41 ao_text(char            *string,
42         uint32_t        *dst_line,
43         int16_t         dst_stride,
44         int16_t         dst_x);
45
46 #define AO_ROP_CLEAR    0x0
47 #define AO_ROP_COPY     0x3
48 #define AO_ROP_SET      0xf
49
50 #define AO_SHIFT        5
51 #define AO_UNIT         (1 << AO_SHIFT)
52 #define AO_MASK         (AO_UNIT - 1)
53 #define AO_ALLONES      ((uint32_t) -1)
54
55 static inline uint32_t
56 ao_left(uint32_t bits, int16_t shift) {
57         return bits >> shift;
58 }
59
60 static inline uint32_t
61 ao_right(uint32_t bits, int16_t shift) {
62         return bits << shift;
63 }
64
65 static inline uint32_t
66 ao_right_mask(int16_t x) {
67         if ((AO_UNIT - x) & AO_MASK)
68                 return ao_left(AO_ALLONES,(AO_UNIT - x) & AO_MASK);
69         else
70                 return 0;
71 }
72
73 static inline uint32_t
74 ao_left_mask(int16_t x) {
75         if (x & AO_MASK)
76                 return ao_right(AO_ALLONES, x & AO_MASK);
77         else
78                 return 0;
79 }
80
81 static inline uint32_t
82 ao_bits_mask(int16_t x, int16_t w) {
83         return ao_right(AO_ALLONES, x & AO_MASK) &
84                 ao_left(AO_ALLONES,(AO_UNIT - (x + w)) & AO_MASK);
85 }
86
87 #define ao_mask_bits(x,w,l,n,r) { \
88     n = (w); \
89     r = ao_right_mask((x)+n); \
90     l = ao_left_mask(x); \
91     if (l) { \
92         n -= AO_UNIT - ((x) & AO_MASK); \
93         if (n < 0) { \
94             n = 0; \
95             l &= r; \
96             r = 0; \
97         } \
98     } \
99     n >>= AO_SHIFT; \
100 }
101
102 static inline uint32_t
103 ao_do_mask_rrop(uint32_t dst, uint32_t and, uint32_t xor, uint32_t mask) {
104         return (dst & (and | ~mask)) ^ (xor & mask);
105 }
106
107 static inline uint32_t
108 ao_do_rrop(uint32_t dst, uint32_t and, uint32_t xor) {
109         return (dst & and) ^ xor;
110 }
111
112 #define AO_CLEAR         0x0    /* 0 */
113 #define AO_AND           0x1    /* src AND dst */
114 #define AO_AND_REVERSE   0x2    /* src AND NOT dst */
115 #define AO_COPY          0x3    /* src */
116 #define AO_AND_INVERTED  0x4    /* NOT src AND dst */
117 #define AO_NOOP          0x5    /* dst */
118 #define AO_XOR           0x6    /* src XOR dst */
119 #define AO_OR            0x7    /* src OR dst */
120 #define AO_NOR           0x8    /* NOT src AND NOT dst */
121 #define AO_EQUIV         0x9    /* NOT src XOR dst */
122 #define AO_INVERT        0xa    /* NOT dst */
123 #define AO_OR_REVERSE    0xb    /* src OR NOT dst */
124 #define AO_COPY_INVERTED 0xc    /* NOT src */
125 #define AO_OR_INVERTED   0xd    /* NOT src OR dst */
126 #define AO_NAND          0xe    /* NOT src OR NOT dst */
127 #define AO_SET           0xf    /* 1 */
128
129 #endif /* _AO_DRAW_H_ */