rasops.c revision 1.75 1 /* $NetBSD: rasops.c,v 1.75 2017/04/22 15:05:02 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.75 2017/04/22 15:05:02 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 if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) {
419 ri->ri_ops.allocattr = rasops_allocattr_mono;
420 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
421 } else {
422 ri->ri_ops.allocattr = rasops_allocattr_color;
423 ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
424 WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
425 }
426
427 switch (ri->ri_depth) {
428 #if NRASOPS1 > 0
429 case 1:
430 rasops1_init(ri);
431 break;
432 #endif
433 #if NRASOPS2 > 0
434 case 2:
435 rasops2_init(ri);
436 break;
437 #endif
438 #if NRASOPS4 > 0
439 case 4:
440 rasops4_init(ri);
441 break;
442 #endif
443 #if NRASOPS8 > 0
444 case 8:
445 rasops8_init(ri);
446 break;
447 #endif
448 #if NRASOPS15 > 0 || NRASOPS16 > 0
449 case 15:
450 case 16:
451 rasops15_init(ri);
452 break;
453 #endif
454 #if NRASOPS24 > 0
455 case 24:
456 rasops24_init(ri);
457 break;
458 #endif
459 #if NRASOPS32 > 0
460 case 32:
461 rasops32_init(ri);
462 break;
463 #endif
464 default:
465 ri->ri_flg &= ~RI_CFGDONE;
466 splx(s);
467 return (-1);
468 }
469
470 #if NRASOPS_ROTATION > 0
471 if (ri->ri_flg & RI_ROTATE_MASK) {
472 if (ri->ri_flg & RI_ROTATE_CW) {
473 ri->ri_real_ops = ri->ri_ops;
474 ri->ri_ops.copycols = rasops_copycols_rotated_cw;
475 ri->ri_ops.copyrows = rasops_copyrows_rotated_cw;
476 ri->ri_ops.erasecols = rasops_erasecols_rotated_cw;
477 ri->ri_ops.eraserows = rasops_eraserows_rotated_cw;
478 ri->ri_ops.putchar = rasops_putchar_rotated_cw;
479 } else if (ri->ri_flg & RI_ROTATE_CCW) {
480 ri->ri_real_ops = ri->ri_ops;
481 ri->ri_ops.copycols = rasops_copycols_rotated_ccw;
482 ri->ri_ops.copyrows = rasops_copyrows_rotated_ccw;
483 ri->ri_ops.erasecols = rasops_erasecols_rotated_ccw;
484 ri->ri_ops.eraserows = rasops_eraserows_rotated_ccw;
485 ri->ri_ops.putchar = rasops_putchar_rotated_ccw;
486 }
487 }
488 #endif
489
490 ri->ri_flg |= RI_CFGDONE;
491 splx(s);
492 return (0);
493 }
494
495 /*
496 * Map a character.
497 */
498 static int
499 rasops_mapchar(void *cookie, int c, u_int *cp)
500 {
501 struct rasops_info *ri;
502
503 ri = (struct rasops_info *)cookie;
504
505 #ifdef DIAGNOSTIC
506 if (ri->ri_font == NULL)
507 panic("rasops_mapchar: no font selected");
508 #endif
509
510 if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) {
511 *cp = ' ';
512 return (0);
513 }
514
515 if (c < ri->ri_font->firstchar) {
516 *cp = ' ';
517 return (0);
518 }
519
520 #if 0
521 if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
522 *cp = ' ';
523 return (0);
524 }
525 #endif
526 *cp = c;
527 return (5);
528 }
529
530 /*
531 * Allocate a color attribute.
532 */
533 static int
534 rasops_allocattr_color(void *cookie, int fg, int bg, int flg,
535 long *attr)
536 {
537 int swap;
538
539 if (__predict_false((unsigned int)fg >= sizeof(rasops_isgray) ||
540 (unsigned int)bg >= sizeof(rasops_isgray)))
541 return (EINVAL);
542
543 #ifdef RASOPS_CLIPPING
544 fg &= 7;
545 bg &= 7;
546 #endif
547 if ((flg & WSATTR_BLINK) != 0)
548 return (EINVAL);
549
550 if ((flg & WSATTR_WSCOLORS) == 0) {
551 #ifdef WS_DEFAULT_FG
552 fg = WS_DEFAULT_FG;
553 #else
554 fg = WSCOL_WHITE;
555 #endif
556 #ifdef WS_DEFAULT_BG
557 bg = WS_DEFAULT_BG;
558 #else
559 bg = WSCOL_BLACK;
560 #endif
561 }
562
563 if ((flg & WSATTR_REVERSE) != 0) {
564 swap = fg;
565 fg = bg;
566 bg = swap;
567 }
568
569 if ((flg & WSATTR_HILIT) != 0)
570 fg += 8;
571
572 flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
573
574 if (rasops_isgray[fg])
575 flg |= 2;
576
577 if (rasops_isgray[bg])
578 flg |= 4;
579
580 *attr = (bg << 16) | (fg << 24) | flg;
581 return (0);
582 }
583
584 /*
585 * Allocate a mono attribute.
586 */
587 static int
588 rasops_allocattr_mono(void *cookie, int fg, int bg, int flg,
589 long *attr)
590 {
591 int swap;
592
593 if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
594 return (EINVAL);
595
596 fg = 1;
597 bg = 0;
598
599 if ((flg & WSATTR_REVERSE) != 0) {
600 swap = fg;
601 fg = bg;
602 bg = swap;
603 }
604
605 *attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
606 return (0);
607 }
608
609 /*
610 * Copy rows.
611 */
612 static void
613 rasops_copyrows(void *cookie, int src, int dst, int num)
614 {
615 int32_t *sp, *dp, *hp, *srp, *drp, *hrp;
616 struct rasops_info *ri;
617 int n8, n1, cnt, delta;
618
619 ri = (struct rasops_info *)cookie;
620 hp = hrp = NULL;
621
622 #ifdef RASOPS_CLIPPING
623 if (dst == src)
624 return;
625
626 if (src < 0) {
627 num += src;
628 src = 0;
629 }
630
631 if ((src + num) > ri->ri_rows)
632 num = ri->ri_rows - src;
633
634 if (dst < 0) {
635 num += dst;
636 dst = 0;
637 }
638
639 if ((dst + num) > ri->ri_rows)
640 num = ri->ri_rows - dst;
641
642 if (num <= 0)
643 return;
644 #endif
645
646 num *= ri->ri_font->fontheight;
647 n8 = ri->ri_emustride >> 5;
648 n1 = (ri->ri_emustride >> 2) & 7;
649
650 if (dst < src) {
651 srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
652 drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
653 if (ri->ri_hwbits)
654 hrp = (int32_t *)(ri->ri_hwbits + dst *
655 ri->ri_yscale);
656 delta = ri->ri_stride;
657 } else {
658 src = ri->ri_font->fontheight * src + num - 1;
659 dst = ri->ri_font->fontheight * dst + num - 1;
660 srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
661 drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
662 if (ri->ri_hwbits)
663 hrp = (int32_t *)(ri->ri_hwbits + dst *
664 ri->ri_stride);
665
666 delta = -ri->ri_stride;
667 }
668
669 while (num--) {
670 dp = drp;
671 sp = srp;
672 if (ri->ri_hwbits)
673 hp = hrp;
674
675 DELTA(drp, delta, int32_t *);
676 DELTA(srp, delta, int32_t *);
677 if (ri->ri_hwbits)
678 DELTA(hrp, delta, int32_t *);
679
680 for (cnt = n8; cnt; cnt--) {
681 dp[0] = sp[0];
682 dp[1] = sp[1];
683 dp[2] = sp[2];
684 dp[3] = sp[3];
685 dp[4] = sp[4];
686 dp[5] = sp[5];
687 dp[6] = sp[6];
688 dp[7] = sp[7];
689 dp += 8;
690 sp += 8;
691 }
692 if (ri->ri_hwbits) {
693 sp -= (8 * n8);
694 for (cnt = n8; cnt; cnt--) {
695 hp[0] = sp[0];
696 hp[1] = sp[1];
697 hp[2] = sp[2];
698 hp[3] = sp[3];
699 hp[4] = sp[4];
700 hp[5] = sp[5];
701 hp[6] = sp[6];
702 hp[7] = sp[7];
703 hp += 8;
704 sp += 8;
705 }
706 }
707
708 for (cnt = n1; cnt; cnt--) {
709 *dp++ = *sp++;
710 if (ri->ri_hwbits)
711 *hp++ = *(sp - 1);
712 }
713 }
714 }
715
716 /*
717 * Copy columns. This is slow, and hard to optimize due to alignment,
718 * and the fact that we have to copy both left->right and right->left.
719 * We simply cop-out here and use memmove(), since it handles all of
720 * these cases anyway.
721 */
722 void
723 rasops_copycols(void *cookie, int row, int src, int dst, int num)
724 {
725 struct rasops_info *ri;
726 u_char *sp, *dp, *hp;
727 int height;
728
729 ri = (struct rasops_info *)cookie;
730 hp = NULL;
731
732 #ifdef RASOPS_CLIPPING
733 if (dst == src)
734 return;
735
736 /* Catches < 0 case too */
737 if ((unsigned)row >= (unsigned)ri->ri_rows)
738 return;
739
740 if (src < 0) {
741 num += src;
742 src = 0;
743 }
744
745 if ((src + num) > ri->ri_cols)
746 num = ri->ri_cols - src;
747
748 if (dst < 0) {
749 num += dst;
750 dst = 0;
751 }
752
753 if ((dst + num) > ri->ri_cols)
754 num = ri->ri_cols - dst;
755
756 if (num <= 0)
757 return;
758 #endif
759
760 num *= ri->ri_xscale;
761 row *= ri->ri_yscale;
762 height = ri->ri_font->fontheight;
763
764 sp = ri->ri_bits + row + src * ri->ri_xscale;
765 dp = ri->ri_bits + row + dst * ri->ri_xscale;
766 if (ri->ri_hwbits)
767 hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
768
769 while (height--) {
770 memmove(dp, sp, num);
771 if (ri->ri_hwbits) {
772 memcpy(hp, sp, num);
773 hp += ri->ri_stride;
774 }
775 dp += ri->ri_stride;
776 sp += ri->ri_stride;
777 }
778 }
779
780 /*
781 * Turn cursor off/on.
782 */
783 static void
784 rasops_cursor(void *cookie, int on, int row, int col)
785 {
786 struct rasops_info *ri;
787
788 ri = (struct rasops_info *)cookie;
789
790 /* Turn old cursor off */
791 if ((ri->ri_flg & RI_CURSOR) != 0)
792 #ifdef RASOPS_CLIPPING
793 if ((ri->ri_flg & RI_CURSORCLIP) == 0)
794 #endif
795 ri->ri_do_cursor(ri);
796
797 /* Select new cursor */
798 #ifdef RASOPS_CLIPPING
799 ri->ri_flg &= ~RI_CURSORCLIP;
800
801 if (row < 0 || row >= ri->ri_rows)
802 ri->ri_flg |= RI_CURSORCLIP;
803 else if (col < 0 || col >= ri->ri_cols)
804 ri->ri_flg |= RI_CURSORCLIP;
805 #endif
806 ri->ri_crow = row;
807 ri->ri_ccol = col;
808
809 if (on) {
810 ri->ri_flg |= RI_CURSOR;
811 #ifdef RASOPS_CLIPPING
812 if ((ri->ri_flg & RI_CURSORCLIP) == 0)
813 #endif
814 ri->ri_do_cursor(ri);
815 } else
816 ri->ri_flg &= ~RI_CURSOR;
817 }
818
819 /*
820 * Make the device colormap
821 */
822 static void
823 rasops_init_devcmap(struct rasops_info *ri)
824 {
825 const u_char *p;
826 int i, c;
827
828 switch (ri->ri_depth) {
829 case 1:
830 ri->ri_devcmap[0] = 0;
831 for (i = 1; i < 16; i++)
832 ri->ri_devcmap[i] = -1;
833 return;
834
835 case 2:
836 for (i = 1; i < 15; i++)
837 ri->ri_devcmap[i] = 0xaaaaaaaa;
838
839 ri->ri_devcmap[0] = 0;
840 ri->ri_devcmap[8] = 0x55555555;
841 ri->ri_devcmap[15] = -1;
842 return;
843
844 case 8:
845 if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
846 for (i = 0; i < 16; i++)
847 ri->ri_devcmap[i] =
848 i | (i<<8) | (i<<16) | (i<<24);
849 return;
850 }
851 }
852
853 p = rasops_cmap;
854
855 for (i = 0; i < 16; i++) {
856 if (ri->ri_rnum <= 8)
857 c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
858 else
859 c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
860 p++;
861
862 if (ri->ri_gnum <= 8)
863 c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
864 else
865 c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
866 p++;
867
868 if (ri->ri_bnum <= 8)
869 c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
870 else
871 c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
872 p++;
873
874 /* Fill the word for generic routines, which want this */
875 if (ri->ri_depth == 24)
876 c = c | ((c & 0xff) << 24);
877 else if (ri->ri_depth == 8) {
878 c = c | (c << 8);
879 c |= c << 16;
880 } else if (ri->ri_depth <= 16)
881 c = c | (c << 16);
882
883 /* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
884 if ((ri->ri_flg & RI_BSWAP) == 0)
885 ri->ri_devcmap[i] = c;
886 else if (ri->ri_depth == 32)
887 ri->ri_devcmap[i] = bswap32(c);
888 else if (ri->ri_depth == 16 || ri->ri_depth == 15)
889 ri->ri_devcmap[i] = bswap16(c);
890 else
891 ri->ri_devcmap[i] = c;
892 }
893 }
894
895 /*
896 * Unpack a rasops attribute
897 */
898 void
899 rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
900 {
901
902 *fg = ((u_int)attr >> 24) & 0xf;
903 *bg = ((u_int)attr >> 16) & 0xf;
904 if (underline != NULL)
905 *underline = (u_int)attr & 1;
906 }
907
908 /*
909 * Erase rows. This isn't static, since 24-bpp uses it in special cases.
910 */
911 void
912 rasops_eraserows(void *cookie, int row, int num, long attr)
913 {
914 struct rasops_info *ri;
915 int np, nw, cnt, delta;
916 int32_t *dp, *hp, clr;
917 int i;
918
919 ri = (struct rasops_info *)cookie;
920 hp = NULL;
921
922 #ifdef RASOPS_CLIPPING
923 if (row < 0) {
924 num += row;
925 row = 0;
926 }
927
928 if ((row + num) > ri->ri_rows)
929 num = ri->ri_rows - row;
930
931 if (num <= 0)
932 return;
933 #endif
934
935 clr = ri->ri_devcmap[(attr >> 16) & 0xf];
936
937 /*
938 * XXX The wsdisplay_emulops interface seems a little deficient in
939 * that there is no way to clear the *entire* screen. We provide a
940 * workaround here: if the entire console area is being cleared, and
941 * the RI_FULLCLEAR flag is set, clear the entire display.
942 */
943 if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
944 np = ri->ri_stride >> 5;
945 nw = (ri->ri_stride >> 2) & 7;
946 num = ri->ri_height;
947 dp = (int32_t *)ri->ri_origbits;
948 if (ri->ri_hwbits)
949 hp = (int32_t *)ri->ri_hworigbits;
950 delta = 0;
951 } else {
952 np = ri->ri_emustride >> 5;
953 nw = (ri->ri_emustride >> 2) & 7;
954 num *= ri->ri_font->fontheight;
955 dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
956 if (ri->ri_hwbits)
957 hp = (int32_t *)(ri->ri_hwbits + row *
958 ri->ri_yscale);
959 delta = ri->ri_delta;
960 }
961
962 while (num--) {
963 for (cnt = np; cnt; cnt--) {
964 for (i = 0; i < 8; i++) {
965 dp[i] = clr;
966 if (ri->ri_hwbits)
967 hp[i] = clr;
968 }
969 dp += 8;
970 if (ri->ri_hwbits)
971 hp += 8;
972 }
973
974 for (cnt = nw; cnt; cnt--) {
975 *(int32_t *)dp = clr;
976 DELTA(dp, 4, int32_t *);
977 if (ri->ri_hwbits) {
978 *(int32_t *)hp = clr;
979 DELTA(hp, 4, int32_t *);
980 }
981 }
982
983 DELTA(dp, delta, int32_t *);
984 if (ri->ri_hwbits)
985 DELTA(hp, delta, int32_t *);
986 }
987 }
988
989 /*
990 * Actually turn the cursor on or off. This does the dirty work for
991 * rasops_cursor().
992 */
993 static void
994 rasops_do_cursor(struct rasops_info *ri)
995 {
996 int full1, height, cnt, slop1, slop2, row, col;
997 u_char *dp, *rp, *hrp, *hp, tmp = 0;
998
999 hrp = hp = NULL;
1000
1001 #if NRASOPS_ROTATION > 0
1002 if (ri->ri_flg & RI_ROTATE_MASK) {
1003 if (ri->ri_flg & RI_ROTATE_CW) {
1004 /* Rotate rows/columns */
1005 row = ri->ri_ccol;
1006 col = ri->ri_rows - ri->ri_crow - 1;
1007 } else if (ri->ri_flg & RI_ROTATE_CCW) {
1008 /* Rotate rows/columns */
1009 row = ri->ri_cols - ri->ri_ccol - 1;
1010 col = ri->ri_crow;
1011 } else { /* upside-down */
1012 row = ri->ri_crow;
1013 col = ri->ri_ccol;
1014 }
1015 } else
1016 #endif
1017 {
1018 row = ri->ri_crow;
1019 col = ri->ri_ccol;
1020 }
1021
1022 rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1023 if (ri->ri_hwbits)
1024 hrp = ri->ri_hwbits + row * ri->ri_yscale + col
1025 * ri->ri_xscale;
1026 height = ri->ri_font->fontheight;
1027 slop1 = (4 - ((long)rp & 3)) & 3;
1028
1029 if (slop1 > ri->ri_xscale)
1030 slop1 = ri->ri_xscale;
1031
1032 slop2 = (ri->ri_xscale - slop1) & 3;
1033 full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
1034
1035 if ((slop1 | slop2) == 0) {
1036 uint32_t tmp32;
1037 /* A common case */
1038 while (height--) {
1039 dp = rp;
1040 rp += ri->ri_stride;
1041 if (ri->ri_hwbits) {
1042 hp = hrp;
1043 hrp += ri->ri_stride;
1044 }
1045
1046 for (cnt = full1; cnt; cnt--) {
1047 tmp32 = *(int32_t *)dp ^ ~0;
1048 *(int32_t *)dp = tmp32;
1049 dp += 4;
1050 if (ri->ri_hwbits) {
1051 *(int32_t *)hp = tmp32;
1052 hp += 4;
1053 }
1054 }
1055 }
1056 } else {
1057 uint16_t tmp16;
1058 uint32_t tmp32;
1059 /* XXX this is stupid.. use masks instead */
1060 while (height--) {
1061 dp = rp;
1062 rp += ri->ri_stride;
1063 if (ri->ri_hwbits) {
1064 hp = hrp;
1065 hrp += ri->ri_stride;
1066 }
1067
1068 if (slop1 & 1) {
1069 tmp = *dp ^ ~0;
1070 *dp = tmp;
1071 dp++;
1072 if (ri->ri_hwbits) {
1073 *hp++ = tmp;
1074 }
1075 }
1076
1077 if (slop1 & 2) {
1078 tmp16 = *(int16_t *)dp ^ ~0;
1079 *(uint16_t *)dp = tmp16;
1080 dp += 2;
1081 if (ri->ri_hwbits) {
1082 *(int16_t *)hp = tmp16;
1083 hp += 2;
1084 }
1085 }
1086
1087 for (cnt = full1; cnt; cnt--) {
1088 tmp32 = *(int32_t *)dp ^ ~0;
1089 *(uint32_t *)dp = tmp32;
1090 dp += 4;
1091 if (ri->ri_hwbits) {
1092 *(int32_t *)hp = tmp32;
1093 hp += 4;
1094 }
1095 }
1096
1097 if (slop2 & 1) {
1098 tmp = *dp ^ ~0;
1099 *dp = tmp;
1100 dp++;
1101 if (ri->ri_hwbits)
1102 *hp++ = tmp;
1103 }
1104
1105 if (slop2 & 2) {
1106 tmp16 = *(int16_t *)dp ^ ~0;
1107 *(uint16_t *)dp = tmp16;
1108 if (ri->ri_hwbits)
1109 *(int16_t *)hp = tmp16;
1110 }
1111 }
1112 }
1113 }
1114
1115 /*
1116 * Erase columns.
1117 */
1118 void
1119 rasops_erasecols(void *cookie, int row, int col, int num, long attr)
1120 {
1121 int n8, height, cnt, slop1, slop2, clr;
1122 struct rasops_info *ri;
1123 int32_t *rp, *dp, *hrp, *hp;
1124 int i;
1125
1126 ri = (struct rasops_info *)cookie;
1127 hrp = hp = NULL;
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 = num * ri->ri_xscale;
1146 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
1147 if (ri->ri_hwbits)
1148 hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
1149 col*ri->ri_xscale);
1150 height = ri->ri_font->fontheight;
1151 clr = ri->ri_devcmap[(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 DELTA(rp, ri->ri_stride, int32_t *);
1162 if (ri->ri_hwbits) {
1163 hp = hrp;
1164 DELTA(hrp, ri->ri_stride, int32_t *);
1165 }
1166
1167 for (cnt = num; cnt; cnt--) {
1168 *dp++ = clr;
1169 if (ri->ri_hwbits)
1170 *hp++ = clr;
1171 }
1172 }
1173 } else if (((num | ri->ri_xscale) & 1) == 0) {
1174 /*
1175 * Halfword aligned blt. This is needed so the
1176 * 15/16 bit ops can use this function.
1177 */
1178 num >>= 1;
1179
1180 while (height--) {
1181 dp = rp;
1182 DELTA(rp, ri->ri_stride, int32_t *);
1183 if (ri->ri_hwbits) {
1184 hp = hrp;
1185 DELTA(hrp, ri->ri_stride, int32_t *);
1186 }
1187
1188 for (cnt = num; cnt; cnt--) {
1189 *(int16_t *)dp = clr;
1190 DELTA(dp, 2, int32_t *);
1191 if (ri->ri_hwbits) {
1192 *(int16_t *)hp = clr;
1193 DELTA(hp, 2, int32_t *);
1194 }
1195 }
1196 }
1197 } else {
1198 while (height--) {
1199 dp = rp;
1200 DELTA(rp, ri->ri_stride, int32_t *);
1201 if (ri->ri_hwbits) {
1202 hp = hrp;
1203 DELTA(hrp, ri->ri_stride, int32_t *);
1204 }
1205
1206 for (cnt = num; cnt; cnt--) {
1207 *(u_char *)dp = clr;
1208 DELTA(dp, 1, int32_t *);
1209 if (ri->ri_hwbits) {
1210 *(u_char *)hp = clr;
1211 DELTA(hp, 1, int32_t *);
1212 }
1213 }
1214 }
1215 }
1216
1217 return;
1218 }
1219
1220 slop1 = (4 - ((long)rp & 3)) & 3;
1221 slop2 = (num - slop1) & 3;
1222 num -= slop1 + slop2;
1223 n8 = num >> 5;
1224 num = (num >> 2) & 7;
1225
1226 while (height--) {
1227 dp = rp;
1228 DELTA(rp, ri->ri_stride, int32_t *);
1229 if (ri->ri_hwbits) {
1230 hp = hrp;
1231 DELTA(hrp, ri->ri_stride, int32_t *);
1232 }
1233
1234 /* Align span to 4 bytes */
1235 if (slop1 & 1) {
1236 *(u_char *)dp = clr;
1237 DELTA(dp, 1, int32_t *);
1238 if (ri->ri_hwbits) {
1239 *(u_char *)hp = clr;
1240 DELTA(hp, 1, int32_t *);
1241 }
1242 }
1243
1244 if (slop1 & 2) {
1245 *(int16_t *)dp = clr;
1246 DELTA(dp, 2, int32_t *);
1247 if (ri->ri_hwbits) {
1248 *(int16_t *)hp = clr;
1249 DELTA(hp, 2, int32_t *);
1250 }
1251 }
1252
1253 /* Write 32 bytes per loop */
1254 for (cnt = n8; cnt; cnt--) {
1255 for (i = 0; i < 8; i++) {
1256 dp[i] = clr;
1257 if (ri->ri_hwbits)
1258 hp[i] = clr;
1259 }
1260 dp += 8;
1261 if (ri->ri_hwbits)
1262 hp += 8;
1263 }
1264
1265 /* Write 4 bytes per loop */
1266 for (cnt = num; cnt; cnt--) {
1267 *dp++ = clr;
1268 if (ri->ri_hwbits)
1269 *hp++ = clr;
1270 }
1271
1272 /* Write unaligned trailing slop */
1273 if (slop2 & 1) {
1274 *(u_char *)dp = clr;
1275 DELTA(dp, 1, int32_t *);
1276 if (ri->ri_hwbits) {
1277 *(u_char *)hp = clr;
1278 DELTA(hp, 1, int32_t *);
1279 }
1280 }
1281
1282 if (slop2 & 2) {
1283 *(int16_t *)dp = clr;
1284 if (ri->ri_hwbits)
1285 *(int16_t *)hp = clr;
1286 }
1287 }
1288 }
1289
1290 #if NRASOPS_ROTATION > 0
1291 /*
1292 * Quarter clockwise rotation routines (originally intended for the
1293 * built-in Zaurus C3x00 display in 16bpp).
1294 */
1295
1296 #include <sys/malloc.h>
1297
1298 static void
1299 rasops_rotate_font(int *cookie, int rotate)
1300 {
1301 struct rotatedfont *f;
1302 int ncookie;
1303
1304 SLIST_FOREACH(f, &rotatedfonts, rf_next) {
1305 if (f->rf_cookie == *cookie) {
1306 *cookie = f->rf_rotated;
1307 return;
1308 }
1309 }
1310
1311 /*
1312 * We did not find a rotated version of this font. Ask the wsfont
1313 * code to compute one for us.
1314 */
1315
1316 f = malloc(sizeof(struct rotatedfont), M_DEVBUF, M_WAITOK);
1317 if (f == NULL)
1318 goto fail0;
1319
1320 if ((ncookie = wsfont_rotate(*cookie, rotate)) == -1)
1321 goto fail1;
1322
1323 f->rf_cookie = *cookie;
1324 f->rf_rotated = ncookie;
1325 SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next);
1326
1327 *cookie = ncookie;
1328 return;
1329
1330 fail1: free(f, M_DEVBUF);
1331 fail0: /* Just use the existing font, I guess... */
1332 return;
1333 }
1334
1335 static void
1336 rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
1337 {
1338 struct rasops_info *ri;
1339 u_char *sp, *dp;
1340 int height;
1341 int r_srcrow, r_dstrow, r_srccol, r_dstcol;
1342
1343 ri = (struct rasops_info *)cookie;
1344
1345 r_srcrow = srccol;
1346 r_dstrow = dstcol;
1347 r_srccol = ri->ri_rows - srcrow - 1;
1348 r_dstcol = ri->ri_rows - dstrow - 1;
1349
1350 r_srcrow *= ri->ri_yscale;
1351 r_dstrow *= ri->ri_yscale;
1352 height = ri->ri_font->fontheight;
1353
1354 sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
1355 dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
1356
1357 while (height--) {
1358 memmove(dp, sp, ri->ri_xscale);
1359 dp += ri->ri_stride;
1360 sp += ri->ri_stride;
1361 }
1362 }
1363
1364 static void
1365 rasops_putchar_rotated_cw(void *cookie, int row, int col, u_int uc, long attr)
1366 {
1367 struct rasops_info *ri;
1368 u_char *rp;
1369 int height;
1370
1371 ri = (struct rasops_info *)cookie;
1372
1373 if (__predict_false((unsigned int)row > ri->ri_rows ||
1374 (unsigned int)col > ri->ri_cols))
1375 return;
1376
1377 /* Avoid underflow */
1378 if ((ri->ri_rows - row - 1) < 0)
1379 return;
1380
1381 /* Do rotated char sans (side)underline */
1382 ri->ri_real_ops.putchar(cookie, col, ri->ri_rows - row - 1, uc,
1383 attr & ~1);
1384
1385 /* Do rotated underline */
1386 rp = ri->ri_bits + col * ri->ri_yscale + (ri->ri_rows - row - 1) *
1387 ri->ri_xscale;
1388 height = ri->ri_font->fontheight;
1389
1390 /* XXX this assumes 16-bit color depth */
1391 if ((attr & 1) != 0) {
1392 int16_t c = (int16_t)ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
1393
1394 while (height--) {
1395 *(int16_t *)rp = c;
1396 rp += ri->ri_stride;
1397 }
1398 }
1399 }
1400
1401 static void
1402 rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
1403 {
1404 struct rasops_info *ri;
1405 int i;
1406
1407 ri = (struct rasops_info *)cookie;
1408
1409 for (i = col; i < col + num; i++)
1410 ri->ri_ops.putchar(cookie, row, i, ' ', attr);
1411 }
1412
1413 /* XXX: these could likely be optimised somewhat. */
1414 static void
1415 rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
1416 {
1417 struct rasops_info *ri = (struct rasops_info *)cookie;
1418 int col, roff;
1419
1420 if (src > dst)
1421 for (roff = 0; roff < num; roff++)
1422 for (col = 0; col < ri->ri_cols; col++)
1423 rasops_copychar(cookie, src + roff, dst + roff,
1424 col, col);
1425 else
1426 for (roff = num - 1; roff >= 0; roff--)
1427 for (col = 0; col < ri->ri_cols; col++)
1428 rasops_copychar(cookie, src + roff, dst + roff,
1429 col, col);
1430 }
1431
1432 static void
1433 rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
1434 {
1435 int coff;
1436
1437 if (src > dst)
1438 for (coff = 0; coff < num; coff++)
1439 rasops_copychar(cookie, row, row, src + coff, dst + coff);
1440 else
1441 for (coff = num - 1; coff >= 0; coff--)
1442 rasops_copychar(cookie, row, row, src + coff, dst + coff);
1443 }
1444
1445 static void
1446 rasops_eraserows_rotated_cw(void *cookie, int row, int num, long attr)
1447 {
1448 struct rasops_info *ri;
1449 int col, rn;
1450
1451 ri = (struct rasops_info *)cookie;
1452
1453 for (rn = row; rn < row + num; rn++)
1454 for (col = 0; col < ri->ri_cols; col++)
1455 ri->ri_ops.putchar(cookie, rn, col, ' ', attr);
1456 }
1457
1458 /*
1459 * Quarter counter-clockwise rotation routines (originally intended for the
1460 * built-in Sharp W-ZERO3 display in 16bpp).
1461 */
1462 static void
1463 rasops_copychar_ccw(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
1464 {
1465 struct rasops_info *ri;
1466 u_char *sp, *dp;
1467 int height;
1468 int r_srcrow, r_dstrow, r_srccol, r_dstcol;
1469
1470 ri = (struct rasops_info *)cookie;
1471
1472 r_srcrow = ri->ri_cols - srccol - 1;
1473 r_dstrow = ri->ri_cols - dstcol - 1;
1474 r_srccol = srcrow;
1475 r_dstcol = dstrow;
1476
1477 r_srcrow *= ri->ri_yscale;
1478 r_dstrow *= ri->ri_yscale;
1479 height = ri->ri_font->fontheight;
1480
1481 sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
1482 dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
1483
1484 while (height--) {
1485 memmove(dp, sp, ri->ri_xscale);
1486 dp += ri->ri_stride;
1487 sp += ri->ri_stride;
1488 }
1489 }
1490
1491 static void
1492 rasops_putchar_rotated_ccw(void *cookie, int row, int col, u_int uc, long attr)
1493 {
1494 struct rasops_info *ri;
1495 u_char *rp;
1496 int height;
1497
1498 ri = (struct rasops_info *)cookie;
1499
1500 if (__predict_false((unsigned int)row > ri->ri_rows ||
1501 (unsigned int)col > ri->ri_cols))
1502 return;
1503
1504 /* Avoid underflow */
1505 if ((ri->ri_cols - col - 1) < 0)
1506 return;
1507
1508 /* Do rotated char sans (side)underline */
1509 ri->ri_real_ops.putchar(cookie, ri->ri_cols - col - 1, row, uc,
1510 attr & ~1);
1511
1512 /* Do rotated underline */
1513 rp = ri->ri_bits + (ri->ri_cols - col - 1) * ri->ri_yscale +
1514 row * ri->ri_xscale +
1515 (ri->ri_font->fontwidth - 1) * ri->ri_pelbytes;
1516 height = ri->ri_font->fontheight;
1517
1518 /* XXX this assumes 16-bit color depth */
1519 if ((attr & 1) != 0) {
1520 int16_t c = (int16_t)ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
1521
1522 while (height--) {
1523 *(int16_t *)rp = c;
1524 rp += ri->ri_stride;
1525 }
1526 }
1527 }
1528
1529 /* XXX: these could likely be optimised somewhat. */
1530 static void
1531 rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
1532 {
1533 struct rasops_info *ri = (struct rasops_info *)cookie;
1534 int col, roff;
1535
1536 if (src > dst)
1537 for (roff = 0; roff < num; roff++)
1538 for (col = 0; col < ri->ri_cols; col++)
1539 rasops_copychar_ccw(cookie,
1540 src + roff, dst + roff, col, col);
1541 else
1542 for (roff = num - 1; roff >= 0; roff--)
1543 for (col = 0; col < ri->ri_cols; col++)
1544 rasops_copychar_ccw(cookie,
1545 src + roff, dst + roff, col, col);
1546 }
1547
1548 static void
1549 rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
1550 {
1551 int coff;
1552
1553 if (src > dst)
1554 for (coff = 0; coff < num; coff++)
1555 rasops_copychar_ccw(cookie, row, row,
1556 src + coff, dst + coff);
1557 else
1558 for (coff = num - 1; coff >= 0; coff--)
1559 rasops_copychar_ccw(cookie, row, row,
1560 src + coff, dst + coff);
1561 }
1562 #endif /* NRASOPS_ROTATION */
1563
1564 void
1565 rasops_make_box_chars_16(struct rasops_info *ri)
1566 {
1567 uint16_t vert_mask, hmask_left, hmask_right;
1568 uint16_t *data = (uint16_t *)ri->ri_optfont.data;
1569 int c, i, mid;
1570
1571 vert_mask = 0xc000 >> ((ri->ri_font->fontwidth >> 1) - 1);
1572 hmask_left = 0xff00 << (8 - (ri->ri_font->fontwidth >> 1));
1573 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
1574 mid = (ri->ri_font->fontheight + 1) >> 1;
1575
1576 /* 0x00 would be empty anyway so don't bother */
1577 for (c = 1; c < 16; c++) {
1578 data += ri->ri_font->fontheight;
1579 if (c & 1) {
1580 /* upper segment */
1581 for (i = 0; i < mid; i++)
1582 data[i] = vert_mask;
1583 }
1584 if (c & 4) {
1585 /* lower segment */
1586 for (i = mid; i < ri->ri_font->fontheight; i++)
1587 data[i] = vert_mask;
1588 }
1589 if (c & 2) {
1590 /* right segment */
1591 i = ri->ri_font->fontheight >> 1;
1592 data[mid - 1] |= hmask_right;
1593 data[mid] |= hmask_right;
1594 }
1595 if (c & 8) {
1596 /* left segment */
1597 data[mid - 1] |= hmask_left;
1598 data[mid] |= hmask_left;
1599 }
1600 }
1601 }
1602
1603 void
1604 rasops_make_box_chars_8(struct rasops_info *ri)
1605 {
1606 uint8_t vert_mask, hmask_left, hmask_right;
1607 uint8_t *data = (uint8_t *)ri->ri_optfont.data;
1608 int c, i, mid;
1609
1610 vert_mask = 0xc0 >> ((ri->ri_font->fontwidth >> 1) - 1);
1611 hmask_left = 0xf0 << (4 - (ri->ri_font->fontwidth >> 1));
1612 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
1613 mid = (ri->ri_font->fontheight + 1) >> 1;
1614
1615 /* 0x00 would be empty anyway so don't bother */
1616 for (c = 1; c < 16; c++) {
1617 data += ri->ri_font->fontheight;
1618 if (c & 1) {
1619 /* upper segment */
1620 for (i = 0; i < mid; i++)
1621 data[i] = vert_mask;
1622 }
1623 if (c & 4) {
1624 /* lower segment */
1625 for (i = mid; i < ri->ri_font->fontheight; i++)
1626 data[i] = vert_mask;
1627 }
1628 if (c & 2) {
1629 /* right segment */
1630 i = ri->ri_font->fontheight >> 1;
1631 data[mid - 1] |= hmask_right;
1632 data[mid] |= hmask_right;
1633 }
1634 if (c & 8) {
1635 /* left segment */
1636 data[mid - 1] |= hmask_left;
1637 data[mid] |= hmask_left;
1638 }
1639 }
1640 }
1641
1642 void
1643 rasops_make_box_chars_32(struct rasops_info *ri)
1644 {
1645 uint32_t vert_mask, hmask_left, hmask_right;
1646 uint32_t *data = (uint32_t *)ri->ri_optfont.data;
1647 int c, i, mid;
1648
1649 vert_mask = 0xc0000000 >> ((ri->ri_font->fontwidth >> 1) - 1);
1650 hmask_left = 0xffff0000 << (16 - (ri->ri_font->fontwidth >> 1));
1651 hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1)>> 1);
1652 mid = (ri->ri_font->fontheight + 1) >> 1;
1653
1654 /* 0x00 would be empty anyway so don't bother */
1655 for (c = 1; c < 16; c++) {
1656 data += ri->ri_font->fontheight;
1657 if (c & 1) {
1658 /* upper segment */
1659 for (i = 0; i < mid; i++)
1660 data[i] = vert_mask;
1661 }
1662 if (c & 4) {
1663 /* lower segment */
1664 for (i = mid; i < ri->ri_font->fontheight; i++)
1665 data[i] = vert_mask;
1666 }
1667 if (c & 2) {
1668 /* right segment */
1669 i = ri->ri_font->fontheight >> 1;
1670 data[mid - 1] |= hmask_right;
1671 data[mid] |= hmask_right;
1672 }
1673 if (c & 8) {
1674 /* left segment */
1675 data[mid - 1] |= hmask_left;
1676 data[mid] |= hmask_left;
1677 }
1678 }
1679 }
1680
1681 void
1682 rasops_make_box_chars_alpha(struct rasops_info *ri)
1683 {
1684 uint8_t *data = (uint8_t *)ri->ri_optfont.data;
1685 uint8_t *ddata;
1686 int c, i, hmid, vmid, wi, he;
1687
1688 wi = ri->ri_font->fontwidth;
1689 he = ri->ri_font->fontheight;
1690
1691 vmid = (he + 1) >> 1;
1692 hmid = (wi + 1) >> 1;
1693
1694 /* 0x00 would be empty anyway so don't bother */
1695 for (c = 1; c < 16; c++) {
1696 data += ri->ri_fontscale;
1697 if (c & 1) {
1698 /* upper segment */
1699 ddata = data + hmid;
1700 for (i = 0; i <= vmid; i++) {
1701 *ddata = 0xff;
1702 ddata += wi;
1703 }
1704 }
1705 if (c & 4) {
1706 /* lower segment */
1707 ddata = data + wi * vmid + hmid;
1708 for (i = vmid; i < he; i++) {
1709 *ddata = 0xff;
1710 ddata += wi;
1711 }
1712 }
1713 if (c & 2) {
1714 /* right segment */
1715 ddata = data + wi * vmid + hmid;
1716 for (i = hmid; i < wi; i++) {
1717 *ddata = 0xff;
1718 ddata++;
1719 }
1720 }
1721 if (c & 8) {
1722 /* left segment */
1723 ddata = data + wi * vmid;
1724 for (i = 0; i <= hmid; i++) {
1725 *ddata = 0xff;
1726 ddata++;
1727 }
1728 }
1729 }
1730 }
1731
1732 /*
1733 * Return a colour map appropriate for the given struct rasops_info in the
1734 * same form used by rasops_cmap[]
1735 * For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
1736 * probably be a linear ( or gamma corrected? ) ramp for higher depths.
1737 */
1738
1739 int
1740 rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
1741 {
1742 if ((ri->ri_depth == 8 ) && ((ri->ri_flg & RI_8BIT_IS_RGB) > 0)) {
1743 /* generate an R3G3B2 palette */
1744 int i, idx = 0;
1745 uint8_t tmp;
1746
1747 if (bytes < 768)
1748 return EINVAL;
1749 for (i = 0; i < 256; i++) {
1750 tmp = i & 0xe0;
1751 /*
1752 * replicate bits so 0xe0 maps to a red value of 0xff
1753 * in order to make white look actually white
1754 */
1755 tmp |= (tmp >> 3) | (tmp >> 6);
1756 palette[idx] = tmp;
1757 idx++;
1758
1759 tmp = (i & 0x1c) << 3;
1760 tmp |= (tmp >> 3) | (tmp >> 6);
1761 palette[idx] = tmp;
1762 idx++;
1763
1764 tmp = (i & 0x03) << 6;
1765 tmp |= tmp >> 2;
1766 tmp |= tmp >> 4;
1767 palette[idx] = tmp;
1768 idx++;
1769 }
1770 } else {
1771 memcpy(palette, rasops_cmap, MIN(bytes, sizeof(rasops_cmap)));
1772 }
1773 return 0;
1774 }
1775