Home | History | Annotate | Line # | Download | only in rasops
rasops2.c revision 1.23
      1 /* 	$NetBSD: rasops2.c,v 1.23 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: rasops2.c,v 1.23 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	rasops2_copycols(void *, int, int, int, int);
     48 static void	rasops2_erasecols(void *, int, int, int, long);
     49 static void	rasops2_do_cursor(struct rasops_info *);
     50 static void	rasops2_putchar(void *, int, int col, u_int, long);
     51 #ifndef RASOPS_SMALL
     52 static void	rasops2_putchar8(void *, int, int col, u_int, long);
     53 static void	rasops2_putchar12(void *, int, int col, u_int, long);
     54 static void	rasops2_putchar16(void *, int, int col, u_int, long);
     55 static void	rasops2_makestamp(struct rasops_info *, long);
     56 
     57 /*
     58  * 4x1 stamp for optimized character blitting
     59  */
     60 static uint8_t	stamp[16];
     61 static long	stamp_attr;
     62 static int	stamp_mutex;	/* XXX see note in README */
     63 #endif
     64 
     65 /*
     66  * Initialize rasops_info struct for this colordepth.
     67  */
     68 void
     69 rasops2_init(struct rasops_info *ri)
     70 {
     71 
     72 	switch (ri->ri_font->fontwidth) {
     73 #ifndef RASOPS_SMALL
     74 	case 8:
     75 		ri->ri_ops.putchar = rasops2_putchar8;
     76 		break;
     77 	case 12:
     78 		ri->ri_ops.putchar = rasops2_putchar12;
     79 		break;
     80 	case 16:
     81 		ri->ri_ops.putchar = rasops2_putchar16;
     82 		break;
     83 #endif	/* !RASOPS_SMALL */
     84 	default:
     85 		panic("fontwidth not 8/12/16 or RASOPS_SMALL - fixme!");
     86 		ri->ri_ops.putchar = rasops2_putchar;
     87 		break;
     88 	}
     89 
     90 	if ((ri->ri_font->fontwidth & 3) != 0) {
     91 		ri->ri_ops.erasecols = rasops2_erasecols;
     92 		ri->ri_ops.copycols = rasops2_copycols;
     93 		ri->ri_do_cursor = rasops2_do_cursor;
     94 	}
     95 }
     96 
     97 #ifdef notyet
     98 /*
     99  * Paint a single character. This is the generic version, this is ugly.
    100  */
    101 static void
    102 rasops2_putchar(void *cookie, int row, int col, u_int uc, long attr)
    103 {
    104 	int height, width, fs, rs, fb, bg, fg, lmask, rmask;
    105 	struct rasops_info *ri = (struct rasops_info *)cookie;
    106 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    107 	uint32_t *rp;
    108 	uint8_t *fr;
    109 
    110 #ifdef RASOPS_CLIPPING
    111 	/* Catches 'row < 0' case too */
    112 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    113 		return;
    114 
    115 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    116 		return;
    117 #endif
    118 
    119 	width = font->fontwidth << 1;
    120 	height = font->fontheight;
    121 	col *= width;
    122 	rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
    123 	    ((col >> 3) & ~3));
    124 	col = col & 31;
    125 	rs = ri->ri_stride;
    126 
    127 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    128 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    129 
    130 	/* If fg and bg match this becomes a space character */
    131 	if (fg == bg || uc == ' ') {
    132 		uc = (u_int)-1;
    133 		fr = 0;		/* shutup gcc */
    134 		fs = 0;		/* shutup gcc */
    135 	} else {
    136 		fr = FONT_GLYPH(uc, font, ri);
    137 		fs = font->stride;
    138 	}
    139 
    140 	/* Single word, one mask */
    141 	if ((col + width) <= 32) {
    142 		rmask = rasops_pmask[col][width];
    143 		lmask = ~rmask;
    144 
    145 		if (uc == (u_int)-1) {
    146 			bg &= rmask;
    147 
    148 			while (height--) {
    149 				*rp = (*rp & lmask) | bg;
    150 				DELTA(rp, rs, uint32_t *);
    151 			}
    152 		} else {
    153 			while (height--) {
    154 				/* get bits, mask */
    155 				/* compose sl */
    156 				/* mask sl */
    157 				/* put word */
    158 			}
    159 		}
    160 
    161 		/* Do underline */
    162 		if (attr & WSATTR_UNDERLINE) {
    163 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    164 			*rp = (*rp & lmask) | (fg & rmask);
    165 		}
    166 	} else {
    167 		lmask = ~rasops_lmask[col];
    168 		rmask = ~rasops_rmask[(col + width) & 31];
    169 
    170 		if (uc == (u_int)-1) {
    171 			bg = bg & ~lmask;
    172 			width = bg & ~rmask;
    173 
    174 			while (height--) {
    175 				rp[0] = (rp[0] & lmask) | bg;
    176 				rp[1] = (rp[1] & rmask) | width;
    177 				DELTA(rp, rs, uint32_t *);
    178 			}
    179 		} else {
    180 			width = 32 - col;
    181 
    182 			/* NOT fontbits if bg is white */
    183 			while (height--) {
    184 				fb = ~(fr[3] | (fr[2] << 8) |
    185 				    (fr[1] << 16) | (fr[0] << 24));
    186 
    187 				rp[0] = (rp[0] & lmask)
    188 				    | MBE((u_int)fb >> col);
    189 
    190 				rp[1] = (rp[1] & rmask)
    191 				   | (MBE((u_int)fb << width) & ~rmask);
    192 
    193 				fr += fs;
    194 				DELTA(rp, rs, uint32_t *);
    195 			}
    196 		}
    197 
    198 		/* Do underline */
    199 		if (attr & WSATTR_UNDERLINE) {
    200 			DELTA(rp, -(ri->ri_stride << 1), uint32_t *);
    201 			rp[0] = (rp[0] & lmask) | (fg & ~lmask);
    202 			rp[1] = (rp[1] & rmask) | (fg & ~rmask);
    203 		}
    204 	}
    205 }
    206 #endif
    207 
    208 /*
    209  * Put a single character. This is the generic version.
    210  */
    211 static void
    212 rasops2_putchar(void *cookie, int row, int col, u_int uc, long attr)
    213 {
    214 
    215 	/* XXX punt */
    216 }
    217 
    218 #ifndef RASOPS_SMALL
    219 /*
    220  * Recompute the blitting stamp.
    221  */
    222 static void
    223 rasops2_makestamp(struct rasops_info *ri, long attr)
    224 {
    225 	int i, fg, bg;
    226 
    227 	fg = ri->ri_devcmap[(attr >> 24) & 0xf] & 3;
    228 	bg = ri->ri_devcmap[(attr >> 16) & 0xf] & 3;
    229 	stamp_attr = attr;
    230 
    231 	for (i = 0; i < 16; i++) {
    232 #if BYTE_ORDER == BIG_ENDIAN
    233 #define NEED_LITTLE_ENDIAN_STAMP RI_BSWAP
    234 #else
    235 #define NEED_LITTLE_ENDIAN_STAMP 0
    236 #endif
    237 		if ((ri->ri_flg & RI_BSWAP) == NEED_LITTLE_ENDIAN_STAMP) {
    238 			/* littel endian */
    239 			stamp[i] = (i & 8 ? fg : bg);
    240 			stamp[i] |= (i & 4 ? fg : bg) << 2;
    241 			stamp[i] |= (i & 2 ? fg : bg) << 4;
    242 			stamp[i] |= (i & 1 ? fg : bg) << 6;
    243 		} else {
    244 			/* big endian */
    245 			stamp[i] = (i & 1 ? fg : bg);
    246 			stamp[i] |= (i & 2 ? fg : bg) << 2;
    247 			stamp[i] |= (i & 4 ? fg : bg) << 4;
    248 			stamp[i] |= (i & 8 ? fg : bg) << 6;
    249 		}
    250 	}
    251 }
    252 
    253 /*
    254  * Put a single character. This is for 8-pixel wide fonts.
    255  */
    256 static void
    257 rasops2_putchar8(void *cookie, int row, int col, u_int uc, long attr)
    258 {
    259 	struct rasops_info *ri = (struct rasops_info *)cookie;
    260 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    261 	int height, fs, rs;
    262 	uint8_t *fr, *rp;
    263 
    264 	/* Can't risk remaking the stamp if it's already in use */
    265 	if (stamp_mutex++) {
    266 		stamp_mutex--;
    267 		rasops2_putchar(cookie, row, col, uc, attr);
    268 		return;
    269 	}
    270 
    271 #ifdef RASOPS_CLIPPING
    272 	/* Catches 'row < 0' case too */
    273 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
    274 		stamp_mutex--;
    275 		return;
    276 	}
    277 
    278 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
    279 		stamp_mutex--;
    280 		return;
    281 	}
    282 #endif
    283 
    284 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    285 	height = font->fontheight;
    286 	rs = ri->ri_stride;
    287 
    288 	/* Recompute stamp? */
    289 	if (attr != stamp_attr)
    290 		rasops2_makestamp(ri, attr);
    291 
    292 	if (uc == ' ') {
    293 		while (height--) {
    294 			*(uint16_t *)rp = stamp[0];
    295 			rp += rs;
    296 		}
    297 	} else {
    298 		fr = FONT_GLYPH(uc, font, ri);
    299 		fs = font->stride;
    300 
    301 		while (height--) {
    302 			rp[0] = stamp[(*fr >> 4) & 0xf];
    303 			rp[1] = stamp[*fr & 0xf];
    304 			fr += fs;
    305 			rp += rs;
    306 		}
    307 	}
    308 
    309 	/* Do underline */
    310 	if ((attr & WSATTR_UNDERLINE) != 0)
    311 		*(uint16_t *)(rp - (ri->ri_stride << 1)) = stamp[15];
    312 
    313 	stamp_mutex--;
    314 }
    315 
    316 /*
    317  * Put a single character. This is for 12-pixel wide fonts.
    318  */
    319 static void
    320 rasops2_putchar12(void *cookie, int row, int col, u_int uc, long attr)
    321 {
    322 	struct rasops_info *ri = (struct rasops_info *)cookie;
    323 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    324 	int height, fs, rs;
    325 	uint8_t *fr, *rp;
    326 
    327 	/* Can't risk remaking the stamp if it's already in use */
    328 	if (stamp_mutex++) {
    329 		stamp_mutex--;
    330 		rasops2_putchar(cookie, row, col, uc, attr);
    331 		return;
    332 	}
    333 
    334 #ifdef RASOPS_CLIPPING
    335 	/* Catches 'row < 0' case too */
    336 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
    337 		stamp_mutex--;
    338 		return;
    339 	}
    340 
    341 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
    342 		stamp_mutex--;
    343 		return;
    344 	}
    345 #endif
    346 
    347 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    348 	height = font->fontheight;
    349 	rs = ri->ri_stride;
    350 
    351 	/* Recompute stamp? */
    352 	if (attr != stamp_attr)
    353 		rasops2_makestamp(ri, attr);
    354 
    355 	if (uc == ' ') {
    356 		while (height--) {
    357 			rp[0] = rp[1] = rp[2] = stamp[0];
    358 			rp += rs;
    359 		}
    360 	} else {
    361 		fr = FONT_GLYPH(uc, font, ri);
    362 		fs = font->stride;
    363 
    364 		while (height--) {
    365 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
    366 			rp[1] = stamp[fr[0] & 0xf];
    367 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
    368 			fr += fs;
    369 			rp += rs;
    370 		}
    371 	}
    372 
    373 	/* Do underline */
    374 	if ((attr & WSATTR_UNDERLINE) != 0) {
    375 		rp -= ri->ri_stride << 1;
    376 		rp[0] = rp[1] = rp[2] = stamp[15];
    377 	}
    378 
    379 	stamp_mutex--;
    380 }
    381 
    382 /*
    383  * Put a single character. This is for 16-pixel wide fonts.
    384  */
    385 static void
    386 rasops2_putchar16(void *cookie, int row, int col, u_int uc, long attr)
    387 {
    388 	struct rasops_info *ri = (struct rasops_info *)cookie;
    389 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    390 	int height, fs, rs;
    391 	uint8_t *fr, *rp;
    392 
    393 	/* Can't risk remaking the stamp if it's already in use */
    394 	if (stamp_mutex++) {
    395 		stamp_mutex--;
    396 		rasops2_putchar(cookie, row, col, uc, attr);
    397 		return;
    398 	}
    399 
    400 #ifdef RASOPS_CLIPPING
    401 	/* Catches 'row < 0' case too */
    402 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
    403 		stamp_mutex--;
    404 		return;
    405 	}
    406 
    407 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
    408 		stamp_mutex--;
    409 		return;
    410 	}
    411 #endif
    412 
    413 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    414 	height = font->fontheight;
    415 	rs = ri->ri_stride;
    416 
    417 	/* Recompute stamp? */
    418 	if (attr != stamp_attr)
    419 		rasops2_makestamp(ri, attr);
    420 
    421 	if (uc == ' ') {
    422 		while (height--) {
    423 			*(uint32_t *)rp = stamp[0];
    424 			rp += rs;
    425 		}
    426 	} else {
    427 		fr = FONT_GLYPH(uc, font, ri);
    428 		fs = font->stride;
    429 
    430 		while (height--) {
    431 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
    432 			rp[1] = stamp[fr[0] & 0xf];
    433 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
    434 			rp[3] = stamp[fr[1] & 0xf];
    435 			fr += fs;
    436 			rp += rs;
    437 		}
    438 	}
    439 
    440 	/* Do underline */
    441 	if ((attr & WSATTR_UNDERLINE) != 0)
    442 		*(uint32_t *)(rp - (ri->ri_stride << 1)) = stamp[15];
    443 
    444 	stamp_mutex--;
    445 }
    446 #endif	/* !RASOPS_SMALL */
    447 
    448 /*
    449  * Grab routines common to depths where (bpp < 8)
    450  */
    451 #define NAME(ident)	rasops2_##ident
    452 #define PIXEL_SHIFT	1
    453 
    454 #include <dev/rasops/rasops_bitops.h>
    455