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