xref: /netbsd-src/sys/dev/rasops/rasops4.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* 	$NetBSD: rasops4.c,v 1.7 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: rasops4.c,v 1.7 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	rasops4_copycols(void *, int, int, int, int);
48 static void	rasops4_erasecols(void *, int, int, int, long);
49 static void	rasops4_do_cursor(struct rasops_info *);
50 static void	rasops4_putchar(void *, int, int col, u_int, long);
51 #ifndef RASOPS_SMALL
52 static void	rasops4_putchar8(void *, int, int col, u_int, long);
53 static void	rasops4_putchar12(void *, int, int col, u_int, long);
54 static void	rasops4_putchar16(void *, int, int col, u_int, long);
55 static void	rasops4_makestamp(struct rasops_info *, long);
56 
57 /*
58  * 4x1 stamp for optimized character blitting
59  */
60 static u_int16_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 rasops4_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 = rasops4_putchar8;
77 		break;
78 	case 12:
79 		ri->ri_ops.putchar = rasops4_putchar12;
80 		break;
81 	case 16:
82 		ri->ri_ops.putchar = rasops4_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 = rasops4_putchar;
88 		break;
89 	}
90 
91 	if ((ri->ri_font->fontwidth & 1) != 0) {
92 		ri->ri_ops.erasecols = rasops4_erasecols;
93 		ri->ri_ops.copycols = rasops4_copycols;
94 		ri->ri_do_cursor = rasops4_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 rasops4_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 rasops4_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 rasops4_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] & 0xf;
240 	bg = ri->ri_devcmap[(attr >> 16) & 0xf] & 0xf;
241 	stamp_attr = attr;
242 
243 	for (i = 0; i < 16; i++) {
244 		stamp[i] =  (i & 1 ? fg : bg) << 8;
245 		stamp[i] |= (i & 2 ? fg : bg) << 12;
246 		stamp[i] |= (i & 4 ? fg : bg) << 0;
247 		stamp[i] |= (i & 8 ? fg : bg) << 4;
248 	}
249 }
250 
251 /*
252  * Put a single character. This is for 8-pixel wide fonts.
253  */
254 static void
255 rasops4_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;
264 	u_int16_t *rp;
265 
266 	/* Can't risk remaking the stamp if it's already in use */
267 	if (stamp_mutex++) {
268 		stamp_mutex--;
269 		rasops4_putchar(cookie, row, col, uc, attr);
270 		return;
271 	}
272 
273 	ri = (struct rasops_info *)cookie;
274 
275 #ifdef RASOPS_CLIPPING
276 	/* Catches 'row < 0' case too */
277 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
278 		stamp_mutex--;
279 		return;
280 	}
281 
282 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
283 		stamp_mutex--;
284 		return;
285 	}
286 #endif
287 
288 	rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
289 	height = ri->ri_font->fontheight;
290 	rs = ri->ri_stride / sizeof(*rp);
291 
292 	/* Recompute stamp? */
293 	if (attr != stamp_attr)
294 		rasops4_makestamp(ri, attr);
295 
296 	if (uc == ' ') {
297 		u_int16_t c = stamp[0];
298 		while (height--) {
299 			rp[0] = c;
300 			rp[1] = c;
301 			rp += rs;
302 		}
303 	} else {
304 		uc -= ri->ri_font->firstchar;
305 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
306 		fs = ri->ri_font->stride;
307 
308 		while (height--) {
309 			rp[0] = stamp[(*fr >> 4) & 0xf];
310 			rp[1] = stamp[*fr & 0xf];
311 			fr += fs;
312 			rp += rs;
313 		}
314 	}
315 
316 	/* Do underline */
317 	if ((attr & 1) != 0) {
318 		rp -= (rs << 1);
319 		rp[0] = stamp[15];
320 		rp[1] = stamp[15];
321 	}
322 
323 	stamp_mutex--;
324 }
325 
326 /*
327  * Put a single character. This is for 12-pixel wide fonts.
328  */
329 static void
330 rasops4_putchar12(cookie, row, col, uc, attr)
331 	void *cookie;
332 	int row, col;
333 	u_int uc;
334 	long attr;
335 {
336 	struct rasops_info *ri;
337 	int height, fs, rs;
338 	u_char *fr;
339 	u_int16_t *rp;
340 
341 	/* Can't risk remaking the stamp if it's already in use */
342 	if (stamp_mutex++) {
343 		stamp_mutex--;
344 		rasops4_putchar(cookie, row, col, uc, attr);
345 		return;
346 	}
347 
348 	ri = (struct rasops_info *)cookie;
349 
350 #ifdef RASOPS_CLIPPING
351 	/* Catches 'row < 0' case too */
352 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
353 		stamp_mutex--;
354 		return;
355 	}
356 
357 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
358 		stamp_mutex--;
359 		return;
360 	}
361 #endif
362 
363 	rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
364 	height = ri->ri_font->fontheight;
365 	rs = ri->ri_stride / sizeof(*rp);
366 
367 	/* Recompute stamp? */
368 	if (attr != stamp_attr)
369 		rasops4_makestamp(ri, attr);
370 
371 	if (uc == ' ') {
372 		u_int16_t c = stamp[0];
373 		while (height--) {
374 			rp[0] = c;
375 			rp[1] = c;
376 			rp[2] = c;
377 			rp += rs;
378 		}
379 	} else {
380 		uc -= ri->ri_font->firstchar;
381 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
382 		fs = ri->ri_font->stride;
383 
384 		while (height--) {
385 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
386 			rp[1] = stamp[fr[0] & 0xf];
387 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
388 			fr += fs;
389 			rp += rs;
390 		}
391 	}
392 
393 	/* Do underline */
394 	if ((attr & 1) != 0) {
395 		rp -= (rs << 1);
396 		rp[0] = stamp[15];
397 		rp[1] = stamp[15];
398 		rp[2] = stamp[15];
399 	}
400 
401 	stamp_mutex--;
402 }
403 
404 /*
405  * Put a single character. This is for 16-pixel wide fonts.
406  */
407 static void
408 rasops4_putchar16(cookie, row, col, uc, attr)
409 	void *cookie;
410 	int row, col;
411 	u_int uc;
412 	long attr;
413 {
414 	struct rasops_info *ri;
415 	int height, fs, rs;
416 	u_char *fr;
417 	u_int16_t *rp;
418 
419 	/* Can't risk remaking the stamp if it's already in use */
420 	if (stamp_mutex++) {
421 		stamp_mutex--;
422 		rasops4_putchar(cookie, row, col, uc, attr);
423 		return;
424 	}
425 
426 	ri = (struct rasops_info *)cookie;
427 
428 #ifdef RASOPS_CLIPPING
429 	/* Catches 'row < 0' case too */
430 	if ((unsigned)row >= (unsigned)ri->ri_rows) {
431 		stamp_mutex--;
432 		return;
433 	}
434 
435 	if ((unsigned)col >= (unsigned)ri->ri_cols) {
436 		stamp_mutex--;
437 		return;
438 	}
439 #endif
440 
441 	rp = (u_int16_t *)(ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale);
442 	height = ri->ri_font->fontheight;
443 	rs = ri->ri_stride / sizeof(*rp);
444 
445 	/* Recompute stamp? */
446 	if (attr != stamp_attr)
447 		rasops4_makestamp(ri, attr);
448 
449 	if (uc == ' ') {
450 		u_int16_t c = stamp[0];
451 		while (height--) {
452 			rp[0] = c;
453 			rp[1] = c;
454 			rp[2] = c;
455 			rp[3] = c;
456 			rp += rs;
457 		}
458 	} else {
459 		uc -= ri->ri_font->firstchar;
460 		fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
461 		fs = ri->ri_font->stride;
462 
463 		while (height--) {
464 			rp[0] = stamp[(fr[0] >> 4) & 0xf];
465 			rp[1] = stamp[fr[0] & 0xf];
466 			rp[2] = stamp[(fr[1] >> 4) & 0xf];
467 			rp[3] = stamp[fr[1] & 0xf];
468 			fr += fs;
469 			rp += rs;
470 		}
471 	}
472 
473 	/* Do underline */
474 	if ((attr & 1) != 0) {
475 		rp -= (rs << 1);
476 		rp[0] = stamp[15];
477 		rp[1] = stamp[15];
478 		rp[2] = stamp[15];
479 		rp[3] = stamp[15];
480 	}
481 
482 	stamp_mutex--;
483 }
484 #endif	/* !RASOPS_SMALL */
485 
486 /*
487  * Grab routines common to depths where (bpp < 8)
488  */
489 #define NAME(ident)	rasops4_##ident
490 #define PIXEL_SHIFT	3
491 
492 #include <dev/rasops/rasops_bitops.h>
493