rasops.c revision 1.35
1/*	 $NetBSD: rasops.c,v 1.35 2001/02/02 06:01:01 marcus 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.35 2001/02/02 06:01:01 marcus Exp $");
41
42#include "opt_rasops.h"
43#include "rasops_glue.h"
44
45#include <sys/types.h>
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/time.h>
49
50#include <machine/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 */
63const 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 _CMWHITE16
95#undef _CMWHITE16
96#undef _CMWHITE
97};
98
99/* True if color is gray */
100const u_char rasops_isgray[16] = {
101	1, 0, 0, 0,
102	0, 0, 0, 1,
103	1, 0, 0, 0,
104	0, 0, 0, 1
105};
106
107/* Generic functions */
108static void	rasops_copyrows __P((void *, int, int, int));
109static int	rasops_mapchar __P((void *, int, u_int *));
110static void	rasops_cursor __P((void *, int, int, int));
111static int	rasops_alloc_cattr __P((void *, int, int, int, long *));
112static int	rasops_alloc_mattr __P((void *, int, int, int, long *));
113static void	rasops_do_cursor __P((struct rasops_info *));
114static void	rasops_init_devcmap __P((struct rasops_info *));
115
116/*
117 * Initalize a 'rasops_info' descriptor.
118 */
119int
120rasops_init(ri, wantrows, wantcols)
121	struct rasops_info *ri;
122	int wantrows, wantcols;
123{
124
125#ifdef _KERNEL
126	/* Select a font if the caller doesn't care */
127	if (ri->ri_font == NULL) {
128		int cookie;
129
130		wsfont_init();
131
132		/* Want 8 pixel wide, don't care about aestethics */
133		if ((cookie = wsfont_find(NULL, 8, 0, 0)) <= 0)
134			cookie = wsfont_find(NULL, 0, 0, 0);
135
136		if (cookie <= 0) {
137			printf("rasops_init: font table is empty\n");
138			return (-1);
139		}
140
141		if (wsfont_lock(cookie, &ri->ri_font,
142		    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R) <= 0) {
143			printf("rasops_init: couldn't lock font\n");
144			return (-1);
145		}
146
147		ri->ri_wsfcookie = cookie;
148	}
149#endif
150
151	/* This should never happen in reality... */
152#ifdef DEBUG
153	if ((long)ri->ri_bits & 3) {
154		printf("rasops_init: bits not aligned on 32-bit boundary\n");
155		return (-1);
156	}
157
158	if ((int)ri->ri_stride & 3) {
159		printf("rasops_init: stride not aligned on 32-bit boundary\n");
160		return (-1);
161	}
162#endif
163
164	if (rasops_reconfig(ri, wantrows, wantcols))
165		return (-1);
166
167	rasops_init_devcmap(ri);
168	return (0);
169}
170
171/*
172 * Reconfigure (because parameters have changed in some way).
173 */
174int
175rasops_reconfig(ri, wantrows, wantcols)
176	struct rasops_info *ri;
177	int wantrows, wantcols;
178{
179	int bpp, s;
180
181	s = splhigh();
182
183	if (ri->ri_font->fontwidth > 32 || ri->ri_font->fontwidth < 4)
184		panic("rasops_init: fontwidth assumptions botched!\n");
185
186	/* Need this to frob the setup below */
187	bpp = (ri->ri_depth == 15 ? 16 : ri->ri_depth);
188
189	if ((ri->ri_flg & RI_CFGDONE) != 0)
190		ri->ri_bits = ri->ri_origbits;
191
192	/* Don't care if the caller wants a hideously small console */
193	if (wantrows < 10)
194		wantrows = 10;
195
196	if (wantcols < 20)
197		wantcols = 20;
198
199	/* Now constrain what they get */
200	ri->ri_emuwidth = ri->ri_font->fontwidth * wantcols;
201	ri->ri_emuheight = ri->ri_font->fontheight * wantrows;
202
203	if (ri->ri_emuwidth > ri->ri_width)
204		ri->ri_emuwidth = ri->ri_width;
205
206	if (ri->ri_emuheight > ri->ri_height)
207		ri->ri_emuheight = ri->ri_height;
208
209	/* Reduce width until aligned on a 32-bit boundary */
210	while ((ri->ri_emuwidth * bpp & 31) != 0)
211		ri->ri_emuwidth--;
212
213	ri->ri_cols = ri->ri_emuwidth / ri->ri_font->fontwidth;
214	ri->ri_rows = ri->ri_emuheight / ri->ri_font->fontheight;
215	ri->ri_emustride = ri->ri_emuwidth * bpp >> 3;
216	ri->ri_delta = ri->ri_stride - ri->ri_emustride;
217	ri->ri_ccol = 0;
218	ri->ri_crow = 0;
219	ri->ri_pelbytes = bpp >> 3;
220
221	ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
222	ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
223	ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
224
225#ifdef DEBUG
226	if ((ri->ri_delta & 3) != 0)
227		panic("rasops_init: ri_delta not aligned on 32-bit boundary");
228#endif
229	/* Clear the entire display */
230	if ((ri->ri_flg & RI_CLEAR) != 0)
231		memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
232
233	/* Now centre our window if needs be */
234	ri->ri_origbits = ri->ri_bits;
235
236	if ((ri->ri_flg & RI_CENTER) != 0) {
237		ri->ri_bits += ((ri->ri_width - ri->ri_emustride) >> 1) & ~3;
238		ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
239		    ri->ri_stride;
240
241		ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits)
242		   / ri->ri_stride;
243		ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits)
244		   % ri->ri_stride) * 8 / bpp);
245	} else
246		ri->ri_xorigin = ri->ri_yorigin = 0;
247
248	/*
249	 * Fill in defaults for operations set.  XXX this nukes private
250	 * routines used by accelerated fb drivers.
251	 */
252	ri->ri_ops.mapchar = rasops_mapchar;
253	ri->ri_ops.copyrows = rasops_copyrows;
254	ri->ri_ops.copycols = rasops_copycols;
255	ri->ri_ops.erasecols = rasops_erasecols;
256	ri->ri_ops.eraserows = rasops_eraserows;
257	ri->ri_ops.cursor = rasops_cursor;
258	ri->ri_do_cursor = rasops_do_cursor;
259
260	if (ri->ri_depth < 8 || (ri->ri_flg & RI_FORCEMONO) != 0) {
261		ri->ri_ops.alloc_attr = rasops_alloc_mattr;
262		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
263	} else {
264		ri->ri_ops.alloc_attr = rasops_alloc_cattr;
265		ri->ri_caps = WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
266		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
267	}
268
269	switch (ri->ri_depth) {
270#if NRASOPS1 > 0
271	case 1:
272		rasops1_init(ri);
273		break;
274#endif
275#if NRASOPS2 > 0
276	case 2:
277		rasops2_init(ri);
278		break;
279#endif
280#if NRASOPS4 > 0
281	case 4:
282		rasops4_init(ri);
283		break;
284#endif
285#if NRASOPS8 > 0
286	case 8:
287		rasops8_init(ri);
288		break;
289#endif
290#if NRASOPS15 > 0 || NRASOPS16 > 0
291	case 15:
292	case 16:
293		rasops15_init(ri);
294		break;
295#endif
296#if NRASOPS24 > 0
297	case 24:
298		rasops24_init(ri);
299		break;
300#endif
301#if NRASOPS32 > 0
302	case 32:
303		rasops32_init(ri);
304		break;
305#endif
306	default:
307		ri->ri_flg &= ~RI_CFGDONE;
308		splx(s);
309		return (-1);
310	}
311
312	ri->ri_flg |= RI_CFGDONE;
313	splx(s);
314	return (0);
315}
316
317/*
318 * Map a character.
319 */
320static int
321rasops_mapchar(cookie, c, cp)
322	void *cookie;
323	int c;
324	u_int *cp;
325{
326	struct rasops_info *ri;
327
328	ri = (struct rasops_info *)cookie;
329
330#ifdef DIAGNOSTIC
331	if (ri->ri_font == NULL)
332		panic("rasops_mapchar: no font selected\n");
333#endif
334	if (ri->ri_font->encoding != WSDISPLAY_FONTENC_ISO) {
335
336		if ( (c = wsfont_map_unichar(ri->ri_font, c)) < 0) {
337
338			*cp = ' ';
339			return (0);
340
341		}
342	}
343
344
345	if (c < ri->ri_font->firstchar) {
346		*cp = ' ';
347		return (0);
348	}
349
350	if (c - ri->ri_font->firstchar >= ri->ri_font->numchars) {
351		*cp = ' ';
352		return (0);
353	}
354
355	*cp = c;
356	return (5);
357}
358
359/*
360 * Allocate a color attribute.
361 */
362static int
363rasops_alloc_cattr(cookie, fg, bg, flg, attr)
364	void *cookie;
365	int fg, bg, flg;
366	long *attr;
367{
368	int swap;
369
370#ifdef RASOPS_CLIPPING
371	fg &= 7;
372	bg &= 7;
373#endif
374	if ((flg & WSATTR_BLINK) != 0)
375		return (EINVAL);
376
377	if ((flg & WSATTR_WSCOLORS) == 0) {
378		fg = WSCOL_WHITE;
379		bg = WSCOL_BLACK;
380	}
381
382	if ((flg & WSATTR_REVERSE) != 0) {
383		swap = fg;
384		fg = bg;
385		bg = swap;
386	}
387
388	if ((flg & WSATTR_HILIT) != 0)
389		fg += 8;
390
391	flg = ((flg & WSATTR_UNDERLINE) ? 1 : 0);
392
393	if (rasops_isgray[fg])
394		flg |= 2;
395
396	if (rasops_isgray[bg])
397		flg |= 4;
398
399	*attr = (bg << 16) | (fg << 24) | flg;
400	return (0);
401}
402
403/*
404 * Allocate a mono attribute.
405 */
406static int
407rasops_alloc_mattr(cookie, fg, bg, flg, attr)
408	void *cookie;
409	int fg, bg, flg;
410	long *attr;
411{
412	int swap;
413
414	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
415		return (EINVAL);
416
417	fg = 1;
418	bg = 0;
419
420	if ((flg & WSATTR_REVERSE) != 0) {
421		swap = fg;
422		fg = bg;
423		bg = swap;
424	}
425
426	*attr = (bg << 16) | (fg << 24) | ((flg & WSATTR_UNDERLINE) ? 7 : 6);
427	return (0);
428}
429
430/*
431 * Copy rows.
432 */
433static void
434rasops_copyrows(cookie, src, dst, num)
435	void *cookie;
436	int src, dst, num;
437{
438	int32_t *sp, *dp, *srp, *drp;
439	struct rasops_info *ri;
440	int n8, n1, cnt, delta;
441
442	ri = (struct rasops_info *)cookie;
443
444#ifdef RASOPS_CLIPPING
445	if (dst == src)
446		return;
447
448	if (src < 0) {
449		num += src;
450		src = 0;
451	}
452
453	if ((src + num) > ri->ri_rows)
454		num = ri->ri_rows - src;
455
456	if (dst < 0) {
457		num += dst;
458		dst = 0;
459	}
460
461	if ((dst + num) > ri->ri_rows)
462		num = ri->ri_rows - dst;
463
464	if (num <= 0)
465		return;
466#endif
467
468	num *= ri->ri_font->fontheight;
469	n8 = ri->ri_emustride >> 5;
470	n1 = (ri->ri_emustride >> 2) & 7;
471
472	if (dst < src) {
473		srp = (int32_t *)(ri->ri_bits + src * ri->ri_yscale);
474		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_yscale);
475		delta = ri->ri_stride;
476	} else {
477		src = ri->ri_font->fontheight * src + num - 1;
478		dst = ri->ri_font->fontheight * dst + num - 1;
479		srp = (int32_t *)(ri->ri_bits + src * ri->ri_stride);
480		drp = (int32_t *)(ri->ri_bits + dst * ri->ri_stride);
481		delta = -ri->ri_stride;
482	}
483
484	while (num--) {
485		dp = drp;
486		sp = srp;
487		DELTA(drp, delta, int32_t *);
488		DELTA(srp, delta, int32_t *);
489
490		for (cnt = n8; cnt; cnt--) {
491			dp[0] = sp[0];
492			dp[1] = sp[1];
493			dp[2] = sp[2];
494			dp[3] = sp[3];
495			dp[4] = sp[4];
496			dp[5] = sp[5];
497			dp[6] = sp[6];
498			dp[7] = sp[7];
499			dp += 8;
500			sp += 8;
501		}
502
503		for (cnt = n1; cnt; cnt--)
504			*dp++ = *sp++;
505	}
506}
507
508/*
509 * Copy columns. This is slow, and hard to optimize due to alignment,
510 * and the fact that we have to copy both left->right and right->left.
511 * We simply cop-out here and use bcopy(), since it handles all of
512 * these cases anyway.
513 */
514void
515rasops_copycols(cookie, row, src, dst, num)
516	void *cookie;
517	int row, src, dst, num;
518{
519	struct rasops_info *ri;
520	u_char *sp, *dp;
521	int height;
522
523	ri = (struct rasops_info *)cookie;
524
525#ifdef RASOPS_CLIPPING
526	if (dst == src)
527		return;
528
529	/* Catches < 0 case too */
530	if ((unsigned)row >= (unsigned)ri->ri_rows)
531		return;
532
533	if (src < 0) {
534		num += src;
535		src = 0;
536	}
537
538	if ((src + num) > ri->ri_cols)
539		num = ri->ri_cols - src;
540
541	if (dst < 0) {
542		num += dst;
543		dst = 0;
544	}
545
546	if ((dst + num) > ri->ri_cols)
547		num = ri->ri_cols - dst;
548
549	if (num <= 0)
550		return;
551#endif
552
553	num *= ri->ri_xscale;
554	row *= ri->ri_yscale;
555	height = ri->ri_font->fontheight;
556
557	sp = ri->ri_bits + row + src * ri->ri_xscale;
558	dp = ri->ri_bits + row + dst * ri->ri_xscale;
559
560	while (height--) {
561		bcopy(sp, dp, num);
562		dp += ri->ri_stride;
563		sp += ri->ri_stride;
564	}
565}
566
567/*
568 * Turn cursor off/on.
569 */
570static void
571rasops_cursor(cookie, on, row, col)
572	void *cookie;
573	int on, row, col;
574{
575	struct rasops_info *ri;
576
577	ri = (struct rasops_info *)cookie;
578
579	/* Turn old cursor off */
580	if ((ri->ri_flg & RI_CURSOR) != 0)
581#ifdef RASOPS_CLIPPING
582		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
583#endif
584			ri->ri_do_cursor(ri);
585
586	/* Select new cursor */
587#ifdef RASOPS_CLIPPING
588	ri->ri_flg &= ~RI_CURSORCLIP;
589
590	if (row < 0 || row >= ri->ri_rows)
591		ri->ri_flg |= RI_CURSORCLIP;
592	else if (col < 0 || col >= ri->ri_cols)
593		ri->ri_flg |= RI_CURSORCLIP;
594#endif
595	ri->ri_crow = row;
596	ri->ri_ccol = col;
597
598	if (on) {
599		ri->ri_flg |= RI_CURSOR;
600#ifdef RASOPS_CLIPPING
601		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
602#endif
603			ri->ri_do_cursor(ri);
604	} else
605		ri->ri_flg &= ~RI_CURSOR;
606}
607
608/*
609 * Make the device colormap
610 */
611static void
612rasops_init_devcmap(ri)
613	struct rasops_info *ri;
614{
615	const u_char *p;
616	int i, c;
617
618	switch (ri->ri_depth) {
619	case 1:
620		ri->ri_devcmap[0] = 0;
621		for (i = 1; i < 16; i++)
622			ri->ri_devcmap[i] = -1;
623		return;
624
625	case 2:
626		for (i = 1; i < 15; i++)
627			ri->ri_devcmap[i] = 0xaaaaaaaa;
628
629		ri->ri_devcmap[0] = 0;
630		ri->ri_devcmap[8] = 0x55555555;
631		ri->ri_devcmap[15] = -1;
632		return;
633
634	case 8:
635		for (i = 0; i < 16; i++)
636			ri->ri_devcmap[i] = i | (i<<8) | (i<<16) | (i<<24);
637		return;
638	}
639
640	p = rasops_cmap;
641
642	for (i = 0; i < 16; i++) {
643		if (ri->ri_rnum <= 8)
644			c = (*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
645		else
646			c = (*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
647		p++;
648
649		if (ri->ri_gnum <= 8)
650			c |= (*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
651		else
652			c |= (*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
653		p++;
654
655		if (ri->ri_bnum <= 8)
656			c |= (*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
657		else
658			c |= (*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
659		p++;
660
661		/* Fill the word for generic routines, which want this */
662		if (ri->ri_depth == 24)
663			c = c | ((c & 0xff) << 24);
664		else if (ri->ri_depth <= 16)
665			c = c | (c << 16);
666
667		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
668		if ((ri->ri_flg & RI_BSWAP) == 0)
669			ri->ri_devcmap[i] = c;
670		else if (ri->ri_depth == 32)
671			ri->ri_devcmap[i] = bswap32(c);
672		else if (ri->ri_depth == 16 || ri->ri_depth == 15)
673			ri->ri_devcmap[i] = bswap16(c);
674		else
675			ri->ri_devcmap[i] = c;
676	}
677}
678
679/*
680 * Unpack a rasops attribute
681 */
682void
683rasops_unpack_attr(attr, fg, bg, underline)
684	long attr;
685	int *fg, *bg, *underline;
686{
687
688	*fg = ((u_int)attr >> 24) & 0xf;
689	*bg = ((u_int)attr >> 16) & 0xf;
690	if (underline != NULL)
691		*underline = (u_int)attr & 1;
692}
693
694/*
695 * Erase rows. This isn't static, since 24-bpp uses it in special cases.
696 */
697void
698rasops_eraserows(cookie, row, num, attr)
699	void *cookie;
700	int row, num;
701	long attr;
702{
703	struct rasops_info *ri;
704	int np, nw, cnt, delta;
705	int32_t *dp, clr;
706
707	ri = (struct rasops_info *)cookie;
708
709#ifdef RASOPS_CLIPPING
710	if (row < 0) {
711		num += row;
712		row = 0;
713	}
714
715	if ((row + num) > ri->ri_rows)
716		num = ri->ri_rows - row;
717
718	if (num <= 0)
719		return;
720#endif
721
722	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
723
724	/*
725	 * XXX The wsdisplay_emulops interface seems a little deficient in
726	 * that there is no way to clear the *entire* screen. We provide a
727	 * workaround here: if the entire console area is being cleared, and
728	 * the RI_FULLCLEAR flag is set, clear the entire display.
729	 */
730	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
731		np = ri->ri_stride >> 5;
732		nw = (ri->ri_stride >> 2) & 7;
733		num = ri->ri_height;
734		dp = (int32_t *)ri->ri_origbits;
735		delta = 0;
736	} else {
737		np = ri->ri_emustride >> 5;
738		nw = (ri->ri_emustride >> 2) & 7;
739		num *= ri->ri_font->fontheight;
740		dp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale);
741		delta = ri->ri_delta;
742	}
743
744	while (num--) {
745		for (cnt = np; cnt; cnt--) {
746			dp[0] = clr;
747			dp[1] = clr;
748			dp[2] = clr;
749			dp[3] = clr;
750			dp[4] = clr;
751			dp[5] = clr;
752			dp[6] = clr;
753			dp[7] = clr;
754			dp += 8;
755		}
756
757		for (cnt = nw; cnt; cnt--) {
758			*(int32_t *)dp = clr;
759			DELTA(dp, 4, int32_t *);
760		}
761
762		DELTA(dp, delta, int32_t *);
763	}
764}
765
766/*
767 * Actually turn the cursor on or off. This does the dirty work for
768 * rasops_cursor().
769 */
770static void
771rasops_do_cursor(ri)
772	struct rasops_info *ri;
773{
774	int full1, height, cnt, slop1, slop2, row, col;
775	u_char *dp, *rp;
776
777	row = ri->ri_crow;
778	col = ri->ri_ccol;
779
780	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
781	height = ri->ri_font->fontheight;
782	slop1 = (4 - ((long)rp & 3)) & 3;
783
784	if (slop1 > ri->ri_xscale)
785		slop1 = ri->ri_xscale;
786
787	slop2 = (ri->ri_xscale - slop1) & 3;
788	full1 = (ri->ri_xscale - slop1 - slop2) >> 2;
789
790	if ((slop1 | slop2) == 0) {
791		/* A common case */
792		while (height--) {
793			dp = rp;
794			rp += ri->ri_stride;
795
796			for (cnt = full1; cnt; cnt--) {
797				*(int32_t *)dp ^= ~0;
798				dp += 4;
799			}
800		}
801	} else {
802		/* XXX this is stupid.. use masks instead */
803		while (height--) {
804			dp = rp;
805			rp += ri->ri_stride;
806
807			if (slop1 & 1)
808				*dp++ ^= ~0;
809
810			if (slop1 & 2) {
811				*(int16_t *)dp ^= ~0;
812				dp += 2;
813			}
814
815			for (cnt = full1; cnt; cnt--) {
816				*(int32_t *)dp ^= ~0;
817				dp += 4;
818			}
819
820			if (slop2 & 1)
821				*dp++ ^= ~0;
822
823			if (slop2 & 2)
824				*(int16_t *)dp ^= ~0;
825		}
826	}
827}
828
829/*
830 * Erase columns.
831 */
832void
833rasops_erasecols(cookie, row, col, num, attr)
834	void *cookie;
835	int row, col, num;
836	long attr;
837{
838	int n8, height, cnt, slop1, slop2, clr;
839	struct rasops_info *ri;
840	int32_t *rp, *dp;
841
842	ri = (struct rasops_info *)cookie;
843
844#ifdef RASOPS_CLIPPING
845	if ((unsigned)row >= (unsigned)ri->ri_rows)
846		return;
847
848	if (col < 0) {
849		num += col;
850		col = 0;
851	}
852
853	if ((col + num) > ri->ri_cols)
854		num = ri->ri_cols - col;
855
856	if (num <= 0)
857		return;
858#endif
859
860	num = num * ri->ri_xscale;
861	rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
862	height = ri->ri_font->fontheight;
863	clr = ri->ri_devcmap[(attr >> 16) & 0xf];
864
865	/* Don't bother using the full loop for <= 32 pels */
866	if (num <= 32) {
867		if (((num | ri->ri_xscale) & 3) == 0) {
868			/* Word aligned blt */
869			num >>= 2;
870
871			while (height--) {
872				dp = rp;
873				DELTA(rp, ri->ri_stride, int32_t *);
874
875				for (cnt = num; cnt; cnt--)
876					*dp++ = clr;
877			}
878		} else if (((num | ri->ri_xscale) & 1) == 0) {
879			/*
880			 * Halfword aligned blt. This is needed so the
881			 * 15/16 bit ops can use this function.
882			 */
883			num >>= 1;
884
885			while (height--) {
886				dp = rp;
887				DELTA(rp, ri->ri_stride, int32_t *);
888
889				for (cnt = num; cnt; cnt--) {
890					*(int16_t *)dp = clr;
891					DELTA(dp, 2, int32_t *);
892				}
893			}
894		} else {
895			while (height--) {
896				dp = rp;
897				DELTA(rp, ri->ri_stride, int32_t *);
898
899				for (cnt = num; cnt; cnt--) {
900					*(u_char *)dp = clr;
901					DELTA(dp, 1, int32_t *);
902				}
903			}
904		}
905
906		return;
907	}
908
909	slop1 = (4 - ((long)rp & 3)) & 3;
910	slop2 = (num - slop1) & 3;
911	num -= slop1 + slop2;
912	n8 = num >> 5;
913	num = (num >> 2) & 7;
914
915	while (height--) {
916		dp = rp;
917		DELTA(rp, ri->ri_stride, int32_t *);
918
919		/* Align span to 4 bytes */
920		if (slop1 & 1) {
921			*(u_char *)dp = clr;
922			DELTA(dp, 1, int32_t *);
923		}
924
925		if (slop1 & 2) {
926			*(int16_t *)dp = clr;
927			DELTA(dp, 2, int32_t *);
928		}
929
930		/* Write 32 bytes per loop */
931		for (cnt = n8; cnt; cnt--) {
932			dp[0] = clr;
933			dp[1] = clr;
934			dp[2] = clr;
935			dp[3] = clr;
936			dp[4] = clr;
937			dp[5] = clr;
938			dp[6] = clr;
939			dp[7] = clr;
940			dp += 8;
941		}
942
943		/* Write 4 bytes per loop */
944		for (cnt = num; cnt; cnt--)
945			*dp++ = clr;
946
947		/* Write unaligned trailing slop */
948		if (slop2 & 1) {
949			*(u_char *)dp = clr;
950			DELTA(dp, 1, int32_t *);
951		}
952
953		if (slop2 & 2)
954			*(int16_t *)dp = clr;
955	}
956}
957