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