rasops.c revision 1.96 1 /* $NetBSD: rasops.c,v 1.96 2019/07/29 14:43:14 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: rasops.c,v 1.96 2019/07/29 14:43:14 rin Exp $");
34
35 #include "opt_rasops.h"
36 #include "rasops_glue.h"
37 #include "opt_wsmsgattrs.h"
38
39 #include <sys/param.h>
40 #include <sys/bswap.h>
41 #include <sys/kmem.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44
45 #include <machine/endian.h>
46
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wsfont/wsfont.h>
50 #include <dev/rasops/rasops.h>
51
52 #ifndef _KERNEL
53 #include <errno.h>
54 #endif
55
56 #ifdef RASOPS_DEBUG
57 #define DPRINTF(...) aprint_error(...)
58 #else
59 #define DPRINTF(...) __nothing
60 #endif
61
62 struct rasops_matchdata {
63 struct rasops_info *ri;
64 int wantcols, wantrows;
65 int bestscore;
66 struct wsdisplay_font *pick;
67 int ident;
68 };
69
70 /* ANSI colormap (R,G,B). Upper 8 are high-intensity */
71 const uint8_t rasops_cmap[256 * 3] = {
72 0x00, 0x00, 0x00, /* black */
73 0x7f, 0x00, 0x00, /* red */
74 0x00, 0x7f, 0x00, /* green */
75 0x7f, 0x7f, 0x00, /* brown */
76 0x00, 0x00, 0x7f, /* blue */
77 0x7f, 0x00, 0x7f, /* magenta */
78 0x00, 0x7f, 0x7f, /* cyan */
79 0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
80
81 0x7f, 0x7f, 0x7f, /* black */
82 0xff, 0x00, 0x00, /* red */
83 0x00, 0xff, 0x00, /* green */
84 0xff, 0xff, 0x00, /* brown */
85 0x00, 0x00, 0xff, /* blue */
86 0xff, 0x00, 0xff, /* magenta */
87 0x00, 0xff, 0xff, /* cyan */
88 0xff, 0xff, 0xff, /* white */
89
90 /*
91 * For the cursor, we need at least the last (255th)
92 * color to be white. Fill up white completely for
93 * simplicity.
94 */
95 #define _CMWHITE 0xff, 0xff, 0xff,
96 #define _CMWHITE16 _CMWHITE _CMWHITE _CMWHITE _CMWHITE \
97 _CMWHITE _CMWHITE _CMWHITE _CMWHITE \
98 _CMWHITE _CMWHITE _CMWHITE _CMWHITE \
99 _CMWHITE _CMWHITE _CMWHITE _CMWHITE
100 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
101 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
102 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 /* but not the last one */
103 #undef _CMWHITE16
104 #undef _CMWHITE
105
106 /*
107 * For the cursor the fg/bg indices are bit inverted, so
108 * provide complimentary colors in the upper 16 entries.
109 */
110 0x7f, 0x7f, 0x7f, /* black */
111 0xff, 0x00, 0x00, /* red */
112 0x00, 0xff, 0x00, /* green */
113 0xff, 0xff, 0x00, /* brown */
114 0x00, 0x00, 0xff, /* blue */
115 0xff, 0x00, 0xff, /* magenta */
116 0x00, 0xff, 0xff, /* cyan */
117 0xff, 0xff, 0xff, /* white */
118
119 0x00, 0x00, 0x00, /* black */
120 0x7f, 0x00, 0x00, /* red */
121 0x00, 0x7f, 0x00, /* green */
122 0x7f, 0x7f, 0x00, /* brown */
123 0x00, 0x00, 0x7f, /* blue */
124 0x7f, 0x00, 0x7f, /* magenta */
125 0x00, 0x7f, 0x7f, /* cyan */
126 0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
127 };
128
129 /* True if color is gray */
130 const uint8_t rasops_isgray[16] = {
131 1, 0, 0, 0,
132 0, 0, 0, 1,
133 1, 0, 0, 0,
134 0, 0, 0, 1,
135 };
136
137 #ifdef RASOPS_APPLE_PALETTE
138 /*
139 * Approximate ANSI colormap for legacy Apple color palettes
140 */
141 static const uint8_t apple8_devcmap[16] = {
142 0xff, /* black 0x00, 0x00, 0x00 */
143 0x6b, /* red 0x99, 0x00, 0x00 */
144 0xc5, /* green 0x00, 0x99, 0x00 */
145 0x59, /* yellow 0x99, 0x99, 0x00 */
146 0xd4, /* blue 0x00, 0x00, 0x99 */
147 0x68, /* magenta 0x99, 0x00, 0x99 */
148 0xc2, /* cyan 0x00, 0x99, 0x99 */
149 0x2b, /* white 0xcc, 0xcc, 0xcc */
150
151 0x56, /* black 0x99, 0x99, 0x99 */
152 0x23, /* red 0xff, 0x00, 0x00 */
153 0xb9, /* green 0x00, 0xff, 0x00 */
154 0x05, /* yellow 0xff, 0xff, 0x00 */
155 0xd2, /* blue 0x00, 0x00, 0xff */
156 0x1e, /* magenta 0xff, 0x00, 0xff */
157 0xb4, /* cyan 0x00, 0xff, 0xff */
158 0x00, /* white 0xff, 0xff, 0xff */
159 };
160
161 static const uint8_t apple4_devcmap[16] = {
162 15, /* black */
163 3, /* red */
164 8, /* green */
165 1, /* yellow */
166 6, /* blue */
167 4, /* magenta */
168 7, /* cyan */
169 12, /* light grey */
170
171 13, /* medium grey */
172 3, /* red */
173 8, /* green */
174 1, /* yellow */
175 6, /* blue */
176 4, /* magenta */
177 7, /* cyan */
178 0, /* white */
179 };
180 #endif
181
182 /* Generic functions */
183 static void rasops_copyrows(void *, int, int, int);
184 static int rasops_mapchar(void *, int, u_int *);
185 static void rasops_cursor(void *, int, int, int);
186 static int rasops_allocattr_color(void *, int, int, int, long *);
187 static int rasops_allocattr_mono(void *, int, int, int, long *);
188 static void rasops_do_cursor(struct rasops_info *);
189 static void rasops_init_devcmap(struct rasops_info *);
190
191 #if NRASOPS_ROTATION > 0
192 static void rasops_rotate_font(int *, int);
193 static void rasops_copychar(void *, int, int, int, int);
194
195 /* rotate clockwise */
196 static void rasops_copycols_rotated_cw(void *, int, int, int, int);
197 static void rasops_copyrows_rotated_cw(void *, int, int, int);
198 static void rasops_erasecols_rotated_cw(void *, int, int, int, long);
199 static void rasops_eraserows_rotated_cw(void *, int, int, long);
200 static void rasops_putchar_rotated_cw(void *, int, int, u_int, long);
201
202 /* rotate counter-clockwise */
203 static void rasops_copychar_ccw(void *, int, int, int, int);
204 static void rasops_copycols_rotated_ccw(void *, int, int, int, int);
205 static void rasops_copyrows_rotated_ccw(void *, int, int, int);
206 #define rasops_erasecols_rotated_ccw rasops_erasecols_rotated_cw
207 #define rasops_eraserows_rotated_ccw rasops_eraserows_rotated_cw
208 static void rasops_putchar_rotated_ccw(void *, int, int, u_int, long);
209
210 /*
211 * List of all rotated fonts
212 */
213 SLIST_HEAD(, rotatedfont) rotatedfonts = SLIST_HEAD_INITIALIZER(rotatedfonts);
214 struct rotatedfont {
215 SLIST_ENTRY(rotatedfont) rf_next;
216 int rf_cookie;
217 int rf_rotated;
218 };
219 #endif /* NRASOPS_ROTATION > 0 */
220
221 void rasops_make_box_chars_8(struct rasops_info *);
222 void rasops_make_box_chars_16(struct rasops_info *);
223 void rasops_make_box_chars_32(struct rasops_info *);
224 void rasops_make_box_chars_alpha(struct rasops_info *);
225
226 extern int cold;
227
228 /*
229 * Initialize a 'rasops_info' descriptor.
230 */
231 int
232 rasops_init(struct rasops_info *ri, int wantrows, int wantcols)
233 {
234
235 memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
236 #ifdef _KERNEL
237 /* Select a font if the caller doesn't care */
238 if (ri->ri_font == NULL) {
239 int cookie = -1;
240 int flags;
241
242 wsfont_init();
243
244 /*
245 * first, try to find something that's as close as possible
246 * to the caller's requested terminal size
247 */
248 if (wantrows == 0)
249 wantrows = RASOPS_DEFAULT_HEIGHT;
250 if (wantcols == 0)
251 wantcols = RASOPS_DEFAULT_WIDTH;
252
253 flags = WSFONT_FIND_BESTWIDTH | WSFONT_FIND_BITMAP;
254 if ((ri->ri_flg & RI_ENABLE_ALPHA) != 0)
255 flags |= WSFONT_FIND_ALPHA;
256 if ((ri->ri_flg & RI_PREFER_ALPHA) != 0)
257 flags |= WSFONT_PREFER_ALPHA;
258 cookie = wsfont_find(NULL,
259 ri->ri_width / wantcols,
260 0,
261 0,
262 WSDISPLAY_FONTORDER_L2R,
263 WSDISPLAY_FONTORDER_L2R,
264 flags);
265
266 /*
267 * this means there is no supported font in the list
268 */
269 if (cookie <= 0) {
270 aprint_error("%s: font table is empty\n", __func__);
271 return -1;
272 }
273
274 #if NRASOPS_ROTATION > 0
275 /*
276 * Pick the rotated version of this font. This will create it
277 * if necessary.
278 */
279 if (ri->ri_flg & RI_ROTATE_MASK) {
280 if (ri->ri_flg & RI_ROTATE_CW)
281 rasops_rotate_font(&cookie, WSFONT_ROTATE_CW);
282 else if (ri->ri_flg & RI_ROTATE_CCW)
283 rasops_rotate_font(&cookie, WSFONT_ROTATE_CCW);
284 }
285 #endif
286
287 if (wsfont_lock(cookie, &ri->ri_font)) {
288 aprint_error("%s: couldn't lock font\n", __func__);
289 return -1;
290 }
291
292 ri->ri_wsfcookie = cookie;
293 }
294 #endif
295
296 /* This should never happen in reality... */
297 if ((uintptr_t)ri->ri_bits & 3) {
298 aprint_error("%s: bits not aligned on 32-bit boundary\n",
299 __func__);
300 return -1;
301 }
302
303 if (ri->ri_stride & 3) {
304 aprint_error("%s: stride not aligned on 32-bit boundary\n",
305 __func__);
306 return -1;
307 }
308
309 if (rasops_reconfig(ri, wantrows, wantcols))
310 return -1;
311
312 rasops_init_devcmap(ri);
313 return 0;
314 }
315
316 /*
317 * Reconfigure (because parameters have changed in some way).
318 */
319 int
320 rasops_reconfig(struct rasops_info *ri, int wantrows, int wantcols)
321 {
322 int bpp, s;
323 size_t len;
324
325 s = splhigh();
326
327 if (wantrows == 0)
328 wantrows = RASOPS_DEFAULT_HEIGHT;
329 if (wantcols == 0)
330 wantcols = RASOPS_DEFAULT_WIDTH;
331
332 /* throw away old line drawing character bitmaps, if we have any */
333 if (ri->ri_optfont.data != NULL) {
334 kmem_free(ri->ri_optfont.data, ri->ri_optfont.stride *
335 ri->ri_optfont.fontheight * ri->ri_optfont.numchars);
336 ri->ri_optfont.data = NULL;
337 }
338
339 /* autogenerate box drawing characters */
340 ri->ri_optfont.firstchar = WSFONT_FLAG_OPT;
341 ri->ri_optfont.numchars = 16;
342 ri->ri_optfont.fontwidth = ri->ri_font->fontwidth;
343 ri->ri_optfont.fontheight = ri->ri_font->fontheight;
344 ri->ri_optfont.stride = ri->ri_font->stride;
345 len = ri->ri_optfont.fontheight * ri->ri_optfont.stride *
346 ri->ri_optfont.numchars;
347
348 if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4) {
349 aprint_error("%s: fontwidth assumptions botched", __func__);
350 splx(s);
351 return -1;
352 }
353
354 if ((ri->ri_flg & RI_NO_AUTO) == 0) {
355 ri->ri_optfont.data = kmem_zalloc(len, KM_SLEEP);
356 if (FONT_IS_ALPHA(&ri->ri_optfont))
357 rasops_make_box_chars_alpha(ri);
358 else {
359 switch (ri->ri_optfont.stride) {
360 case 1:
361 rasops_make_box_chars_8(ri);
362 break;
363 case 2:
364 rasops_make_box_chars_16(ri);
365 break;
366 case 4:
367 rasops_make_box_chars_32(ri);
368 break;
369 default:
370 aprint_error(
371 "%s: font stride assumptions botched",
372 __func__);
373 splx(s);
374 return -1;
375 }
376 }
377 } else
378 memset(&ri->ri_optfont, 0, sizeof(ri->ri_optfont));
379
380 /* Need this to frob the setup below */
381 bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
382
383 if ((ri->ri_flg & RI_CFGDONE) != 0) {
384 ri->ri_bits = ri->ri_origbits;
385 ri->ri_hwbits = ri->ri_hworigbits;
386 }
387
388 /* Don't care if the caller wants a hideously small console */
389 if (wantrows < 10)
390 wantrows = 10;
391
392 if (wantcols < 20)
393 wantcols = 20;
394
395 /* Now constrain what they get */
396 ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
397 ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
398
399 if (ri->ri_emuwidth > ri->ri_width)
400 ri->ri_emuwidth = ri->ri_width;
401
402 if (ri->ri_emuheight > ri->ri_height)
403 ri->ri_emuheight = ri->ri_height;
404
405 /* Reduce width until aligned on a 32-bit boundary */
406 while ((ri->ri_emuwidth * bpp & 31) != 0)
407 ri->ri_emuwidth--;
408
409 #if NRASOPS_ROTATION > 0
410 if (ri->ri_flg & (RI_ROTATE_CW|RI_ROTATE_CCW)) {
411 ri->ri_rows = ri->ri_emuwidth / ri->ri_font->fontwidth;
412 ri->ri_cols = ri->ri_emuheight / ri->ri_font->fontheight;
413 } else
414 #endif
415 {
416
417 ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
418 ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
419 }
420 ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
421 ri->ri_delta = ri->ri_stride - ri->ri_emustride;
422 ri->ri_ccol = 0;
423 ri->ri_crow = 0;
424 ri->ri_pelbytes = bpp >> 3;
425
426 ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
427 ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
428 ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
429
430 if ((ri->ri_delta & 3) != 0) {
431 aprint_error(
432 "%s: ri_delta not aligned on 32-bit boundary", __func__);
433 splx(s);
434 return -1;
435 }
436 ri->ri_origbits = ri->ri_bits;
437 ri->ri_hworigbits = ri->ri_hwbits;
438
439 /* Clear the entire display */
440 if ((ri->ri_flg & RI_CLEAR) != 0)
441 memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
442
443 /* Now centre our window if needs be */
444 if ((ri->ri_flg & RI_CENTER) != 0) {
445 ri->ri_bits += (((ri->ri_width * bpp >> 3) -
446 ri->ri_emustride) >> 1) & ~3;
447 ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
448 ri->ri_stride;
449 if (ri->ri_hwbits != NULL) {
450 ri->ri_hwbits += (((ri->ri_width * bpp >> 3) -
451 ri->ri_emustride) >> 1) & ~3;
452 ri->ri_hwbits +=
453 ((ri->ri_height - ri->ri_emuheight) >> 1) *
454 ri->ri_stride;
455 }
456 ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits) /
457 ri->ri_stride;
458 ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits) %
459 ri->ri_stride) * 8 / bpp);
460 } else
461 ri->ri_xorigin = ri->ri_yorigin = 0;
462
463 /*
464 * Fill in defaults for operations set. XXX this nukes private
465 * routines used by accelerated fb drivers.
466 */
467 ri->ri_ops.mapchar = rasops_mapchar;
468 ri->ri_ops.copyrows = rasops_copyrows;
469 ri->ri_ops.copycols = rasops_copycols;
470 ri->ri_ops.erasecols = rasops_erasecols;
471 ri->ri_ops.eraserows = rasops_eraserows;
472 ri->ri_ops.cursor = rasops_cursor;
473 ri->ri_do_cursor = rasops_do_cursor;
474
475 ri->ri_caps &= ~(WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
476 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE);
477
478 if ((ri->ri_flg & RI_FORCEMONO) != 0 ||
479 #ifdef RASOPS_APPLE_PALETTE
480 ri->ri_depth == 1
481 #else
482 ri->ri_depth < 8
483 #endif
484 ) {
485 ri->ri_ops.allocattr = rasops_allocattr_mono;
486 ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
487 } else {
488 ri->ri_ops.allocattr = rasops_allocattr_color;
489 ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
490 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
491 }
492
493 switch (ri->ri_depth) {
494 #if NRASOPS1 > 0
495 case 1:
496 rasops1_init(ri);
497 break;
498 #endif
499 #if NRASOPS2 > 0
500 case 2:
501 rasops2_init(ri);
502 break;
503 #endif
504 #if NRASOPS4 > 0
505 case 4:
506 rasops4_init(ri);
507 break;
508 #endif
509 #if NRASOPS8 > 0
510 case 8:
511 rasops8_init(ri);
512 break;
513 #endif
514 #if (NRASOPS15 + NRASOPS16) > 0
515 case 15:
516 case 16:
517 rasops15_init(ri);
518 break;
519 #endif
520 #if NRASOPS24 > 0
521 case 24:
522 rasops24_init(ri);
523 break;
524 #endif
525 #if NRASOPS32 > 0
526 case 32:
527 rasops32_init(ri);
528 break;
529 #endif
530 default:
531 ri->ri_flg &= ~RI_CFGDONE;
532 aprint_error("%s: depth not supported\n", __func__);
533 splx(s);
534 return -1;
535 }
536
537 #if NRASOPS_ROTATION > 0
538 if (ri->ri_flg & RI_ROTATE_MASK) {
539 if (ri->ri_flg & RI_ROTATE_CW) {
540 ri->ri_real_ops = ri->ri_ops;
541 ri->ri_ops.copycols = rasops_copycols_rotated_cw;
542 ri->ri_ops.copyrows = rasops_copyrows_rotated_cw;
543 ri->ri_ops.erasecols = rasops_erasecols_rotated_cw;
544 ri->ri_ops.eraserows = rasops_eraserows_rotated_cw;
545 ri->ri_ops.putchar = rasops_putchar_rotated_cw;
546 } else if (ri->ri_flg & RI_ROTATE_CCW) {
547 ri->ri_real_ops = ri->ri_ops;
548 ri->ri_ops.copycols = rasops_copycols_rotated_ccw;
549 ri->ri_ops.copyrows = rasops_copyrows_rotated_ccw;
550 ri->ri_ops.erasecols = rasops_erasecols_rotated_ccw;
551 ri->ri_ops.eraserows = rasops_eraserows_rotated_ccw;
552 ri->ri_ops.putchar = rasops_putchar_rotated_ccw;
553 }
554 }
555 #endif
556
557 ri->ri_flg |= RI_CFGDONE;
558 splx(s);
559 return 0;
560 }
561
562 /*
563 * Map a character.
564 */
565 static int
566 rasops_mapchar(void *cookie, int c, u_int *cp)
567 {
568 struct rasops_info *ri = (struct rasops_info *)cookie;
569
570 KASSERT(ri->ri_font != NULL);
571
572 if ((c = wsfont_map_unichar(ri->ri_font, c)) < 0 ||
573 c < ri->ri_font->firstchar) {
574 *cp = ' ';
575 return 0;
576 }
577
578 #if 0 /* XXXRO */
579 if (CHAR_IN_FONT(c, ri->ri_font)) {
580 *cp = ' ';
581 return 0;
582 }
583 #endif
584
585 *cp = c;
586 return 5;
587 }
588
589 /*
590 * Allocate a color attribute.
591 */
592 static int
593 rasops_allocattr_color(void *cookie, int fg0, int bg0, int flg, long *attr)
594 {
595 uint32_t fg = fg0, bg = bg0;
596
597 if (__predict_false(fg >= sizeof(rasops_isgray) ||
598 bg >= sizeof(rasops_isgray)))
599 return EINVAL;
600
601 #ifdef RASOPS_CLIPPING
602 fg &= 7;
603 bg &= 7;
604 #endif
605 if ((flg & WSATTR_BLINK) != 0)
606 return EINVAL;
607
608 if ((flg & WSATTR_WSCOLORS) == 0) {
609 #ifdef WS_DEFAULT_FG
610 fg = WS_DEFAULT_FG;
611 #else
612 fg = WSCOL_WHITE;
613 #endif
614 #ifdef WS_DEFAULT_BG
615 bg = WS_DEFAULT_BG;
616 #else
617 bg = WSCOL_BLACK;
618 #endif
619 }
620
621 if ((flg & WSATTR_REVERSE) != 0) {
622 uint32_t swap = fg;
623 fg = bg;
624 bg = swap;
625 }
626
627 if ((flg & WSATTR_HILIT) != 0)
628 fg += 8;
629
630 flg = flg & WSATTR_USERMASK;
631
632 if (rasops_isgray[fg])
633 flg |= WSATTR_PRIVATE1;
634
635 if (rasops_isgray[bg])
636 flg |= WSATTR_PRIVATE2;
637
638 *attr = (bg << 16) | (fg << 24) | flg;
639 return 0;
640 }
641
642 /*
643 * Allocate a mono attribute.
644 */
645 static int
646 rasops_allocattr_mono(void *cookie, int fg0, int bg0, int flg, long *attr)
647 {
648 uint32_t fg = fg0, bg = bg0;
649
650 if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
651 return EINVAL;
652
653 fg = 1;
654 bg = 0;
655
656 if ((flg & WSATTR_REVERSE) != 0) {
657 uint32_t swap = fg;
658 fg = bg;
659 bg = swap;
660 }
661
662 *attr = (bg << 16) | (fg << 24) | flg;
663 return 0;
664 }
665
666 /*
667 * Copy rows.
668 */
669 static void
670 rasops_copyrows(void *cookie, int src, int dst, int num)
671 {
672 struct rasops_info *ri = (struct rasops_info *)cookie;
673 uint8_t *sp, *dp, *hp;
674 int n;
675
676 hp = NULL; /* XXX GCC */
677
678 #ifdef RASOPS_CLIPPING
679 if (dst == src)
680 return;
681
682 if (src < 0) {
683 num += src;
684 src = 0;
685 }
686
687 if (src + num > ri->ri_rows)
688 num = ri->ri_rows - src;
689
690 if (dst < 0) {
691 num += dst;
692 dst = 0;
693 }
694
695 if (dst + num > ri->ri_rows)
696 num = ri->ri_rows - dst;
697
698 if (num <= 0)
699 return;
700 #endif
701
702 num *= ri->ri_font->fontheight;
703 n = ri->ri_emustride;
704
705 sp = ri->ri_bits + src * ri->ri_yscale;
706 dp = ri->ri_bits + dst * ri->ri_yscale;
707 if (ri->ri_hwbits)
708 hp = ri->ri_hwbits + dst * ri->ri_yscale;
709
710 while (num--) {
711 memmove(dp, sp, n);
712 dp += n;
713 if (ri->ri_hwbits) {
714 memcpy(hp, sp, n);
715 hp += n;
716 }
717 sp += n;
718 }
719 }
720
721 /*
722 * Copy columns. This is slow, and hard to optimize due to alignment,
723 * and the fact that we have to copy both left->right and right->left.
724 * We simply cop-out here and use memmove(), since it handles all of
725 * these cases anyway.
726 */
727 void
728 rasops_copycols(void *cookie, int row, int src, int dst, int num)
729 {
730 struct rasops_info *ri = (struct rasops_info *)cookie;
731 uint8_t *sp, *dp, *hp;
732 int height;
733
734 hp = NULL; /* XXX GCC */
735
736 #ifdef RASOPS_CLIPPING
737 if (dst == src)
738 return;
739
740 /* Catches < 0 case too */
741 if ((unsigned)row >= (unsigned)ri->ri_rows)
742 return;
743
744 if (src < 0) {
745 num += src;
746 src = 0;
747 }
748
749 if (src + num > ri->ri_cols)
750 num = ri->ri_cols - src;
751
752 if (dst < 0) {
753 num += dst;
754 dst = 0;
755 }
756
757 if (dst + num > ri->ri_cols)
758 num = ri->ri_cols - dst;
759
760 if (num <= 0)
761 return;
762 #endif
763
764 num *= ri->ri_xscale;
765 row *= ri->ri_yscale;
766 height = ri->ri_font->fontheight;
767
768 sp = ri->ri_bits + row + src * ri->ri_xscale;
769 dp = ri->ri_bits + row + dst * ri->ri_xscale;
770 if (ri->ri_hwbits)
771 hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
772
773 while (height--) {
774 memmove(dp, sp, num);
775 dp += ri->ri_stride;
776 if (ri->ri_hwbits) {
777 memcpy(hp, sp, num);
778 hp += ri->ri_stride;
779 }
780 sp += ri->ri_stride;
781 }
782 }
783
784 /*
785 * Turn cursor off/on.
786 */
787 static void
788 rasops_cursor(void *cookie, int on, int row, int col)
789 {
790 struct rasops_info *ri = (struct rasops_info *)cookie;
791
792 /* Turn old cursor off */
793 if ((ri->ri_flg & RI_CURSOR) != 0)
794 #ifdef RASOPS_CLIPPING
795 if ((ri->ri_flg & RI_CURSORCLIP) == 0)
796 #endif
797 ri->ri_do_cursor(ri);
798
799 /* Select new cursor */
800 #ifdef RASOPS_CLIPPING
801 ri->ri_flg &= ~RI_CURSORCLIP;
802
803 if (row < 0 || row >= ri->ri_rows)
804 ri->ri_flg |= RI_CURSORCLIP;
805 else if (col < 0 || col >= ri->ri_cols)
806 ri->ri_flg |= RI_CURSORCLIP;
807 #endif
808 ri->ri_crow = row;
809 ri->ri_ccol = col;
810
811 if (on) {
812 ri->ri_flg |= RI_CURSOR;
813 #ifdef RASOPS_CLIPPING
814 if ((ri->ri_flg & RI_CURSORCLIP) == 0)
815 #endif
816 ri->ri_do_cursor(ri);
817 } else
818 ri->ri_flg &= ~RI_CURSOR;
819 }
820
821 /*
822 * Make the device colormap
823 */
824 static void
825 rasops_init_devcmap(struct rasops_info *ri)
826 {
827 int i;
828 uint32_t c;
829 const uint8_t *p;
830
831 switch (ri->ri_depth) {
832 case 1:
833 ri->ri_devcmap[0] = 0;
834 for (i = 1; i < 16; i++)
835 ri->ri_devcmap[i] = -1;
836 return;
837
838 case 2:
839 for (i = 1; i < 15; i++)
840 ri->ri_devcmap[i] = 0xaaaaaaaa;
841
842 ri->ri_devcmap[0] = 0;
843 ri->ri_devcmap[8] = 0x55555555;
844 ri->ri_devcmap[15] = -1;
845 return;
846
847 #ifdef RASOPS_APPLE_PALETTE
848 case 4:
849 for (i = 0; i < 16; i++) {
850 c = apple4_devcmap[i];
851 ri->ri_devcmap[i] =
852 (c << 0) | (c << 4) | (c << 8) | (c << 12) |
853 (c << 16) | (c << 20) | (c << 24) | (c << 28);
854 }
855 return;
856 #else
857 /* XXXRO What should we do here? */
858 #endif
859
860 case 8:
861 if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
862 for (i = 0; i < 16; i++) {
863 #ifdef RASOPS_APPLE_PALETTE
864 c = apple8_devcmap[i];
865 #else
866 c = i;
867 #endif
868 ri->ri_devcmap[i] =
869 c | (c << 8) | (c << 16) | (c << 24);
870 }
871 return;
872 }
873 }
874
875 p = rasops_cmap;
876
877 for (i = 0; i < 16; i++) {
878 if (ri->ri_rnum <= 8)
879 c = (uint32_t)(*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
880 else
881 c = (uint32_t)(*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
882 p++;
883
884 if (ri->ri_gnum <= 8)
885 c |= (uint32_t)(*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
886 else
887 c |= (uint32_t)(*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
888 p++;
889
890 if (ri->ri_bnum <= 8)
891 c |= (uint32_t)(*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
892 else
893 c |= (uint32_t)(*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
894 p++;
895
896 /* Fill the word for generic routines, which want this */
897 if (ri->ri_depth == 8) {
898 c |= c << 8;
899 c |= c << 16;
900 } else if (ri->ri_depth == 15 || ri->ri_depth == 16)
901 c |= c << 16;
902 else if (ri->ri_depth == 24) {
903 #if BYTE_ORDER == LITTLE_ENDIAN
904 c = (c & 0x0000ff) << 16 | (c & 0x00ff00) |
905 (c & 0xff0000) >> 16;
906 #endif
907 c |= (c & 0xff) << 24;
908 }
909
910 /* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
911 if ((ri->ri_flg & RI_BSWAP) == 0)
912 ri->ri_devcmap[i] = c;
913 else if (ri->ri_depth == 15 || ri->ri_depth == 16)
914 ri->ri_devcmap[i] = bswap16(c);
915 else if (ri->ri_depth == 32)
916 ri->ri_devcmap[i] = bswap32(c);
917 else /* 8, 24 */
918 ri->ri_devcmap[i] = c;
919 }
920 }
921
922 /*
923 * Unpack a rasops attribute
924 */
925 void
926 rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
927 {
928
929 *fg = ((uint32_t)attr >> 24) & 0xf;
930 *bg = ((uint32_t)attr >> 16) & 0xf;
931 if (underline != NULL)
932 *underline = (uint32_t)attr & WSATTR_UNDERLINE;
933 }
934
935 /*
936 * Erase rows. This isn't static, since 24-bpp uses it in special cases.
937 */
938 void
939 rasops_eraserows(void *cookie, int row, int num, long attr)
940 {
941 struct rasops_info *ri = (struct rasops_info *)cookie;
942 uint32_t *dp, *hp, clr;
943 int n, cnt, delta;
944
945 hp = NULL; /* XXX GCC */
946
947 #ifdef RASOPS_CLIPPING
948 if (row < 0) {
949 num += row;
950 row = 0;
951 }
952
953 if (row + num > ri->ri_rows)
954 num = ri->ri_rows - row;
955
956 if (num <= 0)
957 return;
958 #endif
959
960 clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
961
962 /*
963 * XXX The wsdisplay_emulops interface seems a little deficient in
964 * that there is no way to clear the *entire* screen. We provide a
965 * workaround here: if the entire console area is being cleared, and
966 * the RI_FULLCLEAR flag is set, clear the entire display.
967 */
968 if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
969 n = ri->ri_stride >> 2;
970 num = ri->ri_height;
971 dp = (uint32_t *)ri->ri_origbits;
972 if (ri->ri_hwbits)
973 hp = (uint32_t *)ri->ri_hworigbits;
974 delta = 0;
975 } else {
976 n = ri->ri_emustride >> 2;
977 num *= ri->ri_font->fontheight;
978 dp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
979 if (ri->ri_hwbits)
980 hp = (uint32_t *)(ri->ri_hwbits + row *
981 ri->ri_yscale);
982 delta = ri->ri_delta;
983 }
984
985 while (num--) {
986 for (cnt = n; cnt; cnt--) {
987 *dp++ = clr;
988 if (ri->ri_hwbits)
989 *hp++ = clr;
990 }
991 DELTA(dp, delta, uint32_t *);
992 if (ri->ri_hwbits)
993 DELTA(hp, delta, uint32_t *);
994 }
995 }
996
997 /*
998 * Actually turn the cursor on or off. This does the dirty work for
999 * rasops_cursor().
1000 */
1001 static void
1002 rasops_do_cursor(struct rasops_info *ri)
1003 {
1004 int full, height, cnt, slop1, slop2, row, col;
1005 uint32_t tmp32, msk1, msk2;
1006 uint8_t tmp8;
1007 uint8_t *dp, *rp, *hrp, *hp;
1008
1009 hrp = hp = NULL; /* XXX GCC */
1010
1011 #if NRASOPS_ROTATION > 0
1012 if (ri->ri_flg & RI_ROTATE_MASK) {
1013 if (ri->ri_flg & RI_ROTATE_CW) {
1014 /* Rotate rows/columns */
1015 row = ri->ri_ccol;
1016 col = ri->ri_rows - ri->ri_crow - 1;
1017 } else if (ri->ri_flg & RI_ROTATE_CCW) {
1018 /* Rotate rows/columns */
1019 row = ri->ri_cols - ri->ri_ccol - 1;
1020 col = ri->ri_crow;
1021 } else { /* upside-down */
1022 row = ri->ri_crow;
1023 col = ri->ri_ccol;
1024 }
1025 } else
1026 #endif
1027 {
1028 row = ri->ri_crow;
1029 col = ri->ri_ccol;
1030 }
1031
1032 rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1033 if (ri->ri_hwbits)
1034 hrp = ri->ri_hwbits + row * ri->ri_yscale + col
1035 * ri->ri_xscale;
1036 height = ri->ri_font->fontheight;
1037
1038 /*
1039 * For ri_xscale = 1:
1040 *
1041 * Logic below does not work for ri_xscale = 1, e.g.,
1042 * fontwidth = 8 and bpp = 1. So we take care of it.
1043 */
1044 if (ri->ri_xscale == 1) {
1045 while (height--) {
1046 tmp8 = ~*rp;
1047
1048 *rp = tmp8;
1049 rp += ri->ri_stride;
1050
1051 if (ri->ri_hwbits) {
1052 *hrp = tmp8;
1053 hrp += ri->ri_stride;
1054 }
1055 }
1056 return;
1057 }
1058
1059 /*
1060 * For ri_xscale = 2, 3, 4, ...:
1061 *
1062 * Note that siop1 <= ri_xscale even for ri_xscale = 2,
1063 * since rp % 3 = 0 or 2 (ri_stride % 4 = 0).
1064 */
1065 slop1 = (4 - ((uintptr_t)rp & 3)) & 3;
1066 slop2 = (ri->ri_xscale - slop1) & 3;
1067 full = (ri->ri_xscale - slop1 /* - slop2 */) >> 2;
1068
1069 rp = (uint8_t *)((uintptr_t)rp & ~3);
1070 hrp = (uint8_t *)((uintptr_t)hrp & ~3);
1071
1072 msk1 = !slop1 ? 0 : be32toh(0xffffffffU >> (32 - (8 * slop1)));
1073 msk2 = !slop2 ? 0 : be32toh(0xffffffffU << (32 - (8 * slop2)));
1074
1075 while (height--) {
1076 dp = rp;
1077 rp += ri->ri_stride;
1078 if (ri->ri_hwbits) {
1079 hp = hrp;
1080 hrp += ri->ri_stride;
1081 }
1082
1083 if (slop1) {
1084 tmp32 = *(uint32_t *)dp ^ msk1;
1085 *(uint32_t *)dp = tmp32;
1086 dp += 4;
1087 if (ri->ri_hwbits) {
1088 *(uint32_t *)hp = tmp32;
1089 hp += 4;
1090 }
1091 }
1092
1093 for (cnt = full; cnt; cnt--) {
1094 tmp32 = ~*(uint32_t *)dp;
1095 *(uint32_t *)dp = tmp32;
1096 dp += 4;
1097 if (ri->ri_hwbits) {
1098 *(uint32_t *)hp = tmp32;
1099 hp += 4;
1100 }
1101 }
1102
1103 if (slop2) {
1104 tmp32 = *(uint32_t *)dp ^ msk2;
1105 *(uint32_t *)dp = tmp32;
1106 if (ri->ri_hwbits)
1107 *(uint32_t *)hp = tmp32;
1108 }
1109 }
1110 }
1111
1112 /*
1113 * Erase columns.
1114 */
1115 void
1116 rasops_erasecols(void *cookie, int row, int col, int num, long attr)
1117 {
1118 struct rasops_info *ri = (struct rasops_info *)cookie;
1119 int height, cnt, slop1, slop2, clr;
1120 uint32_t *rp, *dp, *hrp, *hp;
1121
1122 hrp = hp = NULL; /* XXX GCC */
1123
1124 #ifdef RASOPS_CLIPPING
1125 if ((unsigned)row >= (unsigned)ri->ri_rows)
1126 return;
1127
1128 if (col < 0) {
1129 num += col;
1130 col = 0;
1131 }
1132
1133 if ((col + num) > ri->ri_cols)
1134 num = ri->ri_cols - col;
1135
1136 if (num <= 0)
1137 return;
1138 #endif
1139
1140 num *= ri->ri_xscale;
1141 rp = (uint32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
1142 if (ri->ri_hwbits)
1143 hrp = (uint32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
1144 col*ri->ri_xscale);
1145 height = ri->ri_font->fontheight;
1146 clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
1147
1148 /* Don't bother using the full loop for <= 32 pels */
1149 if (num <= 32) {
1150 if (((num | ri->ri_xscale) & 3) == 0) {
1151 /* Word aligned blt */
1152 num >>= 2;
1153
1154 while (height--) {
1155 dp = rp;
1156 DELTA(rp, ri->ri_stride, uint32_t *);
1157 if (ri->ri_hwbits) {
1158 hp = hrp;
1159 DELTA(hrp, ri->ri_stride, uint32_t *);
1160 }
1161
1162 for (cnt = num; cnt; cnt--) {
1163 *dp++ = clr;
1164 if (ri->ri_hwbits)
1165 *hp++ = clr;
1166 }
1167 }
1168 } else if (((num | ri->ri_xscale) & 1) == 0) {
1169 /*
1170 * Halfword aligned blt. This is needed so the
1171 * 15/16 bit ops can use this function.
1172 */
1173 num >>= 1;
1174
1175 while (height--) {
1176 dp = rp;
1177 DELTA(rp, ri->ri_stride, uint32_t *);
1178 if (ri->ri_hwbits) {
1179 hp = hrp;
1180 DELTA(hrp, ri->ri_stride, uint32_t *);
1181 }
1182
1183 for (cnt = num; cnt; cnt--) {
1184 *(uint16_t *)dp = clr;
1185 DELTA(dp, 2, uint32_t *);
1186 if (ri->ri_hwbits) {
1187 *(uint16_t *)hp = clr;
1188 DELTA(hp, 2, uint32_t *);
1189 }
1190 }
1191 }
1192 } else {
1193 while (height--) {
1194 dp = rp;
1195 DELTA(rp, ri->ri_stride, uint32_t *);
1196 if (ri->ri_hwbits) {
1197 hp = hrp;
1198 DELTA(hrp, ri->ri_stride, uint32_t *);
1199 }
1200
1201 for (cnt = num; cnt; cnt--) {
1202 *(uint8_t *)dp = clr;
1203 DELTA(dp, 1, uint32_t *);
1204 if (ri->ri_hwbits) {
1205 *(uint8_t *)hp = clr;
1206 DELTA(hp, 1, uint32_t *);
1207 }
1208 }
1209 }
1210 }
1211
1212 return;
1213 }
1214
1215 slop1 = (4 - ((uintptr_t)rp & 3)) & 3;
1216 slop2 = (num - slop1) & 3;
1217 num = (num - slop1 /* - slop2 */) >> 2;
1218
1219 while (height--) {
1220 dp = rp;
1221 DELTA(rp, ri->ri_stride, uint32_t *);
1222 if (ri->ri_hwbits) {
1223 hp = hrp;
1224 DELTA(hrp, ri->ri_stride, uint32_t *);
1225 }
1226
1227 /* Align span to 4 bytes */
1228 if (slop1 & 1) {
1229 *(uint8_t *)dp = clr;
1230 DELTA(dp, 1, uint32_t *);
1231 if (ri->ri_hwbits) {
1232 *(uint8_t *)hp = clr;
1233 DELTA(hp, 1, uint32_t *);
1234 }
1235 }
1236
1237 if (slop1 & 2) {
1238 *(uint16_t *)dp = clr;
1239 DELTA(dp, 2, uint32_t *);
1240 if (ri->ri_hwbits) {
1241 *(uint16_t *)hp = clr;
1242 DELTA(hp, 2, uint32_t *);
1243 }
1244 }
1245
1246 /* Write 4 bytes per loop */
1247 for (cnt = num; cnt; cnt--) {
1248 *dp++ = clr;
1249 if (ri->ri_hwbits)
1250 *hp++ = clr;
1251 }
1252
1253 /* Write unaligned trailing slop */
1254 if (slop2 & 1) {
1255 *(uint8_t *)dp = clr;
1256 DELTA(dp, 1, uint32_t *);
1257 if (ri->ri_hwbits) {
1258 *(uint8_t *)hp = clr;
1259 DELTA(hp, 1, uint32_t *);
1260 }
1261 }
1262
1263 if (slop2 & 2) {
1264 *(uint16_t *)dp = clr;
1265 if (ri->ri_hwbits)
1266 *(uint16_t *)hp = clr;
1267 }
1268 }
1269 }
1270
1271 #if NRASOPS_ROTATION > 0
1272 /*
1273 * Quarter clockwise rotation routines (originally intended for the
1274 * built-in Zaurus C3x00 display in 16bpp).
1275 */
1276
1277 static void
1278 rasops_rotate_font(int *cookie, int rotate)
1279 {
1280 struct rotatedfont *f;
1281 int ncookie;
1282
1283 SLIST_FOREACH(f, &rotatedfonts, rf_next) {
1284 if (f->rf_cookie == *cookie) {
1285 *cookie = f->rf_rotated;
1286 return;
1287 }
1288 }
1289
1290 /*
1291 * We did not find a rotated version of this font. Ask the wsfont
1292 * code to compute one for us.
1293 */
1294
1295 f = kmem_alloc(sizeof(*f), KM_SLEEP);
1296
1297 if ((ncookie = wsfont_rotate(*cookie, rotate)) == -1)
1298 goto fail;
1299
1300 f->rf_cookie = *cookie;
1301 f->rf_rotated = ncookie;
1302 SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next);
1303
1304 *cookie = ncookie;
1305 return;
1306
1307 fail: kmem_free(f, sizeof(*f));
1308 return;
1309 }
1310
1311 static void
1312 rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
1313 {
1314 struct rasops_info *ri = (struct rasops_info *)cookie;
1315 int height;
1316 int r_srcrow, r_dstrow, r_srccol, r_dstcol;
1317 uint8_t *sp, *dp;
1318
1319 r_srcrow = srccol;
1320 r_dstrow = dstcol;
1321 r_srccol = ri->ri_rows - srcrow - 1;
1322 r_dstcol = ri->ri_rows - dstrow - 1;
1323
1324 r_srcrow *= ri->ri_yscale;
1325 r_dstrow *= ri->ri_yscale;
1326 height = ri->ri_font->fontheight;
1327
1328 sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
1329 dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
1330
1331 while (height--) {
1332 memmove(dp, sp, ri->ri_xscale);
1333 dp += ri->ri_stride;
1334 sp += ri->ri_stride;
1335 }
1336 }
1337
1338 static void
1339 rasops_putchar_rotated_cw(void *cookie, int row, int col, u_int uc, long attr)
1340 {
1341 struct rasops_info *ri = (struct rasops_info *)cookie;
1342 int height;
1343 uint8_t *rp;
1344
1345 if (__predict_false((unsigned int)row > ri->ri_rows ||
1346 (unsigned int)col > ri->ri_cols))
1347 return;
1348
1349 /* Avoid underflow */
1350 if (ri->ri_rows - row - 1 < 0)
1351 return;
1352
1353 /* Do rotated char sans (side)underline */
1354 ri->ri_real_ops.putchar(cookie, col, ri->ri_rows - row - 1, uc,
1355 attr & ~WSATTR_UNDERLINE);
1356
1357 /* Do rotated underline */
1358 rp = ri->ri_bits + col * ri->ri_yscale + (ri->ri_rows - row - 1) *
1359 ri->ri_xscale;
1360 height = ri->ri_font->fontheight;
1361
1362 /* XXX this assumes 16-bit color depth */
1363 if ((attr & WSATTR_UNDERLINE) != 0) {
1364 uint16_t c =
1365 (uint16_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
1366
1367 while (height--) {
1368 *(uint16_t *)rp = c;
1369 rp += ri->ri_stride;
1370 }
1371 }
1372 }
1373
1374 static void
1375 rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
1376 {
1377 struct rasops_info *ri = (struct rasops_info *)cookie;
1378 int i;
1379
1380 for (i = col; i < col + num; i++)
1381 ri->ri_ops.putchar(cookie, row, i, ' ', attr);
1382 }
1383
1384 /* XXX: these could likely be optimised somewhat. */
1385 static void
1386 rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
1387 {
1388 struct rasops_info *ri = (struct rasops_info *)cookie;
1389 int col, roff;
1390
1391 if (src > dst)
1392 for (roff = 0; roff < num; roff++)
1393 for (col = 0; col < ri->ri_cols; col++)
1394 rasops_copychar(cookie, src + roff, dst + roff,
1395 col, col);
1396 else
1397 for (roff = num - 1; roff >= 0; roff--)
1398 for (col = 0; col < ri->ri_cols; col++)
1399 rasops_copychar(cookie, src + roff, dst + roff,
1400 col, col);
1401 }
1402
1403 static void
1404 rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
1405 {
1406 int coff;
1407
1408 if (src > dst)
1409 for (coff = 0; coff < num; coff++)
1410 rasops_copychar(cookie, row, row, src + coff,
1411 dst + coff);
1412 else
1413 for (coff = num - 1; coff >= 0; coff--)
1414 rasops_copychar(cookie, row, row, src + coff,
1415 dst + coff);
1416 }
1417
1418 static void
1419 rasops_eraserows_rotated_cw(void *cookie, int row, int num, long attr)
1420 {
1421 struct rasops_info *ri = (struct rasops_info *)cookie;
1422 int col, rn;
1423
1424 for (rn = row; rn < row + num; rn++)
1425 for (col = 0; col < ri->ri_cols; col++)
1426 ri->ri_ops.putchar(cookie, rn, col, ' ', attr);
1427 }
1428
1429 /*
1430 * Quarter counter-clockwise rotation routines (originally intended for the
1431 * built-in Sharp W-ZERO3 display in 16bpp).
1432 */
1433 static void
1434 rasops_copychar_ccw(void *cookie, int srcrow, int dstrow, int srccol,
1435 int dstcol)
1436 {
1437 struct rasops_info *ri = (struct rasops_info *)cookie;
1438 int height, r_srcrow, r_dstrow, r_srccol, r_dstcol;
1439 uint8_t *sp, *dp;
1440
1441 r_srcrow = ri->ri_cols - srccol - 1;
1442 r_dstrow = ri->ri_cols - dstcol - 1;
1443 r_srccol = srcrow;
1444 r_dstcol = dstrow;
1445
1446 r_srcrow *= ri->ri_yscale;
1447 r_dstrow *= ri->ri_yscale;
1448 height = ri->ri_font->fontheight;
1449
1450 sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
1451 dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
1452
1453 while (height--) {
1454 memmove(dp, sp, ri->ri_xscale);
1455 dp += ri->ri_stride;
1456 sp += ri->ri_stride;
1457 }
1458 }
1459
1460 static void
1461 rasops_putchar_rotated_ccw(void *cookie, int row, int col, u_int uc, long attr)
1462 {
1463 struct rasops_info *ri = (struct rasops_info *)cookie;
1464 int height;
1465 uint8_t *rp;
1466
1467 if (__predict_false((unsigned int)row > ri->ri_rows ||
1468 (unsigned int)col > ri->ri_cols))
1469 return;
1470
1471 /* Avoid underflow */
1472 if (ri->ri_cols - col - 1 < 0)
1473 return;
1474
1475 /* Do rotated char sans (side)underline */
1476 ri->ri_real_ops.putchar(cookie, ri->ri_cols - col - 1, row, uc,
1477 attr & ~WSATTR_UNDERLINE);
1478
1479 /* Do rotated underline */
1480 rp = ri->ri_bits + (ri->ri_cols - col - 1) * ri->ri_yscale +
1481 row * ri->ri_xscale +
1482 (ri->ri_font->fontwidth - 1) * ri->ri_pelbytes;
1483 height = ri->ri_font->fontheight;
1484
1485 /* XXX this assumes 16-bit color depth */
1486 if ((attr & WSATTR_UNDERLINE) != 0) {
1487 uint16_t c =
1488 (uint16_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
1489
1490 while (height--) {
1491 *(uint16_t *)rp = c;
1492 rp += ri->ri_stride;
1493 }
1494 }
1495 }
1496
1497 /* XXX: these could likely be optimised somewhat. */
1498 static void
1499 rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
1500 {
1501 struct rasops_info *ri = (struct rasops_info *)cookie;
1502 int col, roff;
1503
1504 if (src > dst)
1505 for (roff = 0; roff < num; roff++)
1506 for (col = 0; col < ri->ri_cols; col++)
1507 rasops_copychar_ccw(cookie,
1508 src + roff, dst + roff, col, col);
1509 else
1510 for (roff = num - 1; roff >= 0; roff--)
1511 for (col = 0; col < ri->ri_cols; col++)
1512 rasops_copychar_ccw(cookie,
1513 src + roff, dst + roff, col, col);
1514 }
1515
1516 static void
1517 rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
1518 {
1519 int coff;
1520
1521 if (src > dst)
1522 for (coff = 0; coff < num; coff++)
1523 rasops_copychar_ccw(cookie, row, row,
1524 src + coff, dst + coff);
1525 else
1526 for (coff = num - 1; coff >= 0; coff--)
1527 rasops_copychar_ccw(cookie, row, row,
1528 src + coff, dst + coff);
1529 }
1530 #endif /* NRASOPS_ROTATION */
1531
1532 void
1533 rasops_make_box_chars_16(struct rasops_info *ri)
1534 {
1535 int c, i, mid;
1536 uint16_t vert_mask, hmask_left, hmask_right;
1537 uint16_t *data = (uint16_t *)ri->ri_optfont.data;
1538
1539 vert_mask = 0xc000U >> ((ri->ri_font->fontwidth >> 1) - 1);
1540 hmask_left = 0xff00U << (8 - (ri->ri_font->fontwidth >> 1));
1541 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
1542 mid = (ri->ri_font->fontheight + 1) >> 1;
1543
1544 /* 0x00 would be empty anyway so don't bother */
1545 for (c = 1; c < 16; c++) {
1546 data += ri->ri_font->fontheight;
1547 if (c & 1) {
1548 /* upper segment */
1549 for (i = 0; i < mid; i++)
1550 data[i] = vert_mask;
1551 }
1552 if (c & 4) {
1553 /* lower segment */
1554 for (i = mid; i < ri->ri_font->fontheight; i++)
1555 data[i] = vert_mask;
1556 }
1557 if (c & 2) {
1558 /* right segment */
1559 i = ri->ri_font->fontheight >> 1;
1560 data[mid - 1] |= hmask_right;
1561 data[mid] |= hmask_right;
1562 }
1563 if (c & 8) {
1564 /* left segment */
1565 data[mid - 1] |= hmask_left;
1566 data[mid] |= hmask_left;
1567 }
1568 }
1569 }
1570
1571 void
1572 rasops_make_box_chars_8(struct rasops_info *ri)
1573 {
1574 int c, i, mid;
1575 uint8_t vert_mask, hmask_left, hmask_right;
1576 uint8_t *data = (uint8_t *)ri->ri_optfont.data;
1577
1578 vert_mask = 0xc0U >> ((ri->ri_font->fontwidth >> 1) - 1);
1579 hmask_left = 0xf0U << (4 - (ri->ri_font->fontwidth >> 1));
1580 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
1581 mid = (ri->ri_font->fontheight + 1) >> 1;
1582
1583 /* 0x00 would be empty anyway so don't bother */
1584 for (c = 1; c < 16; c++) {
1585 data += ri->ri_font->fontheight;
1586 if (c & 1) {
1587 /* upper segment */
1588 for (i = 0; i < mid; i++)
1589 data[i] = vert_mask;
1590 }
1591 if (c & 4) {
1592 /* lower segment */
1593 for (i = mid; i < ri->ri_font->fontheight; i++)
1594 data[i] = vert_mask;
1595 }
1596 if (c & 2) {
1597 /* right segment */
1598 i = ri->ri_font->fontheight >> 1;
1599 data[mid - 1] |= hmask_right;
1600 data[mid] |= hmask_right;
1601 }
1602 if (c & 8) {
1603 /* left segment */
1604 data[mid - 1] |= hmask_left;
1605 data[mid] |= hmask_left;
1606 }
1607 }
1608 }
1609
1610 void
1611 rasops_make_box_chars_32(struct rasops_info *ri)
1612 {
1613 int c, i, mid;
1614 uint32_t vert_mask, hmask_left, hmask_right;
1615 uint32_t *data = (uint32_t *)ri->ri_optfont.data;
1616
1617 vert_mask = 0xc0000000U >> ((ri->ri_font->fontwidth >> 1) - 1);
1618 hmask_left = 0xffff0000U << (16 - (ri->ri_font->fontwidth >> 1));
1619 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
1620 mid = (ri->ri_font->fontheight + 1) >> 1;
1621
1622 /* 0x00 would be empty anyway so don't bother */
1623 for (c = 1; c < 16; c++) {
1624 data += ri->ri_font->fontheight;
1625 if (c & 1) {
1626 /* upper segment */
1627 for (i = 0; i < mid; i++)
1628 data[i] = vert_mask;
1629 }
1630 if (c & 4) {
1631 /* lower segment */
1632 for (i = mid; i < ri->ri_font->fontheight; i++)
1633 data[i] = vert_mask;
1634 }
1635 if (c & 2) {
1636 /* right segment */
1637 i = ri->ri_font->fontheight >> 1;
1638 data[mid - 1] |= hmask_right;
1639 data[mid] |= hmask_right;
1640 }
1641 if (c & 8) {
1642 /* left segment */
1643 data[mid - 1] |= hmask_left;
1644 data[mid] |= hmask_left;
1645 }
1646 }
1647 }
1648
1649 void
1650 rasops_make_box_chars_alpha(struct rasops_info *ri)
1651 {
1652 int c, i, hmid, vmid, wi, he;
1653 uint8_t *data = (uint8_t *)ri->ri_optfont.data;
1654 uint8_t *ddata;
1655
1656 he = ri->ri_font->fontheight;
1657 wi = ri->ri_font->fontwidth;
1658
1659 vmid = (he + 1) >> 1;
1660 hmid = (wi + 1) >> 1;
1661
1662 /* 0x00 would be empty anyway so don't bother */
1663 for (c = 1; c < 16; c++) {
1664 data += ri->ri_fontscale;
1665 if (c & 1) {
1666 /* upper segment */
1667 ddata = data + hmid;
1668 for (i = 0; i <= vmid; i++) {
1669 *ddata = 0xff;
1670 ddata += wi;
1671 }
1672 }
1673 if (c & 4) {
1674 /* lower segment */
1675 ddata = data + wi * vmid + hmid;
1676 for (i = vmid; i < he; i++) {
1677 *ddata = 0xff;
1678 ddata += wi;
1679 }
1680 }
1681 if (c & 2) {
1682 /* right segment */
1683 ddata = data + wi * vmid + hmid;
1684 for (i = hmid; i < wi; i++) {
1685 *ddata = 0xff;
1686 ddata++;
1687 }
1688 }
1689 if (c & 8) {
1690 /* left segment */
1691 ddata = data + wi * vmid;
1692 for (i = 0; i <= hmid; i++) {
1693 *ddata = 0xff;
1694 ddata++;
1695 }
1696 }
1697 }
1698 }
1699
1700 /*
1701 * Return a colour map appropriate for the given struct rasops_info in the
1702 * same form used by rasops_cmap[]
1703 * For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
1704 * probably be a linear ( or gamma corrected? ) ramp for higher depths.
1705 */
1706
1707 int
1708 rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
1709 {
1710
1711 if ((ri->ri_depth == 8) && ((ri->ri_flg & RI_8BIT_IS_RGB) != 0)) {
1712 /* generate an R3G3B2 palette */
1713 int i, idx = 0;
1714 uint8_t tmp;
1715
1716 if (bytes < 768)
1717 return EINVAL;
1718 for (i = 0; i < 256; i++) {
1719 tmp = i & 0xe0;
1720 /*
1721 * replicate bits so 0xe0 maps to a red value of 0xff
1722 * in order to make white look actually white
1723 */
1724 tmp |= (tmp >> 3) | (tmp >> 6);
1725 palette[idx] = tmp;
1726 idx++;
1727
1728 tmp = (i & 0x1c) << 3;
1729 tmp |= (tmp >> 3) | (tmp >> 6);
1730 palette[idx] = tmp;
1731 idx++;
1732
1733 tmp = (i & 0x03) << 6;
1734 tmp |= tmp >> 2;
1735 tmp |= tmp >> 4;
1736 palette[idx] = tmp;
1737 idx++;
1738 }
1739 } else
1740 memcpy(palette, rasops_cmap, uimin(bytes, sizeof(rasops_cmap)));
1741 return 0;
1742 }
1743