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