rasops24.c revision 1.48 1 1.48 rin /* $NetBSD: rasops24.c,v 1.48 2019/08/07 12:33:48 rin Exp $ */
2 1.1 ad
3 1.6 ad /*-
4 1.6 ad * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.6 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.13 ad * by Andrew Doran.
9 1.6 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.6 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.2 ad
32 1.15 lukem #include <sys/cdefs.h>
33 1.48 rin __KERNEL_RCSID(0, "$NetBSD: rasops24.c,v 1.48 2019/08/07 12:33:48 rin Exp $");
34 1.15 lukem
35 1.1 ad #include "opt_rasops.h"
36 1.1 ad
37 1.1 ad #include <sys/param.h>
38 1.1 ad #include <sys/systm.h>
39 1.1 ad #include <sys/time.h>
40 1.1 ad
41 1.4 ad #include <machine/endian.h>
42 1.22 dsl #include <sys/bswap.h>
43 1.4 ad
44 1.1 ad #include <dev/wscons/wsdisplayvar.h>
45 1.1 ad #include <dev/wscons/wsconsio.h>
46 1.40 rin
47 1.40 rin #define _RASOPS_PRIVATE
48 1.44 rin #define RASOPS_DEPTH 24
49 1.1 ad #include <dev/rasops/rasops.h>
50 1.1 ad
51 1.18 perry static void rasops24_erasecols(void *, int, int, int, long);
52 1.18 perry static void rasops24_eraserows(void *, int, int, long);
53 1.36 rin static void rasops24_putchar(void *, int, int, u_int, long);
54 1.37 rin static void rasops24_putchar_aa(void *, int, int, u_int, long);
55 1.43 rin static __inline void
56 1.43 rin rasops24_makestamp1(struct rasops_info *, uint32_t *,
57 1.43 rin uint32_t, uint32_t, uint32_t, uint32_t);
58 1.9 ad #ifndef RASOPS_SMALL
59 1.36 rin static void rasops24_putchar8(void *, int, int, u_int, long);
60 1.36 rin static void rasops24_putchar12(void *, int, int, u_int, long);
61 1.36 rin static void rasops24_putchar16(void *, int, int, u_int, long);
62 1.18 perry static void rasops24_makestamp(struct rasops_info *, long);
63 1.29 njoly #endif
64 1.4 ad
65 1.47 rin #ifndef RASOPS_SMALL
66 1.47 rin /* 4x1 stamp for optimized character blitting */
67 1.47 rin static uint32_t stamp[64];
68 1.47 rin static long stamp_attr;
69 1.47 rin static struct rasops_info *stamp_ri;
70 1.47 rin
71 1.4 ad /*
72 1.4 ad * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
73 1.32 rin * destination uint32_t[0] = STAMP_READ(offset)
74 1.32 rin * destination uint32_t[1] = STAMP_READ(offset + 4)
75 1.32 rin * destination uint32_t[2] = STAMP_READ(offset + 8)
76 1.4 ad */
77 1.36 rin #define STAMP_SHIFT(fb, n) ((n) ? (fb) : (fb) << 4)
78 1.36 rin #define STAMP_MASK (0xf << 4)
79 1.36 rin #define STAMP_READ(o) (*(uint32_t *)((uint8_t *)stamp + (o)))
80 1.47 rin #endif
81 1.4 ad
82 1.1 ad /*
83 1.14 wiz * Initialize rasops_info struct for this colordepth.
84 1.1 ad */
85 1.1 ad void
86 1.26 dsl rasops24_init(struct rasops_info *ri)
87 1.1 ad {
88 1.1 ad
89 1.36 rin if (ri->ri_rnum == 0) {
90 1.36 rin ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
91 1.36 rin
92 1.36 rin ri->ri_rpos = 0;
93 1.36 rin ri->ri_gpos = 8;
94 1.36 rin ri->ri_bpos = 16;
95 1.36 rin }
96 1.36 rin
97 1.36 rin ri->ri_ops.erasecols = rasops24_erasecols;
98 1.36 rin ri->ri_ops.eraserows = rasops24_eraserows;
99 1.36 rin
100 1.37 rin if (FONT_IS_ALPHA(ri->ri_font)) {
101 1.37 rin ri->ri_ops.putchar = rasops24_putchar_aa;
102 1.37 rin return;
103 1.37 rin }
104 1.37 rin
105 1.1 ad switch (ri->ri_font->fontwidth) {
106 1.9 ad #ifndef RASOPS_SMALL
107 1.1 ad case 8:
108 1.4 ad ri->ri_ops.putchar = rasops24_putchar8;
109 1.1 ad break;
110 1.1 ad case 12:
111 1.4 ad ri->ri_ops.putchar = rasops24_putchar12;
112 1.1 ad break;
113 1.1 ad case 16:
114 1.4 ad ri->ri_ops.putchar = rasops24_putchar16;
115 1.1 ad break;
116 1.9 ad #endif
117 1.1 ad default:
118 1.4 ad ri->ri_ops.putchar = rasops24_putchar;
119 1.41 rin return;
120 1.1 ad }
121 1.41 rin
122 1.41 rin #ifndef RASOPS_SMALL
123 1.47 rin stamp_attr = 0;
124 1.47 rin stamp_ri = NULL;
125 1.41 rin #endif
126 1.1 ad }
127 1.1 ad
128 1.48 rin #undef RASOPS_AA
129 1.35 rin #include "rasops_putchar.h"
130 1.48 rin
131 1.48 rin #define RASOPS_AA
132 1.48 rin #include "rasops_putchar.h"
133 1.48 rin #undef RASOPS_AA
134 1.1 ad
135 1.43 rin static __inline void
136 1.47 rin rasops24_makestamp1(struct rasops_info *ri, uint32_t *xstamp,
137 1.43 rin uint32_t c1, uint32_t c2, uint32_t c3, uint32_t c4)
138 1.43 rin {
139 1.43 rin
140 1.47 rin xstamp[0] = (c1 << 8) | (c2 >> 16);
141 1.47 rin xstamp[1] = (c2 << 16) | (c3 >> 8);
142 1.47 rin xstamp[2] = (c3 << 24) | c4;
143 1.43 rin
144 1.43 rin #if BYTE_ORDER == LITTLE_ENDIAN
145 1.43 rin if ((ri->ri_flg & RI_BSWAP) == 0)
146 1.43 rin #else
147 1.43 rin if ((ri->ri_flg & RI_BSWAP) != 0)
148 1.43 rin #endif
149 1.43 rin {
150 1.47 rin xstamp[0] = bswap32(xstamp[0]);
151 1.47 rin xstamp[1] = bswap32(xstamp[1]);
152 1.47 rin xstamp[2] = bswap32(xstamp[2]);
153 1.43 rin }
154 1.43 rin }
155 1.43 rin
156 1.9 ad #ifndef RASOPS_SMALL
157 1.9 ad /*
158 1.9 ad * Recompute the blitting stamp.
159 1.9 ad */
160 1.9 ad static void
161 1.26 dsl rasops24_makestamp(struct rasops_info *ri, long attr)
162 1.9 ad {
163 1.36 rin uint32_t fg, bg, c1, c2, c3, c4;
164 1.9 ad int i;
165 1.12 pk
166 1.47 rin stamp_attr = attr;
167 1.47 rin stamp_ri = ri;
168 1.47 rin
169 1.36 rin fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xffffff;
170 1.36 rin bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
171 1.12 pk
172 1.9 ad for (i = 0; i < 64; i += 4) {
173 1.9 ad #if BYTE_ORDER == LITTLE_ENDIAN
174 1.36 rin c1 = i & 32 ? fg : bg;
175 1.36 rin c2 = i & 16 ? fg : bg;
176 1.36 rin c3 = i & 8 ? fg : bg;
177 1.36 rin c4 = i & 4 ? fg : bg;
178 1.9 ad #else
179 1.36 rin c1 = i & 8 ? fg : bg;
180 1.36 rin c2 = i & 4 ? fg : bg;
181 1.36 rin c3 = i & 16 ? fg : bg;
182 1.36 rin c4 = i & 32 ? fg : bg;
183 1.9 ad #endif
184 1.43 rin rasops24_makestamp1(ri, &stamp[i], c1, c2, c3, c4);
185 1.9 ad }
186 1.9 ad }
187 1.1 ad
188 1.35 rin #define RASOPS_WIDTH 8
189 1.35 rin #include "rasops_putchar_width.h"
190 1.35 rin #undef RASOPS_WIDTH
191 1.35 rin
192 1.35 rin #define RASOPS_WIDTH 12
193 1.35 rin #include "rasops_putchar_width.h"
194 1.35 rin #undef RASOPS_WIDTH
195 1.35 rin
196 1.35 rin #define RASOPS_WIDTH 16
197 1.35 rin #include "rasops_putchar_width.h"
198 1.35 rin #undef RASOPS_WIDTH
199 1.12 pk
200 1.11 ad #endif /* !RASOPS_SMALL */
201 1.1 ad
202 1.1 ad /*
203 1.4 ad * Erase rows. This is nice and easy due to alignment.
204 1.1 ad */
205 1.1 ad static void
206 1.27 dsl rasops24_eraserows(void *cookie, int row, int num, long attr)
207 1.1 ad {
208 1.39 rin struct rasops_info *ri = (struct rasops_info *)cookie;
209 1.42 rin int full, slop, cnt, stride;
210 1.47 rin uint32_t *rp, *dp, *hp, clr, xstamp[3];
211 1.39 rin
212 1.39 rin hp = NULL; /* XXX GCC */
213 1.12 pk
214 1.12 pk /*
215 1.4 ad * If the color is gray, we can cheat and use the generic routines
216 1.4 ad * (which are faster, hopefully) since the r,g,b values are the same.
217 1.4 ad */
218 1.30 mlelstv if ((attr & WSATTR_PRIVATE2) != 0) {
219 1.4 ad rasops_eraserows(cookie, row, num, attr);
220 1.4 ad return;
221 1.4 ad }
222 1.4 ad
223 1.1 ad #ifdef RASOPS_CLIPPING
224 1.1 ad if (row < 0) {
225 1.1 ad num += row;
226 1.1 ad row = 0;
227 1.1 ad }
228 1.1 ad
229 1.39 rin if (row + num > ri->ri_rows)
230 1.1 ad num = ri->ri_rows - row;
231 1.12 pk
232 1.1 ad if (num <= 0)
233 1.1 ad return;
234 1.1 ad #endif
235 1.12 pk
236 1.36 rin clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
237 1.47 rin rasops24_makestamp1(ri, xstamp, clr, clr, clr, clr);
238 1.4 ad
239 1.12 pk /*
240 1.7 ad * XXX the wsdisplay_emulops interface seems a little deficient in
241 1.12 pk * that there is no way to clear the *entire* screen. We provide a
242 1.12 pk * workaround here: if the entire console area is being cleared, and
243 1.7 ad * the RI_FULLCLEAR flag is set, clear the entire display.
244 1.12 pk */
245 1.7 ad if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
246 1.7 ad stride = ri->ri_stride;
247 1.7 ad num = ri->ri_height;
248 1.39 rin rp = (uint32_t *)ri->ri_origbits;
249 1.39 rin if (ri->ri_hwbits)
250 1.39 rin hp = (uint32_t *)ri->ri_hworigbits;
251 1.7 ad } else {
252 1.7 ad stride = ri->ri_emustride;
253 1.7 ad num *= ri->ri_font->fontheight;
254 1.39 rin rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
255 1.39 rin if (ri->ri_hwbits)
256 1.39 rin hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
257 1.7 ad }
258 1.7 ad
259 1.42 rin full = stride / (4 * 3);
260 1.42 rin slop = (stride - full * (4 * 3)) / 4;
261 1.12 pk
262 1.47 rin while (num--) {
263 1.47 rin dp = rp;
264 1.47 rin for (cnt = full; cnt; cnt--) {
265 1.47 rin dp[0] = xstamp[0];
266 1.47 rin dp[1] = xstamp[1];
267 1.47 rin dp[2] = xstamp[2];
268 1.47 rin dp += 3;
269 1.47 rin }
270 1.47 rin for (cnt = 0; cnt < slop; cnt++)
271 1.47 rin *dp++ = xstamp[cnt];
272 1.1 ad
273 1.39 rin if (ri->ri_hwbits) {
274 1.47 rin memcpy(hp, rp, stride);
275 1.39 rin DELTA(hp, ri->ri_stride, uint32_t *);
276 1.39 rin }
277 1.47 rin
278 1.47 rin DELTA(rp, ri->ri_stride, uint32_t *);
279 1.4 ad }
280 1.4 ad }
281 1.4 ad
282 1.4 ad /*
283 1.4 ad * Erase columns.
284 1.4 ad */
285 1.4 ad static void
286 1.27 dsl rasops24_erasecols(void *cookie, int row, int col, int num, long attr)
287 1.4 ad {
288 1.39 rin struct rasops_info *ri = (struct rasops_info *)cookie;
289 1.47 rin int height, cnt, slop1, slop2, full;
290 1.47 rin uint32_t clr, xstamp[3], *dp;
291 1.39 rin uint8_t *rp, *hp, *dbp;
292 1.39 rin
293 1.39 rin hp = NULL; /* XXX GCC */
294 1.4 ad
295 1.12 pk /*
296 1.4 ad * If the color is gray, we can cheat and use the generic routines
297 1.4 ad * (which are faster, hopefully) since the r,g,b values are the same.
298 1.4 ad */
299 1.30 mlelstv if ((attr & WSATTR_PRIVATE2) != 0) {
300 1.4 ad rasops_erasecols(cookie, row, col, num, attr);
301 1.4 ad return;
302 1.4 ad }
303 1.12 pk
304 1.12 pk #ifdef RASOPS_CLIPPING
305 1.12 pk /* Catches 'row < 0' case too */
306 1.4 ad if ((unsigned)row >= (unsigned)ri->ri_rows)
307 1.4 ad return;
308 1.4 ad
309 1.4 ad if (col < 0) {
310 1.4 ad num += col;
311 1.4 ad col = 0;
312 1.4 ad }
313 1.4 ad
314 1.39 rin if (col + num > ri->ri_cols)
315 1.4 ad num = ri->ri_cols - col;
316 1.12 pk
317 1.4 ad if (num <= 0)
318 1.4 ad return;
319 1.4 ad #endif
320 1.12 pk
321 1.39 rin rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
322 1.39 rin if (ri->ri_hwbits)
323 1.39 rin hp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
324 1.39 rin
325 1.47 rin num *= ri->ri_xscale;
326 1.4 ad height = ri->ri_font->fontheight;
327 1.4 ad
328 1.36 rin clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
329 1.47 rin rasops24_makestamp1(ri, xstamp, clr, clr, clr, clr);
330 1.12 pk
331 1.47 rin /*
332 1.47 rin * Align to word boundary by 24-bit-wise operations:
333 1.47 rin *
334 1.47 rin * rp % 4 == 1 ---> slop1 = 3:
335 1.47 rin * 0123
336 1.47 rin * -RGB
337 1.47 rin *
338 1.47 rin * rp % 4 == 2 ---> slop1 = 6:
339 1.47 rin * 0123 0123
340 1.47 rin * --RG BRGB
341 1.47 rin *
342 1.47 rin * rp % 4 == 3 ---> slop1 = 9:
343 1.47 rin * 0123 0123 0123
344 1.47 rin * ---R GBRG BRGB
345 1.47 rin */
346 1.47 rin slop1 = 3 * ((uintptr_t)rp % 4);
347 1.47 rin slop2 = (num - slop1) % 12;
348 1.47 rin full = (num - slop1 /* - slop2 */) / 12;
349 1.47 rin
350 1.47 rin while (height--) {
351 1.47 rin /* Align to word boundary */
352 1.47 rin dbp = rp;
353 1.47 rin for (cnt = slop1; cnt; cnt -= 3) {
354 1.47 rin *dbp++ = (clr >> 16);
355 1.47 rin *dbp++ = (clr >> 8);
356 1.47 rin *dbp++ = clr;
357 1.47 rin }
358 1.47 rin
359 1.47 rin /* 4 pels per loop */
360 1.47 rin dp = (uint32_t *)dbp;
361 1.47 rin for (cnt = full; cnt; cnt--) {
362 1.47 rin dp[0] = xstamp[0];
363 1.47 rin dp[1] = xstamp[1];
364 1.47 rin dp[2] = xstamp[2];
365 1.47 rin dp += 3;
366 1.47 rin }
367 1.4 ad
368 1.47 rin /* Trailing slop */
369 1.47 rin dbp = (uint8_t *)dp;
370 1.47 rin for (cnt = slop2; cnt; cnt -= 3) {
371 1.47 rin *dbp++ = (clr >> 16);
372 1.47 rin *dbp++ = (clr >> 8);
373 1.47 rin *dbp++ = clr;
374 1.47 rin }
375 1.39 rin
376 1.39 rin if (ri->ri_hwbits) {
377 1.47 rin memcpy(hp, rp, num);
378 1.39 rin hp += ri->ri_stride;
379 1.12 pk }
380 1.47 rin
381 1.47 rin rp += ri->ri_stride;
382 1.1 ad }
383 1.1 ad }
384