Home | History | Annotate | Line # | Download | only in rasops
rasops32.c revision 1.37
      1 /*	 $NetBSD: rasops32.c,v 1.37 2019/07/25 15:18:53 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: rasops32.c,v 1.37 2019/07/25 15:18:53 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 	rasops32_putchar(void *, int, int, u_int, long attr);
     46 static void 	rasops32_putchar_aa(void *, int, int, u_int, long attr);
     47 #ifndef RASOPS_SMALL
     48 static void	rasops32_putchar8(void *, int, int, u_int, long);
     49 static void	rasops32_putchar12(void *, int, int, u_int, long);
     50 static void	rasops32_putchar16(void *, int, int, u_int, long);
     51 static void	rasops32_makestamp(struct rasops_info *, long);
     52 
     53 /*
     54  * 4x1 stamp for optimized character blitting
     55  */
     56 static uint32_t	stamp[64];
     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 uint32_t[0] = STAMP_READ(offset)
     64  * destination uint32_t[1] = STAMP_READ(offset +  4)
     65  * destination uint32_t[2] = STAMP_READ(offset +  8)
     66  * destination uint32_t[3] = STAMP_READ(offset + 12)
     67  */
     68 #define	STAMP_SHIFT(fb, n)	((n) ? (fb) : (fb) << 4)
     69 #define	STAMP_MASK		(0xf << 4)
     70 #define	STAMP_READ(o)		(*(uint32_t *)((char *)stamp + (o)))
     71 
     72 /*
     73  * Initialize a 'rasops_info' descriptor for this depth.
     74  */
     75 void
     76 rasops32_init(struct rasops_info *ri)
     77 {
     78 
     79 	if (ri->ri_rnum == 0) {
     80 		ri->ri_rnum = 8;
     81 		ri->ri_rpos = 0;
     82 		ri->ri_gnum = 8;
     83 		ri->ri_gpos = 8;
     84 		ri->ri_bnum = 8;
     85 		ri->ri_bpos = 16;
     86 	}
     87 
     88 	if (FONT_IS_ALPHA(ri->ri_font)) {
     89 		ri->ri_ops.putchar = rasops32_putchar_aa;
     90 		return;
     91 	}
     92 
     93 	switch (ri->ri_font->fontwidth) {
     94 #ifndef RASOPS_SMALL
     95 	case 8:
     96 		ri->ri_ops.putchar = rasops32_putchar8;
     97 		break;
     98 	case 12:
     99 		ri->ri_ops.putchar = rasops32_putchar12;
    100 		break;
    101 	case 16:
    102 		ri->ri_ops.putchar = rasops32_putchar16;
    103 		break;
    104 #endif
    105 	default:
    106 		ri->ri_ops.putchar = rasops32_putchar;
    107 		break;
    108 	}
    109 }
    110 
    111 #define	RASOPS_DEPTH	32
    112 #include "rasops_putchar.h"
    113 
    114 static void
    115 rasops32_putchar_aa(void *cookie, int row, int col, u_int uc, long attr)
    116 {
    117 	int width, height, cnt, clr[2];
    118 	struct rasops_info *ri = (struct rasops_info *)cookie;
    119 	struct wsdisplay_font *font = PICK_FONT(ri, uc);
    120 	uint32_t *dp, *rp;
    121 	uint8_t *rrp;
    122 	uint8_t *fr;
    123 	uint32_t buffer[64]; /* XXX */
    124 	int x, y, r, g, b, aval;
    125 	int r1, g1, b1, r0, g0, b0;
    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 
    136 	/* check if character fits into font limits */
    137 	if (!CHAR_IN_FONT(uc, font))
    138 		return;
    139 
    140 	rrp = (ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
    141 	rp = (uint32_t *)rrp;
    142 
    143 	height = font->fontheight;
    144 	width = font->fontwidth;
    145 
    146 	clr[0] = ri->ri_devcmap[(attr >> 16) & 0xf];
    147 	clr[1] = ri->ri_devcmap[(attr >> 24) & 0xf];
    148 
    149 	if (uc == ' ') {
    150 		for (cnt = 0; cnt < width; cnt++)
    151 			buffer[cnt] = clr[0];
    152 		while (height--) {
    153 			dp = rp;
    154 			DELTA(rp, ri->ri_stride, uint32_t *);
    155 			memcpy(dp, buffer, width << 2);
    156 		}
    157 	} else {
    158 		fr = FONT_GLYPH(uc, font, ri);
    159 
    160 		r0 = (clr[0] >> 16) & 0xff;
    161 		r1 = (clr[1] >> 16) & 0xff;
    162 		g0 = (clr[0] >> 8) & 0xff;
    163 		g1 = (clr[1] >> 8) & 0xff;
    164 		b0 =  clr[0] & 0xff;
    165 		b1 =  clr[1] & 0xff;
    166 
    167 		for (y = 0; y < height; y++) {
    168 			dp = (uint32_t *)(rrp + ri->ri_stride * y);
    169 			for (x = 0; x < width; x++) {
    170 				aval = *fr;
    171 				if (aval == 0) {
    172 					buffer[x] = clr[0];
    173 				} else if (aval == 255) {
    174 					buffer[x] = clr[1];
    175 				} else {
    176 					r = aval * r1 + (255 - aval) * r0;
    177 					g = aval * g1 + (255 - aval) * g0;
    178 					b = aval * b1 + (255 - aval) * b0;
    179 					buffer[x] = (r & 0xff00) << 8 |
    180 					      (g & 0xff00) |
    181 					      (b & 0xff00) >> 8;
    182 				}
    183 				fr++;
    184 			}
    185 			memcpy(dp, buffer, width << 2);
    186 		}
    187 	}
    188 
    189 	/* Do underline */
    190 	if ((attr & WSATTR_UNDERLINE) != 0) {
    191 		rp = (uint32_t *)rrp;
    192 		height = font->fontheight;
    193 		DELTA(rp, (ri->ri_stride * (height - 2)), uint32_t *);
    194 		while (width--)
    195 			*rp++ = clr[1];
    196 	}
    197 }
    198 
    199 #ifndef RASOPS_SMALL
    200 /*
    201  * Recompute the blitting stamp.
    202  */
    203 static void
    204 rasops32_makestamp(struct rasops_info *ri, long attr)
    205 {
    206 	uint32_t fg, bg;
    207 	int i;
    208 
    209 	fg = ri->ri_devcmap[((uint32_t)attr >> 24) & 0xf];
    210 	bg = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
    211 	stamp_attr = attr;
    212 
    213 	for (i = 0; i < 64; i += 4) {
    214 		stamp[i + 0] = (i & 32 ? fg : bg);
    215 		stamp[i + 1] = (i & 16 ? fg : bg);
    216 		stamp[i + 2] = (i & 8 ? fg : bg);
    217 		stamp[i + 3] = (i & 4 ? fg : bg);
    218 	}
    219 }
    220 
    221 #define	RASOPS_WIDTH	8
    222 #include "rasops_putchar_width.h"
    223 #undef	RASOPS_WIDTH
    224 
    225 #define	RASOPS_WIDTH	12
    226 #include "rasops_putchar_width.h"
    227 #undef	RASOPS_WIDTH
    228 
    229 #define	RASOPS_WIDTH	16
    230 #include "rasops_putchar_width.h"
    231 #undef	RASOPS_WIDTH
    232 
    233 #endif /* !RASOPS_SMALL */
    234