Home | History | Annotate | Line # | Download | only in rasops
rasops24.c revision 1.39
      1 /* 	$NetBSD: rasops24.c,v 1.39 2019/07/30 15:23:23 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: rasops24.c,v 1.39 2019/07/30 15:23:23 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 
     41 #include <machine/endian.h>
     42 #include <sys/bswap.h>
     43 
     44 #include <dev/wscons/wsdisplayvar.h>
     45 #include <dev/wscons/wsconsio.h>
     46 #include <dev/rasops/rasops.h>
     47 
     48 static void 	rasops24_erasecols(void *, int, int, int, long);
     49 static void 	rasops24_eraserows(void *, int, int, long);
     50 static void 	rasops24_putchar(void *, int, int, u_int, long);
     51 static void 	rasops24_putchar_aa(void *, int, int, u_int, long);
     52 #ifndef RASOPS_SMALL
     53 static void 	rasops24_putchar8(void *, int, int, u_int, long);
     54 static void 	rasops24_putchar12(void *, int, int, u_int, long);
     55 static void 	rasops24_putchar16(void *, int, int, u_int, long);
     56 static void	rasops24_makestamp(struct rasops_info *, long);
     57 
     58 /*
     59  * 4x1 stamp for optimized character blitting
     60  */
     61 static uint32_t	stamp[64];
     62 static long	stamp_attr;
     63 static int	stamp_mutex;	/* XXX see note in readme */
     64 #endif
     65 
     66 /*
     67  * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
     68  * destination uint32_t[0] = STAMP_READ(offset)
     69  * destination uint32_t[1] = STAMP_READ(offset + 4)
     70  * destination uint32_t[2] = STAMP_READ(offset + 8)
     71  */
     72 #define	STAMP_SHIFT(fb, n)	((n) ? (fb) : (fb) << 4)
     73 #define	STAMP_MASK		(0xf << 4)
     74 #define	STAMP_READ(o)		(*(uint32_t *)((uint8_t *)stamp + (o)))
     75 
     76 /*
     77  * Initialize rasops_info struct for this colordepth.
     78  */
     79 void
     80 rasops24_init(struct rasops_info *ri)
     81 {
     82 
     83 #ifndef RASOPS_SMALL
     84 	/*
     85 	 * Different devcmap's are used depending on font widths,
     86 	 * therefore we need reset stamp here.
     87 	 */
     88 	stamp_attr = 0;
     89 #endif
     90 
     91 	if (ri->ri_rnum == 0) {
     92 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
     93 
     94 		ri->ri_rpos = 0;
     95 		ri->ri_gpos = 8;
     96 		ri->ri_bpos = 16;
     97 	}
     98 
     99 	ri->ri_ops.erasecols = rasops24_erasecols;
    100 	ri->ri_ops.eraserows = rasops24_eraserows;
    101 
    102 	if (FONT_IS_ALPHA(ri->ri_font)) {
    103 		ri->ri_ops.putchar = rasops24_putchar_aa;
    104 		return;
    105 	}
    106 
    107 	switch (ri->ri_font->fontwidth) {
    108 #ifndef RASOPS_SMALL
    109 	case 8:
    110 		ri->ri_ops.putchar = rasops24_putchar8;
    111 		break;
    112 	case 12:
    113 		ri->ri_ops.putchar = rasops24_putchar12;
    114 		break;
    115 	case 16:
    116 		ri->ri_ops.putchar = rasops24_putchar16;
    117 		break;
    118 #endif
    119 	default:
    120 		ri->ri_ops.putchar = rasops24_putchar;
    121 		break;
    122 	}
    123 }
    124 
    125 #define	RASOPS_DEPTH	24
    126 #include "rasops_putchar.h"
    127 #include "rasops_putchar_aa.h"
    128 
    129 #ifndef RASOPS_SMALL
    130 /*
    131  * Recompute the blitting stamp.
    132  */
    133 static void
    134 rasops24_makestamp(struct rasops_info *ri, long attr)
    135 {
    136 	uint32_t fg, bg, c1, c2, c3, c4;
    137 	int i;
    138 
    139 	fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xffffff;
    140 	bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
    141 	stamp_attr = attr;
    142 
    143 	for (i = 0; i < 64; i += 4) {
    144 #if BYTE_ORDER == LITTLE_ENDIAN
    145 		c1 = i & 32 ? fg : bg;
    146 		c2 = i & 16 ? fg : bg;
    147 		c3 = i &  8 ? fg : bg;
    148 		c4 = i &  4 ? fg : bg;
    149 #else
    150 		c1 = i &  8 ? fg : bg;
    151 		c2 = i &  4 ? fg : bg;
    152 		c3 = i & 16 ? fg : bg;
    153 		c4 = i & 32 ? fg : bg;
    154 #endif
    155 		stamp[i + 0] = (c1 <<  8) | (c2 >> 16);
    156 		stamp[i + 1] = (c2 << 16) | (c3 >>  8);
    157 		stamp[i + 2] = (c3 << 24) |  c4;
    158 
    159 #if BYTE_ORDER == LITTLE_ENDIAN
    160 		if ((ri->ri_flg & RI_BSWAP) == 0) {
    161 #else
    162 		if ((ri->ri_flg & RI_BSWAP) != 0) {
    163 #endif
    164 			stamp[i + 0] = bswap32(stamp[i + 0]);
    165 			stamp[i + 1] = bswap32(stamp[i + 1]);
    166 			stamp[i + 2] = bswap32(stamp[i + 2]);
    167 		}
    168 	}
    169 }
    170 
    171 #define	RASOPS_WIDTH	8
    172 #include "rasops_putchar_width.h"
    173 #undef	RASOPS_WIDTH
    174 
    175 #define	RASOPS_WIDTH	12
    176 #include "rasops_putchar_width.h"
    177 #undef	RASOPS_WIDTH
    178 
    179 #define	RASOPS_WIDTH	16
    180 #include "rasops_putchar_width.h"
    181 #undef	RASOPS_WIDTH
    182 
    183 #endif	/* !RASOPS_SMALL */
    184 
    185 /*
    186  * Erase rows. This is nice and easy due to alignment.
    187  */
    188 static void
    189 rasops24_eraserows(void *cookie, int row, int num, long attr)
    190 {
    191 	struct rasops_info *ri = (struct rasops_info *)cookie;
    192 	int n9, n3, n1, cnt, stride;
    193 	uint32_t *rp, *dp, *hp, clr, xstamp[3];
    194 
    195 	hp = NULL;	/* XXX GCC */
    196 
    197 	/*
    198 	 * If the color is gray, we can cheat and use the generic routines
    199 	 * (which are faster, hopefully) since the r,g,b values are the same.
    200 	 */
    201 	if ((attr & WSATTR_PRIVATE2) != 0) {
    202 		rasops_eraserows(cookie, row, num, attr);
    203 		return;
    204 	}
    205 
    206 #ifdef RASOPS_CLIPPING
    207 	if (row < 0) {
    208 		num += row;
    209 		row = 0;
    210 	}
    211 
    212 	if (row + num > ri->ri_rows)
    213 		num = ri->ri_rows - row;
    214 
    215 	if (num <= 0)
    216 		return;
    217 #endif
    218 
    219 	clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
    220 	xstamp[0] = (clr <<  8) | (clr >> 16);
    221 	xstamp[1] = (clr << 16) | (clr >>  8);
    222 	xstamp[2] = (clr << 24) |  clr;
    223 
    224 #if BYTE_ORDER == LITTLE_ENDIAN
    225 	if ((ri->ri_flg & RI_BSWAP) == 0) {
    226 #else
    227 	if ((ri->ri_flg & RI_BSWAP) != 0) {
    228 #endif
    229 		xstamp[0] = bswap32(xstamp[0]);
    230 		xstamp[1] = bswap32(xstamp[1]);
    231 		xstamp[2] = bswap32(xstamp[2]);
    232 	}
    233 
    234 	/*
    235 	 * XXX the wsdisplay_emulops interface seems a little deficient in
    236 	 * that there is no way to clear the *entire* screen. We provide a
    237 	 * workaround here: if the entire console area is being cleared, and
    238 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    239 	 */
    240 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    241 		stride = ri->ri_stride;
    242 		num = ri->ri_height;
    243 		rp = (uint32_t *)ri->ri_origbits;
    244 		if (ri->ri_hwbits)
    245 			hp = (uint32_t *)ri->ri_hworigbits;
    246 	} else {
    247 		stride = ri->ri_emustride;
    248 		num *= ri->ri_font->fontheight;
    249 		rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
    250 		if (ri->ri_hwbits)
    251 			hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale);
    252 	}
    253 
    254 	n9 = stride / (4 * 9);
    255 	cnt = n9 * (4 * 9);
    256 	n3 = (stride - cnt) / (4 * 3);
    257 	cnt += n3 * (4 * 3);
    258 	n1 = (stride - cnt) / 4;
    259 
    260 	while (num--) {
    261 		dp = rp;
    262 		for (cnt = n9; cnt; cnt--) {
    263 			dp[0] = xstamp[0]; dp[1] = xstamp[1]; dp[2] = xstamp[2];
    264 			dp[3] = xstamp[0]; dp[4] = xstamp[1]; dp[5] = xstamp[2];
    265 			dp[6] = xstamp[0]; dp[7] = xstamp[1]; dp[8] = xstamp[2];
    266 			dp += 9;
    267 		}
    268 
    269 		for (cnt = n3; cnt; cnt--) {
    270 			dp[0] = xstamp[0]; dp[1] = xstamp[1]; dp[2] = xstamp[2];
    271 			dp += 3;
    272 		}
    273 
    274 		for (cnt = 0; cnt < n1; cnt++)
    275 			*dp++ = xstamp[cnt];
    276 
    277 		if (ri->ri_hwbits) {
    278 			memcpy(hp, rp, stride);
    279 			DELTA(hp, ri->ri_stride, uint32_t *);
    280 		}
    281 		DELTA(rp, ri->ri_stride, uint32_t *);
    282 	}
    283 }
    284 
    285 /*
    286  * Erase columns.
    287  */
    288 static void
    289 rasops24_erasecols(void *cookie, int row, int col, int num, long attr)
    290 {
    291 	struct rasops_info *ri = (struct rasops_info *)cookie;
    292 	int n12, n4, height, cnt, slop1, slop2, clr, xstamp[3];
    293 	uint32_t *dp;
    294 	uint8_t *rp, *hp, *dbp;
    295 
    296 	hp = NULL;	/* XXX GCC */
    297 
    298 	/*
    299 	 * If the color is gray, we can cheat and use the generic routines
    300 	 * (which are faster, hopefully) since the r,g,b values are the same.
    301 	 */
    302 	if ((attr & WSATTR_PRIVATE2) != 0) {
    303 		rasops_erasecols(cookie, row, col, num, attr);
    304 		return;
    305 	}
    306 
    307 #ifdef RASOPS_CLIPPING
    308 	/* Catches 'row < 0' case too */
    309 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    310 		return;
    311 
    312 	if (col < 0) {
    313 		num += col;
    314 		col = 0;
    315 	}
    316 
    317 	if (col + num > ri->ri_cols)
    318 		num = ri->ri_cols - col;
    319 
    320 	if (num <= 0)
    321 		return;
    322 #endif
    323 
    324 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    325 	if (ri->ri_hwbits)
    326 		hp = ri->ri_hwbits + row * ri->ri_yscale + col * ri->ri_xscale;
    327 
    328 	num *= ri->ri_font->fontwidth;
    329 	height = ri->ri_font->fontheight;
    330 
    331 	clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
    332 	xstamp[0] = (clr <<  8) | (clr >> 16);
    333 	xstamp[1] = (clr << 16) | (clr >>  8);
    334 	xstamp[2] = (clr << 24) |  clr;
    335 
    336 #if BYTE_ORDER == LITTLE_ENDIAN
    337 	if ((ri->ri_flg & RI_BSWAP) == 0) {
    338 #else
    339 	if ((ri->ri_flg & RI_BSWAP) != 0) {
    340 #endif
    341 		xstamp[0] = bswap32(xstamp[0]);
    342 		xstamp[1] = bswap32(xstamp[1]);
    343 		xstamp[2] = bswap32(xstamp[2]);
    344 	}
    345 
    346 	/*
    347 	 * The current byte offset mod 4 tells us the number of 24-bit pels
    348 	 * we need to write for alignment to 32-bits. Once we're aligned on
    349 	 * a 32-bit boundary, we're also aligned on a 4 pixel boundary, so
    350 	 * the stamp does not need to be rotated. The following shows the
    351 	 * layout of 4 pels in a 3 word region and illustrates this:
    352 	 *
    353 	 *	aaab bbcc cddd
    354 	 */
    355 	slop1 = (uintptr_t)rp & 3;
    356 	cnt = slop1;
    357 	n12 = (num - cnt) / 12;
    358 	cnt += n12 * 12;
    359 	n4 = (num - cnt) / 4;
    360 	cnt += n4 * 4;
    361 	slop2 = num - cnt;
    362 
    363 	while (height--) {
    364 		dbp = rp;
    365 
    366 		/* Align to 4 bytes */
    367 		/* XXX handle with masks, bring under control of RI_BSWAP */
    368 		for (cnt = slop1; cnt; cnt--) {
    369 			*dbp++ = (clr >> 16);
    370 			*dbp++ = (clr >> 8);
    371 			*dbp++ =  clr;
    372 		}
    373 
    374 		dp = (uint32_t *)dbp;
    375 
    376 		/* 12 pels per loop */
    377 		for (cnt = n12; cnt; cnt--) {
    378 			dp[0] = xstamp[0]; dp[1] = xstamp[1]; dp[2] = xstamp[2];
    379 			dp[3] = xstamp[0]; dp[4] = xstamp[1]; dp[5] = xstamp[2];
    380 			dp[6] = xstamp[0]; dp[7] = xstamp[1]; dp[8] = xstamp[2];
    381 			dp += 9;
    382 		}
    383 
    384 		/* 4 pels per loop */
    385 		for (cnt = n4; cnt; cnt--) {
    386 			dp[0] = xstamp[0]; dp[1] = xstamp[1]; dp[2] = xstamp[2];
    387 			dp += 3;
    388 		}
    389 
    390 		/* Trailing slop */
    391 		/* XXX handle with masks, bring under control of RI_BSWAP */
    392 		dbp = (uint8_t *)dp;
    393 		for (cnt = slop2; cnt; cnt--) {
    394 			*dbp++ = (clr >> 16);
    395 			*dbp++ = (clr >> 8);
    396 			*dbp++ =  clr;
    397 		}
    398 
    399 		if (ri->ri_hwbits) {
    400 			memcpy(hp, rp, num * 3);
    401 			hp += ri->ri_stride;
    402 		}
    403 		rp += ri->ri_stride;
    404 	}
    405 }
    406