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