1/* 	$NetBSD: rasops1.c,v 1.37 2019/08/10 01:24:17 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: rasops1.c,v 1.37 2019/08/10 01:24:17 rin Exp $");
34
35#ifdef _KERNEL_OPT
36#include "opt_rasops.h"
37#endif
38
39#include <sys/param.h>
40
41#include <machine/endian.h>
42
43#include <dev/wscons/wsdisplayvar.h>
44#include <dev/wscons/wsconsio.h>
45
46#define	_RASOPS_PRIVATE
47#define	RASOPS_DEPTH	1
48#include <dev/rasops/rasops.h>
49#include <dev/rasops/rasops_masks.h>
50
51static void	rasops1_copycols(void *, int, int, int, int);
52static void	rasops1_erasecols(void *, int, int, int, long);
53static void	rasops1_do_cursor(struct rasops_info *);
54static void	rasops1_putchar(void *, int, int col, u_int, long);
55#ifndef RASOPS_SMALL
56static void	rasops1_putchar8(void *, int, int col, u_int, long);
57static void	rasops1_putchar16(void *, int, int col, u_int, long);
58#endif
59
60/*
61 * Initialize rasops_info struct for this colordepth.
62 */
63void
64rasops1_init(struct rasops_info *ri)
65{
66
67	if ((ri->ri_font->fontwidth & 7) != 0) {
68		ri->ri_ops.erasecols = rasops1_erasecols;
69		ri->ri_ops.copycols = rasops1_copycols;
70		ri->ri_do_cursor = rasops1_do_cursor;
71	}
72
73	switch (ri->ri_font->fontwidth) {
74#ifndef RASOPS_SMALL
75	case 8:
76		ri->ri_ops.putchar = rasops1_putchar8;
77		break;
78	case 16:
79		ri->ri_ops.putchar = rasops1_putchar16;
80		break;
81#endif
82	default:
83		ri->ri_ops.putchar = rasops1_putchar;
84		break;
85	}
86}
87
88/*
89 * Paint a single character. This is the generic version, this is ugly.
90 */
91static void
92rasops1_putchar(void *cookie, int row, int col, u_int uc, long attr)
93{
94	struct rasops_info *ri = (struct rasops_info *)cookie;
95	struct wsdisplay_font *font = PICK_FONT(ri, uc);
96	int height, width;
97	uint32_t bg, fg, lbg, rbg, fb, lmask, rmask, tmp, tmp0, tmp1;
98	uint32_t *rp, *hp;
99	uint8_t *fr;
100	bool space;
101
102	hp = NULL;	/* XXX GCC */
103
104	if (__predict_false(!CHAR_IN_FONT(uc, font)))
105		return;
106
107#ifdef RASOPS_CLIPPING
108	/* Catches 'row < 0' case too */
109	if ((unsigned)row >= (unsigned)ri->ri_rows)
110		return;
111
112	if ((unsigned)col >= (unsigned)ri->ri_cols)
113		return;
114#endif
115
116	height = font->fontheight;
117	width = font->fontwidth;
118	col *= width;
119
120	rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
121	    ((col >> 3) & ~3));
122	if (ri->ri_hwbits)
123		hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale +
124		    ((col >> 3) & ~3));
125
126	col &= 31;
127
128	bg = ATTR_BG(ri, attr);
129	fg = ATTR_FG(ri, attr);
130
131	/* If fg and bg match this becomes a space character */
132	if (uc == ' ' || __predict_false(fg == bg)) {
133		space = true;
134		fr = NULL;	/* XXX GCC */
135	} else {
136		space = false;
137		fr = FONT_GLYPH(uc, font, ri);
138	}
139
140	if (col + width <= 32) {
141		/* Single word, only one mask */
142		rmask = rasops_pmask[col][width & 31];
143		lmask = ~rmask;
144
145		if (space) {
146			bg &= rmask;
147			while (height--) {
148				tmp = (*rp & lmask) | bg;
149				*rp = tmp;
150				if (ri->ri_hwbits) {
151					*hp = tmp;
152					DELTA(hp, ri->ri_stride, uint32_t *);
153				}
154				DELTA(rp, ri->ri_stride, uint32_t *);
155			}
156		} else {
157			while (height--) {
158				fb = rasops_be32uatoh(fr);
159				fr += font->stride;
160				if (bg)
161					fb = ~fb;
162
163				tmp = *rp & lmask;
164				tmp |= (MBE(fb >> col) & rmask);
165				*rp = tmp;
166
167				if (ri->ri_hwbits) {
168					*hp = tmp;
169					DELTA(hp, ri->ri_stride, uint32_t *);
170				}
171
172				DELTA(rp, ri->ri_stride, uint32_t *);
173			}
174		}
175
176		/* Do underline */
177		if ((attr & WSATTR_UNDERLINE) != 0) {
178			DELTA(rp, - ri->ri_stride * ri->ri_ul.off, uint32_t *);
179			if (ri->ri_hwbits)
180				DELTA(hp, - ri->ri_stride * ri->ri_ul.off,
181				    uint32_t *);
182
183			for (height = ri->ri_ul.height; height; height--) {
184				DELTA(rp, - ri->ri_stride, uint32_t *);
185				tmp = (*rp & lmask) | (fg & rmask);
186				*rp = tmp;
187				if (ri->ri_hwbits) {
188					DELTA(hp, - ri->ri_stride, uint32_t *);
189					*hp = tmp;
190				}
191			}
192		}
193	} else {
194		/* Word boundary, two masks needed */
195		lmask = ~rasops_lmask[col];
196		rmask = ~rasops_rmask[(col + width) & 31];
197
198		if (space) {
199			lbg = bg & ~lmask;
200			rbg = bg & ~rmask;
201
202			while (height--) {
203				tmp0 = (rp[0] & lmask) | lbg;
204				tmp1 = (rp[1] & rmask) | rbg;
205
206				rp[0] = tmp0;
207				rp[1] = tmp1;
208
209				if (ri->ri_hwbits) {
210					hp[0] = tmp0;
211					hp[1] = tmp1;
212					DELTA(hp, ri->ri_stride, uint32_t *);
213				}
214
215				DELTA(rp, ri->ri_stride, uint32_t *);
216			}
217		} else {
218			width = 32 - col;
219
220			while (height--) {
221				fb = rasops_be32uatoh(fr);
222				fr += font->stride;
223				if (bg)
224					fb = ~fb;
225
226				tmp0 = rp[0] & lmask;
227				tmp0 |= MBE(fb >> col);
228
229				tmp1 = rp[1] & rmask;
230				tmp1 |= (MBE(fb << width) & ~rmask);
231
232				rp[0] = tmp0;
233				rp[1] = tmp1;
234
235				if (ri->ri_hwbits) {
236					hp[0] = tmp0;
237					hp[1] = tmp1;
238					DELTA(hp, ri->ri_stride, uint32_t *);
239				}
240
241				DELTA(rp, ri->ri_stride, uint32_t *);
242			}
243		}
244
245		/* Do underline */
246		if ((attr & WSATTR_UNDERLINE) != 0) {
247			DELTA(rp, - ri->ri_stride * ri->ri_ul.off, uint32_t *);
248			if (ri->ri_hwbits)
249				DELTA(hp, - ri->ri_stride * ri->ri_ul.off,
250				    uint32_t *);
251
252			for (height = ri->ri_ul.height; height; height--) {
253				DELTA(rp, - ri->ri_stride, uint32_t *);
254				tmp0 = (rp[0] & lmask) | (fg & ~lmask);
255				tmp1 = (rp[1] & rmask) | (fg & ~rmask);
256				rp[0] = tmp0;
257				rp[1] = tmp1;
258				if (ri->ri_hwbits) {
259					DELTA(hp, - ri->ri_stride, uint32_t *);
260					hp[0] = tmp0;
261					hp[1] = tmp1;
262				}
263			}
264		}
265	}
266}
267
268#ifndef RASOPS_SMALL
269/*
270 * Width-optimized putchar functions
271 */
272#define	RASOPS_WIDTH	8
273#include <dev/rasops/rasops1_putchar_width.h>
274#undef	RASOPS_WIDTH
275
276#define	RASOPS_WIDTH	16
277#include <dev/rasops/rasops1_putchar_width.h>
278#undef	RASOPS_WIDTH
279
280#endif	/* !RASOPS_SMALL */
281
282/*
283 * Grab routines common to depths where (bpp < 8)
284 */
285#include <dev/rasops/rasops_bitops.h>
286