rasops15.c revision 1.17 1 /* $NetBSD: rasops15.c,v 1.17 2009/03/14 15:36:20 dsl 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: rasops15.c,v 1.17 2009/03/14 15:36:20 dsl 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
45 static void rasops15_putchar(void *, int, int, u_int, long attr);
46 #ifndef RASOPS_SMALL
47 static void rasops15_putchar8(void *, int, int, u_int, long attr);
48 static void rasops15_putchar12(void *, int, int, u_int, long attr);
49 static void rasops15_putchar16(void *, int, int, u_int, long attr);
50 static void rasops15_makestamp(struct rasops_info *, long);
51 #endif
52
53 #ifndef RASOPS_SMALL
54 /*
55 * (2x2)x1 stamp for optimized character blitting
56 */
57 static int32_t stamp[32];
58 static long stamp_attr;
59 static int stamp_mutex; /* XXX see note in readme */
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 int32_t[0] = STAMP_READ(offset)
67 * destination int32_t[1] = STAMP_READ(offset + 4)
68 */
69 #define STAMP_SHIFT(fb,n) ((n*4-3) >= 0 ? (fb)>>(n*4-3):(fb)<<-(n*4-3))
70 #define STAMP_MASK (15 << 3)
71 #define STAMP_READ(o) (*(int32_t *)((char *)stamp + (o)))
72 #endif
73
74 /*
75 * Initialize rasops_info struct for this colordepth.
76 */
77 void
78 rasops15_init(struct rasops_info *ri)
79 {
80
81 switch (ri->ri_font->fontwidth) {
82 #ifndef RASOPS_SMALL
83 case 8:
84 ri->ri_ops.putchar = rasops15_putchar8;
85 break;
86
87 case 12:
88 ri->ri_ops.putchar = rasops15_putchar12;
89 break;
90
91 case 16:
92 ri->ri_ops.putchar = rasops15_putchar16;
93 break;
94 #endif /* !RASOPS_SMALL */
95 default:
96 ri->ri_ops.putchar = rasops15_putchar;
97 break;
98 }
99
100 if (ri->ri_rnum == 0) {
101 ri->ri_rnum = 5;
102 ri->ri_rpos = 0;
103 ri->ri_gnum = 5 + (ri->ri_depth == 16);
104 ri->ri_gpos = 5;
105 ri->ri_bnum = 5;
106 ri->ri_bpos = 10 + (ri->ri_depth == 16);
107 }
108 }
109
110 /*
111 * Paint a single character.
112 */
113 static void
114 rasops15_putchar(cookie, row, col, uc, attr)
115 void *cookie;
116 int row, col;
117 u_int uc;
118 long attr;
119 {
120 int fb, width, height, cnt, clr[2];
121 struct rasops_info *ri;
122 u_char *dp, *rp, *hp, *hrp, *fr;
123
124 ri = (struct rasops_info *)cookie;
125 hp = hrp = NULL;
126
127 #ifdef RASOPS_CLIPPING
128 /* Catches 'row < 0' case too */
129 if ((unsigned)row >= (unsigned)ri->ri_rows)
130 return;
131
132 if ((unsigned)col >= (unsigned)ri->ri_cols)
133 return;
134 #endif
135
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 +
139 col * ri->ri_xscale;
140 height = ri->ri_font->fontheight;
141 width = ri->ri_font->fontwidth;
142
143 clr[1] = ri->ri_devcmap[((u_int)attr >> 24) & 0xf];
144 clr[0] = ri->ri_devcmap[((u_int)attr >> 16) & 0xf];
145
146 if (uc == ' ') {
147 int16_t c = (int16_t)clr[0];
148 while (height--) {
149 dp = rp;
150 rp += ri->ri_stride;
151 if (ri->ri_hwbits) {
152 hp = hrp;
153 hrp += ri->ri_stride;
154 }
155
156 for (cnt = width; cnt; cnt--) {
157 *(int16_t *)dp = c;
158 dp += 2;
159 if (ri->ri_hwbits) {
160 *(int16_t *)hp = c;
161 hp += 2;
162 }
163 }
164 }
165 } else {
166 uc -= ri->ri_font->firstchar;
167 fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
168
169 while (height--) {
170 dp = rp;
171 fb = fr[3] | (fr[2] << 8) | (fr[1] << 16) | (fr[0] << 24);
172 fr += ri->ri_font->stride;
173 rp += ri->ri_stride;
174 if (ri->ri_hwbits) {
175 hp = hrp;
176 hrp += ri->ri_stride;
177 }
178
179 for (cnt = width; cnt; cnt--) {
180 *(int16_t *)dp = (int16_t)clr[(fb >> 31) & 1];
181 if (ri->ri_hwbits)
182 *(int16_t *)hp =
183 (int16_t)clr[(fb >> 31) & 1];
184 fb <<= 1;
185 dp += 2;
186 if (ri->ri_hwbits)
187 hp += 2;
188 }
189 }
190 }
191
192 /* Do underline */
193 if ((attr & 1) != 0) {
194 int16_t c = (int16_t)clr[1];
195 rp -= ri->ri_stride << 1;
196 if (ri->ri_hwbits)
197 hrp -= ri->ri_stride << 1;
198
199 while (width--) {
200 *(int16_t *)rp = c;
201 rp += 2;
202 if (ri->ri_hwbits) {
203 *(int16_t *)hrp = c;
204 hrp += 2;
205 }
206 }
207 }
208 }
209
210 #ifndef RASOPS_SMALL
211 /*
212 * Recompute the (2x2)x1 blitting stamp.
213 */
214 static void
215 rasops15_makestamp(struct rasops_info *ri, long attr)
216 {
217 int32_t fg, bg;
218 int i;
219
220 fg = ri->ri_devcmap[((u_int)attr >> 24) & 0xf] & 0xffff;
221 bg = ri->ri_devcmap[((u_int)attr >> 16) & 0xf] & 0xffff;
222 stamp_attr = attr;
223
224 for (i = 0; i < 32; i += 2) {
225 #if BYTE_ORDER == LITTLE_ENDIAN
226 stamp[i] = (i & 16 ? fg : bg);
227 stamp[i] |= ((i & 8 ? fg : bg) << 16);
228 stamp[i + 1] = (i & 4 ? fg : bg);
229 stamp[i + 1] |= ((i & 2 ? fg : bg) << 16);
230 #else
231 stamp[i] = (i & 2 ? fg : bg);
232 stamp[i] |= ((i & 4 ? fg : bg) << 16);
233 stamp[i + 1] = (i & 8 ? fg : bg);
234 stamp[i + 1] |= ((i & 16 ? fg : bg) << 16);
235 #endif
236 }
237 }
238
239 /*
240 * Paint a single character. This is for 8-pixel wide fonts.
241 */
242 static void
243 rasops15_putchar8(cookie, row, col, uc, attr)
244 void *cookie;
245 int row, col;
246 u_int uc;
247 long attr;
248 {
249 struct rasops_info *ri;
250 int height, so, fs;
251 int32_t *rp, *hrp;
252 u_char *fr;
253
254 /* Can't risk remaking the stamp if it's already in use */
255 if (stamp_mutex++) {
256 stamp_mutex--;
257 rasops15_putchar(cookie, row, col, uc, attr);
258 return;
259 }
260
261 ri = (struct rasops_info *)cookie;
262 hrp = NULL;
263
264 #ifdef RASOPS_CLIPPING
265 if ((unsigned)row >= (unsigned)ri->ri_rows) {
266 stamp_mutex--;
267 return;
268 }
269
270 if ((unsigned)col >= (unsigned)ri->ri_cols) {
271 stamp_mutex--;
272 return;
273 }
274 #endif
275
276 /* Recompute stamp? */
277 if (attr != stamp_attr)
278 rasops15_makestamp(ri, attr);
279
280 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
281 if (ri->ri_hwbits)
282 hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
283 col*ri->ri_xscale);
284 height = ri->ri_font->fontheight;
285
286 if (uc == (u_int)-1) {
287 int32_t c = stamp[0];
288 while (height--) {
289 rp[0] = rp[1] = rp[2] = rp[3] = c;
290 DELTA(rp, ri->ri_stride, int32_t *);
291 if (ri->ri_hwbits) {
292 hrp[0] = hrp[1] = hrp[2] = hrp[3] = c;
293 DELTA(hrp, ri->ri_stride, int32_t *);
294 }
295 }
296 } else {
297 uc -= ri->ri_font->firstchar;
298 fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
299 fs = ri->ri_font->stride;
300
301 while (height--) {
302 so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
303 rp[0] = STAMP_READ(so);
304 rp[1] = STAMP_READ(so + 4);
305 if (ri->ri_hwbits) {
306 hrp[0] = STAMP_READ(so);
307 hrp[1] = STAMP_READ(so + 4);
308 }
309
310 so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
311 rp[2] = STAMP_READ(so);
312 rp[3] = STAMP_READ(so + 4);
313 if (ri->ri_hwbits) {
314 hrp[2] = STAMP_READ(so);
315 hrp[3] = STAMP_READ(so + 4);
316 }
317
318 fr += fs;
319 DELTA(rp, ri->ri_stride, int32_t *);
320 if (ri->ri_hwbits)
321 DELTA(hrp, ri->ri_stride, int32_t *);
322 }
323 }
324
325 /* Do underline */
326 if ((attr & 1) != 0) {
327 int32_t c = STAMP_READ(28);
328
329 DELTA(rp, -(ri->ri_stride << 1), int32_t *);
330 rp[0] = rp[1] = rp[2] = rp[3] = c;
331 if (ri->ri_hwbits) {
332 DELTA(hrp, -(ri->ri_stride << 1), int32_t *);
333 hrp[0] = hrp[1] = hrp[2] = hrp[3] = c;
334 }
335 }
336
337 stamp_mutex--;
338 }
339
340 /*
341 * Paint a single character. This is for 12-pixel wide fonts.
342 */
343 static void
344 rasops15_putchar12(cookie, row, col, uc, attr)
345 void *cookie;
346 int row, col;
347 u_int uc;
348 long attr;
349 {
350 struct rasops_info *ri;
351 int height, so, fs;
352 int32_t *rp, *hrp;
353 u_char *fr;
354
355 /* Can't risk remaking the stamp if it's already in use */
356 if (stamp_mutex++) {
357 stamp_mutex--;
358 rasops15_putchar(cookie, row, col, uc, attr);
359 return;
360 }
361
362 ri = (struct rasops_info *)cookie;
363 hrp = NULL;
364
365 #ifdef RASOPS_CLIPPING
366 if ((unsigned)row >= (unsigned)ri->ri_rows) {
367 stamp_mutex--;
368 return;
369 }
370
371 if ((unsigned)col >= (unsigned)ri->ri_cols) {
372 stamp_mutex--;
373 return;
374 }
375 #endif
376
377 /* Recompute stamp? */
378 if (attr != stamp_attr)
379 rasops15_makestamp(ri, attr);
380
381 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
382 if (ri->ri_hwbits)
383 hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
384 col*ri->ri_xscale);
385 height = ri->ri_font->fontheight;
386
387 if (uc == (u_int)-1) {
388 int32_t c = stamp[0];
389 while (height--) {
390 rp[0] = rp[1] = rp[2] = rp[3] = rp[4] = rp[5] = c;
391 DELTA(rp, ri->ri_stride, int32_t *);
392 if (ri->ri_hwbits) {
393 hrp[0] = hrp[1] = hrp[2] = hrp[3] = hrp[4] =
394 hrp[5] = c;
395 DELTA(hrp, ri->ri_stride, int32_t *);
396 }
397 }
398 } else {
399 uc -= ri->ri_font->firstchar;
400 fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
401 fs = ri->ri_font->stride;
402
403 while (height--) {
404 so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
405 rp[0] = STAMP_READ(so);
406 rp[1] = STAMP_READ(so + 4);
407 if (ri->ri_hwbits) {
408 hrp[0] = STAMP_READ(so);
409 hrp[1] = STAMP_READ(so + 4);
410 }
411
412 so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
413 rp[2] = STAMP_READ(so);
414 rp[3] = STAMP_READ(so + 4);
415 if (ri->ri_hwbits) {
416 hrp[2] = STAMP_READ(so);
417 hrp[3] = STAMP_READ(so + 4);
418 }
419
420 so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
421 rp[4] = STAMP_READ(so);
422 rp[5] = STAMP_READ(so + 4);
423 if (ri->ri_hwbits) {
424 hrp[4] = STAMP_READ(so);
425 hrp[5] = STAMP_READ(so + 4);
426 }
427
428 fr += fs;
429 DELTA(rp, ri->ri_stride, int32_t *);
430 if (ri->ri_hwbits)
431 DELTA(hrp, ri->ri_stride, int32_t *);
432 }
433 }
434
435 /* Do underline */
436 if (attr & 1) {
437 int32_t c = STAMP_READ(28);
438
439 DELTA(rp, -(ri->ri_stride << 1), int32_t *);
440 rp[0] = rp[1] = rp[2] = rp[3] = rp[4] = rp[5] = c;
441 if (ri->ri_hwbits) {
442 DELTA(hrp, -(ri->ri_stride << 1), int32_t *);
443 hrp[0] = hrp[1] = hrp[2] = hrp[3] = hrp[4] = hrp[5] = c;
444 }
445 }
446
447 stamp_mutex--;
448 }
449
450 /*
451 * Paint a single character. This is for 16-pixel wide fonts.
452 */
453 static void
454 rasops15_putchar16(cookie, row, col, uc, attr)
455 void *cookie;
456 int row, col;
457 u_int uc;
458 long attr;
459 {
460 struct rasops_info *ri;
461 int height, so, fs;
462 int32_t *rp, *hrp;
463 u_char *fr;
464
465 /* Can't risk remaking the stamp if it's already in use */
466 if (stamp_mutex++) {
467 stamp_mutex--;
468 rasops15_putchar(cookie, row, col, uc, attr);
469 return;
470 }
471
472 ri = (struct rasops_info *)cookie;
473 hrp = NULL;
474
475 #ifdef RASOPS_CLIPPING
476 if ((unsigned)row >= (unsigned)ri->ri_rows) {
477 stamp_mutex--;
478 return;
479 }
480
481 if ((unsigned)col >= (unsigned)ri->ri_cols) {
482 stamp_mutex--;
483 return;
484 }
485 #endif
486
487 /* Recompute stamp? */
488 if (attr != stamp_attr)
489 rasops15_makestamp(ri, attr);
490
491 rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
492 if (ri->ri_hwbits)
493 hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
494 col*ri->ri_xscale);
495 height = ri->ri_font->fontheight;
496
497 if (uc == (u_int)-1) {
498 int32_t c = stamp[0];
499 while (height--) {
500 rp[0] = rp[1] = rp[2] = rp[3] =
501 rp[4] = rp[5] = rp[6] = rp[7] = c;
502 DELTA(rp, ri->ri_stride, int32_t *);
503 if (ri->ri_hwbits) {
504 hrp[0] = hrp[1] = hrp[2] = hrp[3] =
505 hrp[4] = hrp[5] = hrp[6] = hrp[7] = c;
506 DELTA(hrp, ri->ri_stride, int32_t *);
507 }
508 }
509 } else {
510 uc -= ri->ri_font->firstchar;
511 fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
512 fs = ri->ri_font->stride;
513
514 while (height--) {
515 so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
516 rp[0] = STAMP_READ(so);
517 rp[1] = STAMP_READ(so + 4);
518 if (ri->ri_hwbits) {
519 hrp[0] = STAMP_READ(so);
520 hrp[1] = STAMP_READ(so + 4);
521 }
522
523 so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
524 rp[2] = STAMP_READ(so);
525 rp[3] = STAMP_READ(so + 4);
526 if (ri->ri_hwbits) {
527 hrp[2] = STAMP_READ(so);
528 hrp[3] = STAMP_READ(so + 4);
529 }
530
531 so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
532 rp[4] = STAMP_READ(so);
533 rp[5] = STAMP_READ(so + 4);
534 if (ri->ri_hwbits) {
535 hrp[4] = STAMP_READ(so);
536 hrp[5] = STAMP_READ(so + 4);
537 }
538
539 so = STAMP_SHIFT(fr[1], 0) & STAMP_MASK;
540 rp[6] = STAMP_READ(so);
541 rp[7] = STAMP_READ(so + 4);
542 if (ri->ri_hwbits) {
543 hrp[6] = STAMP_READ(so);
544 hrp[7] = STAMP_READ(so + 4);
545 }
546
547 DELTA(rp, ri->ri_stride, int32_t *);
548 if (ri->ri_hwbits)
549 DELTA(hrp, ri->ri_stride, int32_t *);
550 fr += fs;
551 }
552 }
553
554 /* Do underline */
555 if (attr & 1) {
556 int32_t c = STAMP_READ(28);
557
558 DELTA(rp, -(ri->ri_stride << 1), int32_t *);
559 rp[0] = rp[1] = rp[2] = rp[3] =
560 rp[4] = rp[5] = rp[6] = rp[7] = c;
561 if (ri->ri_hwbits) {
562 DELTA(hrp, -(ri->ri_stride << 1), int32_t *);
563 hrp[0] = hrp[1] = hrp[2] = hrp[3] =
564 hrp[4] = hrp[5] = hrp[6] = hrp[7] = c;
565 }
566 }
567
568 stamp_mutex--;
569 }
570 #endif /* !RASOPS_SMALL */
571