Home | History | Annotate | Line # | Download | only in rasops
rasops1.c revision 1.20
      1 /* 	$NetBSD: rasops1.c,v 1.20 2009/03/14 21:04:22 dsl 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: rasops1.c,v 1.20 2009/03/14 21:04:22 dsl Exp $");
     34 
     35 #include "opt_rasops.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/time.h>
     40 #include <machine/endian.h>
     41 
     42 #include <dev/wscons/wsdisplayvar.h>
     43 #include <dev/wscons/wsconsio.h>
     44 #include <dev/rasops/rasops.h>
     45 #include <dev/rasops/rasops_masks.h>
     46 
     47 static void	rasops1_copycols(void *, int, int, int, int);
     48 static void	rasops1_erasecols(void *, int, int, int, long);
     49 static void	rasops1_do_cursor(struct rasops_info *);
     50 static void	rasops1_putchar(void *, int, int col, u_int, long);
     51 #ifndef RASOPS_SMALL
     52 static void	rasops1_putchar8(void *, int, int col, u_int, long);
     53 static void	rasops1_putchar16(void *, int, int col, u_int, long);
     54 #endif
     55 
     56 /*
     57  * Initialize rasops_info struct for this colordepth.
     58  */
     59 void
     60 rasops1_init(struct rasops_info *ri)
     61 {
     62 
     63 	switch (ri->ri_font->fontwidth) {
     64 #ifndef RASOPS_SMALL
     65 	case 8:
     66 		ri->ri_ops.putchar = rasops1_putchar8;
     67 		break;
     68 	case 16:
     69 		ri->ri_ops.putchar = rasops1_putchar16;
     70 		break;
     71 #endif
     72 	default:
     73 		ri->ri_ops.putchar = rasops1_putchar;
     74 		break;
     75 	}
     76 
     77 	if ((ri->ri_font->fontwidth & 7) != 0) {
     78 		ri->ri_ops.erasecols = rasops1_erasecols;
     79 		ri->ri_ops.copycols = rasops1_copycols;
     80 		ri->ri_do_cursor = rasops1_do_cursor;
     81 	}
     82 }
     83 
     84 /*
     85  * Paint a single character. This is the generic version, this is ugly.
     86  */
     87 static void
     88 rasops1_putchar(void *cookie, int row, int col, u_int uc, long attr)
     89 {
     90 	u_int fs, rs, fb, bg, fg, lmask, rmask;
     91 	u_int32_t height, width;
     92 	struct rasops_info *ri;
     93 	int32_t *rp;
     94 	u_char *fr;
     95 
     96 	ri = (struct rasops_info *)cookie;
     97 
     98 #ifdef RASOPS_CLIPPING
     99 	/* Catches 'row < 0' case too */
    100 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    101 		return;
    102 
    103 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    104 		return;
    105 #endif
    106 
    107 	col *= ri->ri_font->fontwidth;
    108 	rp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale + ((col >> 3) & ~3));
    109 	height = ri->ri_font->fontheight;
    110 	width = ri->ri_font->fontwidth;
    111 	col = col & 31;
    112 	rs = ri->ri_stride;
    113 
    114 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    115 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    116 
    117 	/* If fg and bg match this becomes a space character */
    118 	if (fg == bg || uc == ' ') {
    119 		uc = (u_int)-1;
    120 		fr = 0;		/* shutup gcc */
    121 		fs = 0;		/* shutup gcc */
    122 	} else {
    123 		uc -= ri->ri_font->firstchar;
    124 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
    125 		fs = ri->ri_font->stride;
    126 	}
    127 
    128 	/* Single word, one mask */
    129 	if ((col + width) <= 32) {
    130 		rmask = rasops_pmask[col][width];
    131 		lmask = ~rmask;
    132 
    133 		if (uc == (u_int)-1) {
    134 			bg &= rmask;
    135 
    136 			while (height--) {
    137 				*rp = (*rp & lmask) | bg;
    138 				DELTA(rp, rs, int32_t *);
    139 			}
    140 		} else {
    141 			/* NOT fontbits if bg is white */
    142 			if (bg) {
    143 				while (height--) {
    144 					fb = ~(fr[3] | (fr[2] << 8) |
    145 					    (fr[1] << 16) | (fr[0] << 24));
    146 					*rp = (*rp & lmask)
    147 					    | (MBE(fb >> col) & rmask);
    148 
    149 					fr += fs;
    150 					DELTA(rp, rs, int32_t *);
    151 				}
    152 			} else {
    153 				while (height--) {
    154 					fb = (fr[3] | (fr[2] << 8) |
    155 					    (fr[1] << 16) | (fr[0] << 24));
    156 					*rp = (*rp & lmask)
    157 					    | (MBE(fb >> col) & rmask);
    158 
    159 					fr += fs;
    160 					DELTA(rp, rs, int32_t *);
    161 				}
    162 			}
    163 		}
    164 
    165 		/* Do underline */
    166 		if ((attr & 1) != 0) {
    167 			DELTA(rp, -(ri->ri_stride << 1), int32_t *);
    168 			*rp = (*rp & lmask) | (fg & rmask);
    169 		}
    170 	} else {
    171 		lmask = ~rasops_lmask[col];
    172 		rmask = ~rasops_rmask[(col + width) & 31];
    173 
    174 		if (uc == (u_int)-1) {
    175 			width = bg & ~rmask;
    176 			bg = bg & ~lmask;
    177 
    178 			while (height--) {
    179 				rp[0] = (rp[0] & lmask) | bg;
    180 				rp[1] = (rp[1] & rmask) | width;
    181 				DELTA(rp, rs, int32_t *);
    182 			}
    183 		} else {
    184 			width = 32 - col;
    185 
    186 			/* NOT fontbits if bg is white */
    187 			if (bg) {
    188 				while (height--) {
    189 					fb = ~(fr[3] | (fr[2] << 8) |
    190 					    (fr[1] << 16) | (fr[0] << 24));
    191 
    192 					rp[0] = (rp[0] & lmask)
    193 					    | MBE((u_int)fb >> col);
    194 
    195 					rp[1] = (rp[1] & rmask)
    196 					    | (MBE((u_int)fb << width) & ~rmask);
    197 
    198 					fr += fs;
    199 					DELTA(rp, rs, int32_t *);
    200 				}
    201 			} else {
    202 				while (height--) {
    203 					fb = (fr[3] | (fr[2] << 8) |
    204 					    (fr[1] << 16) | (fr[0] << 24));
    205 
    206 					rp[0] = (rp[0] & lmask)
    207 					    | MBE(fb >> col);
    208 
    209 					rp[1] = (rp[1] & rmask)
    210 					    | (MBE(fb << width) & ~rmask);
    211 
    212 					fr += fs;
    213 					DELTA(rp, rs, int32_t *);
    214 				}
    215 			}
    216 		}
    217 
    218 		/* Do underline */
    219 		if ((attr & 1) != 0) {
    220 			DELTA(rp, -(ri->ri_stride << 1), int32_t *);
    221 			rp[0] = (rp[0] & lmask) | (fg & ~lmask);
    222 			rp[1] = (rp[1] & rmask) | (fg & ~rmask);
    223 		}
    224 	}
    225 }
    226 
    227 #ifndef RASOPS_SMALL
    228 /*
    229  * Paint a single character. This is for 8-pixel wide fonts.
    230  */
    231 static void
    232 rasops1_putchar8(void *cookie, int row, int col, u_int uc, long attr)
    233 {
    234 	int height, fs, rs, bg, fg;
    235 	struct rasops_info *ri;
    236 	u_char *fr, *rp;
    237 
    238 	ri = (struct rasops_info *)cookie;
    239 
    240 #ifdef RASOPS_CLIPPING
    241 	/* Catches 'row < 0' case too */
    242 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    243 		return;
    244 
    245 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    246 		return;
    247 #endif
    248 
    249 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    250 	height = ri->ri_font->fontheight;
    251 	rs = ri->ri_stride;
    252 
    253 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    254 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    255 
    256 	/* If fg and bg match this becomes a space character */
    257 	if (fg == bg || uc == ' ') {
    258 		while (height--) {
    259 			*rp = bg;
    260 			rp += rs;
    261 		}
    262 	} else {
    263 		uc -= ri->ri_font->firstchar;
    264 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
    265 		fs = ri->ri_font->stride;
    266 
    267 		/* NOT fontbits if bg is white */
    268 		if (bg) {
    269 			while (height--) {
    270 				*rp = ~*fr;
    271 				fr += fs;
    272 				rp += rs;
    273 			}
    274 		} else {
    275 			while (height--) {
    276 				*rp = *fr;
    277 				fr += fs;
    278 				rp += rs;
    279 			}
    280 		}
    281 
    282 	}
    283 
    284 	/* Do underline */
    285 	if ((attr & 1) != 0)
    286 		rp[-(ri->ri_stride << 1)] = fg;
    287 }
    288 
    289 /*
    290  * Paint a single character. This is for 16-pixel wide fonts.
    291  */
    292 static void
    293 rasops1_putchar16(void *cookie, int row, int col, u_int uc, long attr)
    294 {
    295 	int height, fs, rs, bg, fg;
    296 	struct rasops_info *ri;
    297 	u_char *fr, *rp;
    298 
    299 	ri = (struct rasops_info *)cookie;
    300 
    301 #ifdef RASOPS_CLIPPING
    302 	/* Catches 'row < 0' case too */
    303 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    304 		return;
    305 
    306 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    307 		return;
    308 #endif
    309 
    310 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    311 	height = ri->ri_font->fontheight;
    312 	rs = ri->ri_stride;
    313 
    314 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    315 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    316 
    317 	/* If fg and bg match this becomes a space character */
    318 	if (fg == bg || uc == ' ') {
    319 		while (height--) {
    320 			*(int16_t *)rp = bg;
    321 			rp += rs;
    322 		}
    323 	} else {
    324 		uc -= ri->ri_font->firstchar;
    325 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
    326 		fs = ri->ri_font->stride;
    327 
    328 		/* NOT fontbits if bg is white */
    329 		if (bg) {
    330 			while (height--) {
    331 				rp[0] = ~fr[0];
    332 				rp[1] = ~fr[1];
    333 				fr += fs;
    334 				rp += rs;
    335 			}
    336 		} else {
    337 			while (height--) {
    338 				rp[0] = fr[0];
    339 				rp[1] = fr[1];
    340 				fr += fs;
    341 				rp += rs;
    342 			}
    343 		}
    344 	}
    345 
    346 	/* Do underline */
    347 	if ((attr & 1) != 0)
    348 		*(int16_t *)(rp - (ri->ri_stride << 1)) = fg;
    349 }
    350 #endif	/* !RASOPS_SMALL */
    351 
    352 /*
    353  * Grab routines common to depths where (bpp < 8)
    354  */
    355 #define NAME(ident)	rasops1_##ident
    356 #define PIXEL_SHIFT	0
    357 
    358 #include <dev/rasops/rasops_bitops.h>
    359