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