Home | History | Annotate | Line # | Download | only in rasops
rasops.c revision 1.107
      1 /*	 $NetBSD: rasops.c,v 1.107 2019/08/02 04:22:04 rin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: rasops.c,v 1.107 2019/08/02 04:22:04 rin Exp $");
     34 
     35 #include "opt_rasops.h"
     36 #include "rasops_glue.h"
     37 #include "opt_wsmsgattrs.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/bswap.h>
     41 #include <sys/kmem.h>
     42 #include <sys/systm.h>
     43 #include <sys/time.h>
     44 
     45 #include <machine/endian.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wsfont/wsfont.h>
     50 
     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_ccol = 0;
    421 	ri->ri_crow = 0;
    422 	ri->ri_pelbytes = bpp >> 3;
    423 
    424 	ri->ri_xscale = (ri->ri_font->fontwidth * bpp) >> 3;
    425 	ri->ri_yscale = ri->ri_font->fontheight * ri->ri_stride;
    426 	ri->ri_fontscale = ri->ri_font->fontheight * ri->ri_font->stride;
    427 
    428 	ri->ri_origbits = ri->ri_bits;
    429 	ri->ri_hworigbits = ri->ri_hwbits;
    430 
    431 	/* Clear the entire display */
    432 	if ((ri->ri_flg & RI_CLEAR) != 0)
    433 		memset(ri->ri_bits, 0, ri->ri_stride * ri->ri_height);
    434 
    435 	/* Now centre our window if needs be */
    436 	if ((ri->ri_flg & RI_CENTER) != 0) {
    437 		ri->ri_bits += (((ri->ri_width * bpp >> 3) -
    438 		    ri->ri_emustride) >> 1) & ~3;
    439 		ri->ri_bits += ((ri->ri_height - ri->ri_emuheight) >> 1) *
    440 		    ri->ri_stride;
    441 		if (ri->ri_hwbits != NULL) {
    442 			ri->ri_hwbits += (((ri->ri_width * bpp >> 3) -
    443 			    ri->ri_emustride) >> 1) & ~3;
    444 			ri->ri_hwbits +=
    445 			    ((ri->ri_height - ri->ri_emuheight) >> 1) *
    446 			    ri->ri_stride;
    447 		}
    448 		ri->ri_yorigin = (int)(ri->ri_bits - ri->ri_origbits) /
    449 		    ri->ri_stride;
    450 		ri->ri_xorigin = (((int)(ri->ri_bits - ri->ri_origbits) %
    451 		    ri->ri_stride) * 8 / bpp);
    452 	} else
    453 		ri->ri_xorigin = ri->ri_yorigin = 0;
    454 
    455 	/*
    456 	 * Fill in defaults for operations set.  XXX this nukes private
    457 	 * routines used by accelerated fb drivers.
    458 	 */
    459 	ri->ri_ops.mapchar = rasops_mapchar;
    460 	ri->ri_ops.copyrows = rasops_copyrows;
    461 	ri->ri_ops.copycols = rasops_copycols;
    462 	ri->ri_ops.erasecols = rasops_erasecols;
    463 	ri->ri_ops.eraserows = rasops_eraserows;
    464 	ri->ri_ops.cursor = rasops_cursor;
    465 	ri->ri_do_cursor = rasops_do_cursor;
    466 
    467 	ri->ri_caps &= ~(WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
    468 		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE);
    469 
    470 	if ((ri->ri_flg & RI_FORCEMONO) != 0 ||
    471 #ifdef RASOPS_APPLE_PALETTE
    472 	    ri->ri_depth == 1
    473 #else
    474 	    ri->ri_depth < 8
    475 #endif
    476 	) {
    477 		ri->ri_ops.allocattr = rasops_allocattr_mono;
    478 		ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_REVERSE;
    479 	} else {
    480 		ri->ri_ops.allocattr = rasops_allocattr_color;
    481 		ri->ri_caps |= WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
    482 		    WSSCREEN_WSCOLORS | WSSCREEN_REVERSE;
    483 	}
    484 
    485 	if (ri->ri_buf != NULL) {
    486 		kmem_free(ri->ri_buf, ri->ri_buflen);
    487 		ri->ri_buf = NULL;
    488 	}
    489 	len = (ri->ri_flg & RI_FULLCLEAR) ? ri->ri_stride : ri->ri_emustride;
    490 	ri->ri_buflen = len;
    491 	ri->ri_buf = kmem_alloc(len, KM_SLEEP);
    492 
    493 #ifndef RASOPS_SMALL
    494 	if (ri->ri_stamp != NULL) {
    495 		kmem_free(ri->ri_stamp, ri->ri_stamp_len);
    496 		ri->ri_stamp = NULL;
    497 	}
    498 #endif
    499 
    500 	switch (ri->ri_depth) {
    501 #if NRASOPS1 > 0
    502 	case 1:
    503 		rasops1_init(ri);
    504 		break;
    505 #endif
    506 #if NRASOPS2 > 0
    507 	case 2:
    508 		rasops2_init(ri);
    509 		break;
    510 #endif
    511 #if NRASOPS4 > 0
    512 	case 4:
    513 		rasops4_init(ri);
    514 		break;
    515 #endif
    516 #if NRASOPS8 > 0
    517 	case 8:
    518 		rasops8_init(ri);
    519 		break;
    520 #endif
    521 #if (NRASOPS15 + NRASOPS16) > 0
    522 	case 15:
    523 	case 16:
    524 		rasops15_init(ri);
    525 		break;
    526 #endif
    527 #if NRASOPS24 > 0
    528 	case 24:
    529 		rasops24_init(ri);
    530 		break;
    531 #endif
    532 #if NRASOPS32 > 0
    533 	case 32:
    534 		rasops32_init(ri);
    535 		break;
    536 #endif
    537 	default:
    538 		ri->ri_flg &= ~RI_CFGDONE;
    539 		aprint_error("%s: depth not supported\n", __func__);
    540 		splx(s);
    541 		return -1;
    542 	}
    543 
    544 #if NRASOPS_ROTATION > 0
    545 	if (ri->ri_flg & RI_ROTATE_MASK) {
    546 		if (ri->ri_flg & RI_ROTATE_CW) {
    547 			ri->ri_real_ops = ri->ri_ops;
    548 			ri->ri_ops.copycols = rasops_copycols_rotated_cw;
    549 			ri->ri_ops.copyrows = rasops_copyrows_rotated_cw;
    550 			ri->ri_ops.erasecols = rasops_erasecols_rotated_cw;
    551 			ri->ri_ops.eraserows = rasops_eraserows_rotated_cw;
    552 			ri->ri_ops.putchar = rasops_putchar_rotated_cw;
    553 		} else if (ri->ri_flg & RI_ROTATE_CCW) {
    554 			ri->ri_real_ops = ri->ri_ops;
    555 			ri->ri_ops.copycols = rasops_copycols_rotated_ccw;
    556 			ri->ri_ops.copyrows = rasops_copyrows_rotated_ccw;
    557 			ri->ri_ops.erasecols = rasops_erasecols_rotated_ccw;
    558 			ri->ri_ops.eraserows = rasops_eraserows_rotated_ccw;
    559 			ri->ri_ops.putchar = rasops_putchar_rotated_ccw;
    560 		}
    561 	}
    562 #endif
    563 
    564 	ri->ri_flg |= RI_CFGDONE;
    565 	splx(s);
    566 	return 0;
    567 }
    568 
    569 /*
    570  * Map a character.
    571  */
    572 static int
    573 rasops_mapchar(void *cookie, int c, u_int *cp)
    574 {
    575 	struct rasops_info *ri = (struct rasops_info *)cookie;
    576 
    577 	KASSERT(ri->ri_font != NULL);
    578 
    579 	if ((c = wsfont_map_unichar(ri->ri_font, c)) < 0 ||
    580 	    c < ri->ri_font->firstchar) {
    581 		*cp = ' ';
    582 		return 0;
    583 	}
    584 
    585 #if 0 /* XXXRO */
    586 	if (CHAR_IN_FONT(c, ri->ri_font)) {
    587 		*cp = ' ';
    588 		return 0;
    589 	}
    590 #endif
    591 
    592 	*cp = c;
    593 	return 5;
    594 }
    595 
    596 /*
    597  * Allocate a color attribute.
    598  */
    599 static int
    600 rasops_allocattr_color(void *cookie, int fg0, int bg0, int flg, long *attr)
    601 {
    602 	uint32_t fg = fg0, bg = bg0;
    603 
    604 	if (__predict_false(fg >= sizeof(rasops_isgray) ||
    605 	    bg >= sizeof(rasops_isgray)))
    606 		return EINVAL;
    607 
    608 #ifdef RASOPS_CLIPPING
    609 	fg &= 7;
    610 	bg &= 7;
    611 #endif
    612 	if ((flg & WSATTR_BLINK) != 0)
    613 		return EINVAL;
    614 
    615 	if ((flg & WSATTR_WSCOLORS) == 0) {
    616 #ifdef WS_DEFAULT_FG
    617 		fg = WS_DEFAULT_FG;
    618 #else
    619 		fg = WSCOL_WHITE;
    620 #endif
    621 #ifdef WS_DEFAULT_BG
    622 		bg = WS_DEFAULT_BG;
    623 #else
    624 		bg = WSCOL_BLACK;
    625 #endif
    626 	}
    627 
    628 	if ((flg & WSATTR_HILIT) != 0)
    629 		fg += 8;
    630 
    631 	if ((flg & WSATTR_REVERSE) != 0) {
    632 		uint32_t swap = fg;
    633 		fg = bg;
    634 		bg = swap;
    635 	}
    636 
    637 	flg &= WSATTR_USERMASK;
    638 
    639 	if (rasops_isgray[fg])
    640 		flg |= WSATTR_PRIVATE1;
    641 
    642 	if (rasops_isgray[bg])
    643 		flg |= WSATTR_PRIVATE2;
    644 
    645 	*attr = (bg << 16) | (fg << 24) | flg;
    646 	return 0;
    647 }
    648 
    649 /*
    650  * Allocate a mono attribute.
    651  */
    652 static int
    653 rasops_allocattr_mono(void *cookie, int fg0, int bg0, int flg, long *attr)
    654 {
    655 	uint32_t fg = fg0, bg = bg0;
    656 
    657 	if ((flg & (WSATTR_BLINK | WSATTR_HILIT | WSATTR_WSCOLORS)) != 0)
    658 		return EINVAL;
    659 
    660 	fg = 1;
    661 	bg = 0;
    662 
    663 	if ((flg & WSATTR_REVERSE) != 0) {
    664 		uint32_t swap = fg;
    665 		fg = bg;
    666 		bg = swap;
    667 	}
    668 
    669 	*attr = (bg << 16) | (fg << 24) | flg;
    670 	return 0;
    671 }
    672 
    673 /*
    674  * Copy rows.
    675  */
    676 static void
    677 rasops_copyrows(void *cookie, int src, int dst, int num)
    678 {
    679 	struct rasops_info *ri = (struct rasops_info *)cookie;
    680 	uint8_t *sp, *dp, *hp;
    681 	int n, stride;
    682 
    683 	hp = NULL;	/* XXX GCC */
    684 
    685 	if (__predict_false(dst == src))
    686 		return;
    687 
    688 #ifdef RASOPS_CLIPPING
    689 	if (src < 0) {
    690 		num += src;
    691 		src = 0;
    692 	}
    693 
    694 	if (src + num > ri->ri_rows)
    695 		num = ri->ri_rows - src;
    696 
    697 	if (dst < 0) {
    698 		num += dst;
    699 		dst = 0;
    700 	}
    701 
    702 	if (dst + num > ri->ri_rows)
    703 		num = ri->ri_rows - dst;
    704 
    705 	if (num <= 0)
    706 		return;
    707 #endif
    708 
    709 	num *= ri->ri_font->fontheight;
    710 	n = ri->ri_emustride;
    711 	stride = ri->ri_stride;
    712 
    713 	sp = ri->ri_bits + src * ri->ri_yscale;
    714 	dp = ri->ri_bits + dst * ri->ri_yscale;
    715 	if (ri->ri_hwbits)
    716 		hp = ri->ri_hwbits + dst * ri->ri_yscale;
    717 
    718 	while (num--) {
    719 		memcpy(dp, sp, n);
    720 		dp += stride;
    721 		if (ri->ri_hwbits) {
    722 			memcpy(hp, sp, n);
    723 			hp += stride;
    724 		}
    725 		sp += stride;
    726 	}
    727 }
    728 
    729 /*
    730  * Copy columns. This is slow, and hard to optimize due to alignment,
    731  * and the fact that we have to copy both left->right and right->left.
    732  * We simply cop-out here and use memmove(), since it handles all of
    733  * these cases anyway.
    734  */
    735 static void
    736 rasops_copycols(void *cookie, int row, int src, int dst, int num)
    737 {
    738 	struct rasops_info *ri = (struct rasops_info *)cookie;
    739 	uint8_t *sp, *dp, *hp;
    740 	int height;
    741 
    742 	hp = NULL;	/* XXX GCC */
    743 
    744 	if (__predict_false(dst == src))
    745 		return;
    746 
    747 #ifdef RASOPS_CLIPPING
    748 	/* Catches < 0 case too */
    749 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    750 		return;
    751 
    752 	if (src < 0) {
    753 		num += src;
    754 		src = 0;
    755 	}
    756 
    757 	if (src + num > ri->ri_cols)
    758 		num = ri->ri_cols - src;
    759 
    760 	if (dst < 0) {
    761 		num += dst;
    762 		dst = 0;
    763 	}
    764 
    765 	if (dst + num > ri->ri_cols)
    766 		num = ri->ri_cols - dst;
    767 
    768 	if (num <= 0)
    769 		return;
    770 #endif
    771 
    772 	num *= ri->ri_xscale;
    773 	row *= ri->ri_yscale;
    774 	height = ri->ri_font->fontheight;
    775 
    776 	sp = ri->ri_bits + row + src * ri->ri_xscale;
    777 	dp = ri->ri_bits + row + dst * ri->ri_xscale;
    778 	if (ri->ri_hwbits)
    779 		hp = ri->ri_hwbits + row + dst * ri->ri_xscale;
    780 
    781 	while (height--) {
    782 		memmove(dp, sp, num);
    783 		if (ri->ri_hwbits) {
    784 			memcpy(hp, dp, num);
    785 			hp += ri->ri_stride;
    786 		}
    787 		dp += ri->ri_stride;
    788 		sp += ri->ri_stride;
    789 	}
    790 }
    791 
    792 /*
    793  * Turn cursor off/on.
    794  */
    795 static void
    796 rasops_cursor(void *cookie, int on, int row, int col)
    797 {
    798 	struct rasops_info *ri = (struct rasops_info *)cookie;
    799 
    800 	/* Turn old cursor off */
    801 	if ((ri->ri_flg & RI_CURSOR) != 0)
    802 #ifdef RASOPS_CLIPPING
    803 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    804 #endif
    805 			ri->ri_do_cursor(ri);
    806 
    807 	/* Select new cursor */
    808 #ifdef RASOPS_CLIPPING
    809 	ri->ri_flg &= ~RI_CURSORCLIP;
    810 
    811 	if (row < 0 || row >= ri->ri_rows)
    812 		ri->ri_flg |= RI_CURSORCLIP;
    813 	else if (col < 0 || col >= ri->ri_cols)
    814 		ri->ri_flg |= RI_CURSORCLIP;
    815 #endif
    816 	ri->ri_crow = row;
    817 	ri->ri_ccol = col;
    818 
    819 	if (on) {
    820 		ri->ri_flg |= RI_CURSOR;
    821 #ifdef RASOPS_CLIPPING
    822 		if ((ri->ri_flg & RI_CURSORCLIP) == 0)
    823 #endif
    824 			ri->ri_do_cursor(ri);
    825 	} else
    826 		ri->ri_flg &= ~RI_CURSOR;
    827 }
    828 
    829 /*
    830  * Make the device colormap
    831  */
    832 static void
    833 rasops_init_devcmap(struct rasops_info *ri)
    834 {
    835 	int i;
    836 	uint32_t c;
    837 	const uint8_t *p;
    838 
    839 	switch (ri->ri_depth) {
    840 	case 1:
    841 		ri->ri_devcmap[0] = 0;
    842 		for (i = 1; i < 16; i++)
    843 			ri->ri_devcmap[i] = -1;
    844 		return;
    845 
    846 	case 2:
    847 		for (i = 1; i < 15; i++)
    848 			ri->ri_devcmap[i] = 0xaaaaaaaa;
    849 
    850 		ri->ri_devcmap[0] = 0;
    851 		ri->ri_devcmap[8] = 0x55555555;
    852 		ri->ri_devcmap[15] = -1;
    853 		return;
    854 
    855 #ifdef RASOPS_APPLE_PALETTE
    856 	case 4:
    857 		for (i = 0; i < 16; i++) {
    858 			c = apple4_devcmap[i];
    859 			ri->ri_devcmap[i] =
    860 			    (c <<  0) | (c <<  4) | (c <<  8) | (c << 12) |
    861 			    (c << 16) | (c << 20) | (c << 24) | (c << 28);
    862 		}
    863 		return;
    864 #else
    865 	/* XXXRO What should we do here? */
    866 #endif
    867 
    868 	case 8:
    869 		if ((ri->ri_flg & RI_8BIT_IS_RGB) == 0) {
    870 			for (i = 0; i < 16; i++) {
    871 #ifdef RASOPS_APPLE_PALETTE
    872 				c = apple8_devcmap[i];
    873 #else
    874 				c = i;
    875 #endif
    876 				ri->ri_devcmap[i] =
    877 				    c | (c << 8) | (c << 16) | (c << 24);
    878 			}
    879 			return;
    880 		}
    881 	}
    882 
    883 	p = rasops_cmap;
    884 
    885 	for (i = 0; i < 16; i++) {
    886 		if (ri->ri_rnum <= 8)
    887 			c = (uint32_t)(*p >> (8 - ri->ri_rnum)) << ri->ri_rpos;
    888 		else
    889 			c = (uint32_t)(*p << (ri->ri_rnum - 8)) << ri->ri_rpos;
    890 		p++;
    891 
    892 		if (ri->ri_gnum <= 8)
    893 			c |= (uint32_t)(*p >> (8 - ri->ri_gnum)) << ri->ri_gpos;
    894 		else
    895 			c |= (uint32_t)(*p << (ri->ri_gnum - 8)) << ri->ri_gpos;
    896 		p++;
    897 
    898 		if (ri->ri_bnum <= 8)
    899 			c |= (uint32_t)(*p >> (8 - ri->ri_bnum)) << ri->ri_bpos;
    900 		else
    901 			c |= (uint32_t)(*p << (ri->ri_bnum - 8)) << ri->ri_bpos;
    902 		p++;
    903 
    904 		/* Fill the word for generic routines, which want this */
    905 		if (ri->ri_depth == 8) {
    906 			c |= c << 8;
    907 			c |= c << 16;
    908 		} else if (ri->ri_depth == 15 || ri->ri_depth == 16)
    909 			c |= c << 16;
    910 		else if (ri->ri_depth == 24) {
    911 #if BYTE_ORDER == LITTLE_ENDIAN
    912 #  ifndef RASOPS_SMALL
    913 			if (ri->ri_font->fontwidth != 12)
    914 #  endif
    915 				c = (c & 0x0000ff) << 16 | (c & 0x00ff00) |
    916 				    (c & 0xff0000) >> 16;
    917 #  ifndef RASOPS_SMALL
    918 			else
    919 				c = (c & 0x0000ff) | (c & 0x00ff00) << 8 |
    920 				    (c & 0xff0000) >> 8;
    921 #  endif
    922 #else
    923 			/* XXXRO What should we do here? */
    924 #endif
    925 			c |= (c & 0xff) << 24;
    926 		}
    927 
    928 		/* 24bpp does bswap on the fly. {32,16,15}bpp do it here. */
    929 		if ((ri->ri_flg & RI_BSWAP) == 0)
    930 			ri->ri_devcmap[i] = c;
    931 		else if (ri->ri_depth == 15 || ri->ri_depth == 16)
    932 			ri->ri_devcmap[i] = bswap16(c);
    933 		else if (ri->ri_depth == 32)
    934 			ri->ri_devcmap[i] = bswap32(c);
    935 		else /* 8, 24 */
    936 			ri->ri_devcmap[i] = c;
    937 	}
    938 }
    939 
    940 /*
    941  * Unpack a rasops attribute
    942  */
    943 void
    944 rasops_unpack_attr(long attr, int *fg, int *bg, int *underline)
    945 {
    946 
    947 	*fg = ((uint32_t)attr >> 24) & 0xf;
    948 	*bg = ((uint32_t)attr >> 16) & 0xf;
    949 	if (underline != NULL)
    950 		*underline = (uint32_t)attr & WSATTR_UNDERLINE;
    951 }
    952 
    953 /*
    954  * Erase rows. This isn't static, since 24-bpp uses it in special cases.
    955  */
    956 void
    957 rasops_eraserows(void *cookie, int row, int num, long attr)
    958 {
    959 	struct rasops_info *ri = (struct rasops_info *)cookie;
    960 	uint32_t *buf = (uint32_t *)ri->ri_buf;
    961 	uint32_t *rp, *hp, clr;
    962 	int stride, cnt;
    963 
    964 	hp = NULL;	/* XXX GCC */
    965 
    966 #ifdef RASOPS_CLIPPING
    967 	if (row < 0) {
    968 		num += row;
    969 		row = 0;
    970 	}
    971 
    972 	if (row + num > ri->ri_rows)
    973 		num = ri->ri_rows - row;
    974 
    975 	if (num <= 0)
    976 		return;
    977 #endif
    978 
    979 	clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
    980 
    981 	/*
    982 	 * XXX The wsdisplay_emulops interface seems a little deficient in
    983 	 * that there is no way to clear the *entire* screen. We provide a
    984 	 * workaround here: if the entire console area is being cleared, and
    985 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    986 	 */
    987 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    988 		stride = ri->ri_stride;
    989 		num = ri->ri_height;
    990 		rp = (uint32_t *)ri->ri_origbits;
    991 		if (ri->ri_hwbits)
    992 			hp = (uint32_t *)ri->ri_hworigbits;
    993 	} else {
    994 		stride = ri->ri_emustride;
    995 		num *= ri->ri_font->fontheight;
    996 		rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
    997 		if (ri->ri_hwbits)
    998 			hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
    999 	}
   1000 
   1001 	for (cnt = 0; cnt < stride >> 2; cnt++)
   1002 		buf[cnt] = clr;
   1003 
   1004 	while (num--) {
   1005 		memcpy(rp, buf, stride);
   1006 		if (ri->ri_hwbits) {
   1007 			memcpy(hp, buf, stride);
   1008 			DELTA(hp, ri->ri_stride, uint32_t *);
   1009 		}
   1010 		DELTA(rp, ri->ri_stride, uint32_t *);
   1011 	}
   1012 }
   1013 
   1014 /*
   1015  * Actually turn the cursor on or off. This does the dirty work for
   1016  * rasops_cursor().
   1017  */
   1018 static void
   1019 rasops_do_cursor(struct rasops_info *ri)
   1020 {
   1021 	int full, height, cnt, slop1, slop2, row, col;
   1022 	uint32_t tmp32, msk1, msk2;
   1023 	uint8_t tmp8;
   1024 	uint8_t *dp, *rp, *hp;
   1025 
   1026 	hp = NULL;	/* XXX GCC */
   1027 
   1028 #if NRASOPS_ROTATION > 0
   1029 	if (ri->ri_flg & RI_ROTATE_MASK) {
   1030 		if (ri->ri_flg & RI_ROTATE_CW) {
   1031 			/* Rotate rows/columns */
   1032 			row = ri->ri_ccol;
   1033 			col = ri->ri_rows - ri->ri_crow - 1;
   1034 		} else if (ri->ri_flg & RI_ROTATE_CCW) {
   1035 			/* Rotate rows/columns */
   1036 			row = ri->ri_cols - ri->ri_ccol - 1;
   1037 			col = ri->ri_crow;
   1038 		} else {	/* upside-down */
   1039 			row = ri->ri_crow;
   1040 			col = ri->ri_ccol;
   1041 		}
   1042 	} else
   1043 #endif
   1044 	{
   1045 		row = ri->ri_crow;
   1046 		col = ri->ri_ccol;
   1047 	}
   1048 
   1049 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
   1050 	if (ri->ri_hwbits)
   1051 		hp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
   1052 	height = ri->ri_font->fontheight;
   1053 
   1054 	/*
   1055 	 * For ri_xscale = 1:
   1056 	 *
   1057 	 * Logic below does not work for ri_xscale = 1, e.g.,
   1058 	 * fontwidth = 8 and bpp = 1. So we take care of it.
   1059 	 */
   1060 	if (ri->ri_xscale == 1) {
   1061 		while (height--) {
   1062 			tmp8 = ~*rp;
   1063 
   1064 			*rp = tmp8;
   1065 			rp += ri->ri_stride;
   1066 
   1067 			if (ri->ri_hwbits) {
   1068 				*hp = tmp8;
   1069 				hp += ri->ri_stride;
   1070 			}
   1071 		}
   1072 		return;
   1073 	}
   1074 
   1075 	/*
   1076 	 * For ri_xscale = 2, 3, 4, ...:
   1077 	 *
   1078 	 * Note that siop1 <= ri_xscale even for ri_xscale = 2,
   1079 	 * since rp % 3 = 0 or 2 (ri_stride % 4 = 0).
   1080 	 */
   1081 	slop1 = (4 - ((uintptr_t)rp & 3)) & 3;
   1082 	slop2 = (ri->ri_xscale - slop1) & 3;
   1083 	full = (ri->ri_xscale - slop1 /* - slop2 */) >> 2;
   1084 
   1085 	rp = (uint8_t *)((uintptr_t)rp & ~3);
   1086 	hp = (uint8_t *)((uintptr_t)hp & ~3);
   1087 
   1088 	msk1 = !slop1 ? 0 : be32toh(0xffffffffU >> (32 - (8 * slop1)));
   1089 	msk2 = !slop2 ? 0 : be32toh(0xffffffffU << (32 - (8 * slop2)));
   1090 
   1091 	while (height--) {
   1092 		dp = rp;
   1093 
   1094 		if (slop1) {
   1095 			tmp32 = *(uint32_t *)dp ^ msk1;
   1096 			*(uint32_t *)dp = tmp32;
   1097 			dp += 4;
   1098 		}
   1099 
   1100 		for (cnt = full; cnt; cnt--) {
   1101 			tmp32 = ~*(uint32_t *)dp;
   1102 			*(uint32_t *)dp = tmp32;
   1103 			dp += 4;
   1104 		}
   1105 
   1106 		if (slop2) {
   1107 			tmp32 = *(uint32_t *)dp ^ msk2;
   1108 			*(uint32_t *)dp = tmp32;
   1109 		}
   1110 
   1111 		if (ri->ri_hwbits) {
   1112 			memcpy(hp, rp, ((slop1 != 0) + full +
   1113 			    (slop2 != 0)) << 2);
   1114 			hp += ri->ri_stride;
   1115 		}
   1116 		rp += ri->ri_stride;
   1117 	}
   1118 }
   1119 
   1120 /*
   1121  * Erase columns.
   1122  */
   1123 void
   1124 rasops_erasecols(void *cookie, int row, int col, int num, long attr)
   1125 {
   1126 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1127 	uint32_t *buf = ri->ri_buf;
   1128 	int height, cnt, clr;
   1129 	uint32_t *dp, *rp, *hp;
   1130 
   1131 	hp = NULL;	/* XXX GCC */
   1132 
   1133 #ifdef RASOPS_CLIPPING
   1134 	if ((unsigned)row >= (unsigned)ri->ri_rows)
   1135 		return;
   1136 
   1137 	if (col < 0) {
   1138 		num += col;
   1139 		col = 0;
   1140 	}
   1141 
   1142 	if (col + num > ri->ri_cols)
   1143 		num = ri->ri_cols - col;
   1144 
   1145 	if (num <= 0)
   1146 		return;
   1147 #endif
   1148 
   1149 	num *= ri->ri_xscale;
   1150 	rp = (uint32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
   1151 	if (ri->ri_hwbits)
   1152 		hp = (uint32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
   1153 		    col*ri->ri_xscale);
   1154 	height = ri->ri_font->fontheight;
   1155 	clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
   1156 
   1157 	dp = buf;
   1158 
   1159 	/* Write 4 bytes per loop */
   1160 	for (cnt = num >> 2; cnt; cnt--)
   1161 		*dp++ = clr;
   1162 
   1163 	/* Write unaligned trailing slop */
   1164 	for (cnt = num & 3; cnt; cnt--) {
   1165 		*(uint8_t *)dp = clr;
   1166 		DELTA(dp, 1, uint32_t *);
   1167 	}
   1168 
   1169 	while (height--) {
   1170 		memcpy(rp, buf, num);
   1171 		DELTA(rp, ri->ri_stride, uint32_t *);
   1172 		if (ri->ri_hwbits) {
   1173 			memcpy(hp, buf, num);
   1174 			DELTA(hp, ri->ri_stride, uint32_t *);
   1175 		}
   1176 	}
   1177 }
   1178 
   1179 #if NRASOPS_ROTATION > 0
   1180 /*
   1181  * Quarter clockwise rotation routines (originally intended for the
   1182  * built-in Zaurus C3x00 display in 16bpp).
   1183  */
   1184 
   1185 static void
   1186 rasops_rotate_font(int *cookie, int rotate)
   1187 {
   1188 	struct rotatedfont *f;
   1189 	int ncookie;
   1190 
   1191 	SLIST_FOREACH(f, &rotatedfonts, rf_next) {
   1192 		if (f->rf_cookie == *cookie) {
   1193 			*cookie = f->rf_rotated;
   1194 			return;
   1195 		}
   1196 	}
   1197 
   1198 	/*
   1199 	 * We did not find a rotated version of this font. Ask the wsfont
   1200 	 * code to compute one for us.
   1201 	 */
   1202 
   1203 	f = kmem_alloc(sizeof(*f), KM_SLEEP);
   1204 
   1205 	if ((ncookie = wsfont_rotate(*cookie, rotate)) == -1)
   1206 		goto fail;
   1207 
   1208 	f->rf_cookie = *cookie;
   1209 	f->rf_rotated = ncookie;
   1210 	SLIST_INSERT_HEAD(&rotatedfonts, f, rf_next);
   1211 
   1212 	*cookie = ncookie;
   1213 	return;
   1214 
   1215 fail:	kmem_free(f, sizeof(*f));
   1216 	return;
   1217 }
   1218 
   1219 static void
   1220 rasops_copychar(void *cookie, int srcrow, int dstrow, int srccol, int dstcol)
   1221 {
   1222 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1223 	int height;
   1224 	int r_srcrow, r_dstrow, r_srccol, r_dstcol;
   1225 	uint8_t *sp, *dp;
   1226 
   1227 	r_srcrow = srccol;
   1228 	r_dstrow = dstcol;
   1229 	r_srccol = ri->ri_rows - srcrow - 1;
   1230 	r_dstcol = ri->ri_rows - dstrow - 1;
   1231 
   1232 	r_srcrow *= ri->ri_yscale;
   1233 	r_dstrow *= ri->ri_yscale;
   1234 	height = ri->ri_font->fontheight;
   1235 
   1236 	sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
   1237 	dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
   1238 
   1239 	while (height--) {
   1240 		memmove(dp, sp, ri->ri_xscale);
   1241 		dp += ri->ri_stride;
   1242 		sp += ri->ri_stride;
   1243 	}
   1244 }
   1245 
   1246 static void
   1247 rasops_putchar_rotated_cw(void *cookie, int row, int col, u_int uc, long attr)
   1248 {
   1249 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1250 	int height;
   1251 	uint8_t *rp;
   1252 
   1253 	if (__predict_false((unsigned int)row > ri->ri_rows ||
   1254 	    (unsigned int)col > ri->ri_cols))
   1255 		return;
   1256 
   1257 	/* Avoid underflow */
   1258 	if (ri->ri_rows - row - 1 < 0)
   1259 		return;
   1260 
   1261 	/* Do rotated char sans (side)underline */
   1262 	ri->ri_real_ops.putchar(cookie, col, ri->ri_rows - row - 1, uc,
   1263 	    attr & ~WSATTR_UNDERLINE);
   1264 
   1265 	/* Do rotated underline */
   1266 	rp = ri->ri_bits + col * ri->ri_yscale + (ri->ri_rows - row - 1) *
   1267 	    ri->ri_xscale;
   1268 	height = ri->ri_font->fontheight;
   1269 
   1270 	/* XXX this assumes 16-bit color depth */
   1271 	if ((attr & WSATTR_UNDERLINE) != 0) {
   1272 		uint16_t c =
   1273 		    (uint16_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
   1274 
   1275 		while (height--) {
   1276 			*(uint16_t *)rp = c;
   1277 			rp += ri->ri_stride;
   1278 		}
   1279 	}
   1280 }
   1281 
   1282 static void
   1283 rasops_erasecols_rotated_cw(void *cookie, int row, int col, int num, long attr)
   1284 {
   1285 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1286 	int i;
   1287 
   1288 	for (i = col; i < col + num; i++)
   1289 		ri->ri_ops.putchar(cookie, row, i, ' ', attr);
   1290 }
   1291 
   1292 /* XXX: these could likely be optimised somewhat. */
   1293 static void
   1294 rasops_copyrows_rotated_cw(void *cookie, int src, int dst, int num)
   1295 {
   1296 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1297 	int col, roff;
   1298 
   1299 	if (src > dst)
   1300 		for (roff = 0; roff < num; roff++)
   1301 			for (col = 0; col < ri->ri_cols; col++)
   1302 				rasops_copychar(cookie, src + roff, dst + roff,
   1303 				    col, col);
   1304 	else
   1305 		for (roff = num - 1; roff >= 0; roff--)
   1306 			for (col = 0; col < ri->ri_cols; col++)
   1307 				rasops_copychar(cookie, src + roff, dst + roff,
   1308 				    col, col);
   1309 }
   1310 
   1311 static void
   1312 rasops_copycols_rotated_cw(void *cookie, int row, int src, int dst, int num)
   1313 {
   1314 	int coff;
   1315 
   1316 	if (src > dst)
   1317 		for (coff = 0; coff < num; coff++)
   1318 			rasops_copychar(cookie, row, row, src + coff,
   1319 			    dst + coff);
   1320 	else
   1321 		for (coff = num - 1; coff >= 0; coff--)
   1322 			rasops_copychar(cookie, row, row, src + coff,
   1323 			    dst + coff);
   1324 }
   1325 
   1326 static void
   1327 rasops_eraserows_rotated_cw(void *cookie, int row, int num, long attr)
   1328 {
   1329 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1330 	int col, rn;
   1331 
   1332 	for (rn = row; rn < row + num; rn++)
   1333 		for (col = 0; col < ri->ri_cols; col++)
   1334 			ri->ri_ops.putchar(cookie, rn, col, ' ', attr);
   1335 }
   1336 
   1337 /*
   1338  * Quarter counter-clockwise rotation routines (originally intended for the
   1339  * built-in Sharp W-ZERO3 display in 16bpp).
   1340  */
   1341 static void
   1342 rasops_copychar_ccw(void *cookie, int srcrow, int dstrow, int srccol,
   1343     int dstcol)
   1344 {
   1345 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1346 	int height, r_srcrow, r_dstrow, r_srccol, r_dstcol;
   1347 	uint8_t *sp, *dp;
   1348 
   1349 	r_srcrow = ri->ri_cols - srccol - 1;
   1350 	r_dstrow = ri->ri_cols - dstcol - 1;
   1351 	r_srccol = srcrow;
   1352 	r_dstcol = dstrow;
   1353 
   1354 	r_srcrow *= ri->ri_yscale;
   1355 	r_dstrow *= ri->ri_yscale;
   1356 	height = ri->ri_font->fontheight;
   1357 
   1358 	sp = ri->ri_bits + r_srcrow + r_srccol * ri->ri_xscale;
   1359 	dp = ri->ri_bits + r_dstrow + r_dstcol * ri->ri_xscale;
   1360 
   1361 	while (height--) {
   1362 		memmove(dp, sp, ri->ri_xscale);
   1363 		dp += ri->ri_stride;
   1364 		sp += ri->ri_stride;
   1365 	}
   1366 }
   1367 
   1368 static void
   1369 rasops_putchar_rotated_ccw(void *cookie, int row, int col, u_int uc, long attr)
   1370 {
   1371 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1372 	int height;
   1373 	uint8_t *rp;
   1374 
   1375 	if (__predict_false((unsigned int)row > ri->ri_rows ||
   1376 	    (unsigned int)col > ri->ri_cols))
   1377 		return;
   1378 
   1379 	/* Avoid underflow */
   1380 	if (ri->ri_cols - col - 1 < 0)
   1381 		return;
   1382 
   1383 	/* Do rotated char sans (side)underline */
   1384 	ri->ri_real_ops.putchar(cookie, ri->ri_cols - col - 1, row, uc,
   1385 	    attr & ~WSATTR_UNDERLINE);
   1386 
   1387 	/* Do rotated underline */
   1388 	rp = ri->ri_bits + (ri->ri_cols - col - 1) * ri->ri_yscale +
   1389 	    row * ri->ri_xscale +
   1390 	    (ri->ri_font->fontwidth - 1) * ri->ri_pelbytes;
   1391 	height = ri->ri_font->fontheight;
   1392 
   1393 	/* XXX this assumes 16-bit color depth */
   1394 	if ((attr & WSATTR_UNDERLINE) != 0) {
   1395 		uint16_t c =
   1396 		    (uint16_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
   1397 
   1398 		while (height--) {
   1399 			*(uint16_t *)rp = c;
   1400 			rp += ri->ri_stride;
   1401 		}
   1402 	}
   1403 }
   1404 
   1405 /* XXX: these could likely be optimised somewhat. */
   1406 static void
   1407 rasops_copyrows_rotated_ccw(void *cookie, int src, int dst, int num)
   1408 {
   1409 	struct rasops_info *ri = (struct rasops_info *)cookie;
   1410 	int col, roff;
   1411 
   1412 	if (src > dst)
   1413 		for (roff = 0; roff < num; roff++)
   1414 			for (col = 0; col < ri->ri_cols; col++)
   1415 				rasops_copychar_ccw(cookie,
   1416 				    src + roff, dst + roff, col, col);
   1417 	else
   1418 		for (roff = num - 1; roff >= 0; roff--)
   1419 			for (col = 0; col < ri->ri_cols; col++)
   1420 				rasops_copychar_ccw(cookie,
   1421 				    src + roff, dst + roff, col, col);
   1422 }
   1423 
   1424 static void
   1425 rasops_copycols_rotated_ccw(void *cookie, int row, int src, int dst, int num)
   1426 {
   1427 	int coff;
   1428 
   1429 	if (src > dst)
   1430 		for (coff = 0; coff < num; coff++)
   1431 			rasops_copychar_ccw(cookie, row, row,
   1432 			    src + coff, dst + coff);
   1433 	else
   1434 		for (coff = num - 1; coff >= 0; coff--)
   1435 			rasops_copychar_ccw(cookie, row, row,
   1436 			    src + coff, dst + coff);
   1437 }
   1438 #endif	/* NRASOPS_ROTATION */
   1439 
   1440 void
   1441 rasops_make_box_chars_16(struct rasops_info *ri)
   1442 {
   1443 	int c, i, mid;
   1444 	uint16_t vert_mask, hmask_left, hmask_right;
   1445 	uint16_t *data = (uint16_t *)ri->ri_optfont.data;
   1446 
   1447 	vert_mask = 0xc000U >> ((ri->ri_font->fontwidth >> 1) - 1);
   1448 	hmask_left = 0xff00U << (8 - (ri->ri_font->fontwidth >> 1));
   1449 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
   1450 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1451 
   1452 	/* 0x00 would be empty anyway so don't bother */
   1453 	for (c = 1; c < 16; c++) {
   1454 		data += ri->ri_font->fontheight;
   1455 		if (c & 1) {
   1456 			/* upper segment */
   1457 			for (i = 0; i < mid; i++)
   1458 				data[i] = vert_mask;
   1459 		}
   1460 		if (c & 4) {
   1461 			/* lower segment */
   1462 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1463 				data[i] = vert_mask;
   1464 		}
   1465 		if (c & 2) {
   1466 			/* right segment */
   1467 			i = ri->ri_font->fontheight >> 1;
   1468 			data[mid - 1] |= hmask_right;
   1469 			data[mid] |= hmask_right;
   1470 		}
   1471 		if (c & 8) {
   1472 			/* left segment */
   1473 			data[mid - 1] |= hmask_left;
   1474 			data[mid] |= hmask_left;
   1475 		}
   1476 	}
   1477 }
   1478 
   1479 void
   1480 rasops_make_box_chars_8(struct rasops_info *ri)
   1481 {
   1482 	int c, i, mid;
   1483 	uint8_t vert_mask, hmask_left, hmask_right;
   1484 	uint8_t *data = (uint8_t *)ri->ri_optfont.data;
   1485 
   1486 	vert_mask = 0xc0U >> ((ri->ri_font->fontwidth >> 1) - 1);
   1487 	hmask_left = 0xf0U << (4 - (ri->ri_font->fontwidth >> 1));
   1488 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
   1489 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1490 
   1491 	/* 0x00 would be empty anyway so don't bother */
   1492 	for (c = 1; c < 16; c++) {
   1493 		data += ri->ri_font->fontheight;
   1494 		if (c & 1) {
   1495 			/* upper segment */
   1496 			for (i = 0; i < mid; i++)
   1497 				data[i] = vert_mask;
   1498 		}
   1499 		if (c & 4) {
   1500 			/* lower segment */
   1501 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1502 				data[i] = vert_mask;
   1503 		}
   1504 		if (c & 2) {
   1505 			/* right segment */
   1506 			i = ri->ri_font->fontheight >> 1;
   1507 			data[mid - 1] |= hmask_right;
   1508 			data[mid] |= hmask_right;
   1509 		}
   1510 		if (c & 8) {
   1511 			/* left segment */
   1512 			data[mid - 1] |= hmask_left;
   1513 			data[mid] |= hmask_left;
   1514 		}
   1515 	}
   1516 }
   1517 
   1518 void
   1519 rasops_make_box_chars_32(struct rasops_info *ri)
   1520 {
   1521 	int c, i, mid;
   1522 	uint32_t vert_mask, hmask_left, hmask_right;
   1523 	uint32_t *data = (uint32_t *)ri->ri_optfont.data;
   1524 
   1525 	vert_mask = 0xc0000000U >> ((ri->ri_font->fontwidth >> 1) - 1);
   1526 	hmask_left = 0xffff0000U << (16 - (ri->ri_font->fontwidth >> 1));
   1527 	hmask_right = hmask_left >> ((ri->ri_font->fontwidth + 1) >> 1);
   1528 	mid = (ri->ri_font->fontheight + 1) >> 1;
   1529 
   1530 	/* 0x00 would be empty anyway so don't bother */
   1531 	for (c = 1; c < 16; c++) {
   1532 		data += ri->ri_font->fontheight;
   1533 		if (c & 1) {
   1534 			/* upper segment */
   1535 			for (i = 0; i < mid; i++)
   1536 				data[i] = vert_mask;
   1537 		}
   1538 		if (c & 4) {
   1539 			/* lower segment */
   1540 			for (i = mid; i < ri->ri_font->fontheight; i++)
   1541 				data[i] = vert_mask;
   1542 		}
   1543 		if (c & 2) {
   1544 			/* right segment */
   1545 			i = ri->ri_font->fontheight >> 1;
   1546 			data[mid - 1] |= hmask_right;
   1547 			data[mid] |= hmask_right;
   1548 		}
   1549 		if (c & 8) {
   1550 			/* left segment */
   1551 			data[mid - 1] |= hmask_left;
   1552 			data[mid] |= hmask_left;
   1553 		}
   1554 	}
   1555 }
   1556 
   1557 void
   1558 rasops_make_box_chars_alpha(struct rasops_info *ri)
   1559 {
   1560 	int c, i, hmid, vmid, wi, he;
   1561 	uint8_t *data = (uint8_t *)ri->ri_optfont.data;
   1562 	uint8_t *ddata;
   1563 
   1564 	he = ri->ri_font->fontheight;
   1565 	wi = ri->ri_font->fontwidth;
   1566 
   1567 	vmid = (he + 1) >> 1;
   1568 	hmid = (wi + 1) >> 1;
   1569 
   1570 	/* 0x00 would be empty anyway so don't bother */
   1571 	for (c = 1; c < 16; c++) {
   1572 		data += ri->ri_fontscale;
   1573 		if (c & 1) {
   1574 			/* upper segment */
   1575 			ddata = data + hmid;
   1576 			for (i = 0; i <= vmid; i++) {
   1577 				*ddata = 0xff;
   1578 				ddata += wi;
   1579 			}
   1580 		}
   1581 		if (c & 4) {
   1582 			/* lower segment */
   1583 			ddata = data + wi * vmid + hmid;
   1584 			for (i = vmid; i < he; i++) {
   1585 				*ddata = 0xff;
   1586 				ddata += wi;
   1587 			}
   1588 		}
   1589 		if (c & 2) {
   1590 			/* right segment */
   1591 			ddata = data + wi * vmid + hmid;
   1592 			for (i = hmid; i < wi; i++) {
   1593 				*ddata = 0xff;
   1594 				ddata++;
   1595 			}
   1596 		}
   1597 		if (c & 8) {
   1598 			/* left segment */
   1599 			ddata = data + wi * vmid;
   1600 			for (i = 0; i <= hmid; i++) {
   1601 				*ddata = 0xff;
   1602 				ddata++;
   1603 			}
   1604 		}
   1605 	}
   1606 }
   1607 
   1608 /*
   1609  * Return a colour map appropriate for the given struct rasops_info in the
   1610  * same form used by rasops_cmap[]
   1611  * For now this is either a copy of rasops_cmap[] or an R3G3B2 map, it should
   1612  * probably be a linear ( or gamma corrected? ) ramp for higher depths.
   1613  */
   1614 int
   1615 rasops_get_cmap(struct rasops_info *ri, uint8_t *palette, size_t bytes)
   1616 {
   1617 
   1618 	if ((ri->ri_depth == 8) && ((ri->ri_flg & RI_8BIT_IS_RGB) != 0)) {
   1619 		/* generate an R3G3B2 palette */
   1620 		int i, idx = 0;
   1621 		uint8_t tmp;
   1622 
   1623 		if (bytes < 768)
   1624 			return EINVAL;
   1625 		for (i = 0; i < 256; i++) {
   1626 			tmp = i & 0xe0;
   1627 			/*
   1628 			 * replicate bits so 0xe0 maps to a red value of 0xff
   1629 			 * in order to make white look actually white
   1630 			 */
   1631 			tmp |= (tmp >> 3) | (tmp >> 6);
   1632 			palette[idx] = tmp;
   1633 			idx++;
   1634 
   1635 			tmp = (i & 0x1c) << 3;
   1636 			tmp |= (tmp >> 3) | (tmp >> 6);
   1637 			palette[idx] = tmp;
   1638 			idx++;
   1639 
   1640 			tmp = (i & 0x03) << 6;
   1641 			tmp |= tmp >> 2;
   1642 			tmp |= tmp >> 4;
   1643 			palette[idx] = tmp;
   1644 			idx++;
   1645 		}
   1646 	} else
   1647 		memcpy(palette, rasops_cmap, uimin(bytes, sizeof(rasops_cmap)));
   1648 	return 0;
   1649 }
   1650 
   1651 #ifndef RASOPS_SMALL
   1652 void
   1653 rasops_allocstamp(struct rasops_info *ri, size_t len)
   1654 {
   1655 
   1656 	KASSERT(ri->ri_stamp == NULL);
   1657 	ri->ri_stamp_len = len;
   1658 	ri->ri_stamp = kmem_zalloc(len, KM_SLEEP);
   1659 	ri->ri_stamp_attr = 0;
   1660 }
   1661 #endif
   1662