rasops1-4_putchar.h revision 1.1 1 /* $NetBSD: rasops1-4_putchar.h,v 1.1 2019/08/07 12:36:36 rin Exp $ */
2
3 /* NetBSD: rasops_bitops.h,v 1.23 2019/08/02 04:39:09 rin Exp */
4 /*-
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #define PIXEL_BITS RASOPS_DEPTH
34
35 #if RASOPS_DEPTH == 1
36 #define PIXEL_SHIFT 0
37 #elif RASOPS_DEPTH == 2
38 #define PIXEL_SHIFT 1
39 #elif RASOPS_DEPTH == 4
40 #define PIXEL_SHIFT 2
41 #else
42 #error "Depth not supported"
43 #endif
44
45 #ifndef RASOPS_AA
46 #define COLOR_MASK __BITS(32 - PIXEL_BITS, 31)
47 #else
48 # if RASOPS_DEPTH == 2
49 #define COLOR_MASK 0x3
50 # else
51 #error "Anti-aliasing not supported"
52 # endif
53 #endif
54
55 #ifndef RASOPS_AA
56 #define PIXEL_OR(tmp) \
57 do { \
58 (tmp) |= clr[(fb >> 31) & 1] >> bit; \
59 fb <<= 1; \
60 } while (0 /* CONSTCOND */)
61 #else
62 #define PIXEL_OR(tmp) \
63 do { \
64 uint8_t c, av = *fr++; \
65 if (av == 0xff) \
66 c = clr[1]; \
67 else if (av == 0) \
68 c = clr[0]; \
69 else \
70 c = (av * clr[1] + (0xff - av) * clr[0]) >> 8; \
71 (tmp) |= c << (32 - PIXEL_BITS - bit); \
72 } while (0 /* CONSTCOND */)
73 #endif
74
75 #define NAME(depth) NAME1(depth)
76 #ifndef RASOPS_AA
77 #define NAME1(depth) rasops ## depth ## _ ## putchar
78 #else
79 #define NAME1(depth) rasops ## depth ## _ ## putchar_aa
80 #endif
81
82 /*
83 * Paint a single character. This function is also applicable to
84 * monochrome, but that in rasops1.c is much simpler and faster.
85 */
86 static void
87 NAME(RASOPS_DEPTH)(void *cookie, int row, int col, u_int uc, long attr)
88 {
89 struct rasops_info *ri = (struct rasops_info *)cookie;
90 struct wsdisplay_font *font = PICK_FONT(ri, uc);
91 int full, cnt, bit;
92 uint32_t rs, bg, fg, lmask, rmask, lbg, rbg, clr[2];
93 uint32_t height, width;
94 uint32_t *rp, *bp, *hp, tmp;
95 uint8_t *fr;
96 bool space;
97
98 hp = NULL; /* XXX GCC */
99
100 #ifdef RASOPS_CLIPPING
101 /* Catches 'row < 0' case too */
102 if ((unsigned)row >= (unsigned)ri->ri_rows)
103 return;
104
105 if ((unsigned)col >= (unsigned)ri->ri_cols)
106 return;
107 #endif
108
109 width = font->fontwidth << PIXEL_SHIFT;
110 col *= width;
111 height = font->fontheight;
112 rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
113 ((col >> 3) & ~3));
114 if (ri->ri_hwbits)
115 hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale +
116 ((col >> 3) & ~3));
117 col &= 31;
118 rs = ri->ri_stride;
119
120 bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
121 fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
122
123 /* If fg and bg match this becomes a space character */
124 if (uc == ' ' || __predict_false(fg == bg)) {
125 space = true;
126 fr = NULL; /* XXX GCC */
127 } else {
128 space = false;
129 fr = FONT_GLYPH(uc, font, ri);
130 }
131
132 if (col + width <= 32) {
133 /* Single word, only one mask */
134
135 rmask = rasops_pmask[col][width & 31];
136 lmask = ~rmask;
137
138 if (space) {
139 bg &= rmask;
140 while (height--) {
141 tmp = (*rp & lmask) | bg;
142 *rp = tmp;
143 DELTA(rp, rs, uint32_t *);
144 if (ri->ri_hwbits) {
145 *hp = tmp;
146 DELTA(hp, rs, uint32_t *);
147 }
148 }
149 } else {
150 clr[0] = bg & COLOR_MASK;
151 clr[1] = fg & COLOR_MASK;
152 while (height--) {
153 #ifndef RASOPS_AA
154 uint32_t fb = be32uatoh(fr);
155 fr += ri->ri_font->stride;
156 #endif
157
158 tmp = 0;
159 for (bit = col; bit < col + width;
160 bit += PIXEL_BITS)
161 PIXEL_OR(tmp);
162 tmp = (*rp & lmask) | MBE(tmp);
163 *rp = tmp;
164
165 if (ri->ri_hwbits) {
166 *hp = tmp;
167 DELTA(hp, rs, uint32_t *);
168 }
169
170 DELTA(rp, rs, uint32_t *);
171 }
172 }
173
174 /* Do underline */
175 if ((attr & WSATTR_UNDERLINE) != 0) {
176 DELTA(rp, - ri->ri_stride * ri->ri_ul.off, uint32_t *);
177 if (ri->ri_hwbits)
178 DELTA(hp, - ri->ri_stride * ri->ri_ul.off,
179 uint32_t *);
180
181 for (height = ri->ri_ul.height; height; height--) {
182 DELTA(rp, - ri->ri_stride, uint32_t *);
183 tmp = (*rp & lmask) | (fg & rmask);
184 *rp = tmp;
185 if (ri->ri_hwbits) {
186 DELTA(hp, - ri->ri_stride, uint32_t *);
187 *hp = tmp;
188 }
189 }
190 }
191
192 return;
193 }
194
195 /* Word boundary, two masks needed */
196
197 lmask = ~rasops_lmask[col];
198 rmask = ~rasops_rmask[(col + width) & 31];
199
200 if (lmask != -1)
201 width -= 32 - col;
202 full = width / 32;
203 width -= full * 32;
204
205 if (space) {
206 lbg = bg & ~lmask;
207 rbg = bg & ~rmask;
208
209 while (height--) {
210 bp = rp;
211
212 if (lmask != -1) {
213 *bp = (*bp & lmask) | lbg;
214 bp++;
215 }
216
217 for (cnt = full; cnt; cnt--)
218 *bp++ = bg;
219
220 if (rmask != -1)
221 *bp = (*bp & rmask) | rbg;
222
223 if (ri->ri_hwbits) {
224 memcpy(hp, rp, ((lmask != -1) + full +
225 (rmask != -1)) << 2);
226 DELTA(hp, rs, uint32_t *);
227 }
228
229 DELTA(rp, rs, uint32_t *);
230 }
231 } else {
232 clr[0] = bg & COLOR_MASK;
233 clr[1] = fg & COLOR_MASK;
234
235 while (height--) {
236 bp = rp;
237
238 #ifndef RASOPS_AA
239 uint32_t fb = be32uatoh(fr);
240 fr += ri->ri_font->stride;
241 #endif
242
243 if (lmask != -1) {
244 tmp = 0;
245 for (bit = col; bit < 32; bit += PIXEL_BITS)
246 PIXEL_OR(tmp);
247 *bp = (*bp & lmask) | MBE(tmp);
248 bp++;
249 }
250
251 for (cnt = full; cnt; cnt--) {
252 tmp = 0;
253 for (bit = 0; bit < 32; bit += PIXEL_BITS)
254 PIXEL_OR(tmp);
255 *bp++ = MBE(tmp);
256 }
257
258 if (rmask != -1) {
259 tmp = 0;
260 for (bit = 0; bit < width; bit += PIXEL_BITS)
261 PIXEL_OR(tmp);
262 *bp = (*bp & rmask) | MBE(tmp);
263 }
264
265 if (ri->ri_hwbits) {
266 memcpy(hp, rp, ((lmask != -1) + full +
267 (rmask != -1)) << 2);
268 DELTA(hp, rs, uint32_t *);
269 }
270
271 DELTA(rp, rs, uint32_t *);
272 }
273 }
274
275 /* Do underline */
276 if ((attr & WSATTR_UNDERLINE) != 0) {
277 DELTA(rp, - ri->ri_stride * ri->ri_ul.off, uint32_t *);
278 if (ri->ri_hwbits)
279 DELTA(hp, - ri->ri_stride * ri->ri_ul.off, uint32_t *);
280
281 for (height = ri->ri_ul.height; height; height--) {
282 DELTA(rp, - ri->ri_stride, uint32_t *);
283 bp = rp;
284 if (lmask != -1) {
285 *bp = (*bp & lmask) | (fg & ~lmask);
286 bp++;
287 }
288 for (cnt = full; cnt; cnt--)
289 *bp++ = fg;
290 if (rmask != -1)
291 *bp = (*bp & rmask) | (fg & ~rmask);
292 if (ri->ri_hwbits) {
293 DELTA(hp, - ri->ri_stride, uint32_t *);
294 memcpy(hp, rp, ((lmask != -1) + full +
295 (rmask != -1)) << 2);
296 }
297 }
298 }
299 }
300
301 #undef PIXEL_BITS
302 #undef PIXEL_SHIFT
303 #undef COLOR_MASK
304
305 #undef PIXEL_OR
306
307 #undef NAME
308 #undef NAME1
309