Home | History | Annotate | Line # | Download | only in rasops
rasops1.c revision 1.28
      1 /* 	$NetBSD: rasops1.c,v 1.28 2019/07/25 03:02:44 rin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: rasops1.c,v 1.28 2019/07/25 03:02:44 rin 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 	uint32_t height, width;
     92 	struct rasops_info *ri = (struct rasops_info *)cookie;
     93 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
     94 	uint32_t *rp, *hrp = NULL, tmp, tmp2;
     95 	uint8_t *fr;
     96 
     97 #ifdef RASOPS_CLIPPING
     98 	/* Catches 'row < 0' case too */
     99 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    100 		return;
    101 
    102 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    103 		return;
    104 #endif
    105 
    106 	col *= ri->ri_font->fontwidth;
    107 	rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
    108 	    ((col >> 3) & ~3));
    109 	if (ri->ri_hwbits)
    110 		hrp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale +
    111 		    ((col >> 3) & ~3));
    112 	height = font->fontheight;
    113 	width = font->fontwidth;
    114 	col = col & 31;
    115 	rs = ri->ri_stride;
    116 
    117 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    118 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    119 
    120 	/* If fg and bg match this becomes a space character */
    121 	if (fg == bg || uc == ' ') {
    122 		uc = (u_int)-1;
    123 		fr = 0;		/* shutup gcc */
    124 		fs = 0;		/* shutup gcc */
    125 	} else {
    126 		fr = FONT_GLYPH(uc, font, ri);
    127 		fs = font->stride;
    128 	}
    129 
    130 	/* Single word, one mask */
    131 	if ((col + width) <= 32) {
    132 		rmask = rasops_pmask[col][width];
    133 		lmask = ~rmask;
    134 
    135 		if (uc == (u_int)-1) {
    136 			bg &= rmask;
    137 
    138 			while (height--) {
    139 				tmp = (*rp & lmask) | bg;
    140 				*rp = tmp;
    141 				DELTA(rp, rs, uint32_t *);
    142 				if (ri->ri_hwbits) {
    143 					*hrp = tmp;
    144 					DELTA(hrp, rs, uint32_t *);
    145 				}
    146 			}
    147 		} else {
    148 			/* NOT fontbits if bg is white */
    149 			if (bg) {
    150 				while (height--) {
    151 					fb = ~(fr[3] | (fr[2] << 8) |
    152 					    (fr[1] << 16) | (fr[0] << 24));
    153 					tmp = (*rp & lmask)
    154 					    | (MBE(fb >> col) & rmask);
    155 					*rp = tmp;
    156 
    157 					fr += fs;
    158 					DELTA(rp, rs, uint32_t *);
    159 					if (ri->ri_hwbits) {
    160 						*hrp = tmp;
    161 						DELTA(hrp, rs, uint32_t *);
    162 					}
    163 				}
    164 			} else {
    165 				while (height--) {
    166 					fb = (fr[3] | (fr[2] << 8) |
    167 					    (fr[1] << 16) | (fr[0] << 24));
    168 					tmp = (*rp & lmask)
    169 					    | (MBE(fb >> col) & rmask);
    170 					*rp = tmp;
    171 
    172 					fr += fs;
    173 					DELTA(rp, rs, uint32_t *);
    174 					if (ri->ri_hwbits) {
    175 						*hrp = tmp;
    176 						DELTA(hrp, rs, uint32_t *);
    177 					}
    178 				}
    179 			}
    180 		}
    181 
    182 		/* Do underline */
    183 		if ((attr & WSATTR_UNDERLINE) != 0) {
    184 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    185 			tmp = (*rp & lmask) | (fg & rmask);
    186 			*rp = tmp;
    187 			if (ri->ri_hwbits) {
    188 				DELTA(hrp, -(ri->ri_stride << 1), uint32_t *);
    189 				*hrp = tmp;
    190 			}
    191 		}
    192 	} else {
    193 		lmask = ~rasops_lmask[col];
    194 		rmask = ~rasops_rmask[(col + width) & 31];
    195 
    196 		if (uc == (u_int)-1) {
    197 			width = bg & ~rmask;
    198 			bg = bg & ~lmask;
    199 
    200 			while (height--) {
    201 				tmp = (rp[0] & lmask) | bg;
    202 				tmp2 = (rp[1] & rmask) | width;
    203 				rp[0] = tmp;
    204 				rp[1] = tmp2;
    205 				DELTA(rp, rs, uint32_t *);
    206 				if (ri->ri_hwbits) {
    207 					hrp[0] = tmp;
    208 					hrp[1] = tmp2;
    209 					DELTA(hrp, rs, uint32_t *);
    210 				}
    211 			}
    212 		} else {
    213 			width = 32 - col;
    214 
    215 			/* NOT fontbits if bg is white */
    216 			if (bg) {
    217 				while (height--) {
    218 					fb = ~(fr[3] | (fr[2] << 8) |
    219 					    (fr[1] << 16) | (fr[0] << 24));
    220 
    221 					tmp = (rp[0] & lmask) |
    222 					    MBE((u_int)fb >> col);
    223 
    224 					tmp2 = (rp[1] & rmask) |
    225 					    (MBE((u_int)fb << width) & ~rmask);
    226 					rp[0] = tmp;
    227 					rp[1] = tmp2;
    228 					fr += fs;
    229 					DELTA(rp, rs, uint32_t *);
    230 					if (ri->ri_hwbits) {
    231 						hrp[0] = tmp;
    232 						hrp[1] = tmp2;
    233 						DELTA(hrp, rs, uint32_t *);
    234 					}
    235 				}
    236 			} else {
    237 				while (height--) {
    238 					fb = (fr[3] | (fr[2] << 8) |
    239 					    (fr[1] << 16) | (fr[0] << 24));
    240 
    241 					tmp = (rp[0] & lmask) |
    242 					    MBE(fb >> col);
    243 
    244 					tmp2 = (rp[1] & rmask) |
    245 					    (MBE(fb << width) & ~rmask);
    246 					rp[0] = tmp;
    247 					rp[1] = tmp2;
    248 					fr += fs;
    249 					DELTA(rp, rs, uint32_t *);
    250 					if (ri->ri_hwbits) {
    251 						hrp[0] = tmp;
    252 						hrp[1] = tmp2;
    253 						DELTA(hrp, rs, uint32_t *);
    254 					}
    255 				}
    256 			}
    257 		}
    258 
    259 		/* Do underline */
    260 		if ((attr & WSATTR_UNDERLINE) != 0) {
    261 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    262 			tmp = (rp[0] & lmask) | (fg & ~lmask);
    263 			tmp2 = (rp[1] & rmask) | (fg & ~rmask);
    264 			rp[0] = tmp;
    265 			rp[1] = tmp2;
    266 			if (ri->ri_hwbits) {
    267 				DELTA(hrp, -(ri->ri_stride << 1), uint32_t *);
    268 				hrp[0] = tmp;
    269 				hrp[1] = tmp2;
    270 			}
    271 		}
    272 	}
    273 }
    274 
    275 #ifndef RASOPS_SMALL
    276 /*
    277  * Paint a single character. This is for 8-pixel wide fonts.
    278  */
    279 static void
    280 rasops1_putchar8(void *cookie, int row, int col, u_int uc, long attr)
    281 {
    282 	int height, fs, rs, bg, fg;
    283 	struct rasops_info *ri = (struct rasops_info *)cookie;
    284 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    285 	uint8_t *fr, *rp, *hrp = NULL;
    286 
    287 #ifdef RASOPS_CLIPPING
    288 	/* Catches 'row < 0' case too */
    289 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    290 		return;
    291 
    292 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    293 		return;
    294 #endif
    295 
    296 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    297 	if (ri->ri_hwbits)
    298 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
    299 	height = font->fontheight;
    300 	rs = ri->ri_stride;
    301 
    302 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    303 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    304 
    305 	/* If fg and bg match this becomes a space character */
    306 	if (fg == bg || uc == ' ') {
    307 		while (height--) {
    308 			*rp = bg;
    309 			rp += rs;
    310 			if (ri->ri_hwbits) {
    311 				*hrp = bg;
    312 				hrp += rs;
    313 			}
    314 		}
    315 	} else {
    316 		fr = FONT_GLYPH(uc, font, ri);
    317 		fs = font->stride;
    318 
    319 		/* NOT fontbits if bg is white */
    320 		if (bg) {
    321 			while (height--) {
    322 				*rp = ~*fr;
    323 				rp += rs;
    324 				if (ri->ri_hwbits) {
    325 					*hrp = ~*fr;
    326 					hrp += rs;
    327 				}
    328 				fr += fs;
    329 
    330 			}
    331 		} else {
    332 			while (height--) {
    333 				*rp = *fr;
    334 				rp += rs;
    335 				if (ri->ri_hwbits) {
    336 					*hrp = *fr;
    337 					hrp += rs;
    338 				}
    339 				fr += fs;
    340 			}
    341 		}
    342 
    343 	}
    344 
    345 	/* Do underline */
    346 	if ((attr & WSATTR_UNDERLINE) != 0) {
    347 		rp[-(ri->ri_stride << 1)] = fg;
    348 		if (ri->ri_hwbits) {
    349 			hrp[-(ri->ri_stride << 1)] = fg;
    350 		}
    351 	}
    352 }
    353 
    354 /*
    355  * Paint a single character. This is for 16-pixel wide fonts.
    356  */
    357 static void
    358 rasops1_putchar16(void *cookie, int row, int col, u_int uc, long attr)
    359 {
    360 	int height, fs, rs, bg, fg;
    361 	struct rasops_info *ri = (struct rasops_info *)cookie;
    362 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    363 	uint8_t *fr, *rp, *hrp = NULL;
    364 
    365 #ifdef RASOPS_CLIPPING
    366 	/* Catches 'row < 0' case too */
    367 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    368 		return;
    369 
    370 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    371 		return;
    372 #endif
    373 
    374 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    375 	if (ri->ri_hwbits)
    376 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
    377 	height = font->fontheight;
    378 	rs = ri->ri_stride;
    379 
    380 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    381 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    382 
    383 	/* If fg and bg match this becomes a space character */
    384 	if (fg == bg || uc == ' ') {
    385 		while (height--) {
    386 			/* XXX alignment?! */
    387 			*(uint16_t *)rp = bg;
    388 			rp += rs;
    389 			if (ri->ri_hwbits) {
    390 				*(uint16_t *)hrp = bg;
    391 				hrp += rs;
    392 			}
    393 		}
    394 	} else {
    395 		fr = FONT_GLYPH(uc, font, ri);
    396 		fs = font->stride;
    397 
    398 		/* NOT fontbits if bg is white */
    399 		if (bg) {
    400 			while (height--) {
    401 				rp[0] = ~fr[0];
    402 				rp[1] = ~fr[1];
    403 				rp += rs;
    404 				if (ri->ri_hwbits) {
    405 					hrp[0] = ~fr[0];
    406 					hrp[1] = ~fr[1];
    407 					hrp += rs;
    408 				}
    409 				fr += fs;
    410 			}
    411 		} else {
    412 			while (height--) {
    413 				rp[0] = fr[0];
    414 				rp[1] = fr[1];
    415 				rp += rs;
    416 				if (ri->ri_hwbits) {
    417 					hrp[0] = fr[0];
    418 					hrp[1] = fr[1];
    419 					hrp += rs;
    420 				}
    421 				fr += fs;
    422 			}
    423 		}
    424 	}
    425 
    426 	/* Do underline */
    427 	if ((attr & WSATTR_UNDERLINE) != 0) {
    428 		/* XXX alignment?! */
    429 		*(uint16_t *)(rp - (ri->ri_stride << 1)) = fg;
    430 		if (ri->ri_hwbits) {
    431 			*(uint16_t *)(hrp - (ri->ri_stride << 1)) = fg;
    432 		}
    433 	}
    434 }
    435 #endif	/* !RASOPS_SMALL */
    436 
    437 /*
    438  * Grab routines common to depths where (bpp < 8)
    439  */
    440 #define NAME(ident)	rasops1_##ident
    441 #define PIXEL_SHIFT	0
    442 
    443 #include <dev/rasops/rasops_bitops.h>
    444