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