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