Home | History | Annotate | Line # | Download | only in rasops
rasops24.c revision 1.35
      1  1.35       rin /* 	$NetBSD: rasops24.c,v 1.35 2019/07/25 15:18:53 rin Exp $	*/
      2   1.1        ad 
      3   1.6        ad /*-
      4   1.6        ad  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.6        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13        ad  * by Andrew Doran.
      9   1.6        ad  *
     10   1.1        ad  * Redistribution and use in source and binary forms, with or without
     11   1.1        ad  * modification, are permitted provided that the following conditions
     12   1.1        ad  * are met:
     13   1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14   1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15   1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1        ad  *    documentation and/or other materials provided with the distribution.
     18   1.1        ad  *
     19   1.6        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.6        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.6        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.6        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.6        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.6        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.6        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.6        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.6        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.6        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.6        ad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1        ad  */
     31   1.2        ad 
     32  1.15     lukem #include <sys/cdefs.h>
     33  1.35       rin __KERNEL_RCSID(0, "$NetBSD: rasops24.c,v 1.35 2019/07/25 15:18:53 rin Exp $");
     34  1.15     lukem 
     35   1.1        ad #include "opt_rasops.h"
     36   1.1        ad 
     37   1.1        ad #include <sys/param.h>
     38   1.1        ad #include <sys/systm.h>
     39   1.1        ad #include <sys/time.h>
     40   1.1        ad 
     41   1.4        ad #include <machine/endian.h>
     42  1.22       dsl #include <sys/bswap.h>
     43   1.4        ad 
     44   1.1        ad #include <dev/wscons/wsdisplayvar.h>
     45   1.1        ad #include <dev/wscons/wsconsio.h>
     46   1.1        ad #include <dev/rasops/rasops.h>
     47   1.1        ad 
     48  1.18     perry static void 	rasops24_erasecols(void *, int, int, int, long);
     49  1.18     perry static void 	rasops24_eraserows(void *, int, int, long);
     50  1.18     perry static void 	rasops24_putchar(void *, int, int, u_int, long attr);
     51   1.9        ad #ifndef RASOPS_SMALL
     52  1.18     perry static void 	rasops24_putchar8(void *, int, int, u_int, long attr);
     53  1.18     perry static void 	rasops24_putchar12(void *, int, int, u_int, long attr);
     54  1.18     perry static void 	rasops24_putchar16(void *, int, int, u_int, long attr);
     55  1.18     perry static void	rasops24_makestamp(struct rasops_info *, long);
     56   1.1        ad 
     57  1.12        pk /*
     58  1.12        pk  * 4x1 stamp for optimized character blitting
     59   1.4        ad  */
     60  1.32       rin static uint32_t	stamp[64];
     61   1.4        ad static long	stamp_attr;
     62   1.4        ad static int	stamp_mutex;	/* XXX see note in readme */
     63  1.29     njoly #endif
     64   1.4        ad 
     65   1.4        ad /*
     66   1.4        ad  * XXX this confuses the hell out of gcc2 (not egcs) which always insists
     67   1.4        ad  * that the shift count is negative.
     68   1.4        ad  *
     69   1.4        ad  * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
     70  1.32       rin  * destination uint32_t[0] = STAMP_READ(offset)
     71  1.32       rin  * destination uint32_t[1] = STAMP_READ(offset + 4)
     72  1.32       rin  * destination uint32_t[2] = STAMP_READ(offset + 8)
     73   1.4        ad  */
     74   1.4        ad #define STAMP_SHIFT(fb,n)	((n*4-4) >= 0 ? (fb)>>(n*4-4):(fb)<<-(n*4-4))
     75  1.12        pk #define STAMP_MASK		(0xf << 4)
     76  1.32       rin #define STAMP_READ(o)		(*(uint32_t *)((char *)stamp + (o)))
     77   1.4        ad 
     78   1.1        ad /*
     79  1.14       wiz  * Initialize rasops_info struct for this colordepth.
     80   1.1        ad  */
     81   1.1        ad void
     82  1.26       dsl rasops24_init(struct rasops_info *ri)
     83   1.1        ad {
     84   1.1        ad 
     85   1.1        ad 	switch (ri->ri_font->fontwidth) {
     86   1.9        ad #ifndef RASOPS_SMALL
     87   1.1        ad 	case 8:
     88   1.4        ad 		ri->ri_ops.putchar = rasops24_putchar8;
     89   1.1        ad 		break;
     90   1.1        ad 	case 12:
     91   1.4        ad 		ri->ri_ops.putchar = rasops24_putchar12;
     92   1.1        ad 		break;
     93   1.1        ad 	case 16:
     94   1.4        ad 		ri->ri_ops.putchar = rasops24_putchar16;
     95   1.1        ad 		break;
     96   1.9        ad #endif
     97   1.1        ad 	default:
     98   1.4        ad 		ri->ri_ops.putchar = rasops24_putchar;
     99   1.1        ad 		break;
    100   1.1        ad 	}
    101   1.4        ad 
    102   1.1        ad 	if (ri->ri_rnum == 0) {
    103   1.1        ad 		ri->ri_rnum = 8;
    104   1.4        ad 		ri->ri_rpos = 0;
    105   1.1        ad 		ri->ri_gnum = 8;
    106   1.4        ad 		ri->ri_gpos = 8;
    107   1.1        ad 		ri->ri_bnum = 8;
    108   1.1        ad 		ri->ri_bpos = 16;
    109   1.1        ad 	}
    110   1.4        ad 
    111   1.4        ad 	ri->ri_ops.erasecols = rasops24_erasecols;
    112   1.4        ad 	ri->ri_ops.eraserows = rasops24_eraserows;
    113   1.1        ad }
    114   1.1        ad 
    115  1.35       rin #define	RASOPS_DEPTH	24
    116  1.35       rin #include "rasops_putchar.h"
    117   1.1        ad 
    118   1.9        ad #ifndef RASOPS_SMALL
    119   1.9        ad /*
    120   1.9        ad  * Recompute the blitting stamp.
    121   1.9        ad  */
    122   1.9        ad static void
    123  1.26       dsl rasops24_makestamp(struct rasops_info *ri, long attr)
    124   1.9        ad {
    125   1.9        ad 	u_int fg, bg, c1, c2, c3, c4;
    126   1.9        ad 	int i;
    127  1.12        pk 
    128  1.12        pk 	fg = ri->ri_devcmap[((u_int)attr >> 24) & 0xf] & 0xffffff;
    129  1.12        pk 	bg = ri->ri_devcmap[((u_int)attr >> 16) & 0xf] & 0xffffff;
    130   1.9        ad 	stamp_attr = attr;
    131  1.12        pk 
    132   1.9        ad 	for (i = 0; i < 64; i += 4) {
    133   1.9        ad #if BYTE_ORDER == LITTLE_ENDIAN
    134   1.9        ad 		c1 = (i & 32 ? fg : bg);
    135   1.9        ad 		c2 = (i & 16 ? fg : bg);
    136   1.9        ad 		c3 = (i & 8 ? fg : bg);
    137   1.9        ad 		c4 = (i & 4 ? fg : bg);
    138   1.9        ad #else
    139   1.9        ad 		c1 = (i & 8 ? fg : bg);
    140   1.9        ad 		c2 = (i & 4 ? fg : bg);
    141   1.9        ad 		c3 = (i & 16 ? fg : bg);
    142   1.9        ad 		c4 = (i & 32 ? fg : bg);
    143   1.9        ad #endif
    144   1.9        ad 		stamp[i+0] = (c1 <<  8) | (c2 >> 16);
    145   1.9        ad 		stamp[i+1] = (c2 << 16) | (c3 >>  8);
    146   1.9        ad 		stamp[i+2] = (c3 << 24) | c4;
    147   1.9        ad 
    148   1.9        ad #if BYTE_ORDER == LITTLE_ENDIAN
    149   1.9        ad 		if ((ri->ri_flg & RI_BSWAP) == 0) {
    150   1.9        ad #else
    151   1.9        ad 		if ((ri->ri_flg & RI_BSWAP) != 0) {
    152   1.9        ad #endif
    153   1.9        ad 			stamp[i+0] = bswap32(stamp[i+0]);
    154   1.9        ad 			stamp[i+1] = bswap32(stamp[i+1]);
    155   1.9        ad 			stamp[i+2] = bswap32(stamp[i+2]);
    156   1.9        ad 		}
    157   1.9        ad 	}
    158   1.9        ad }
    159   1.1        ad 
    160  1.35       rin #define	RASOPS_WIDTH	8
    161  1.35       rin #include "rasops_putchar_width.h"
    162  1.35       rin #undef	RASOPS_WIDTH
    163  1.35       rin 
    164  1.35       rin #define	RASOPS_WIDTH	12
    165  1.35       rin #include "rasops_putchar_width.h"
    166  1.35       rin #undef	RASOPS_WIDTH
    167  1.35       rin 
    168  1.35       rin #define	RASOPS_WIDTH	16
    169  1.35       rin #include "rasops_putchar_width.h"
    170  1.35       rin #undef	RASOPS_WIDTH
    171  1.12        pk 
    172  1.11        ad #endif	/* !RASOPS_SMALL */
    173   1.1        ad 
    174   1.1        ad /*
    175   1.4        ad  * Erase rows. This is nice and easy due to alignment.
    176   1.1        ad  */
    177   1.1        ad static void
    178  1.27       dsl rasops24_eraserows(void *cookie, int row, int num, long attr)
    179   1.1        ad {
    180   1.8        ad 	int n9, n3, n1, cnt, stride, delta;
    181  1.31       rin 	uint32_t *dp, clr, xstamp[3];
    182   1.1        ad 	struct rasops_info *ri;
    183  1.12        pk 
    184  1.12        pk 	/*
    185   1.4        ad 	 * If the color is gray, we can cheat and use the generic routines
    186   1.4        ad 	 * (which are faster, hopefully) since the r,g,b values are the same.
    187   1.4        ad 	 */
    188  1.30   mlelstv 	if ((attr & WSATTR_PRIVATE2) != 0) {
    189   1.4        ad 		rasops_eraserows(cookie, row, num, attr);
    190   1.4        ad 		return;
    191   1.4        ad 	}
    192   1.4        ad 
    193   1.1        ad 	ri = (struct rasops_info *)cookie;
    194   1.1        ad 
    195   1.1        ad #ifdef RASOPS_CLIPPING
    196   1.1        ad 	if (row < 0) {
    197   1.1        ad 		num += row;
    198   1.1        ad 		row = 0;
    199   1.1        ad 	}
    200   1.1        ad 
    201   1.1        ad 	if ((row + num) > ri->ri_rows)
    202   1.1        ad 		num = ri->ri_rows - row;
    203  1.12        pk 
    204   1.1        ad 	if (num <= 0)
    205   1.1        ad 		return;
    206   1.1        ad #endif
    207  1.12        pk 
    208  1.12        pk 	clr = ri->ri_devcmap[(attr >> 16) & 0xf] & 0xffffff;
    209  1.20  christos 	xstamp[0] = (clr <<  8) | (clr >> 16);
    210  1.20  christos 	xstamp[1] = (clr << 16) | (clr >>  8);
    211  1.20  christos 	xstamp[2] = (clr << 24) | clr;
    212   1.4        ad 
    213   1.4        ad #if BYTE_ORDER == LITTLE_ENDIAN
    214   1.7        ad 	if ((ri->ri_flg & RI_BSWAP) == 0) {
    215   1.4        ad #else
    216   1.7        ad 	if ((ri->ri_flg & RI_BSWAP) != 0) {
    217   1.4        ad #endif
    218  1.20  christos 		xstamp[0] = bswap32(xstamp[0]);
    219  1.20  christos 		xstamp[1] = bswap32(xstamp[1]);
    220  1.20  christos 		xstamp[2] = bswap32(xstamp[2]);
    221   1.4        ad 	}
    222   1.4        ad 
    223  1.12        pk 	/*
    224   1.7        ad 	 * XXX the wsdisplay_emulops interface seems a little deficient in
    225  1.12        pk 	 * that there is no way to clear the *entire* screen. We provide a
    226  1.12        pk 	 * workaround here: if the entire console area is being cleared, and
    227   1.7        ad 	 * the RI_FULLCLEAR flag is set, clear the entire display.
    228  1.12        pk 	 */
    229   1.7        ad 	if (num == ri->ri_rows && (ri->ri_flg & RI_FULLCLEAR) != 0) {
    230   1.7        ad 		stride = ri->ri_stride;
    231   1.7        ad 		num = ri->ri_height;
    232  1.32       rin 		dp = (uint32_t *)ri->ri_origbits;
    233   1.8        ad 		delta = 0;
    234   1.7        ad 	} else {
    235   1.7        ad 		stride = ri->ri_emustride;
    236   1.7        ad 		num *= ri->ri_font->fontheight;
    237  1.32       rin 		dp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale);
    238   1.8        ad 		delta = ri->ri_delta;
    239   1.7        ad 	}
    240   1.7        ad 
    241   1.7        ad 	n9 = stride / 36;
    242   1.1        ad 	cnt = (n9 << 5) + (n9 << 2); /* (32*n9) + (4*n9) */
    243   1.7        ad 	n3 = (stride - cnt) / 12;
    244   1.1        ad 	cnt += (n3 << 3) + (n3 << 2); /* (8*n3) + (4*n3) */
    245   1.7        ad 	n1 = (stride - cnt) >> 2;
    246  1.12        pk 
    247   1.4        ad 	while (num--) {
    248   1.4        ad 		for (cnt = n9; cnt; cnt--) {
    249  1.20  christos 			dp[0] = xstamp[0];
    250  1.20  christos 			dp[1] = xstamp[1];
    251  1.20  christos 			dp[2] = xstamp[2];
    252  1.20  christos 			dp[3] = xstamp[0];
    253  1.20  christos 			dp[4] = xstamp[1];
    254  1.20  christos 			dp[5] = xstamp[2];
    255  1.20  christos 			dp[6] = xstamp[0];
    256  1.20  christos 			dp[7] = xstamp[1];
    257  1.20  christos 			dp[8] = xstamp[2];
    258   1.4        ad 			dp += 9;
    259   1.4        ad 		}
    260   1.1        ad 
    261   1.4        ad 		for (cnt = n3; cnt; cnt--) {
    262  1.20  christos 			dp[0] = xstamp[0];
    263  1.20  christos 			dp[1] = xstamp[1];
    264  1.20  christos 			dp[2] = xstamp[2];
    265   1.4        ad 			dp += 3;
    266   1.4        ad 		}
    267  1.12        pk 
    268   1.4        ad 		for (cnt = 0; cnt < n1; cnt++)
    269  1.20  christos 			*dp++ = xstamp[cnt];
    270  1.12        pk 
    271  1.32       rin 		DELTA(dp, delta, uint32_t *);
    272   1.4        ad 	}
    273   1.4        ad }
    274   1.4        ad 
    275   1.4        ad /*
    276   1.4        ad  * Erase columns.
    277   1.4        ad  */
    278   1.4        ad static void
    279  1.27       dsl rasops24_erasecols(void *cookie, int row, int col, int num, long attr)
    280   1.4        ad {
    281  1.20  christos 	int n12, n4, height, cnt, slop, clr, xstamp[3];
    282   1.4        ad 	struct rasops_info *ri;
    283  1.32       rin 	uint32_t *dp, *rp;
    284  1.31       rin 	uint8_t *dbp;
    285   1.4        ad 
    286  1.12        pk 	/*
    287   1.4        ad 	 * If the color is gray, we can cheat and use the generic routines
    288   1.4        ad 	 * (which are faster, hopefully) since the r,g,b values are the same.
    289   1.4        ad 	 */
    290  1.30   mlelstv 	if ((attr & WSATTR_PRIVATE2) != 0) {
    291   1.4        ad 		rasops_erasecols(cookie, row, col, num, attr);
    292   1.4        ad 		return;
    293   1.4        ad 	}
    294  1.12        pk 
    295   1.4        ad 	ri = (struct rasops_info *)cookie;
    296   1.4        ad 
    297  1.12        pk #ifdef RASOPS_CLIPPING
    298  1.12        pk 	/* Catches 'row < 0' case too */
    299   1.4        ad 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    300   1.4        ad 		return;
    301   1.4        ad 
    302   1.4        ad 	if (col < 0) {
    303   1.4        ad 		num += col;
    304   1.4        ad 		col = 0;
    305   1.4        ad 	}
    306   1.4        ad 
    307   1.4        ad 	if ((col + num) > ri->ri_cols)
    308   1.4        ad 		num = ri->ri_cols - col;
    309  1.12        pk 
    310   1.4        ad 	if (num <= 0)
    311   1.4        ad 		return;
    312   1.4        ad #endif
    313  1.12        pk 
    314  1.32       rin 	rp = (uint32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
    315   1.4        ad 	num *= ri->ri_font->fontwidth;
    316   1.4        ad 	height = ri->ri_font->fontheight;
    317   1.4        ad 
    318  1.12        pk 	clr = ri->ri_devcmap[(attr >> 16) & 0xf] & 0xffffff;
    319  1.20  christos 	xstamp[0] = (clr <<  8) | (clr >> 16);
    320  1.20  christos 	xstamp[1] = (clr << 16) | (clr >>  8);
    321  1.20  christos 	xstamp[2] = (clr << 24) | clr;
    322   1.4        ad 
    323   1.4        ad #if BYTE_ORDER == LITTLE_ENDIAN
    324   1.7        ad 	if ((ri->ri_flg & RI_BSWAP) == 0) {
    325   1.4        ad #else
    326   1.7        ad 	if ((ri->ri_flg & RI_BSWAP) != 0) {
    327   1.4        ad #endif
    328  1.20  christos 		xstamp[0] = bswap32(xstamp[0]);
    329  1.20  christos 		xstamp[1] = bswap32(xstamp[1]);
    330  1.20  christos 		xstamp[2] = bswap32(xstamp[2]);
    331   1.4        ad 	}
    332  1.12        pk 
    333  1.12        pk 	/*
    334   1.4        ad 	 * The current byte offset mod 4 tells us the number of 24-bit pels
    335   1.4        ad 	 * we need to write for alignment to 32-bits. Once we're aligned on
    336   1.4        ad 	 * a 32-bit boundary, we're also aligned on a 4 pixel boundary, so
    337   1.4        ad 	 * the stamp does not need to be rotated. The following shows the
    338   1.9        ad 	 * layout of 4 pels in a 3 word region and illustrates this:
    339   1.4        ad 	 *
    340   1.4        ad 	 *	aaab bbcc cddd
    341   1.4        ad 	 */
    342  1.17    petrov 	slop = (int)(long)rp & 3;	num -= slop;
    343   1.4        ad 	n12 = num / 12;		num -= (n12 << 3) + (n12 << 2);
    344   1.4        ad 	n4 = num >> 2;		num &= 3;
    345  1.12        pk 
    346   1.4        ad 	while (height--) {
    347  1.31       rin 		dbp = (uint8_t *)rp;
    348  1.32       rin 		DELTA(rp, ri->ri_stride, uint32_t *);
    349   1.4        ad 
    350   1.4        ad 		/* Align to 4 bytes */
    351   1.7        ad 		/* XXX handle with masks, bring under control of RI_BSWAP */
    352   1.4        ad 		for (cnt = slop; cnt; cnt--) {
    353   1.4        ad 			*dbp++ = (clr >> 16);
    354   1.4        ad 			*dbp++ = (clr >> 8);
    355  1.12        pk 			*dbp++ = clr;
    356  1.12        pk 		}
    357   1.4        ad 
    358  1.32       rin 		dp = (uint32_t *)dbp;
    359  1.12        pk 
    360   1.4        ad 		/* 12 pels per loop */
    361   1.4        ad 		for (cnt = n12; cnt; cnt--) {
    362  1.20  christos 			dp[0] = xstamp[0];
    363  1.20  christos 			dp[1] = xstamp[1];
    364  1.20  christos 			dp[2] = xstamp[2];
    365  1.20  christos 			dp[3] = xstamp[0];
    366  1.20  christos 			dp[4] = xstamp[1];
    367  1.20  christos 			dp[5] = xstamp[2];
    368  1.20  christos 			dp[6] = xstamp[0];
    369  1.20  christos 			dp[7] = xstamp[1];
    370  1.20  christos 			dp[8] = xstamp[2];
    371   1.4        ad 			dp += 9;
    372   1.1        ad 		}
    373   1.1        ad 
    374   1.4        ad 		/* 4 pels per loop */
    375   1.4        ad 		for (cnt = n4; cnt; cnt--) {
    376  1.20  christos 			dp[0] = xstamp[0];
    377  1.20  christos 			dp[1] = xstamp[1];
    378  1.20  christos 			dp[2] = xstamp[2];
    379   1.4        ad 			dp += 3;
    380   1.4        ad 		}
    381  1.12        pk 
    382   1.4        ad 		/* Trailing slop */
    383   1.7        ad 		/* XXX handle with masks, bring under control of RI_BSWAP */
    384  1.31       rin 		dbp = (uint8_t *)dp;
    385   1.4        ad 		for (cnt = num; cnt; cnt--) {
    386   1.4        ad 			*dbp++ = (clr >> 16);
    387   1.4        ad 			*dbp++ = (clr >> 8);
    388  1.12        pk 			*dbp++ = clr;
    389  1.12        pk 		}
    390   1.1        ad 	}
    391   1.1        ad }
    392