Home | History | Annotate | Line # | Download | only in rasops
rasops8.c revision 1.43
      1 /* 	$NetBSD: rasops8.c,v 1.43 2019/07/28 12:06:10 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: rasops8.c,v 1.43 2019/07/28 12:06:10 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 <dev/wscons/wsdisplayvar.h>
     42 #include <dev/wscons/wsconsio.h>
     43 #include <dev/rasops/rasops.h>
     44 
     45 static void 	rasops8_putchar(void *, int, int, u_int, long);
     46 static void 	rasops8_putchar_aa(void *, int, int, u_int, long);
     47 #ifndef RASOPS_SMALL
     48 static void 	rasops8_putchar8(void *, int, int, u_int, long);
     49 static void 	rasops8_putchar12(void *, int, int, u_int, long);
     50 static void 	rasops8_putchar16(void *, int, int, u_int, long);
     51 static void	rasops8_makestamp(struct rasops_info *ri, long);
     52 
     53 /*
     54  * 4x1 stamp for optimized character blitting
     55  */
     56 static uint32_t	stamp[16];
     57 static long	stamp_attr;
     58 static int	stamp_mutex;	/* XXX see note in README */
     59 #endif
     60 
     61 /*
     62  * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
     63  * destination = STAMP_READ(offset)
     64  */
     65 #define	STAMP_SHIFT(fb, n)	((n) ? (fb) >> 2 : (fb) << 2)
     66 #define	STAMP_MASK		(0xf << 2)
     67 #define	STAMP_READ(o)		(*(uint32_t *)((uint8_t *)stamp + (o)))
     68 
     69 /*
     70  * Initialize a 'rasops_info' descriptor for this depth.
     71  */
     72 void
     73 rasops8_init(struct rasops_info *ri)
     74 {
     75 
     76 	if (ri->ri_flg & RI_8BIT_IS_RGB) {
     77 		ri->ri_rnum = ri->ri_gnum = 3;
     78 		ri->ri_bnum = 2;
     79 
     80 		ri->ri_rpos = 5;
     81 		ri->ri_gpos = 2;
     82 		ri->ri_bpos = 0;
     83 	}
     84 
     85 	if (FONT_IS_ALPHA(ri->ri_font)) {
     86 		ri->ri_ops.putchar = rasops8_putchar_aa;
     87 		return;
     88 	}
     89 
     90 	switch (ri->ri_font->fontwidth) {
     91 #ifndef RASOPS_SMALL
     92 	case 8:
     93 		ri->ri_ops.putchar = rasops8_putchar8;
     94 		break;
     95 	case 12:
     96 		ri->ri_ops.putchar = rasops8_putchar12;
     97 		break;
     98 	case 16:
     99 		ri->ri_ops.putchar = rasops8_putchar16;
    100 		break;
    101 #endif /* !RASOPS_SMALL */
    102 	default:
    103 		ri->ri_ops.putchar = rasops8_putchar;
    104 		break;
    105 	}
    106 }
    107 
    108 #define	RASOPS_DEPTH	8
    109 #include "rasops_putchar.h"
    110 
    111 static void
    112 rasops8_putchar_aa(void *cookie, int row, int col, u_int uc, long attr)
    113 {
    114 	int width, height;
    115 	uint8_t *rp, *hrp, *fr, bg, fg, pixel;
    116 	struct rasops_info *ri = (struct rasops_info *)cookie;
    117 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    118 	int x, y, r, g, b, aval;
    119 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
    120 	uint8_t scanline[32] __attribute__ ((aligned(8)));
    121 
    122 	hrp = NULL;	/* XXX GCC */
    123 
    124 	if (!CHAR_IN_FONT(uc, font))
    125 		return;
    126 
    127 #ifdef RASOPS_CLIPPING
    128 	/* Catches 'row < 0' case too */
    129 	if ((unsigned)row >= (unsigned)ri->ri_rows)
    130 		return;
    131 
    132 	if ((unsigned)col >= (unsigned)ri->ri_cols)
    133 		return;
    134 #endif
    135 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
    136 	if (ri->ri_hwbits)
    137 		hrp = ri->ri_hwbits + row * ri->ri_yscale + col *
    138 		    ri->ri_xscale;
    139 
    140 	height = font->fontheight;
    141 	width = font->fontwidth;
    142 	bg = (uint8_t)ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
    143 	fg = (uint8_t)ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
    144 
    145 	if (uc == ' ') {
    146 
    147 		while (height--) {
    148 			memset(rp, bg, width);
    149 			if (ri->ri_hwbits) {
    150 				memset(hrp, bg, width);
    151 				hrp += ri->ri_stride;
    152 			}
    153 			rp += ri->ri_stride;
    154 		}
    155 	} else {
    156 		fr = FONT_GLYPH(uc, font, ri);
    157 		/*
    158 		 * we need the RGB colours here, get offsets into rasops_cmap
    159 		 */
    160 		fgo = (((uint32_t)attr >> 24) & 0xf) * 3;
    161 		bgo = (((uint32_t)attr >> 16) & 0xf) * 3;
    162 
    163 		r0 = rasops_cmap[bgo];
    164 		r1 = rasops_cmap[fgo];
    165 		g0 = rasops_cmap[bgo + 1];
    166 		g1 = rasops_cmap[fgo + 1];
    167 		b0 = rasops_cmap[bgo + 2];
    168 		b1 = rasops_cmap[fgo + 2];
    169 
    170 		for (y = 0; y < height; y++) {
    171 			for (x = 0; x < width; x++) {
    172 				aval = *fr;
    173 				fr++;
    174 				if (aval == 0) {
    175 					pixel = bg;
    176 				} else if (aval == 255) {
    177 					pixel = fg;
    178 				} else {
    179 					r = aval * r1 + (255 - aval) * r0;
    180 					g = aval * g1 + (255 - aval) * g0;
    181 					b = aval * b1 + (255 - aval) * b0;
    182 					pixel = ((r & 0xe000) >> 8) |
    183 						((g & 0xe000) >> 11) |
    184 						((b & 0xc000) >> 14);
    185 				}
    186 				scanline[x] = pixel;
    187 			}
    188 			memcpy(rp, scanline, width);
    189 			if (ri->ri_hwbits) {
    190 				memcpy(hrp, scanline, width);
    191 				hrp += ri->ri_stride;
    192 			}
    193 			rp += ri->ri_stride;
    194 
    195 		}
    196 	}
    197 
    198 	/* Do underline */
    199 	if ((attr & WSATTR_UNDERLINE) != 0) {
    200 
    201 		rp -= (ri->ri_stride << 1);
    202 		if (ri->ri_hwbits)
    203 			hrp -= (ri->ri_stride << 1);
    204 
    205 		while (width--) {
    206 			*rp++ = fg;
    207 			if (ri->ri_hwbits)
    208 				*hrp++ = fg;
    209 		}
    210 	}
    211 }
    212 
    213 #ifndef RASOPS_SMALL
    214 /*
    215  * Recompute the 4x1 blitting stamp.
    216  */
    217 static void
    218 rasops8_makestamp(struct rasops_info *ri, long attr)
    219 {
    220 	uint32_t fg, bg;
    221 	int i;
    222 
    223 	fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf] & 0xff;
    224 	bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf] & 0xff;
    225 	stamp_attr = attr;
    226 
    227 	for (i = 0; i < 16; i++) {
    228 #if BYTE_ORDER == BIG_ENDIAN
    229 #define NEED_LITTLE_ENDIAN_STAMP RI_BSWAP
    230 #else
    231 #define NEED_LITTLE_ENDIAN_STAMP 0
    232 #endif
    233 		if ((ri->ri_flg & RI_BSWAP) == NEED_LITTLE_ENDIAN_STAMP) {
    234 			/* little endian */
    235 			stamp[i]  = (i & 8 ? fg : bg);
    236 			stamp[i] |= (i & 4 ? fg : bg) << 8;
    237 			stamp[i] |= (i & 2 ? fg : bg) << 16;
    238 			stamp[i] |= (i & 1 ? fg : bg) << 24;
    239 		} else {
    240 			/* big endian */
    241 			stamp[i]  = (i & 1 ? fg : bg);
    242 			stamp[i] |= (i & 2 ? fg : bg) << 8;
    243 			stamp[i] |= (i & 4 ? fg : bg) << 16;
    244 			stamp[i] |= (i & 8 ? fg : bg) << 24;
    245 		}
    246 	}
    247 }
    248 
    249 #define	RASOPS_WIDTH	8
    250 #include "rasops_putchar_width.h"
    251 #undef	RASOPS_WIDTH
    252 
    253 #define	RASOPS_WIDTH	12
    254 #include "rasops_putchar_width.h"
    255 #undef	RASOPS_WIDTH
    256 
    257 #define	RASOPS_WIDTH	16
    258 #include "rasops_putchar_width.h"
    259 #undef	RASOPS_WIDTH
    260 
    261 #endif
    262