rasops24.c revision 1.38 1 1.38 rin /* $NetBSD: rasops24.c,v 1.38 2019/07/29 16:17:29 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.38 rin __KERNEL_RCSID(0, "$NetBSD: rasops24.c,v 1.38 2019/07/29 16:17:29 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.1 ad #include <dev/rasops/rasops.h>
47 1.1 ad
48 1.18 perry static void rasops24_erasecols(void *, int, int, int, long);
49 1.18 perry static void rasops24_eraserows(void *, int, int, long);
50 1.36 rin static void rasops24_putchar(void *, int, int, u_int, long);
51 1.37 rin static void rasops24_putchar_aa(void *, int, int, u_int, long);
52 1.9 ad #ifndef RASOPS_SMALL
53 1.36 rin static void rasops24_putchar8(void *, int, int, u_int, long);
54 1.36 rin static void rasops24_putchar12(void *, int, int, u_int, long);
55 1.36 rin static void rasops24_putchar16(void *, int, int, u_int, long);
56 1.18 perry static void rasops24_makestamp(struct rasops_info *, long);
57 1.1 ad
58 1.12 pk /*
59 1.12 pk * 4x1 stamp for optimized character blitting
60 1.4 ad */
61 1.32 rin static uint32_t stamp[64];
62 1.4 ad static long stamp_attr;
63 1.4 ad static int stamp_mutex; /* XXX see note in readme */
64 1.29 njoly #endif
65 1.4 ad
66 1.4 ad /*
67 1.4 ad * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
68 1.32 rin * destination uint32_t[0] = STAMP_READ(offset)
69 1.32 rin * destination uint32_t[1] = STAMP_READ(offset + 4)
70 1.32 rin * destination uint32_t[2] = STAMP_READ(offset + 8)
71 1.4 ad */
72 1.36 rin #define STAMP_SHIFT(fb, n) ((n) ? (fb) : (fb) << 4)
73 1.36 rin #define STAMP_MASK (0xf << 4)
74 1.36 rin #define STAMP_READ(o) (*(uint32_t *)((uint8_t *)stamp + (o)))
75 1.4 ad
76 1.1 ad /*
77 1.14 wiz * Initialize rasops_info struct for this colordepth.
78 1.1 ad */
79 1.1 ad void
80 1.26 dsl rasops24_init(struct rasops_info *ri)
81 1.1 ad {
82 1.1 ad
83 1.38 rin #ifndef RASOPS_SMALL
84 1.38 rin /*
85 1.38 rin * Different devcmap's are used depending on font widths,
86 1.38 rin * therefore we need reset stamp here.
87 1.38 rin */
88 1.38 rin stamp_attr = 0;
89 1.38 rin #endif
90 1.38 rin
91 1.36 rin if (ri->ri_rnum == 0) {
92 1.36 rin ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
93 1.36 rin
94 1.36 rin ri->ri_rpos = 0;
95 1.36 rin ri->ri_gpos = 8;
96 1.36 rin ri->ri_bpos = 16;
97 1.36 rin }
98 1.36 rin
99 1.36 rin ri->ri_ops.erasecols = rasops24_erasecols;
100 1.36 rin ri->ri_ops.eraserows = rasops24_eraserows;
101 1.36 rin
102 1.37 rin if (FONT_IS_ALPHA(ri->ri_font)) {
103 1.37 rin ri->ri_ops.putchar = rasops24_putchar_aa;
104 1.37 rin return;
105 1.37 rin }
106 1.37 rin
107 1.1 ad switch (ri->ri_font->fontwidth) {
108 1.9 ad #ifndef RASOPS_SMALL
109 1.1 ad case 8:
110 1.4 ad ri->ri_ops.putchar = rasops24_putchar8;
111 1.1 ad break;
112 1.1 ad case 12:
113 1.4 ad ri->ri_ops.putchar = rasops24_putchar12;
114 1.1 ad break;
115 1.1 ad case 16:
116 1.4 ad ri->ri_ops.putchar = rasops24_putchar16;
117 1.1 ad break;
118 1.9 ad #endif
119 1.1 ad default:
120 1.4 ad ri->ri_ops.putchar = rasops24_putchar;
121 1.1 ad break;
122 1.1 ad }
123 1.1 ad }
124 1.1 ad
125 1.35 rin #define RASOPS_DEPTH 24
126 1.35 rin #include "rasops_putchar.h"
127 1.37 rin #include "rasops_putchar_aa.h"
128 1.1 ad
129 1.9 ad #ifndef RASOPS_SMALL
130 1.9 ad /*
131 1.9 ad * Recompute the blitting stamp.
132 1.9 ad */
133 1.9 ad static void
134 1.26 dsl rasops24_makestamp(struct rasops_info *ri, long attr)
135 1.9 ad {
136 1.36 rin uint32_t fg, bg, c1, c2, c3, c4;
137 1.9 ad int i;
138 1.12 pk
139 1.36 rin fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xffffff;
140 1.36 rin bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
141 1.9 ad stamp_attr = attr;
142 1.12 pk
143 1.9 ad for (i = 0; i < 64; i += 4) {
144 1.9 ad #if BYTE_ORDER == LITTLE_ENDIAN
145 1.36 rin c1 = i & 32 ? fg : bg;
146 1.36 rin c2 = i & 16 ? fg : bg;
147 1.36 rin c3 = i & 8 ? fg : bg;
148 1.36 rin c4 = i & 4 ? fg : bg;
149 1.9 ad #else
150 1.36 rin c1 = i & 8 ? fg : bg;
151 1.36 rin c2 = i & 4 ? fg : bg;
152 1.36 rin c3 = i & 16 ? fg : bg;
153 1.36 rin c4 = i & 32 ? fg : bg;
154 1.9 ad #endif
155 1.36 rin stamp[i + 0] = (c1 << 8) | (c2 >> 16);
156 1.36 rin stamp[i + 1] = (c2 << 16) | (c3 >> 8);
157 1.36 rin stamp[i + 2] = (c3 << 24) | c4;
158 1.9 ad
159 1.9 ad #if BYTE_ORDER == LITTLE_ENDIAN
160 1.9 ad if ((ri->ri_flg & RI_BSWAP) == 0) {
161 1.9 ad #else
162 1.9 ad if ((ri->ri_flg & RI_BSWAP) != 0) {
163 1.9 ad #endif
164 1.36 rin stamp[i + 0] = bswap32(stamp[i + 0]);
165 1.36 rin stamp[i + 1] = bswap32(stamp[i + 1]);
166 1.36 rin stamp[i + 2] = bswap32(stamp[i + 2]);
167 1.9 ad }
168 1.9 ad }
169 1.9 ad }
170 1.1 ad
171 1.35 rin #define RASOPS_WIDTH 8
172 1.35 rin #include "rasops_putchar_width.h"
173 1.35 rin #undef RASOPS_WIDTH
174 1.35 rin
175 1.35 rin #define RASOPS_WIDTH 12
176 1.35 rin #include "rasops_putchar_width.h"
177 1.35 rin #undef RASOPS_WIDTH
178 1.35 rin
179 1.35 rin #define RASOPS_WIDTH 16
180 1.35 rin #include "rasops_putchar_width.h"
181 1.35 rin #undef RASOPS_WIDTH
182 1.12 pk
183 1.11 ad #endif /* !RASOPS_SMALL */
184 1.1 ad
185 1.1 ad /*
186 1.4 ad * Erase rows. This is nice and easy due to alignment.
187 1.1 ad */
188 1.1 ad static void
189 1.27 dsl rasops24_eraserows(void *cookie, int row, int num, long attr)
190 1.1 ad {
191 1.8 ad int n9, n3, n1, cnt, stride, delta;
192 1.31 rin uint32_t *dp, clr, xstamp[3];
193 1.1 ad struct rasops_info *ri;
194 1.12 pk
195 1.12 pk /*
196 1.4 ad * If the color is gray, we can cheat and use the generic routines
197 1.4 ad * (which are faster, hopefully) since the r,g,b values are the same.
198 1.4 ad */
199 1.30 mlelstv if ((attr & WSATTR_PRIVATE2) != 0) {
200 1.4 ad rasops_eraserows(cookie, row, num, attr);
201 1.4 ad return;
202 1.4 ad }
203 1.4 ad
204 1.1 ad ri = (struct rasops_info *)cookie;
205 1.1 ad
206 1.1 ad #ifdef RASOPS_CLIPPING
207 1.1 ad if (row < 0) {
208 1.1 ad num += row;
209 1.1 ad row = 0;
210 1.1 ad }
211 1.1 ad
212 1.1 ad if ((row + num) > ri->ri_rows)
213 1.1 ad num = ri->ri_rows - row;
214 1.12 pk
215 1.1 ad if (num <= 0)
216 1.1 ad return;
217 1.1 ad #endif
218 1.12 pk
219 1.36 rin clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
220 1.20 christos xstamp[0] = (clr << 8) | (clr >> 16);
221 1.20 christos xstamp[1] = (clr << 16) | (clr >> 8);
222 1.20 christos xstamp[2] = (clr << 24) | clr;
223 1.4 ad
224 1.4 ad #if BYTE_ORDER == LITTLE_ENDIAN
225 1.7 ad if ((ri->ri_flg & RI_BSWAP) == 0) {
226 1.4 ad #else
227 1.7 ad if ((ri->ri_flg & RI_BSWAP) != 0) {
228 1.4 ad #endif
229 1.20 christos xstamp[0] = bswap32(xstamp[0]);
230 1.20 christos xstamp[1] = bswap32(xstamp[1]);
231 1.20 christos xstamp[2] = bswap32(xstamp[2]);
232 1.4 ad }
233 1.4 ad
234 1.12 pk /*
235 1.7 ad * XXX the wsdisplay_emulops interface seems a little deficient in
236 1.12 pk * that there is no way to clear the *entire* screen. We provide a
237 1.12 pk * workaround here: if the entire console area is being cleared, and
238 1.7 ad * the RI_FULLCLEAR flag is set, clear the entire display.
239 1.12 pk */
240 1.7 ad if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
241 1.7 ad stride = ri->ri_stride;
242 1.7 ad num = ri->ri_height;
243 1.32 rin dp = (uint32_t *)ri->ri_origbits;
244 1.8 ad delta = 0;
245 1.7 ad } else {
246 1.7 ad stride = ri->ri_emustride;
247 1.7 ad num *= ri->ri_font->fontheight;
248 1.32 rin dp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
249 1.8 ad delta = ri->ri_delta;
250 1.7 ad }
251 1.7 ad
252 1.7 ad n9 = stride / 36;
253 1.1 ad cnt = (n9 << 5) + (n9 << 2); /* (32*n9) + (4*n9) */
254 1.7 ad n3 = (stride - cnt) / 12;
255 1.1 ad cnt += (n3 << 3) + (n3 << 2); /* (8*n3) + (4*n3) */
256 1.7 ad n1 = (stride - cnt) >> 2;
257 1.12 pk
258 1.4 ad while (num--) {
259 1.4 ad for (cnt = n9; cnt; cnt--) {
260 1.20 christos dp[0] = xstamp[0];
261 1.20 christos dp[1] = xstamp[1];
262 1.20 christos dp[2] = xstamp[2];
263 1.20 christos dp[3] = xstamp[0];
264 1.20 christos dp[4] = xstamp[1];
265 1.20 christos dp[5] = xstamp[2];
266 1.20 christos dp[6] = xstamp[0];
267 1.20 christos dp[7] = xstamp[1];
268 1.20 christos dp[8] = xstamp[2];
269 1.4 ad dp += 9;
270 1.4 ad }
271 1.1 ad
272 1.4 ad for (cnt = n3; cnt; cnt--) {
273 1.20 christos dp[0] = xstamp[0];
274 1.20 christos dp[1] = xstamp[1];
275 1.20 christos dp[2] = xstamp[2];
276 1.4 ad dp += 3;
277 1.4 ad }
278 1.12 pk
279 1.4 ad for (cnt = 0; cnt < n1; cnt++)
280 1.20 christos *dp++ = xstamp[cnt];
281 1.12 pk
282 1.32 rin DELTA(dp, delta, uint32_t *);
283 1.4 ad }
284 1.4 ad }
285 1.4 ad
286 1.4 ad /*
287 1.4 ad * Erase columns.
288 1.4 ad */
289 1.4 ad static void
290 1.27 dsl rasops24_erasecols(void *cookie, int row, int col, int num, long attr)
291 1.4 ad {
292 1.20 christos int n12, n4, height, cnt, slop, clr, xstamp[3];
293 1.4 ad struct rasops_info *ri;
294 1.32 rin uint32_t *dp, *rp;
295 1.31 rin uint8_t *dbp;
296 1.4 ad
297 1.12 pk /*
298 1.4 ad * If the color is gray, we can cheat and use the generic routines
299 1.4 ad * (which are faster, hopefully) since the r,g,b values are the same.
300 1.4 ad */
301 1.30 mlelstv if ((attr & WSATTR_PRIVATE2) != 0) {
302 1.4 ad rasops_erasecols(cookie, row, col, num, attr);
303 1.4 ad return;
304 1.4 ad }
305 1.12 pk
306 1.4 ad ri = (struct rasops_info *)cookie;
307 1.4 ad
308 1.12 pk #ifdef RASOPS_CLIPPING
309 1.12 pk /* Catches 'row < 0' case too */
310 1.4 ad if ((unsigned)row >= (unsigned)ri->ri_rows)
311 1.4 ad return;
312 1.4 ad
313 1.4 ad if (col < 0) {
314 1.4 ad num += col;
315 1.4 ad col = 0;
316 1.4 ad }
317 1.4 ad
318 1.4 ad if ((col + num) > ri->ri_cols)
319 1.4 ad num = ri->ri_cols - col;
320 1.12 pk
321 1.4 ad if (num <= 0)
322 1.4 ad return;
323 1.4 ad #endif
324 1.12 pk
325 1.32 rin rp = (uint32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
326 1.4 ad num *= ri->ri_font->fontwidth;
327 1.4 ad height = ri->ri_font->fontheight;
328 1.4 ad
329 1.36 rin clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
330 1.20 christos xstamp[0] = (clr << 8) | (clr >> 16);
331 1.20 christos xstamp[1] = (clr << 16) | (clr >> 8);
332 1.20 christos xstamp[2] = (clr << 24) | clr;
333 1.4 ad
334 1.4 ad #if BYTE_ORDER == LITTLE_ENDIAN
335 1.7 ad if ((ri->ri_flg & RI_BSWAP) == 0) {
336 1.4 ad #else
337 1.7 ad if ((ri->ri_flg & RI_BSWAP) != 0) {
338 1.4 ad #endif
339 1.20 christos xstamp[0] = bswap32(xstamp[0]);
340 1.20 christos xstamp[1] = bswap32(xstamp[1]);
341 1.20 christos xstamp[2] = bswap32(xstamp[2]);
342 1.4 ad }
343 1.12 pk
344 1.12 pk /*
345 1.4 ad * The current byte offset mod 4 tells us the number of 24-bit pels
346 1.4 ad * we need to write for alignment to 32-bits. Once we're aligned on
347 1.4 ad * a 32-bit boundary, we're also aligned on a 4 pixel boundary, so
348 1.4 ad * the stamp does not need to be rotated. The following shows the
349 1.9 ad * layout of 4 pels in a 3 word region and illustrates this:
350 1.4 ad *
351 1.4 ad * aaab bbcc cddd
352 1.4 ad */
353 1.17 petrov slop = (int)(long)rp & 3; num -= slop;
354 1.4 ad n12 = num / 12; num -= (n12 << 3) + (n12 << 2);
355 1.4 ad n4 = num >> 2; num &= 3;
356 1.12 pk
357 1.4 ad while (height--) {
358 1.31 rin dbp = (uint8_t *)rp;
359 1.32 rin DELTA(rp, ri->ri_stride, uint32_t *);
360 1.4 ad
361 1.4 ad /* Align to 4 bytes */
362 1.7 ad /* XXX handle with masks, bring under control of RI_BSWAP */
363 1.4 ad for (cnt = slop; cnt; cnt--) {
364 1.4 ad *dbp++ = (clr >> 16);
365 1.4 ad *dbp++ = (clr >> 8);
366 1.12 pk *dbp++ = clr;
367 1.12 pk }
368 1.4 ad
369 1.32 rin dp = (uint32_t *)dbp;
370 1.12 pk
371 1.4 ad /* 12 pels per loop */
372 1.4 ad for (cnt = n12; cnt; cnt--) {
373 1.20 christos dp[0] = xstamp[0];
374 1.20 christos dp[1] = xstamp[1];
375 1.20 christos dp[2] = xstamp[2];
376 1.20 christos dp[3] = xstamp[0];
377 1.20 christos dp[4] = xstamp[1];
378 1.20 christos dp[5] = xstamp[2];
379 1.20 christos dp[6] = xstamp[0];
380 1.20 christos dp[7] = xstamp[1];
381 1.20 christos dp[8] = xstamp[2];
382 1.4 ad dp += 9;
383 1.1 ad }
384 1.1 ad
385 1.4 ad /* 4 pels per loop */
386 1.4 ad for (cnt = n4; cnt; cnt--) {
387 1.20 christos dp[0] = xstamp[0];
388 1.20 christos dp[1] = xstamp[1];
389 1.20 christos dp[2] = xstamp[2];
390 1.4 ad dp += 3;
391 1.4 ad }
392 1.12 pk
393 1.4 ad /* Trailing slop */
394 1.7 ad /* XXX handle with masks, bring under control of RI_BSWAP */
395 1.31 rin dbp = (uint8_t *)dp;
396 1.4 ad for (cnt = num; cnt; cnt--) {
397 1.4 ad *dbp++ = (clr >> 16);
398 1.4 ad *dbp++ = (clr >> 8);
399 1.12 pk *dbp++ = clr;
400 1.12 pk }
401 1.1 ad }
402 1.1 ad }
403