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