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