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