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