xref: /netbsd-src/sys/dev/rasops/rasops.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	 $NetBSD: rasops.c,v 1.53 2006/10/12 01:31:56 christos 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.53 2006/10/12 01:31:56 christos Exp $");
41 
42 #include "opt_rasops.h"
43 #include "rasops_glue.h"
44 #include "opt_wsmsgattrs.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/time.h>
49 
50 #include <sys/bswap.h>
51 #include <machine/endian.h>
52 
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wsconsio.h>
55 #include <dev/wsfont/wsfont.h>
56 #include <dev/rasops/rasops.h>
57 
58 #ifndef _KERNEL
59 #include <errno.h>
60 #endif
61 
62 /* ANSI colormap (R,G,B). Upper 8 are high-intensity */
63 const u_char rasops_cmap[256*3] = {
64 	0x00, 0x00, 0x00, /* black */
65 	0x7f, 0x00, 0x00, /* red */
66 	0x00, 0x7f, 0x00, /* green */
67 	0x7f, 0x7f, 0x00, /* brown */
68 	0x00, 0x00, 0x7f, /* blue */
69 	0x7f, 0x00, 0x7f, /* magenta */
70 	0x00, 0x7f, 0x7f, /* cyan */
71 	0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
72 
73 	0x7f, 0x7f, 0x7f, /* black */
74 	0xff, 0x00, 0x00, /* red */
75 	0x00, 0xff, 0x00, /* green */
76 	0xff, 0xff, 0x00, /* brown */
77 	0x00, 0x00, 0xff, /* blue */
78 	0xff, 0x00, 0xff, /* magenta */
79 	0x00, 0xff, 0xff, /* cyan */
80 	0xff, 0xff, 0xff, /* white */
81 
82 	/*
83 	 * For the cursor, we need at least the last (255th)
84 	 * color to be white. Fill up white completely for
85 	 * simplicity.
86 	 */
87 #define _CMWHITE 0xff, 0xff, 0xff,
88 #define _CMWHITE16	_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
89 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
90 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE \
91 			_CMWHITE _CMWHITE _CMWHITE _CMWHITE
92 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
93 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16
94 	_CMWHITE16 _CMWHITE16 _CMWHITE16 _CMWHITE16 /* but not the last one */
95 #undef _CMWHITE16
96 #undef _CMWHITE
97 
98 	/*
99 	 * For the cursor the fg/bg indices are bit inverted, so
100 	 * provide complimentary colors in the upper 16 entries.
101 	 */
102 	0x7f, 0x7f, 0x7f, /* black */
103 	0xff, 0x00, 0x00, /* red */
104 	0x00, 0xff, 0x00, /* green */
105 	0xff, 0xff, 0x00, /* brown */
106 	0x00, 0x00, 0xff, /* blue */
107 	0xff, 0x00, 0xff, /* magenta */
108 	0x00, 0xff, 0xff, /* cyan */
109 	0xff, 0xff, 0xff, /* white */
110 
111 	0x00, 0x00, 0x00, /* black */
112 	0x7f, 0x00, 0x00, /* red */
113 	0x00, 0x7f, 0x00, /* green */
114 	0x7f, 0x7f, 0x00, /* brown */
115 	0x00, 0x00, 0x7f, /* blue */
116 	0x7f, 0x00, 0x7f, /* magenta */
117 	0x00, 0x7f, 0x7f, /* cyan */
118 	0xc7, 0xc7, 0xc7, /* white - XXX too dim? */
119 };
120 
121 /* True if color is gray */
122 const u_char rasops_isgray[16] = {
123 	1, 0, 0, 0,
124 	0, 0, 0, 1,
125 	1, 0, 0, 0,
126 	0, 0, 0, 1
127 };
128 
129 /* Generic functions */
130 static void	rasops_copyrows(void *, int, int, int);
131 static int	rasops_mapchar(void *, int, u_int *);
132 static void	rasops_cursor(void *, int, int, int);
133 static int	rasops_allocattr_color(void *, int, int, int, long *);
134 static int	rasops_allocattr_mono(void *, int, int, int, long *);
135 static void	rasops_do_cursor(struct rasops_info *);
136 static void	rasops_init_devcmap(struct rasops_info *);
137 
138 /*
139  * Initialize a 'rasops_info' descriptor.
140  */
141 int
142 rasops_init(ri, wantrows, wantcols)
143 	struct rasops_info *ri;
144 	int wantrows, wantcols;
145 {
146 
147 #ifdef _KERNEL
148 	/* Select a font if the caller doesn't care */
149 	if (ri->ri_font == NULL) {
150 		int cookie;
151 
152 		wsfont_init();
153 
154 		/* Want 8 pixel wide, don't care about aestethics */
155 		cookie = wsfont_find(NULL, 8, 0, 0, WSDISPLAY_FONTORDER_L2R,
156 		    WSDISPLAY_FONTORDER_L2R);
157 		if (cookie <= 0)
158 			cookie = wsfont_find(NULL, 0, 0, 0,
159 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R);
160 
161 		if (cookie <= 0) {
162 			printf("rasops_init: font table is empty\n");
163 			return (-1);
164 		}
165 
166 		if (wsfont_lock(cookie, &ri->ri_font)) {
167 			printf("rasops_init: couldn't lock font\n");
168 			return (-1);
169 		}
170 
171 		ri->ri_wsfcookie = cookie;
172 	}
173 #endif
174 
175 	/* This should never happen in reality... */
176 #ifdef DEBUG
177 	if ((long)ri->ri_bits & 3) {
178 		printf("rasops_init: bits not aligned on 32-bit boundary\n");
179 		return (-1);
180 	}
181 
182 	if ((int)ri->ri_stride & 3) {
183 		printf("rasops_init: stride not aligned on 32-bit boundary\n");
184 		return (-1);
185 	}
186 #endif
187 
188 	if (rasops_reconfig(ri, wantrows, wantcols))
189 		return (-1);
190 
191 	rasops_init_devcmap(ri);
192 	return (0);
193 }
194 
195 /*
196  * Reconfigure (because parameters have changed in some way).
197  */
198 int
199 rasops_reconfig(ri, wantrows, wantcols)
200 	struct rasops_info *ri;
201 	int wantrows, wantcols;
202 {
203 	int bpp, s;
204 
205 	s = splhigh();
206 
207 	if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4)
208 		panic("rasops_init: fontwidth assumptions botched!");
209 
210 	/* Need this to frob the setup below */
211 	bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
212 
213 	if ((ri->ri_flg & RI_CFGDONE) != 0)
214 		ri->ri_bits = ri->ri_origbits;
215 
216 	/* Don't care if the caller wants a hideously small console */
217 	if (wantrows < 10)
218 		wantrows = 10;
219 
220 	if (wantcols < 20)
221 		wantcols = 20;
222 
223 	/* Now constrain what they get */
224 	ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
225 	ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
226 
227 	if (ri->ri_emuwidth > ri->ri_width)
228 		ri->ri_emuwidth = ri->ri_width;
229 
230 	if (ri->ri_emuheight > ri->ri_height)
231 		ri->ri_emuheight = ri->ri_height;
232 
233 	/* Reduce width until aligned on a 32-bit boundary */
234 	while ((ri->ri_emuwidth * bpp & 31) != 0)
235 		ri->ri_emuwidth--;
236 
237 	ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
238 	ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
239 	ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
240 	ri->ri_delta = ri->ri_stride - ri->ri_emustride;
241 	ri->ri_ccol = 0;
242 	ri->ri_crow = 0;
243 	ri->ri_pelbytes = bpp >> 3;
244 
245 	ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
246 	ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
247 	ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
248 
249 #ifdef DEBUG
250 	if ((ri->ri_delta & 3) != 0)
251 		panic("rasops_init: ri_delta not aligned on 32-bit boundary");
252 #endif
253 	/* Clear the entire display */
254 	if ((ri->ri_flg & RI_CLEAR) != 0)
255 		memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
256 
257 	/* Now centre our window if needs be */
258 	ri->ri_origbits = ri->ri_bits;
259 
260 	if ((ri->ri_flg & RI_CENTER) != 0) {
261 		ri->ri_bits += (((ri->ri_width * bpp >> 3) -
262 		    ri->ri_emustride) >> 1) & ~3;
263 		ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
264 		    ri->ri_stride;
265 
266 		ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits)
267 		   / ri->ri_stride;
268 		ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits)
269 		   % ri->ri_stride) * 8 / bpp);
270 	} else
271 		ri->ri_xorigin = ri->ri_yorigin = 0;
272 
273 	/*
274 	 * Fill in defaults for operations set.  XXX this nukes private
275 	 * routines used by accelerated fb drivers.
276 	 */
277 	ri->ri_ops.mapchar = rasops_mapchar;
278 	ri->ri_ops.copyrows = rasops_copyrows;
279 	ri->ri_ops.copycols = rasops_copycols;
280 	ri->ri_ops.erasecols = rasops_erasecols;
281 	ri->ri_ops.eraserows = rasops_eraserows;
282 	ri->ri_ops.cursor = rasops_cursor;
283 	ri->ri_do_cursor = rasops_do_cursor;
284 
285 	if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) {
286 		ri->ri_ops.allocattr = rasops_allocattr_mono;
287 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
288 	} else {
289 		ri->ri_ops.allocattr = rasops_allocattr_color;
290 		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
291 		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
292 	}
293 
294 	switch (ri->ri_depth) {
295 #if NRASOPS1 > 0
296 	case 1:
297 		rasops1_init(ri);
298 		break;
299 #endif
300 #if NRASOPS2 > 0
301 	case 2:
302 		rasops2_init(ri);
303 		break;
304 #endif
305 #if NRASOPS4 > 0
306 	case 4:
307 		rasops4_init(ri);
308 		break;
309 #endif
310 #if NRASOPS8 > 0
311 	case 8:
312 		rasops8_init(ri);
313 		break;
314 #endif
315 #if NRASOPS15 > 0 || NRASOPS16 > 0
316 	case 15:
317 	case 16:
318 		rasops15_init(ri);
319 		break;
320 #endif
321 #if NRASOPS24 > 0
322 	case 24:
323 		rasops24_init(ri);
324 		break;
325 #endif
326 #if NRASOPS32 > 0
327 	case 32:
328 		rasops32_init(ri);
329 		break;
330 #endif
331 	default:
332 		ri->ri_flg &= ~RI_CFGDONE;
333 		splx(s);
334 		return (-1);
335 	}
336 
337 	ri->ri_flg |= RI_CFGDONE;
338 	splx(s);
339 	return (0);
340 }
341 
342 /*
343  * Map a character.
344  */
345 static int
346 rasops_mapchar(cookie, c, cp)
347 	void *cookie;
348 	int c;
349 	u_int *cp;
350 {
351 	struct rasops_info *ri;
352 
353 	ri = (struct rasops_info *)cookie;
354 
355 #ifdef DIAGNOSTIC
356 	if (ri->ri_font == NULL)
357 		panic("rasops_mapchar: no font selected");
358 #endif
359 
360 	if (ri->ri_font->encoding != WSDISPLAY_FONTENC_ISO) {
361 		if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) {
362 			*cp = ' ';
363 			return (0);
364 
365 		}
366 	}
367 
368 	if (c < ri->ri_font->firstchar) {
369 		*cp = ' ';
370 		return (0);
371 	}
372 
373 	if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
374 		*cp = ' ';
375 		return (0);
376 	}
377 
378 	*cp = c;
379 	return (5);
380 }
381 
382 /*
383  * Allocate a color attribute.
384  */
385 static int
386 rasops_allocattr_color(void *cookie __unused, int fg, int bg, int flg,
387     long *attr)
388 {
389 	int swap;
390 
391 #ifdef RASOPS_CLIPPING
392 	fg &= 7;
393 	bg &= 7;
394 #endif
395 	if ((flg & WSATTR_BLINK) != 0)
396 		return (EINVAL);
397 
398 	if ((flg & WSATTR_WSCOLORS) == 0) {
399 #ifdef WS_DEFAULT_FG
400 		fg = WS_DEFAULT_FG;
401 #else
402 		fg = WSCOL_WHITE;
403 #endif
404 #ifdef WS_DEFAULT_BG
405 		bg = WS_DEFAULT_BG;
406 #else
407 		bg = WSCOL_BLACK;
408 #endif
409 	}
410 
411 	if ((flg & WSATTR_REVERSE) != 0) {
412 		swap = fg;
413 		fg = bg;
414 		bg = swap;
415 	}
416 
417 	if ((flg & WSATTR_HILIT) != 0)
418 		fg += 8;
419 
420 	flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
421 
422 	if (rasops_isgray[fg])
423 		flg |= 2;
424 
425 	if (rasops_isgray[bg])
426 		flg |= 4;
427 
428 	*attr = (bg << 16) | (fg << 24) | flg;
429 	return (0);
430 }
431 
432 /*
433  * Allocate a mono attribute.
434  */
435 static int
436 rasops_allocattr_mono(void *cookie __unused, int fg, int bg, int flg,
437     long *attr)
438 {
439 	int swap;
440 
441 	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
442 		return (EINVAL);
443 
444 	fg = 1;
445 	bg = 0;
446 
447 	if ((flg & WSATTR_REVERSE) != 0) {
448 		swap = fg;
449 		fg = bg;
450 		bg = swap;
451 	}
452 
453 	*attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
454 	return (0);
455 }
456 
457 /*
458  * Copy rows.
459  */
460 static void
461 rasops_copyrows(cookie, src, dst, num)
462 	void *cookie;
463 	int src, dst, num;
464 {
465 	int32_t *sp, *dp, *hp, *srp, *drp, *hrp;
466 	struct rasops_info *ri;
467 	int n8, n1, cnt, delta;
468 
469 	ri = (struct rasops_info *)cookie;
470 	hp = hrp = NULL;
471 
472 #ifdef RASOPS_CLIPPING
473 	if (dst == src)
474 		return;
475 
476 	if (src < 0) {
477 		num += src;
478 		src = 0;
479 	}
480 
481 	if ((src + num) > ri->ri_rows)
482 		num = ri->ri_rows - src;
483 
484 	if (dst < 0) {
485 		num += dst;
486 		dst = 0;
487 	}
488 
489 	if ((dst + num) > ri->ri_rows)
490 		num = ri->ri_rows - dst;
491 
492 	if (num <= 0)
493 		return;
494 #endif
495 
496 	num *= ri->ri_font->fontheight;
497 	n8 = ri->ri_emustride >> 5;
498 	n1 = (ri->ri_emustride >> 2) & 7;
499 
500 	if (dst < src) {
501 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
502 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
503 		if (ri->ri_hwbits)
504 			hrp = (int32_t *)(ri->ri_hwbits + dst *
505 			    ri->ri_yscale);
506 		delta = ri->ri_stride;
507 	} else {
508 		src = ri->ri_font->fontheight * src + num - 1;
509 		dst = ri->ri_font->fontheight * dst + num - 1;
510 		srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
511 		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
512 		if (ri->ri_hwbits)
513 			hrp = (int32_t *)(ri->ri_hwbits + dst *
514 			    ri->ri_stride);
515 
516 		delta = -ri->ri_stride;
517 	}
518 
519 	while (num--) {
520 		dp = drp;
521 		sp = srp;
522 		if (ri->ri_hwbits)
523 			hp = hrp;
524 
525 		DELTA(drp, delta, int32_t *);
526 		DELTA(srp, delta, int32_t *);
527 		if (ri->ri_hwbits)
528 			DELTA(hrp, delta, int32_t *);
529 
530 		for (cnt = n8; cnt; cnt--) {
531 			dp[0] = sp[0];
532 			dp[1] = sp[1];
533 			dp[2] = sp[2];
534 			dp[3] = sp[3];
535 			dp[4] = sp[4];
536 			dp[5] = sp[5];
537 			dp[6] = sp[6];
538 			dp[7] = sp[7];
539 			dp += 8;
540 			sp += 8;
541 		}
542 		if (ri->ri_hwbits) {
543 			sp -= (8 * n8);
544 			for (cnt = n8; cnt; cnt--) {
545 				hp[0] = sp[0];
546 				hp[1] = sp[1];
547 				hp[2] = sp[2];
548 				hp[3] = sp[3];
549 				hp[4] = sp[4];
550 				hp[5] = sp[5];
551 				hp[6] = sp[6];
552 				hp[7] = sp[7];
553 				hp += 8;
554 				sp += 8;
555 			}
556 		}
557 
558 		for (cnt = n1; cnt; cnt--) {
559 			*dp++ = *sp++;
560 			if (ri->ri_hwbits)
561 				*hp++ = *(sp - 1);
562 		}
563 	}
564 }
565 
566 /*
567  * Copy columns. This is slow, and hard to optimize due to alignment,
568  * and the fact that we have to copy both left->right and right->left.
569  * We simply cop-out here and use memmove(), since it handles all of
570  * these cases anyway.
571  */
572 void
573 rasops_copycols(cookie, row, src, dst, num)
574 	void *cookie;
575 	int row, src, dst, num;
576 {
577 	struct rasops_info *ri;
578 	u_char *sp, *dp, *hp;
579 	int height;
580 
581 	ri = (struct rasops_info *)cookie;
582 	hp = NULL;
583 
584 #ifdef RASOPS_CLIPPING
585 	if (dst == src)
586 		return;
587 
588 	/* Catches < 0 case too */
589 	if ((unsigned)row >= (unsigned)ri->ri_rows)
590 		return;
591 
592 	if (src < 0) {
593 		num += src;
594 		src = 0;
595 	}
596 
597 	if ((src + num) > ri->ri_cols)
598 		num = ri->ri_cols - src;
599 
600 	if (dst < 0) {
601 		num += dst;
602 		dst = 0;
603 	}
604 
605 	if ((dst + num) > ri->ri_cols)
606 		num = ri->ri_cols - dst;
607 
608 	if (num <= 0)
609 		return;
610 #endif
611 
612 	num *= ri->ri_xscale;
613 	row *= ri->ri_yscale;
614 	height = ri->ri_font->fontheight;
615 
616 	sp = ri->ri_bits + row + src * ri->ri_xscale;
617 	dp = ri->ri_bits + row + dst * ri->ri_xscale;
618 	if (ri->ri_hwbits)
619 		hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
620 
621 	while (height--) {
622 		memmove(dp, sp, num);
623 		if (ri->ri_hwbits) {
624 			memcpy(hp, sp, num);
625 			hp += ri->ri_stride;
626 		}
627 		dp += ri->ri_stride;
628 		sp += ri->ri_stride;
629 	}
630 }
631 
632 /*
633  * Turn cursor off/on.
634  */
635 static void
636 rasops_cursor(cookie, on, row, col)
637 	void *cookie;
638 	int on, row, col;
639 {
640 	struct rasops_info *ri;
641 
642 	ri = (struct rasops_info *)cookie;
643 
644 	/* Turn old cursor off */
645 	if ((ri->ri_flg & RI_CURSOR) != 0)
646 #ifdef RASOPS_CLIPPING
647 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
648 #endif
649 			ri->ri_do_cursor(ri);
650 
651 	/* Select new cursor */
652 #ifdef RASOPS_CLIPPING
653 	ri->ri_flg &= ~RI_CURSORCLIP;
654 
655 	if (row < 0 || row >= ri->ri_rows)
656 		ri->ri_flg |= RI_CURSORCLIP;
657 	else if (col < 0 || col >= ri->ri_cols)
658 		ri->ri_flg |= RI_CURSORCLIP;
659 #endif
660 	ri->ri_crow = row;
661 	ri->ri_ccol = col;
662 
663 	if (on) {
664 		ri->ri_flg |= RI_CURSOR;
665 #ifdef RASOPS_CLIPPING
666 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
667 #endif
668 			ri->ri_do_cursor(ri);
669 	} else
670 		ri->ri_flg &= ~RI_CURSOR;
671 }
672 
673 /*
674  * Make the device colormap
675  */
676 static void
677 rasops_init_devcmap(ri)
678 	struct rasops_info *ri;
679 {
680 	const u_char *p;
681 	int i, c;
682 
683 	switch (ri->ri_depth) {
684 	case 1:
685 		ri->ri_devcmap[0] = 0;
686 		for (i = 1; i < 16; i++)
687 			ri->ri_devcmap[i] = -1;
688 		return;
689 
690 	case 2:
691 		for (i = 1; i < 15; i++)
692 			ri->ri_devcmap[i] = 0xaaaaaaaa;
693 
694 		ri->ri_devcmap[0] = 0;
695 		ri->ri_devcmap[8] = 0x55555555;
696 		ri->ri_devcmap[15] = -1;
697 		return;
698 
699 	case 8:
700 		for (i = 0; i < 16; i++)
701 			ri->ri_devcmap[i] = i | (i<<8) | (i<<16) | (i<<24);
702 		return;
703 	}
704 
705 	p = rasops_cmap;
706 
707 	for (i = 0; i < 16; i++) {
708 		if (ri->ri_rnum <= 8)
709 			c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
710 		else
711 			c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
712 		p++;
713 
714 		if (ri->ri_gnum <= 8)
715 			c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
716 		else
717 			c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
718 		p++;
719 
720 		if (ri->ri_bnum <= 8)
721 			c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
722 		else
723 			c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
724 		p++;
725 
726 		/* Fill the word for generic routines, which want this */
727 		if (ri->ri_depth == 24)
728 			c = c | ((c & 0xff) << 24);
729 		else if (ri->ri_depth <= 16)
730 			c = c | (c << 16);
731 
732 		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
733 		if ((ri->ri_flg & RI_BSWAP) == 0)
734 			ri->ri_devcmap[i] = c;
735 		else if (ri->ri_depth == 32)
736 			ri->ri_devcmap[i] = bswap32(c);
737 		else if (ri->ri_depth == 16 || ri->ri_depth == 15)
738 			ri->ri_devcmap[i] = bswap16(c);
739 		else
740 			ri->ri_devcmap[i] = c;
741 	}
742 }
743 
744 /*
745  * Unpack a rasops attribute
746  */
747 void
748 rasops_unpack_attr(attr, fg, bg, underline)
749 	long attr;
750 	int *fg, *bg, *underline;
751 {
752 
753 	*fg = ((u_int)attr >> 24) & 0xf;
754 	*bg = ((u_int)attr >> 16) & 0xf;
755 	if (underline != NULL)
756 		*underline = (u_int)attr & 1;
757 }
758 
759 /*
760  * Erase rows. This isn't static, since 24-bpp uses it in special cases.
761  */
762 void
763 rasops_eraserows(cookie, row, num, attr)
764 	void *cookie;
765 	int row, num;
766 	long attr;
767 {
768 	struct rasops_info *ri;
769 	int np, nw, cnt, delta;
770 	int32_t *dp, *hp, clr;
771 	int i;
772 
773 	ri = (struct rasops_info *)cookie;
774 	hp = NULL;
775 
776 #ifdef RASOPS_CLIPPING
777 	if (row < 0) {
778 		num += row;
779 		row = 0;
780 	}
781 
782 	if ((row + num) > ri->ri_rows)
783 		num = ri->ri_rows - row;
784 
785 	if (num <= 0)
786 		return;
787 #endif
788 
789 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
790 
791 	/*
792 	 * XXX The wsdisplay_emulops interface seems a little deficient in
793 	 * that there is no way to clear the *entire* screen. We provide a
794 	 * workaround here: if the entire console area is being cleared, and
795 	 * the RI_FULLCLEAR flag is set, clear the entire display.
796 	 */
797 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
798 		np = ri->ri_stride >> 5;
799 		nw = (ri->ri_stride >> 2) & 7;
800 		num = ri->ri_height;
801 		dp = (int32_t *)ri->ri_origbits;
802 		if (ri->ri_hwbits)
803 			hp = (int32_t *)ri->ri_hwbits;
804 		delta = 0;
805 	} else {
806 		np = ri->ri_emustride >> 5;
807 		nw = (ri->ri_emustride >> 2) & 7;
808 		num *= ri->ri_font->fontheight;
809 		dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
810 		if (ri->ri_hwbits)
811 			hp = (int32_t *)(ri->ri_hwbits + row *
812 			    ri->ri_yscale);
813 		delta = ri->ri_delta;
814 	}
815 
816 	while (num--) {
817 		for (cnt = np; cnt; cnt--) {
818 			for (i = 0; i < 8; i++) {
819 				dp[i] = clr;
820 				if (ri->ri_hwbits)
821 					hp[i] = clr;
822 			}
823 			dp += 8;
824 			if (ri->ri_hwbits)
825 				hp += 8;
826 		}
827 
828 		for (cnt = nw; cnt; cnt--) {
829 			*(int32_t *)dp = clr;
830 			DELTA(dp, 4, int32_t *);
831 			if (ri->ri_hwbits) {
832 				*(int32_t *)hp = clr;
833 				DELTA(hp, 4, int32_t *);
834 			}
835 		}
836 
837 		DELTA(dp, delta, int32_t *);
838 		if (ri->ri_hwbits)
839 			DELTA(hp, delta, int32_t *);
840 	}
841 }
842 
843 /*
844  * Actually turn the cursor on or off. This does the dirty work for
845  * rasops_cursor().
846  */
847 static void
848 rasops_do_cursor(ri)
849 	struct rasops_info *ri;
850 {
851 	int full1, height, cnt, slop1, slop2, row, col;
852 	u_char *dp, *rp, *hrp, *hp;
853 
854 	hrp = hp = NULL;
855 
856 	row = ri->ri_crow;
857 	col = ri->ri_ccol;
858 
859 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
860 	if (ri->ri_hwbits)
861 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col
862 		    * ri->ri_xscale;
863 	height = ri->ri_font->fontheight;
864 	slop1 = (4 - ((long)rp & 3)) & 3;
865 
866 	if (slop1 > ri->ri_xscale)
867 		slop1 = ri->ri_xscale;
868 
869 	slop2 = (ri->ri_xscale - slop1) & 3;
870 	full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
871 
872 	if ((slop1 | slop2) == 0) {
873 		/* A common case */
874 		while (height--) {
875 			dp = rp;
876 			rp += ri->ri_stride;
877 			if (ri->ri_hwbits) {
878 				hp = hrp;
879 				hrp += ri->ri_stride;
880 			}
881 
882 			for (cnt = full1; cnt; cnt--) {
883 				*(int32_t *)dp ^= ~0;
884 				dp += 4;
885 				if (ri->ri_hwbits) {
886 					*(int32_t *)hp ^= ~0;
887 					hp += 4;
888 				}
889 			}
890 		}
891 	} else {
892 		/* XXX this is stupid.. use masks instead */
893 		while (height--) {
894 			dp = rp;
895 			rp += ri->ri_stride;
896 			if (ri->ri_hwbits) {
897 				hp = hrp;
898 				hrp += ri->ri_stride;
899 			}
900 
901 			if (slop1 & 1) {
902 				*dp++ ^= ~0;
903 				if (ri->ri_hwbits)
904 					*hp++ ^= ~0;
905 			}
906 
907 			if (slop1 & 2) {
908 				*(int16_t *)dp ^= ~0;
909 				dp += 2;
910 				if (ri->ri_hwbits) {
911 					*(int16_t *)hp ^= ~0;
912 					hp += 2;
913 				}
914 			}
915 
916 			for (cnt = full1; cnt; cnt--) {
917 				*(int32_t *)dp ^= ~0;
918 				dp += 4;
919 				if (ri->ri_hwbits) {
920 					*(int32_t *)hp ^= ~0;
921 					hp += 4;
922 				}
923 			}
924 
925 			if (slop2 & 1) {
926 				*dp++ ^= ~0;
927 				if (ri->ri_hwbits)
928 					*hp++ ^= ~0;
929 			}
930 
931 			if (slop2 & 2) {
932 				*(int16_t *)dp ^= ~0;
933 				if (ri->ri_hwbits)
934 					*(int16_t *)hp ^= ~0;
935 			}
936 		}
937 	}
938 }
939 
940 /*
941  * Erase columns.
942  */
943 void
944 rasops_erasecols(cookie, row, col, num, attr)
945 	void *cookie;
946 	int row, col, num;
947 	long attr;
948 {
949 	int n8, height, cnt, slop1, slop2, clr;
950 	struct rasops_info *ri;
951 	int32_t *rp, *dp, *hrp, *hp;
952 	int i;
953 
954 	ri = (struct rasops_info *)cookie;
955 	hrp = hp = NULL;
956 
957 #ifdef RASOPS_CLIPPING
958 	if ((unsigned)row >= (unsigned)ri->ri_rows)
959 		return;
960 
961 	if (col < 0) {
962 		num += col;
963 		col = 0;
964 	}
965 
966 	if ((col + num) > ri->ri_cols)
967 		num = ri->ri_cols - col;
968 
969 	if (num <= 0)
970 		return;
971 #endif
972 
973 	num = num * ri->ri_xscale;
974 	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
975 	if (ri->ri_hwbits)
976 		hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
977 		    col*ri->ri_xscale);
978 	height = ri->ri_font->fontheight;
979 	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
980 
981 	/* Don't bother using the full loop for <= 32 pels */
982 	if (num <= 32) {
983 		if (((num | ri->ri_xscale) & 3) == 0) {
984 			/* Word aligned blt */
985 			num >>= 2;
986 
987 			while (height--) {
988 				dp = rp;
989 				DELTA(rp, ri->ri_stride, int32_t *);
990 				if (ri->ri_hwbits) {
991 					hp = hrp;
992 					DELTA(hrp, ri->ri_stride, int32_t *);
993 				}
994 
995 				for (cnt = num; cnt; cnt--) {
996 					*dp++ = clr;
997 					if (ri->ri_hwbits)
998 						*hp++ = clr;
999 				}
1000 			}
1001 		} else if (((num | ri->ri_xscale) & 1) == 0) {
1002 			/*
1003 			 * Halfword aligned blt. This is needed so the
1004 			 * 15/16 bit ops can use this function.
1005 			 */
1006 			num >>= 1;
1007 
1008 			while (height--) {
1009 				dp = rp;
1010 				DELTA(rp, ri->ri_stride, int32_t *);
1011 				if (ri->ri_hwbits) {
1012 					hp = hrp;
1013 					DELTA(hrp, ri->ri_stride, int32_t *);
1014 				}
1015 
1016 				for (cnt = num; cnt; cnt--) {
1017 					*(int16_t *)dp = clr;
1018 					DELTA(dp, 2, int32_t *);
1019 					if (ri->ri_hwbits) {
1020 						*(int16_t *)hp = clr;
1021 						DELTA(hp, 2, int32_t *);
1022 					}
1023 				}
1024 			}
1025 		} else {
1026 			while (height--) {
1027 				dp = rp;
1028 				DELTA(rp, ri->ri_stride, int32_t *);
1029 				if (ri->ri_hwbits) {
1030 					hp = hrp;
1031 					DELTA(hrp, ri->ri_stride, int32_t *);
1032 				}
1033 
1034 				for (cnt = num; cnt; cnt--) {
1035 					*(u_char *)dp = clr;
1036 					DELTA(dp, 1, int32_t *);
1037 					if (ri->ri_hwbits) {
1038 						*(u_char *)hp = clr;
1039 						DELTA(hp, 1, int32_t *);
1040 					}
1041 				}
1042 			}
1043 		}
1044 
1045 		return;
1046 	}
1047 
1048 	slop1 = (4 - ((long)rp & 3)) & 3;
1049 	slop2 = (num - slop1) & 3;
1050 	num -= slop1 + slop2;
1051 	n8 = num >> 5;
1052 	num = (num >> 2) & 7;
1053 
1054 	while (height--) {
1055 		dp = rp;
1056 		DELTA(rp, ri->ri_stride, int32_t *);
1057 		if (ri->ri_hwbits) {
1058 			hp = hrp;
1059 			DELTA(hrp, ri->ri_stride, int32_t *);
1060 		}
1061 
1062 		/* Align span to 4 bytes */
1063 		if (slop1 & 1) {
1064 			*(u_char *)dp = clr;
1065 			DELTA(dp, 1, int32_t *);
1066 			if (ri->ri_hwbits) {
1067 				*(u_char *)hp = clr;
1068 				DELTA(hp, 1, int32_t *);
1069 			}
1070 		}
1071 
1072 		if (slop1 & 2) {
1073 			*(int16_t *)dp = clr;
1074 			DELTA(dp, 2, int32_t *);
1075 			if (ri->ri_hwbits) {
1076 				*(int16_t *)hp = clr;
1077 				DELTA(hp, 2, int32_t *);
1078 			}
1079 		}
1080 
1081 		/* Write 32 bytes per loop */
1082 		for (cnt = n8; cnt; cnt--) {
1083 			for (i = 0; i < 8; i++) {
1084 				dp[i] = clr;
1085 				if (ri->ri_hwbits)
1086 					hp[i] = clr;
1087 			}
1088 			dp += 8;
1089 			if (ri->ri_hwbits)
1090 				hp += 8;
1091 		}
1092 
1093 		/* Write 4 bytes per loop */
1094 		for (cnt = num; cnt; cnt--) {
1095 			*dp++ = clr;
1096 			if (ri->ri_hwbits)
1097 				*hp++ = clr;
1098 		}
1099 
1100 		/* Write unaligned trailing slop */
1101 		if (slop2 & 1) {
1102 			*(u_char *)dp = clr;
1103 			DELTA(dp, 1, int32_t *);
1104 			if (ri->ri_hwbits) {
1105 				*(u_char *)hp = clr;
1106 				DELTA(hp, 1, int32_t *);
1107 			}
1108 		}
1109 
1110 		if (slop2 & 2) {
1111 			*(int16_t *)dp = clr;
1112 			if (ri->ri_hwbits)
1113 				*(int16_t *)hp = clr;
1114 		}
1115 	}
1116 }
1117