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