Home | History | Annotate | Line # | Download | only in rasops
rasops8.c revision 1.43
      1  1.43       rin /* 	$NetBSD: rasops8.c,v 1.43 2019/07/28 12:06:10 rin Exp $	*/
      2   1.1        ad 
      3   1.4        ad /*-
      4   1.4        ad  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1        ad  * All rights reserved.
      6   1.1        ad  *
      7   1.4        ad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.9        ad  * by Andrew Doran.
      9   1.4        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.4        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.4        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.4        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.4        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.4        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.4        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.4        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.4        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.4        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.4        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.4        ad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1        ad  */
     31   1.2        ad 
     32  1.12     lukem #include <sys/cdefs.h>
     33  1.43       rin __KERNEL_RCSID(0, "$NetBSD: rasops8.c,v 1.43 2019/07/28 12:06:10 rin Exp $");
     34  1.12     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.1        ad #include <dev/wscons/wsdisplayvar.h>
     42   1.1        ad #include <dev/wscons/wsconsio.h>
     43   1.1        ad #include <dev/rasops/rasops.h>
     44   1.1        ad 
     45  1.43       rin static void 	rasops8_putchar(void *, int, int, u_int, long);
     46  1.43       rin static void 	rasops8_putchar_aa(void *, int, int, u_int, long);
     47   1.7        ad #ifndef RASOPS_SMALL
     48  1.43       rin static void 	rasops8_putchar8(void *, int, int, u_int, long);
     49  1.43       rin static void 	rasops8_putchar12(void *, int, int, u_int, long);
     50  1.43       rin static void 	rasops8_putchar16(void *, int, int, u_int, long);
     51  1.18     perry static void	rasops8_makestamp(struct rasops_info *ri, long);
     52   1.1        ad 
     53   1.8        pk /*
     54   1.8        pk  * 4x1 stamp for optimized character blitting
     55   1.1        ad  */
     56  1.37       rin static uint32_t	stamp[16];
     57   1.1        ad static long	stamp_attr;
     58   1.1        ad static int	stamp_mutex;	/* XXX see note in README */
     59  1.10     bjh21 #endif
     60   1.1        ad 
     61   1.1        ad /*
     62   1.1        ad  * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
     63   1.1        ad  * destination = STAMP_READ(offset)
     64   1.1        ad  */
     65  1.43       rin #define	STAMP_SHIFT(fb, n)	((n) ? (fb) >> 2 : (fb) << 2)
     66  1.43       rin #define	STAMP_MASK		(0xf << 2)
     67  1.43       rin #define	STAMP_READ(o)		(*(uint32_t *)((uint8_t *)stamp + (o)))
     68   1.1        ad 
     69   1.1        ad /*
     70  1.11       wiz  * Initialize a 'rasops_info' descriptor for this depth.
     71   1.1        ad  */
     72   1.1        ad void
     73  1.25       dsl rasops8_init(struct rasops_info *ri)
     74   1.1        ad {
     75   1.8        pk 
     76  1.43       rin 	if (ri->ri_flg & RI_8BIT_IS_RGB) {
     77  1.43       rin 		ri->ri_rnum = ri->ri_gnum = 3;
     78  1.43       rin 		ri->ri_bnum = 2;
     79  1.43       rin 
     80  1.43       rin 		ri->ri_rpos = 5;
     81  1.43       rin 		ri->ri_gpos = 2;
     82  1.43       rin 		ri->ri_bpos = 0;
     83  1.43       rin 	}
     84  1.43       rin 
     85  1.33    martin 	if (FONT_IS_ALPHA(ri->ri_font)) {
     86  1.30  macallan 		ri->ri_ops.putchar = rasops8_putchar_aa;
     87  1.43       rin 		return;
     88  1.43       rin 	}
     89  1.43       rin 
     90  1.43       rin 	switch (ri->ri_font->fontwidth) {
     91   1.7        ad #ifndef RASOPS_SMALL
     92  1.43       rin 	case 8:
     93  1.43       rin 		ri->ri_ops.putchar = rasops8_putchar8;
     94  1.43       rin 		break;
     95  1.43       rin 	case 12:
     96  1.43       rin 		ri->ri_ops.putchar = rasops8_putchar12;
     97  1.43       rin 		break;
     98  1.43       rin 	case 16:
     99  1.43       rin 		ri->ri_ops.putchar = rasops8_putchar16;
    100  1.43       rin 		break;
    101   1.7        ad #endif /* !RASOPS_SMALL */
    102  1.43       rin 	default:
    103  1.43       rin 		ri->ri_ops.putchar = rasops8_putchar;
    104  1.43       rin 		break;
    105  1.28  macallan 	}
    106   1.1        ad }
    107   1.1        ad 
    108  1.42       rin #define	RASOPS_DEPTH	8
    109  1.42       rin #include "rasops_putchar.h"
    110   1.1        ad 
    111  1.30  macallan static void
    112  1.30  macallan rasops8_putchar_aa(void *cookie, int row, int col, u_int uc, long attr)
    113  1.30  macallan {
    114  1.34    martin 	int width, height;
    115  1.36       rin 	uint8_t *rp, *hrp, *fr, bg, fg, pixel;
    116  1.30  macallan 	struct rasops_info *ri = (struct rasops_info *)cookie;
    117  1.30  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    118  1.30  macallan 	int x, y, r, g, b, aval;
    119  1.30  macallan 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
    120  1.31  macallan 	uint8_t scanline[32] __attribute__ ((aligned(8)));
    121  1.30  macallan 
    122  1.43       rin 	hrp = NULL;	/* XXX GCC */
    123  1.30  macallan 
    124  1.30  macallan 	if (!CHAR_IN_FONT(uc, font))
    125  1.30  macallan 		return;
    126  1.30  macallan 
    127  1.30  macallan #ifdef RASOPS_CLIPPING
    128  1.30  macallan 	/* Catches 'row < 0' case too */
    129  1.30  macallan 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    130  1.30  macallan 		return;
    131  1.30  macallan 
    132  1.30  macallan 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    133  1.30  macallan 		return;
    134  1.30  macallan #endif
    135  1.30  macallan 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    136  1.30  macallan 	if (ri->ri_hwbits)
    137  1.30  macallan 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col *
    138  1.30  macallan 		    ri->ri_xscale;
    139  1.30  macallan 
    140  1.30  macallan 	height = font->fontheight;
    141  1.30  macallan 	width = font->fontwidth;
    142  1.43       rin 	bg = (uint8_t)ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
    143  1.43       rin 	fg = (uint8_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
    144  1.30  macallan 
    145  1.30  macallan 	if (uc == ' ') {
    146  1.30  macallan 
    147  1.30  macallan 		while (height--) {
    148  1.32  macallan 			memset(rp, bg, width);
    149  1.30  macallan 			if (ri->ri_hwbits) {
    150  1.32  macallan 				memset(hrp, bg, width);
    151  1.30  macallan 				hrp += ri->ri_stride;
    152  1.30  macallan 			}
    153  1.32  macallan 			rp += ri->ri_stride;
    154  1.30  macallan 		}
    155  1.30  macallan 	} else {
    156  1.41       rin 		fr = FONT_GLYPH(uc, font, ri);
    157  1.30  macallan 		/*
    158  1.30  macallan 		 * we need the RGB colours here, get offsets into rasops_cmap
    159  1.30  macallan 		 */
    160  1.43       rin 		fgo = (((uint32_t)attr >> 24) & 0xf) * 3;
    161  1.43       rin 		bgo = (((uint32_t)attr >> 16) & 0xf) * 3;
    162  1.30  macallan 
    163  1.30  macallan 		r0 = rasops_cmap[bgo];
    164  1.30  macallan 		r1 = rasops_cmap[fgo];
    165  1.30  macallan 		g0 = rasops_cmap[bgo + 1];
    166  1.30  macallan 		g1 = rasops_cmap[fgo + 1];
    167  1.30  macallan 		b0 = rasops_cmap[bgo + 2];
    168  1.30  macallan 		b1 = rasops_cmap[fgo + 2];
    169  1.30  macallan 
    170  1.30  macallan 		for (y = 0; y < height; y++) {
    171  1.30  macallan 			for (x = 0; x < width; x++) {
    172  1.30  macallan 				aval = *fr;
    173  1.30  macallan 				fr++;
    174  1.30  macallan 				if (aval == 0) {
    175  1.30  macallan 					pixel = bg;
    176  1.30  macallan 				} else if (aval == 255) {
    177  1.30  macallan 					pixel = fg;
    178  1.30  macallan 				} else {
    179  1.30  macallan 					r = aval * r1 + (255 - aval) * r0;
    180  1.30  macallan 					g = aval * g1 + (255 - aval) * g0;
    181  1.30  macallan 					b = aval * b1 + (255 - aval) * b0;
    182  1.30  macallan 					pixel = ((r & 0xe000) >> 8) |
    183  1.30  macallan 						((g & 0xe000) >> 11) |
    184  1.30  macallan 						((b & 0xc000) >> 14);
    185  1.30  macallan 				}
    186  1.31  macallan 				scanline[x] = pixel;
    187  1.30  macallan 			}
    188  1.31  macallan 			memcpy(rp, scanline, width);
    189  1.30  macallan 			if (ri->ri_hwbits) {
    190  1.31  macallan 				memcpy(hrp, scanline, width);
    191  1.30  macallan 				hrp += ri->ri_stride;
    192  1.30  macallan 			}
    193  1.30  macallan 			rp += ri->ri_stride;
    194  1.30  macallan 
    195  1.30  macallan 		}
    196  1.30  macallan 	}
    197  1.30  macallan 
    198  1.30  macallan 	/* Do underline */
    199  1.35   mlelstv 	if ((attr & WSATTR_UNDERLINE) != 0) {
    200  1.30  macallan 
    201  1.30  macallan 		rp -= (ri->ri_stride << 1);
    202  1.30  macallan 		if (ri->ri_hwbits)
    203  1.30  macallan 			hrp -= (ri->ri_stride << 1);
    204  1.30  macallan 
    205  1.30  macallan 		while (width--) {
    206  1.30  macallan 			*rp++ = fg;
    207  1.30  macallan 			if (ri->ri_hwbits)
    208  1.30  macallan 				*hrp++ = fg;
    209  1.30  macallan 		}
    210  1.30  macallan 	}
    211  1.30  macallan }
    212  1.30  macallan 
    213   1.7        ad #ifndef RASOPS_SMALL
    214   1.1        ad /*
    215   1.1        ad  * Recompute the 4x1 blitting stamp.
    216   1.1        ad  */
    217   1.1        ad static void
    218  1.25       dsl rasops8_makestamp(struct rasops_info *ri, long attr)
    219   1.1        ad {
    220  1.37       rin 	uint32_t fg, bg;
    221   1.1        ad 	int i;
    222   1.5        ad 
    223  1.43       rin 	fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xff;
    224  1.43       rin 	bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xff;
    225   1.1        ad 	stamp_attr = attr;
    226   1.8        pk 
    227   1.1        ad 	for (i = 0; i < 16; i++) {
    228  1.16       uwe #if BYTE_ORDER == BIG_ENDIAN
    229  1.16       uwe #define NEED_LITTLE_ENDIAN_STAMP RI_BSWAP
    230   1.1        ad #else
    231  1.16       uwe #define NEED_LITTLE_ENDIAN_STAMP 0
    232   1.1        ad #endif
    233  1.16       uwe 		if ((ri->ri_flg & RI_BSWAP) == NEED_LITTLE_ENDIAN_STAMP) {
    234  1.16       uwe 			/* little endian */
    235  1.43       rin 			stamp[i]  = (i & 8 ? fg : bg);
    236  1.43       rin 			stamp[i] |= (i & 4 ? fg : bg) << 8;
    237  1.43       rin 			stamp[i] |= (i & 2 ? fg : bg) << 16;
    238  1.43       rin 			stamp[i] |= (i & 1 ? fg : bg) << 24;
    239  1.16       uwe 		} else {
    240  1.16       uwe 			/* big endian */
    241  1.43       rin 			stamp[i]  = (i & 1 ? fg : bg);
    242  1.43       rin 			stamp[i] |= (i & 2 ? fg : bg) << 8;
    243  1.43       rin 			stamp[i] |= (i & 4 ? fg : bg) << 16;
    244  1.43       rin 			stamp[i] |= (i & 8 ? fg : bg) << 24;
    245  1.16       uwe 		}
    246   1.1        ad 	}
    247   1.1        ad }
    248   1.1        ad 
    249  1.42       rin #define	RASOPS_WIDTH	8
    250  1.42       rin #include "rasops_putchar_width.h"
    251  1.42       rin #undef	RASOPS_WIDTH
    252  1.42       rin 
    253  1.42       rin #define	RASOPS_WIDTH	12
    254  1.42       rin #include "rasops_putchar_width.h"
    255  1.42       rin #undef	RASOPS_WIDTH
    256  1.42       rin 
    257  1.42       rin #define	RASOPS_WIDTH	16
    258  1.42       rin #include "rasops_putchar_width.h"
    259  1.42       rin #undef	RASOPS_WIDTH
    260  1.17    petrov 
    261   1.1        ad #endif
    262