rasops24.c revision 1.45 1 /* $NetBSD: rasops24.c,v 1.45 2019/08/02 23:05:42 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.45 2019/08/02 23:05:42 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 #define RASOPS_DEPTH 24
49 #include <dev/rasops/rasops.h>
50
51 static void rasops24_erasecols(void *, int, int, int, long);
52 static void rasops24_eraserows(void *, int, int, long);
53 static void rasops24_putchar(void *, int, int, u_int, long);
54 static void rasops24_putchar_aa(void *, int, int, u_int, long);
55 static __inline void
56 rasops24_makestamp1(struct rasops_info *, uint32_t *,
57 uint32_t, uint32_t, uint32_t, uint32_t);
58 #ifndef RASOPS_SMALL
59 static void rasops24_putchar8(void *, int, int, u_int, long);
60 static void rasops24_putchar12(void *, int, int, u_int, long);
61 static void rasops24_putchar16(void *, int, int, u_int, long);
62 static void rasops24_makestamp(struct rasops_info *, long);
63 #endif
64
65 /*
66 * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
67 * destination uint32_t[0] = STAMP_READ(offset)
68 * destination uint32_t[1] = STAMP_READ(offset + 4)
69 * destination uint32_t[2] = STAMP_READ(offset + 8)
70 */
71 #define STAMP_SHIFT(fb, n) ((n) ? (fb) : (fb) << 4)
72 #define STAMP_MASK (0xf << 4)
73 #define STAMP_READ(o) (*(uint32_t *)((uint8_t *)stamp + (o)))
74
75 /*
76 * Initialize rasops_info struct for this colordepth.
77 */
78 void
79 rasops24_init(struct rasops_info *ri)
80 {
81
82 if (ri->ri_rnum == 0) {
83 ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
84
85 ri->ri_rpos = 0;
86 ri->ri_gpos = 8;
87 ri->ri_bpos = 16;
88 }
89
90 ri->ri_ops.erasecols = rasops24_erasecols;
91 ri->ri_ops.eraserows = rasops24_eraserows;
92
93 if (FONT_IS_ALPHA(ri->ri_font)) {
94 ri->ri_ops.putchar = rasops24_putchar_aa;
95 return;
96 }
97
98 switch (ri->ri_font->fontwidth) {
99 #ifndef RASOPS_SMALL
100 case 8:
101 ri->ri_ops.putchar = rasops24_putchar8;
102 break;
103 case 12:
104 ri->ri_ops.putchar = rasops24_putchar12;
105 break;
106 case 16:
107 ri->ri_ops.putchar = rasops24_putchar16;
108 break;
109 #endif
110 default:
111 ri->ri_ops.putchar = rasops24_putchar;
112 return;
113 }
114
115 #ifndef RASOPS_SMALL
116 rasops_allocstamp(ri, sizeof(uint32_t) * 64);
117 #endif
118 }
119
120 #include "rasops_putchar.h"
121 #include "rasops_putchar_aa.h"
122
123 static __inline void
124 rasops24_makestamp1(struct rasops_info *ri, uint32_t *stamp,
125 uint32_t c1, uint32_t c2, uint32_t c3, uint32_t c4)
126 {
127
128 stamp[0] = (c1 << 8) | (c2 >> 16);
129 stamp[1] = (c2 << 16) | (c3 >> 8);
130 stamp[2] = (c3 << 24) | c4;
131
132 #if BYTE_ORDER == LITTLE_ENDIAN
133 if ((ri->ri_flg & RI_BSWAP) == 0)
134 #else
135 if ((ri->ri_flg & RI_BSWAP) != 0)
136 #endif
137 {
138 stamp[0] = bswap32(stamp[0]);
139 stamp[1] = bswap32(stamp[1]);
140 stamp[2] = bswap32(stamp[2]);
141 }
142 }
143
144 #ifndef RASOPS_SMALL
145 /*
146 * Recompute the blitting stamp.
147 */
148 static void
149 rasops24_makestamp(struct rasops_info *ri, long attr)
150 {
151 uint32_t *stamp = (uint32_t *)ri->ri_stamp;
152 uint32_t fg, bg, c1, c2, c3, c4;
153 int i;
154
155 fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xffffff;
156 bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
157 ri->ri_stamp_attr = attr;
158
159 for (i = 0; i < 64; i += 4) {
160 #if BYTE_ORDER == LITTLE_ENDIAN
161 c1 = i & 32 ? fg : bg;
162 c2 = i & 16 ? fg : bg;
163 c3 = i & 8 ? fg : bg;
164 c4 = i & 4 ? fg : bg;
165 #else
166 c1 = i & 8 ? fg : bg;
167 c2 = i & 4 ? fg : bg;
168 c3 = i & 16 ? fg : bg;
169 c4 = i & 32 ? fg : bg;
170 #endif
171 rasops24_makestamp1(ri, &stamp[i], c1, c2, c3, c4);
172 }
173 }
174
175 #define RASOPS_WIDTH 8
176 #include "rasops_putchar_width.h"
177 #undef RASOPS_WIDTH
178
179 #define RASOPS_WIDTH 12
180 #include "rasops_putchar_width.h"
181 #undef RASOPS_WIDTH
182
183 #define RASOPS_WIDTH 16
184 #include "rasops_putchar_width.h"
185 #undef RASOPS_WIDTH
186
187 #endif /* !RASOPS_SMALL */
188
189 /*
190 * Erase rows. This is nice and easy due to alignment.
191 */
192 static void
193 rasops24_eraserows(void *cookie, int row, int num, long attr)
194 {
195 struct rasops_info *ri = (struct rasops_info *)cookie;
196 uint32_t *buf = (uint32_t *)ri->ri_buf;
197 int full, slop, cnt, stride;
198 uint32_t *rp, *dp, *hp, clr, stamp[3];
199
200 hp = NULL; /* XXX GCC */
201
202 /*
203 * If the color is gray, we can cheat and use the generic routines
204 * (which are faster, hopefully) since the r,g,b values are the same.
205 */
206 if ((attr & WSATTR_PRIVATE2) != 0) {
207 rasops_eraserows(cookie, row, num, attr);
208 return;
209 }
210
211 #ifdef RASOPS_CLIPPING
212 if (row < 0) {
213 num += row;
214 row = 0;
215 }
216
217 if (row + num > ri->ri_rows)
218 num = ri->ri_rows - row;
219
220 if (num <= 0)
221 return;
222 #endif
223
224 clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
225 rasops24_makestamp1(ri, stamp, clr, clr, clr, clr);
226
227 /*
228 * XXX the wsdisplay_emulops interface seems a little deficient in
229 * that there is no way to clear the *entire* screen. We provide a
230 * workaround here: if the entire console area is being cleared, and
231 * the RI_FULLCLEAR flag is set, clear the entire display.
232 */
233 if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
234 stride = ri->ri_stride;
235 num = ri->ri_height;
236 rp = (uint32_t *)ri->ri_origbits;
237 if (ri->ri_hwbits)
238 hp = (uint32_t *)ri->ri_hworigbits;
239 } else {
240 stride = ri->ri_emustride;
241 num *= ri->ri_font->fontheight;
242 rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
243 if (ri->ri_hwbits)
244 hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
245 }
246
247 full = stride / (4 * 3);
248 slop = (stride - full * (4 * 3)) / 4;
249
250 dp = buf;
251
252 for (cnt = full; cnt; cnt--) {
253 dp[0] = stamp[0];
254 dp[1] = stamp[1];
255 dp[2] = stamp[2];
256 dp += 3;
257 }
258
259 for (cnt = 0; cnt < slop; cnt++)
260 *dp++ = stamp[cnt];
261
262 while (num--) {
263 memcpy(rp, buf, stride);
264 DELTA(rp, ri->ri_stride, uint32_t *);
265 if (ri->ri_hwbits) {
266 memcpy(hp, buf, stride);
267 DELTA(hp, ri->ri_stride, uint32_t *);
268 }
269 }
270 }
271
272 /*
273 * Erase columns.
274 */
275 static void
276 rasops24_erasecols(void *cookie, int row, int col, int num, long attr)
277 {
278 struct rasops_info *ri = (struct rasops_info *)cookie;
279 void *buf = ri->ri_buf;
280 int height, cnt, clr, stamp[3];
281 uint32_t *dp;
282 uint8_t *rp, *hp, *dbp;
283
284 hp = NULL; /* XXX GCC */
285
286 /*
287 * If the color is gray, we can cheat and use the generic routines
288 * (which are faster, hopefully) since the r,g,b values are the same.
289 */
290 if ((attr & WSATTR_PRIVATE2) != 0) {
291 rasops_erasecols(cookie, row, col, num, attr);
292 return;
293 }
294
295 #ifdef RASOPS_CLIPPING
296 /* Catches 'row < 0' case too */
297 if ((unsigned)row >= (unsigned)ri->ri_rows)
298 return;
299
300 if (col < 0) {
301 num += col;
302 col = 0;
303 }
304
305 if (col + num > ri->ri_cols)
306 num = ri->ri_cols - col;
307
308 if (num <= 0)
309 return;
310 #endif
311
312 rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
313 if (ri->ri_hwbits)
314 hp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
315
316 num *= ri->ri_font->fontwidth;
317 height = ri->ri_font->fontheight;
318
319 clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
320 rasops24_makestamp1(ri, stamp, clr, clr, clr, clr);
321
322 /* 4 pels per loop */
323 dp = (uint32_t *)buf;
324 for (cnt = num >> 2; cnt; cnt--) {
325 dp[0] = stamp[0];
326 dp[1] = stamp[1];
327 dp[2] = stamp[2];
328 dp += 3;
329 }
330
331 /* Trailing slop */
332 /* XXX handle with masks, bring under control of RI_BSWAP */
333 dbp = (uint8_t *)dp;
334 for (cnt = num & 3; cnt; cnt--) {
335 *dbp++ = (clr >> 16);
336 *dbp++ = (clr >> 8);
337 *dbp++ = clr;
338 }
339
340 num *= 3;
341
342 while (height--) {
343 memcpy(rp, buf, num);
344 rp += ri->ri_stride;
345 if (ri->ri_hwbits) {
346 memcpy(hp, buf, num);
347 hp += ri->ri_stride;
348 }
349 }
350 }
351