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