Home | History | Annotate | Line # | Download | only in rasops
rasops1.c revision 1.26
      1 /* 	$NetBSD: rasops1.c,v 1.26 2019/07/24 18:33:49 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.26 2019/07/24 18:33:49 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 		uc -= font->firstchar;
    127 		fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
    128 		fs = font->stride;
    129 	}
    130 
    131 	/* Single word, one mask */
    132 	if ((col + width) <= 32) {
    133 		rmask = rasops_pmask[col][width];
    134 		lmask = ~rmask;
    135 
    136 		if (uc == (u_int)-1) {
    137 			bg &= rmask;
    138 
    139 			while (height--) {
    140 				tmp = (*rp & lmask) | bg;
    141 				*rp = tmp;
    142 				DELTA(rp, rs, uint32_t *);
    143 				if (ri->ri_hwbits) {
    144 					*hrp = tmp;
    145 					DELTA(hrp, rs, uint32_t *);
    146 				}
    147 			}
    148 		} else {
    149 			/* NOT fontbits if bg is white */
    150 			if (bg) {
    151 				while (height--) {
    152 					fb = ~(fr[3] | (fr[2] << 8) |
    153 					    (fr[1] << 16) | (fr[0] << 24));
    154 					tmp = (*rp & lmask)
    155 					    | (MBE(fb >> col) & rmask);
    156 					*rp = tmp;
    157 
    158 					fr += fs;
    159 					DELTA(rp, rs, uint32_t *);
    160 					if (ri->ri_hwbits) {
    161 						*hrp = tmp;
    162 						DELTA(hrp, rs, uint32_t *);
    163 					}
    164 				}
    165 			} else {
    166 				while (height--) {
    167 					fb = (fr[3] | (fr[2] << 8) |
    168 					    (fr[1] << 16) | (fr[0] << 24));
    169 					tmp = (*rp & lmask)
    170 					    | (MBE(fb >> col) & rmask);
    171 					*rp = tmp;
    172 
    173 					fr += fs;
    174 					DELTA(rp, rs, uint32_t *);
    175 					if (ri->ri_hwbits) {
    176 						*hrp = tmp;
    177 						DELTA(hrp, rs, uint32_t *);
    178 					}
    179 				}
    180 			}
    181 		}
    182 
    183 		/* Do underline */
    184 		if ((attr & WSATTR_UNDERLINE) != 0) {
    185 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    186 			tmp = (*rp & lmask) | (fg & rmask);
    187 			*rp = tmp;
    188 			if (ri->ri_hwbits) {
    189 				DELTA(hrp, -(ri->ri_stride << 1), uint32_t *);
    190 				*hrp = tmp;
    191 			}
    192 		}
    193 	} else {
    194 		lmask = ~rasops_lmask[col];
    195 		rmask = ~rasops_rmask[(col + width) & 31];
    196 
    197 		if (uc == (u_int)-1) {
    198 			width = bg & ~rmask;
    199 			bg = bg & ~lmask;
    200 
    201 			while (height--) {
    202 				tmp = (rp[0] & lmask) | bg;
    203 				tmp2 = (rp[1] & rmask) | width;
    204 				rp[0] = tmp;
    205 				rp[1] = tmp2;
    206 				DELTA(rp, rs, uint32_t *);
    207 				if (ri->ri_hwbits) {
    208 					hrp[0] = tmp;
    209 					hrp[1] = tmp2;
    210 					DELTA(hrp, rs, uint32_t *);
    211 				}
    212 			}
    213 		} else {
    214 			width = 32 - col;
    215 
    216 			/* NOT fontbits if bg is white */
    217 			if (bg) {
    218 				while (height--) {
    219 					fb = ~(fr[3] | (fr[2] << 8) |
    220 					    (fr[1] << 16) | (fr[0] << 24));
    221 
    222 					tmp = (rp[0] & lmask)
    223 					    | MBE((u_int)fb >> col);
    224 
    225 					tmp2 = (rp[1] & rmask)
    226 					    | (MBE((u_int)fb << width) & ~rmask);
    227 					rp[0] = tmp;
    228 					rp[1] = tmp2;
    229 					fr += fs;
    230 					DELTA(rp, rs, uint32_t *);
    231 					if (ri->ri_hwbits) {
    232 						hrp[0] = tmp;
    233 						hrp[1] = tmp2;
    234 						DELTA(hrp, rs, uint32_t *);
    235 					}
    236 				}
    237 			} else {
    238 				while (height--) {
    239 					fb = (fr[3] | (fr[2] << 8) |
    240 					    (fr[1] << 16) | (fr[0] << 24));
    241 
    242 					tmp = (rp[0] & lmask)
    243 					    | MBE(fb >> col);
    244 
    245 					tmp2 = (rp[1] & rmask)
    246 					    | (MBE(fb << width) & ~rmask);
    247 					rp[0] = tmp;
    248 					rp[1] = tmp2;
    249 					fr += fs;
    250 					DELTA(rp, rs, uint32_t *);
    251 					if (ri->ri_hwbits) {
    252 						hrp[0] = tmp;
    253 						hrp[1] = tmp2;
    254 						DELTA(hrp, rs, uint32_t *);
    255 					}
    256 				}
    257 			}
    258 		}
    259 
    260 		/* Do underline */
    261 		if ((attr & WSATTR_UNDERLINE) != 0) {
    262 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    263 			tmp = (rp[0] & lmask) | (fg & ~lmask);
    264 			tmp2 = (rp[1] & rmask) | (fg & ~rmask);
    265 			rp[0] = tmp;
    266 			rp[1] = tmp2;
    267 			if (ri->ri_hwbits) {
    268 				DELTA(hrp, -(ri->ri_stride << 1), uint32_t *);
    269 				hrp[0] = tmp;
    270 				hrp[1] = tmp2;
    271 			}
    272 		}
    273 	}
    274 }
    275 
    276 #ifndef RASOPS_SMALL
    277 /*
    278  * Paint a single character. This is for 8-pixel wide fonts.
    279  */
    280 static void
    281 rasops1_putchar8(void *cookie, int row, int col, u_int uc, long attr)
    282 {
    283 	int height, fs, rs, bg, fg;
    284 	struct rasops_info *ri = (struct rasops_info *)cookie;
    285 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    286 	uint8_t *fr, *rp, *hrp = NULL;
    287 
    288 #ifdef RASOPS_CLIPPING
    289 	/* Catches 'row < 0' case too */
    290 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    291 		return;
    292 
    293 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    294 		return;
    295 #endif
    296 
    297 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    298 	if (ri->ri_hwbits)
    299 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
    300 	height = font->fontheight;
    301 	rs = ri->ri_stride;
    302 
    303 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    304 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    305 
    306 	/* If fg and bg match this becomes a space character */
    307 	if (fg == bg || uc == ' ') {
    308 		while (height--) {
    309 			*rp = bg;
    310 			rp += rs;
    311 			if (ri->ri_hwbits) {
    312 				*hrp = bg;
    313 				hrp += rs;
    314 			}
    315 		}
    316 	} else {
    317 		uc -= font->firstchar;
    318 		fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
    319 		fs = font->stride;
    320 
    321 		/* NOT fontbits if bg is white */
    322 		if (bg) {
    323 			while (height--) {
    324 				*rp = ~*fr;
    325 				rp += rs;
    326 				if (ri->ri_hwbits) {
    327 					*hrp = ~*fr;
    328 					hrp += rs;
    329 				}
    330 				fr += fs;
    331 
    332 			}
    333 		} else {
    334 			while (height--) {
    335 				*rp = *fr;
    336 				rp += rs;
    337 				if (ri->ri_hwbits) {
    338 					*hrp = *fr;
    339 					hrp += rs;
    340 				}
    341 				fr += fs;
    342 			}
    343 		}
    344 
    345 	}
    346 
    347 	/* Do underline */
    348 	if ((attr & WSATTR_UNDERLINE) != 0) {
    349 		rp[-(ri->ri_stride << 1)] = fg;
    350 		if (ri->ri_hwbits) {
    351 			hrp[-(ri->ri_stride << 1)] = fg;
    352 		}
    353 	}
    354 }
    355 
    356 /*
    357  * Paint a single character. This is for 16-pixel wide fonts.
    358  */
    359 static void
    360 rasops1_putchar16(void *cookie, int row, int col, u_int uc, long attr)
    361 {
    362 	int height, fs, rs, bg, fg;
    363 	struct rasops_info *ri = (struct rasops_info *)cookie;
    364 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    365 	uint8_t *fr, *rp, *hrp = NULL;
    366 
    367 #ifdef RASOPS_CLIPPING
    368 	/* Catches 'row < 0' case too */
    369 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    370 		return;
    371 
    372 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    373 		return;
    374 #endif
    375 
    376 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    377 	if (ri->ri_hwbits)
    378 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
    379 	height = font->fontheight;
    380 	rs = ri->ri_stride;
    381 
    382 	bg = (attr & 0x000f0000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    383 	fg = (attr & 0x0f000000) ? ri->ri_devcmap[1] : ri->ri_devcmap[0];
    384 
    385 	/* If fg and bg match this becomes a space character */
    386 	if (fg == bg || uc == ' ') {
    387 		while (height--) {
    388 			/* XXX alignment?! */
    389 			*(uint16_t *)rp = bg;
    390 			rp += rs;
    391 			if (ri->ri_hwbits) {
    392 				*(uint16_t *)hrp = bg;
    393 				hrp += rs;
    394 			}
    395 		}
    396 	} else {
    397 		uc -= font->firstchar;
    398 		fr = (uint8_t *)font->data + uc * ri->ri_fontscale;
    399 		fs = font->stride;
    400 
    401 		/* NOT fontbits if bg is white */
    402 		if (bg) {
    403 			while (height--) {
    404 				rp[0] = ~fr[0];
    405 				rp[1] = ~fr[1];
    406 				rp += rs;
    407 				if (ri->ri_hwbits) {
    408 					hrp[0] = ~fr[0];
    409 					hrp[1] = ~fr[1];
    410 					hrp += rs;
    411 				}
    412 				fr += fs;
    413 			}
    414 		} else {
    415 			while (height--) {
    416 				rp[0] = fr[0];
    417 				rp[1] = fr[1];
    418 				rp += rs;
    419 				if (ri->ri_hwbits) {
    420 					hrp[0] = fr[0];
    421 					hrp[1] = fr[1];
    422 					hrp += rs;
    423 				}
    424 				fr += fs;
    425 			}
    426 		}
    427 	}
    428 
    429 	/* Do underline */
    430 	if ((attr & WSATTR_UNDERLINE) != 0) {
    431 		/* XXX alignment?! */
    432 		*(uint16_t *)(rp - (ri->ri_stride << 1)) = fg;
    433 		if (ri->ri_hwbits) {
    434 			*(uint16_t *)(hrp - (ri->ri_stride << 1)) = fg;
    435 		}
    436 	}
    437 }
    438 #endif	/* !RASOPS_SMALL */
    439 
    440 /*
    441  * Grab routines common to depths where (bpp < 8)
    442  */
    443 #define NAME(ident)	rasops1_##ident
    444 #define PIXEL_SHIFT	0
    445 
    446 #include <dev/rasops/rasops_bitops.h>
    447