xref: /netbsd-src/sys/dev/rasops/rasops2.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* 	$NetBSD: rasops2.c,v 1.12 2008/04/28 20:23:56 martin 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: rasops2.c,v 1.12 2008/04/28 20:23:56 martin 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 #include <dev/rasops/rasops.h>
45 #include <dev/rasops/rasops_masks.h>
46 
47 static void	rasops2_copycols(void *, int, int, int, int);
48 static void	rasops2_erasecols(void *, int, int, int, long);
49 static void	rasops2_do_cursor(struct rasops_info *);
50 static void	rasops2_putchar(void *, int, int col, u_int, long);
51 #ifndef RASOPS_SMALL
52 static void	rasops2_putchar8(void *, int, int col, u_int, long);
53 static void	rasops2_putchar12(void *, int, int col, u_int, long);
54 static void	rasops2_putchar16(void *, int, int col, u_int, long);
55 static void	rasops2_makestamp(struct rasops_info *, long);
56 
57 /*
58  * 4x1 stamp for optimized character blitting
59  */
60 static int8_t	stamp[16];
61 static long	stamp_attr;
62 static int	stamp_mutex;	/* XXX see note in README */
63 #endif
64 
65 /*
66  * Initialize rasops_info struct for this colordepth.
67  */
68 void
69 rasops2_init(ri)
70 	struct rasops_info *ri;
71 {
72 
73 	switch (ri->ri_font->fontwidth) {
74 #ifndef RASOPS_SMALL
75 	case 8:
76 		ri->ri_ops.putchar = rasops2_putchar8;
77 		break;
78 	case 12:
79 		ri->ri_ops.putchar = rasops2_putchar12;
80 		break;
81 	case 16:
82 		ri->ri_ops.putchar = rasops2_putchar16;
83 		break;
84 #endif	/* !RASOPS_SMALL */
85 	default:
86 		panic("fontwidth not 8/12/16 or RASOPS_SMALL - fixme!");
87 		ri->ri_ops.putchar = rasops2_putchar;
88 		break;
89 	}
90 
91 	if ((ri->ri_font->fontwidth & 3) != 0) {
92 		ri->ri_ops.erasecols = rasops2_erasecols;
93 		ri->ri_ops.copycols = rasops2_copycols;
94 		ri->ri_do_cursor = rasops2_do_cursor;
95 	}
96 }
97 
98 #ifdef notyet
99 /*
100  * Paint a single character. This is the generic version, this is ugly.
101  */
102 static void
103 rasops2_putchar(cookie, row, col, uc, attr)
104 	void *cookie;
105 	int row, col;
106 	u_int uc;
107 	long attr;
108 {
109 	int height, width, fs, rs, fb, bg, fg, lmask, rmask;
110 	struct rasops_info *ri;
111 	int32_t *rp;
112 	u_char *fr;
113 
114 	ri = (struct rasops_info *)cookie;
115 
116 #ifdef RASOPS_CLIPPING
117 	/* Catches 'row < 0' case too */
118 	if ((unsigned)row >= (unsigned)ri->ri_rows)
119 		return;
120 
121 	if ((unsigned)col >= (unsigned)ri->ri_cols)
122 		return;
123 #endif
124 
125 	width = ri->ri_font->fontwidth << 1;
126 	height = ri->ri_font->fontheight;
127 	col *= width;
128 	rp = (int32_t *)(ri->ri_bits + row * ri->ri_yscale + ((col >> 3) & ~3));
129 	col = col & 31;
130 	rs = ri->ri_stride;
131 
132 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
133 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
134 
135 	/* If fg and bg match this becomes a space character */
136 	if (fg == bg || uc == ' ') {
137 		uc = (u_int)-1;
138 		fr = 0;		/* shutup gcc */
139 		fs = 0;		/* shutup gcc */
140 	} else {
141 		uc -= ri->ri_font->firstchar;
142 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
143 		fs = ri->ri_font->stride;
144 	}
145 
146 	/* Single word, one mask */
147 	if ((col + width) <= 32) {
148 		rmask = rasops_pmask[col][width];
149 		lmask = ~rmask;
150 
151 		if (uc == (u_int)-1) {
152 			bg &= rmask;
153 
154 			while (height--) {
155 				*rp = (*rp & lmask) | bg;
156 				DELTA(rp, rs, int32_t *);
157 			}
158 		} else {
159 			while (height--) {
160 				/* get bits, mask */
161 				/* compose sl */
162 				/* mask sl */
163 				/* put word */
164 			}
165 		}
166 
167 		/* Do underline */
168 		if (attr & 1) {
169 			DELTA(rp, -(ri->ri_stride << 1), int32_t *);
170 			*rp = (*rp & lmask) | (fg & rmask);
171 		}
172 	} else {
173 		lmask = ~rasops_lmask[col];
174 		rmask = ~rasops_rmask[(col + width) & 31];
175 
176 		if (uc == (u_int)-1) {
177 			bg = bg & ~lmask;
178 			width = bg & ~rmask;
179 
180 			while (height--) {
181 				rp[0] = (rp[0] & lmask) | bg;
182 				rp[1] = (rp[1] & rmask) | width;
183 				DELTA(rp, rs, int32_t *);
184 			}
185 		} else {
186 			width = 32 - col;
187 
188 			/* NOT fontbits if bg is white */
189 			while (height--) {
190 				fb = ~(fr[3] | (fr[2] << 8) |
191 				    (fr[1] << 16) | (fr[0] << 24));
192 
193 				rp[0] = (rp[0] & lmask)
194 				    | MBE((u_int)fb >> col);
195 
196 				rp[1] = (rp[1] & rmask)
197 				   | (MBE((u_int)fb << width) & ~rmask);
198 
199 				fr += fs;
200 				DELTA(rp, rs, int32_t *);
201 			}
202 		}
203 
204 		/* Do underline */
205 		if (attr & 1) {
206 			DELTA(rp, -(ri->ri_stride << 1), int32_t *);
207 			rp[0] = (rp[0] & lmask) | (fg & ~lmask);
208 			rp[1] = (rp[1] & rmask) | (fg & ~rmask);
209 		}
210 	}
211 }
212 #endif
213 
214 /*
215  * Put a single character. This is the generic version.
216  */
217 static void
218 rasops2_putchar(cookie, row, col, uc, attr)
219 	void *cookie;
220 	int row, col;
221 	u_int uc;
222 	long attr;
223 {
224 
225 	/* XXX punt */
226 }
227 
228 #ifndef RASOPS_SMALL
229 /*
230  * Recompute the blitting stamp.
231  */
232 static void
233 rasops2_makestamp(ri, attr)
234 	struct rasops_info *ri;
235 	long attr;
236 {
237 	int i, fg, bg;
238 
239 	fg = ri->ri_devcmap[(attr >> 24) & 0xf] & 3;
240 	bg = ri->ri_devcmap[(attr >> 16) & 0xf] & 3;
241 	stamp_attr = attr;
242 
243 	for (i = 0; i < 16; i++) {
244 		stamp[i] = (i & 1 ? fg : bg);
245 		stamp[i] |= (i & 2 ? fg : bg) << 2;
246 		stamp[i] |= (i & 4 ? fg : bg) << 4;
247 		stamp[i] |= (i & 8 ? fg : bg) << 6;
248 	}
249 }
250 
251 /*
252  * Put a single character. This is for 8-pixel wide fonts.
253  */
254 static void
255 rasops2_putchar8(cookie, row, col, uc, attr)
256 	void *cookie;
257 	int row, col;
258 	u_int uc;
259 	long attr;
260 {
261 	struct rasops_info *ri;
262 	int height, fs, rs;
263 	u_char *fr, *rp;
264 
265 	/* Can't risk remaking the stamp if it's already in use */
266 	if (stamp_mutex++) {
267 		stamp_mutex--;
268 		rasops2_putchar(cookie, row, col, uc, attr);
269 		return;
270 	}
271 
272 	ri = (struct rasops_info *)cookie;
273 
274 #ifdef RASOPS_CLIPPING
275 	/* Catches 'row < 0' case too */
276 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
277 		stamp_mutex--;
278 		return;
279 	}
280 
281 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
282 		stamp_mutex--;
283 		return;
284 	}
285 #endif
286 
287 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
288 	height = ri->ri_font->fontheight;
289 	rs = ri->ri_stride;
290 
291 	/* Recompute stamp? */
292 	if (attr != stamp_attr)
293 		rasops2_makestamp(ri, attr);
294 
295 	if (uc == ' ') {
296 		int8_t c = stamp[0];
297 		while (height--) {
298 			*(int16_t *)rp = c;
299 			rp += rs;
300 		}
301 	} else {
302 		uc -= ri->ri_font->firstchar;
303 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
304 		fs = ri->ri_font->stride;
305 
306 		while (height--) {
307 			rp[0] = stamp[(*fr >> 4) & 0xf];
308 			rp[1] = stamp[*fr & 0xf];
309 			fr += fs;
310 			rp += rs;
311 		}
312 	}
313 
314 	/* Do underline */
315 	if ((attr & 1) != 0)
316 		*(int16_t *)(rp - (ri->ri_stride << 1)) = stamp[15];
317 
318 	stamp_mutex--;
319 }
320 
321 /*
322  * Put a single character. This is for 12-pixel wide fonts.
323  */
324 static void
325 rasops2_putchar12(cookie, row, col, uc, attr)
326 	void *cookie;
327 	int row, col;
328 	u_int uc;
329 	long attr;
330 {
331 	struct rasops_info *ri;
332 	int height, fs, rs;
333 	u_char *fr, *rp;
334 
335 	/* Can't risk remaking the stamp if it's already in use */
336 	if (stamp_mutex++) {
337 		stamp_mutex--;
338 		rasops2_putchar(cookie, row, col, uc, attr);
339 		return;
340 	}
341 
342 	ri = (struct rasops_info *)cookie;
343 
344 #ifdef RASOPS_CLIPPING
345 	/* Catches 'row < 0' case too */
346 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
347 		stamp_mutex--;
348 		return;
349 	}
350 
351 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
352 		stamp_mutex--;
353 		return;
354 	}
355 #endif
356 
357 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
358 	height = ri->ri_font->fontheight;
359 	rs = ri->ri_stride;
360 
361 	/* Recompute stamp? */
362 	if (attr != stamp_attr)
363 		rasops2_makestamp(ri, attr);
364 
365 	if (uc == ' ') {
366 		int8_t c = stamp[0];
367 		while (height--) {
368 			rp[0] = rp[1] = rp[2] = c;
369 			rp += rs;
370 		}
371 	} else {
372 		uc -= ri->ri_font->firstchar;
373 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
374 		fs = ri->ri_font->stride;
375 
376 		while (height--) {
377 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
378 			rp[1] = stamp[fr[0] & 0xf];
379 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
380 			fr += fs;
381 			rp += rs;
382 		}
383 	}
384 
385 	/* Do underline */
386 	if ((attr & 1) != 0) {
387 		rp -= ri->ri_stride << 1;
388 		rp[0] = rp[1] = rp[2] = stamp[15];
389 	}
390 
391 	stamp_mutex--;
392 }
393 
394 /*
395  * Put a single character. This is for 16-pixel wide fonts.
396  */
397 static void
398 rasops2_putchar16(cookie, row, col, uc, attr)
399 	void *cookie;
400 	int row, col;
401 	u_int uc;
402 	long attr;
403 {
404 	struct rasops_info *ri;
405 	int height, fs, rs;
406 	u_char *fr, *rp;
407 
408 	/* Can't risk remaking the stamp if it's already in use */
409 	if (stamp_mutex++) {
410 		stamp_mutex--;
411 		rasops2_putchar(cookie, row, col, uc, attr);
412 		return;
413 	}
414 
415 	ri = (struct rasops_info *)cookie;
416 
417 #ifdef RASOPS_CLIPPING
418 	/* Catches 'row < 0' case too */
419 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
420 		stamp_mutex--;
421 		return;
422 	}
423 
424 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
425 		stamp_mutex--;
426 		return;
427 	}
428 #endif
429 
430 	rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
431 	height = ri->ri_font->fontheight;
432 	rs = ri->ri_stride;
433 
434 	/* Recompute stamp? */
435 	if (attr != stamp_attr)
436 		rasops2_makestamp(ri, attr);
437 
438 	if (uc == ' ') {
439 		int8_t c = stamp[0];
440 		while (height--) {
441 			*(int32_t *)rp = c;
442 			rp += rs;
443 		}
444 	} else {
445 		uc -= ri->ri_font->firstchar;
446 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
447 		fs = ri->ri_font->stride;
448 
449 		while (height--) {
450 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
451 			rp[1] = stamp[fr[0] & 0xf];
452 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
453 			rp[3] = stamp[fr[1] & 0xf];
454 			fr += fs;
455 			rp += rs;
456 		}
457 	}
458 
459 	/* Do underline */
460 	if ((attr & 1) != 0)
461 		*(int32_t *)(rp - (ri->ri_stride << 1)) = stamp[15];
462 
463 	stamp_mutex--;
464 }
465 #endif	/* !RASOPS_SMALL */
466 
467 /*
468  * Grab routines common to depths where (bpp < 8)
469  */
470 #define NAME(ident)	rasops2_##ident
471 #define PIXEL_SHIFT	1
472 
473 #include <dev/rasops/rasops_bitops.h>
474