Home | History | Annotate | Line # | Download | only in rasops
rasops24.c revision 1.44
      1 /* 	$NetBSD: rasops24.c,v 1.44 2019/08/02 04:39:09 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.44 2019/08/02 04:39:09 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 #define	RASOPS_DEPTH	24
     49 #include <dev/rasops/rasops.h>
     50 
     51 static void 	rasops24_erasecols(void *, int, int, int, long);
     52 static void 	rasops24_eraserows(void *, int, int, long);
     53 static void 	rasops24_putchar(void *, int, int, u_int, long);
     54 static void 	rasops24_putchar_aa(void *, int, int, u_int, long);
     55 static __inline void
     56 		rasops24_makestamp1(struct rasops_info *, uint32_t *,
     57 				    uint32_t, uint32_t, uint32_t, uint32_t);
     58 #ifndef RASOPS_SMALL
     59 static void 	rasops24_putchar8(void *, int, int, u_int, long);
     60 static void 	rasops24_putchar12(void *, int, int, u_int, long);
     61 static void 	rasops24_putchar16(void *, int, int, u_int, long);
     62 static void	rasops24_makestamp(struct rasops_info *, long);
     63 #endif
     64 
     65 /*
     66  * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
     67  * destination uint32_t[0] = STAMP_READ(offset)
     68  * destination uint32_t[1] = STAMP_READ(offset + 4)
     69  * destination uint32_t[2] = STAMP_READ(offset + 8)
     70  */
     71 #define	STAMP_SHIFT(fb, n)	((n) ? (fb) : (fb) << 4)
     72 #define	STAMP_MASK		(0xf << 4)
     73 #define	STAMP_READ(o)		(*(uint32_t *)((uint8_t *)stamp + (o)))
     74 
     75 /*
     76  * Initialize rasops_info struct for this colordepth.
     77  */
     78 void
     79 rasops24_init(struct rasops_info *ri)
     80 {
     81 
     82 	if (ri->ri_rnum == 0) {
     83 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
     84 
     85 		ri->ri_rpos = 0;
     86 		ri->ri_gpos = 8;
     87 		ri->ri_bpos = 16;
     88 	}
     89 
     90 	ri->ri_ops.erasecols = rasops24_erasecols;
     91 	ri->ri_ops.eraserows = rasops24_eraserows;
     92 
     93 	if (FONT_IS_ALPHA(ri->ri_font)) {
     94 		ri->ri_ops.putchar = rasops24_putchar_aa;
     95 		return;
     96 	}
     97 
     98 	switch (ri->ri_font->fontwidth) {
     99 #ifndef RASOPS_SMALL
    100 	case 8:
    101 		ri->ri_ops.putchar = rasops24_putchar8;
    102 		break;
    103 	case 12:
    104 		ri->ri_ops.putchar = rasops24_putchar12;
    105 		break;
    106 	case 16:
    107 		ri->ri_ops.putchar = rasops24_putchar16;
    108 		break;
    109 #endif
    110 	default:
    111 		ri->ri_ops.putchar = rasops24_putchar;
    112 		return;
    113 	}
    114 
    115 #ifndef RASOPS_SMALL
    116 	rasops_allocstamp(ri, sizeof(uint32_t) * 64);
    117 #endif
    118 }
    119 
    120 #include "rasops_putchar.h"
    121 #include "rasops_putchar_aa.h"
    122 
    123 static __inline void
    124 rasops24_makestamp1(struct rasops_info *ri, uint32_t *stamp,
    125     uint32_t c1, uint32_t c2, uint32_t c3, uint32_t c4)
    126 {
    127 
    128 	stamp[0] = (c1 <<  8) | (c2 >> 16);
    129 	stamp[1] = (c2 << 16) | (c3 >>  8);
    130 	stamp[2] = (c3 << 24) |  c4;
    131 
    132 #if BYTE_ORDER == LITTLE_ENDIAN
    133 	if ((ri->ri_flg & RI_BSWAP) == 0)
    134 #else
    135 	if ((ri->ri_flg & RI_BSWAP) != 0)
    136 #endif
    137 	{
    138 		stamp[0] = bswap32(stamp[0]);
    139 		stamp[1] = bswap32(stamp[1]);
    140 		stamp[2] = bswap32(stamp[2]);
    141 	}
    142 }
    143 
    144 #ifndef RASOPS_SMALL
    145 /*
    146  * Recompute the blitting stamp.
    147  */
    148 static void
    149 rasops24_makestamp(struct rasops_info *ri, long attr)
    150 {
    151 	uint32_t *stamp = (uint32_t *)ri->ri_stamp;
    152 	uint32_t fg, bg, c1, c2, c3, c4;
    153 	int i;
    154 
    155 	fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xffffff;
    156 	bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
    157 	ri->ri_stamp_attr = attr;
    158 
    159 	for (i = 0; i < 64; i += 4) {
    160 #if BYTE_ORDER == LITTLE_ENDIAN
    161 		c1 = i & 32 ? fg : bg;
    162 		c2 = i & 16 ? fg : bg;
    163 		c3 = i &  8 ? fg : bg;
    164 		c4 = i &  4 ? fg : bg;
    165 #else
    166 		c1 = i &  8 ? fg : bg;
    167 		c2 = i &  4 ? fg : bg;
    168 		c3 = i & 16 ? fg : bg;
    169 		c4 = i & 32 ? fg : bg;
    170 #endif
    171 		rasops24_makestamp1(ri, &stamp[i], c1, c2, c3, c4);
    172 	}
    173 }
    174 
    175 #define	RASOPS_WIDTH	8
    176 #include "rasops_putchar_width.h"
    177 #undef	RASOPS_WIDTH
    178 
    179 #define	RASOPS_WIDTH	12
    180 #include "rasops_putchar_width.h"
    181 #undef	RASOPS_WIDTH
    182 
    183 #define	RASOPS_WIDTH	16
    184 #include "rasops_putchar_width.h"
    185 #undef	RASOPS_WIDTH
    186 
    187 #endif	/* !RASOPS_SMALL */
    188 
    189 /*
    190  * Erase rows. This is nice and easy due to alignment.
    191  */
    192 static void
    193 rasops24_eraserows(void *cookie, int row, int num, long attr)
    194 {
    195 	struct rasops_info *ri = (struct rasops_info *)cookie;
    196 	uint32_t *buf = (uint32_t *)ri->ri_buf;
    197 	int full, slop, cnt, stride;
    198 	uint32_t *rp, *dp, *hp, clr, stamp[3];
    199 
    200 	hp = NULL;	/* XXX GCC */
    201 
    202 	/*
    203 	 * If the color is gray, we can cheat and use the generic routines
    204 	 * (which are faster, hopefully) since the r,g,b values are the same.
    205 	 */
    206 	if ((attr & WSATTR_PRIVATE2) != 0) {
    207 		rasops_eraserows(cookie, row, num, attr);
    208 		return;
    209 	}
    210 
    211 #ifdef RASOPS_CLIPPING
    212 	if (row < 0) {
    213 		num += row;
    214 		row = 0;
    215 	}
    216 
    217 	if (row + num > ri->ri_rows)
    218 		num = ri->ri_rows - row;
    219 
    220 	if (num <= 0)
    221 		return;
    222 #endif
    223 
    224 	clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xffffff;
    225 	rasops24_makestamp1(ri, stamp, clr, clr, clr, clr);
    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 	rasops24_makestamp1(ri, stamp, clr, clr, clr, clr);
    321 
    322 	/*
    323 	 * The current byte offset mod 4 tells us the number of 24-bit pels
    324 	 * we need to write for alignment to 32-bits. Once we're aligned on
    325 	 * a 32-bit boundary, we're also aligned on a 4 pixel boundary, so
    326 	 * the stamp does not need to be rotated. The following shows the
    327 	 * layout of 4 pels in a 3 word region and illustrates this:
    328 	 *
    329 	 *	aaab bbcc cddd
    330 	 */
    331 	slop1 = (uintptr_t)rp & 3;
    332 	cnt = slop1;
    333 	full = (num /* - cnt */) / 4;
    334 	cnt += full * 4;
    335 	slop2 = num - cnt;
    336 
    337 	/* Align to 4 bytes */
    338 	/* XXX handle with masks, bring under control of RI_BSWAP */
    339 	dbp = buf;
    340 	for (cnt = slop1; cnt; cnt--) {
    341 		*dbp++ = (clr >> 16);
    342 		*dbp++ = (clr >> 8);
    343 		*dbp++ =  clr;
    344 	}
    345 
    346 	/* 4 pels per loop */
    347 	dp = (uint32_t *)dbp;
    348 	for (cnt = full; cnt; cnt--) {
    349 		dp[0] = stamp[0];
    350 		dp[1] = stamp[1];
    351 		dp[2] = stamp[2];
    352 		dp += 3;
    353 	}
    354 
    355 	/* Trailing slop */
    356 	/* XXX handle with masks, bring under control of RI_BSWAP */
    357 	dbp = (uint8_t *)dp;
    358 	for (cnt = slop2; cnt; cnt--) {
    359 		*dbp++ = (clr >> 16);
    360 		*dbp++ = (clr >> 8);
    361 		*dbp++ =  clr;
    362 	}
    363 
    364 	num *= 3;
    365 
    366 	while (height--) {
    367 		memcpy(rp, buf, num);
    368 		rp += ri->ri_stride;
    369 		if (ri->ri_hwbits) {
    370 			memcpy(hp, buf, num);
    371 			hp += ri->ri_stride;
    372 		}
    373 	}
    374 }
    375