rasops4.c revision 1.15 1 /* $NetBSD: rasops4.c,v 1.15 2019/07/24 18:33:49 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: rasops4.c,v 1.15 2019/07/24 18:33:49 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 #include <machine/endian.h>
41
42 #include <dev/wscons/wsdisplayvar.h>
43 #include <dev/wscons/wsconsio.h>
44 #include <dev/rasops/rasops.h>
45 #include <dev/rasops/rasops_masks.h>
46
47 static void rasops4_copycols(void *, int, int, int, int);
48 static void rasops4_erasecols(void *, int, int, int, long);
49 static void rasops4_do_cursor(struct rasops_info *);
50 static void rasops4_putchar(void *, int, int col, u_int, long);
51 #ifndef RASOPS_SMALL
52 static void rasops4_putchar8(void *, int, int col, u_int, long);
53 static void rasops4_putchar12(void *, int, int col, u_int, long);
54 static void rasops4_putchar16(void *, int, int col, u_int, long);
55 static void rasops4_makestamp(struct rasops_info *, long);
56
57 /*
58 * 4x1 stamp for optimized character blitting
59 */
60 static uint16_t stamp[16];
61 static long stamp_attr;
62 static int stamp_mutex; /* XXX see note in README */
63 #endif
64
65 /*
66 * Initialize rasops_info struct for this colordepth.
67 */
68 void
69 rasops4_init(struct rasops_info *ri)
70 {
71
72 switch (ri->ri_font->fontwidth) {
73 #ifndef RASOPS_SMALL
74 case 8:
75 ri->ri_ops.putchar = rasops4_putchar8;
76 break;
77 case 12:
78 ri->ri_ops.putchar = rasops4_putchar12;
79 break;
80 case 16:
81 ri->ri_ops.putchar = rasops4_putchar16;
82 break;
83 #endif /* !RASOPS_SMALL */
84 default:
85 panic("fontwidth not 8/12/16 or RASOPS_SMALL - fixme!");
86 ri->ri_ops.putchar = rasops4_putchar;
87 break;
88 }
89
90 if ((ri->ri_font->fontwidth & 1) != 0) {
91 ri->ri_ops.erasecols = rasops4_erasecols;
92 ri->ri_ops.copycols = rasops4_copycols;
93 ri->ri_do_cursor = rasops4_do_cursor;
94 }
95 }
96
97 #ifdef notyet
98 /*
99 * Paint a single character. This is the generic version, this is ugly.
100 */
101 static void
102 rasops4_putchar(void *cookie, int row, int col, u_int uc, long attr)
103 {
104 int height, width, fs, rs, fb, bg, fg, lmask, rmask;
105 struct rasops_info *ri = (struct rasops_info *)cookie;
106 struct wsdisplay_font *font = PICK_FONT(ri, uc);
107 uint32_t *rp;
108 uint8_t *fr;
109
110 #ifdef RASOPS_CLIPPING
111 /* Catches 'row < 0' case too */
112 if ((unsigned)row >= (unsigned)ri->ri_rows)
113 return;
114
115 if ((unsigned)col >= (unsigned)ri->ri_cols)
116 return;
117 #endif
118
119 width = font->fontwidth << 1;
120 height = font->fontheight;
121 col *= width;
122 rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
123 ((col >> 3) & ~3));
124 col = col & 31;
125 rs = ri->ri_stride;
126
127 bg = ri->ri_devcmap[(attr >> 16) & 0xf];
128 fg = ri->ri_devcmap[(attr >> 24) & 0xf];
129
130 /* If fg and bg match this becomes a space character */
131 if (fg == bg || uc == ' ') {
132 uc = (u_int)-1;
133 fr = 0; /* shutup gcc */
134 fs = 0; /* shutup gcc */
135 } else {
136 uc -= font->firstchar;
137 fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
138 fs = font->stride;
139 }
140
141 /* Single word, one mask */
142 if ((col + width) <= 32) {
143 rmask = rasops_pmask[col][width];
144 lmask = ~rmask;
145
146 if (uc == (u_int)-1) {
147 bg &= rmask;
148
149 while (height--) {
150 *rp = (*rp & lmask) | bg;
151 DELTA(rp, rs, uint32_t *);
152 }
153 } else {
154 while (height--) {
155 /* get bits, mask */
156 /* compose sl */
157 /* mask sl */
158 /* put word */
159 }
160 }
161
162 /* Do underline */
163 if (attr & WSATTR_UNDERLINE) {
164 DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
165 *rp = (*rp & lmask) | (fg & rmask);
166 }
167 } else {
168 lmask = ~rasops_lmask[col];
169 rmask = ~rasops_rmask[(col + width) & 31];
170
171 if (uc == (u_int)-1) {
172 bg = bg & ~lmask;
173 width = bg & ~rmask;
174
175 while (height--) {
176 rp[0] = (rp[0] & lmask) | bg;
177 rp[1] = (rp[1] & rmask) | width;
178 DELTA(rp, rs, uint32_t *);
179 }
180 } else {
181 width = 32 - col;
182
183 /* NOT fontbits if bg is white */
184 while (height--) {
185 fb = ~(fr[3] | (fr[2] << 8) |
186 (fr[1] << 16) | (fr[0] << 24));
187
188 rp[0] = (rp[0] & lmask)
189 | MBE((u_int)fb >> col);
190
191 rp[1] = (rp[1] & rmask)
192 | (MBE((u_int)fb << width) & ~rmask);
193
194 fr += fs;
195 DELTA(rp, rs, uint32_t *);
196 }
197 }
198
199 /* Do underline */
200 if (attr & WSATTR_UNDERLINE) {
201 DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
202 rp[0] = (rp[0] & lmask) | (fg & ~lmask);
203 rp[1] = (rp[1] & rmask) | (fg & ~rmask);
204 }
205 }
206 }
207 #endif
208
209 /*
210 * Put a single character. This is the generic version.
211 */
212 static void
213 rasops4_putchar(void *cookie, int row, int col, u_int uc, long attr)
214 {
215
216 /* XXX punt */
217 }
218
219 #ifndef RASOPS_SMALL
220 /*
221 * Recompute the blitting stamp.
222 */
223 static void
224 rasops4_makestamp(struct rasops_info *ri, long attr)
225 {
226 int i, fg, bg;
227
228 fg = ri->ri_devcmap[(attr >> 24) & 0xf] & 0xf;
229 bg = ri->ri_devcmap[(attr >> 16) & 0xf] & 0xf;
230 stamp_attr = attr;
231
232 for (i = 0; i < 16; i++) {
233 #if BYTE_ORDER == BIG_ENDIAN
234 #define NEED_LITTLE_ENDIAN_STAMP RI_BSWAP
235 #else
236 #define NEED_LITTLE_ENDIAN_STAMP 0
237 #endif
238 if ((ri->ri_flg & RI_BSWAP) == NEED_LITTLE_ENDIAN_STAMP) {
239 /* little endian */
240 stamp[i] = (i & 1 ? fg : bg) << 12;
241 stamp[i] |= (i & 2 ? fg : bg) << 8;
242 stamp[i] |= (i & 4 ? fg : bg) << 4;
243 stamp[i] |= (i & 8 ? fg : bg) << 0;
244 } else {
245 /* big endian */
246 stamp[i] = (i & 1 ? fg : bg) << 8;
247 stamp[i] |= (i & 2 ? fg : bg) << 12;
248 stamp[i] |= (i & 4 ? fg : bg) << 0;
249 stamp[i] |= (i & 8 ? fg : bg) << 4;
250 }
251 }
252 }
253
254 /*
255 * Put a single character. This is for 8-pixel wide fonts.
256 */
257 static void
258 rasops4_putchar8(void *cookie, int row, int col, u_int uc, long attr)
259 {
260 struct rasops_info *ri = (struct rasops_info *)cookie;
261 struct wsdisplay_font *font = PICK_FONT(ri, uc);
262 int height, fs, rs;
263 uint8_t *fr;
264 uint16_t *rp;
265
266 /* Can't risk remaking the stamp if it's already in use */
267 if (stamp_mutex++) {
268 stamp_mutex--;
269 rasops4_putchar(cookie, row, col, uc, attr);
270 return;
271 }
272
273 #ifdef RASOPS_CLIPPING
274 /* Catches 'row < 0' case too */
275 if ((unsigned)row >= (unsigned)ri->ri_rows) {
276 stamp_mutex--;
277 return;
278 }
279
280 if ((unsigned)col >= (unsigned)ri->ri_cols) {
281 stamp_mutex--;
282 return;
283 }
284 #endif
285
286 rp = (uint16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
287 height = font->fontheight;
288 rs = ri->ri_stride / sizeof(*rp);
289
290 /* Recompute stamp? */
291 if (attr != stamp_attr)
292 rasops4_makestamp(ri, attr);
293
294 if (uc == ' ') {
295 uint16_t c = stamp[0];
296 while (height--) {
297 rp[0] = c;
298 rp[1] = c;
299 rp += rs;
300 }
301 } else {
302 uc -= font->firstchar;
303 fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
304 fs = font->stride;
305
306 while (height--) {
307 rp[0] = stamp[(*fr >> 4) & 0xf];
308 rp[1] = stamp[*fr & 0xf];
309 fr += fs;
310 rp += rs;
311 }
312 }
313
314 /* Do underline */
315 if ((attr & WSATTR_UNDERLINE) != 0) {
316 rp -= (rs << 1);
317 rp[0] = stamp[15];
318 rp[1] = stamp[15];
319 }
320
321 stamp_mutex--;
322 }
323
324 /*
325 * Put a single character. This is for 12-pixel wide fonts.
326 */
327 static void
328 rasops4_putchar12(void *cookie, int row, int col, u_int uc, long attr)
329 {
330 struct rasops_info *ri = (struct rasops_info *)cookie;
331 struct wsdisplay_font *font = PICK_FONT(ri, uc);
332 int height, fs, rs;
333 uint8_t *fr;
334 uint16_t *rp;
335
336 /* Can't risk remaking the stamp if it's already in use */
337 if (stamp_mutex++) {
338 stamp_mutex--;
339 rasops4_putchar(cookie, row, col, uc, attr);
340 return;
341 }
342
343 #ifdef RASOPS_CLIPPING
344 /* Catches 'row < 0' case too */
345 if ((unsigned)row >= (unsigned)ri->ri_rows) {
346 stamp_mutex--;
347 return;
348 }
349
350 if ((unsigned)col >= (unsigned)ri->ri_cols) {
351 stamp_mutex--;
352 return;
353 }
354 #endif
355
356 rp = (uint16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
357 height = font->fontheight;
358 rs = ri->ri_stride / sizeof(*rp);
359
360 /* Recompute stamp? */
361 if (attr != stamp_attr)
362 rasops4_makestamp(ri, attr);
363
364 if (uc == ' ') {
365 uint16_t c = stamp[0];
366 while (height--) {
367 rp[0] = c;
368 rp[1] = c;
369 rp[2] = c;
370 rp += rs;
371 }
372 } else {
373 uc -= font->firstchar;
374 fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
375 fs = font->stride;
376
377 while (height--) {
378 rp[0] = stamp[(fr[0] >> 4) & 0xf];
379 rp[1] = stamp[fr[0] & 0xf];
380 rp[2] = stamp[(fr[1] >> 4) & 0xf];
381 fr += fs;
382 rp += rs;
383 }
384 }
385
386 /* Do underline */
387 if ((attr & WSATTR_UNDERLINE) != 0) {
388 rp -= (rs << 1);
389 rp[0] = stamp[15];
390 rp[1] = stamp[15];
391 rp[2] = stamp[15];
392 }
393
394 stamp_mutex--;
395 }
396
397 /*
398 * Put a single character. This is for 16-pixel wide fonts.
399 */
400 static void
401 rasops4_putchar16(void *cookie, int row, int col, u_int uc, long attr)
402 {
403 struct rasops_info *ri = (struct rasops_info *)cookie;
404 struct wsdisplay_font *font = PICK_FONT(ri, uc);
405 int height, fs, rs;
406 uint8_t *fr;
407 uint16_t *rp;
408
409 /* Can't risk remaking the stamp if it's already in use */
410 if (stamp_mutex++) {
411 stamp_mutex--;
412 rasops4_putchar(cookie, row, col, uc, attr);
413 return;
414 }
415
416 #ifdef RASOPS_CLIPPING
417 /* Catches 'row < 0' case too */
418 if ((unsigned)row >= (unsigned)ri->ri_rows) {
419 stamp_mutex--;
420 return;
421 }
422
423 if ((unsigned)col >= (unsigned)ri->ri_cols) {
424 stamp_mutex--;
425 return;
426 }
427 #endif
428
429 rp = (uint16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
430 height = font->fontheight;
431 rs = ri->ri_stride / sizeof(*rp);
432
433 /* Recompute stamp? */
434 if (attr != stamp_attr)
435 rasops4_makestamp(ri, attr);
436
437 if (uc == ' ') {
438 uint16_t c = stamp[0];
439 while (height--) {
440 rp[0] = c;
441 rp[1] = c;
442 rp[2] = c;
443 rp[3] = c;
444 rp += rs;
445 }
446 } else {
447 uc -= font->firstchar;
448 fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
449 fs = font->stride;
450
451 while (height--) {
452 rp[0] = stamp[(fr[0] >> 4) & 0xf];
453 rp[1] = stamp[fr[0] & 0xf];
454 rp[2] = stamp[(fr[1] >> 4) & 0xf];
455 rp[3] = stamp[fr[1] & 0xf];
456 fr += fs;
457 rp += rs;
458 }
459 }
460
461 /* Do underline */
462 if ((attr & WSATTR_UNDERLINE) != 0) {
463 rp -= (rs << 1);
464 rp[0] = stamp[15];
465 rp[1] = stamp[15];
466 rp[2] = stamp[15];
467 rp[3] = stamp[15];
468 }
469
470 stamp_mutex--;
471 }
472 #endif /* !RASOPS_SMALL */
473
474 /*
475 * Grab routines common to depths where (bpp < 8)
476 */
477 #define NAME(ident) rasops4_##ident
478 #define PIXEL_SHIFT 3
479
480 #include <dev/rasops/rasops_bitops.h>
481