rasops15.c revision 1.3 1 1.3 ad /* $NetBSD: rasops15.c,v 1.3 1999/04/26 04:27:47 ad Exp $ */
2 1.1 ad
3 1.2 ad /*
4 1.2 ad * Copyright (c) 1999 Andy Doran <ad (at) NetBSD.org>
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * Redistribution and use in source and binary forms, with or without
8 1.1 ad * modification, are permitted provided that the following conditions
9 1.1 ad * are met:
10 1.1 ad * 1. Redistributions of source code must retain the above copyright
11 1.1 ad * notice, this list of conditions and the following disclaimer.
12 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ad * notice, this list of conditions and the following disclaimer in the
14 1.1 ad * documentation and/or other materials provided with the distribution.
15 1.1 ad *
16 1.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2 ad * SUCH DAMAGE.
27 1.2 ad *
28 1.1 ad */
29 1.2 ad
30 1.1 ad #include "opt_rasops.h"
31 1.1 ad #include <sys/cdefs.h>
32 1.3 ad __KERNEL_RCSID(0, "$NetBSD: rasops15.c,v 1.3 1999/04/26 04:27:47 ad Exp $");
33 1.1 ad
34 1.1 ad #include <sys/types.h>
35 1.1 ad #include <sys/param.h>
36 1.1 ad #include <sys/systm.h>
37 1.1 ad #include <sys/time.h>
38 1.1 ad
39 1.1 ad #include <dev/wscons/wsdisplayvar.h>
40 1.1 ad #include <dev/wscons/wsconsio.h>
41 1.1 ad #include <dev/rasops/rasops.h>
42 1.1 ad
43 1.1 ad static void rasops15_putchar __P((void *, int, int, u_int, long attr));
44 1.1 ad static void rasops15_putchar8 __P((void *, int, int, u_int, long attr));
45 1.1 ad static void rasops15_putchar12 __P((void *, int, int, u_int, long attr));
46 1.1 ad static void rasops15_putchar16 __P((void *, int, int, u_int, long attr));
47 1.1 ad static void rasops15_makestamp __P((struct rasops_info *, long));
48 1.1 ad
49 1.1 ad void rasops15_init __P((struct rasops_info *ri));
50 1.1 ad
51 1.1 ad /*
52 1.1 ad * (2x2)x1 stamp for optimized character blitting
53 1.1 ad */
54 1.1 ad static int32_t stamp[32];
55 1.1 ad static long stamp_attr;
56 1.1 ad static int stamp_mutex; /* XXX see note in readme */
57 1.1 ad
58 1.1 ad /*
59 1.1 ad * XXX this confuses the hell out of gcc2 (not egcs) which always insists
60 1.1 ad * that the shift count is negative.
61 1.1 ad *
62 1.1 ad * offset = STAMP_SHIFT(fontbits, nibble #) & STAMP_MASK
63 1.1 ad * destination int32_t[0] = STAMP_READ(offset)
64 1.1 ad * destination int32_t[1] = STAMP_READ(offset + 4)
65 1.1 ad */
66 1.1 ad #define STAMP_SHIFT(fb,n) ((n*4-3) >= 0 ? (fb)>>(n*4-3):(fb)<<-(n*4-3))
67 1.1 ad #define STAMP_MASK (15 << 3)
68 1.1 ad #define STAMP_READ(o) (*(int32_t *)((caddr_t)stamp + (o)))
69 1.1 ad
70 1.1 ad
71 1.1 ad /*
72 1.1 ad * Initalize rasops_info struct for this colordepth.
73 1.1 ad */
74 1.1 ad void
75 1.1 ad rasops15_init(ri)
76 1.1 ad struct rasops_info *ri;
77 1.1 ad {
78 1.1 ad
79 1.1 ad switch (ri->ri_font->fontwidth) {
80 1.1 ad case 8:
81 1.1 ad ri->ri_ops.putchar = rasops15_putchar8;
82 1.1 ad break;
83 1.1 ad
84 1.1 ad case 12:
85 1.1 ad ri->ri_ops.putchar = rasops15_putchar12;
86 1.1 ad break;
87 1.1 ad
88 1.1 ad case 16:
89 1.1 ad ri->ri_ops.putchar = rasops15_putchar16;
90 1.1 ad break;
91 1.3 ad
92 1.1 ad default:
93 1.1 ad ri->ri_ops.putchar = rasops15_putchar;
94 1.1 ad break;
95 1.1 ad }
96 1.1 ad
97 1.1 ad if (ri->ri_rnum == 0) {
98 1.1 ad ri->ri_rnum = 5;
99 1.1 ad ri->ri_rpos = 0;
100 1.1 ad ri->ri_gnum = 5 + (ri->ri_depth == 16);
101 1.1 ad ri->ri_gpos = 5;
102 1.1 ad ri->ri_bnum = 5;
103 1.1 ad ri->ri_bpos = 10 + (ri->ri_depth == 16);
104 1.1 ad }
105 1.1 ad }
106 1.1 ad
107 1.1 ad
108 1.1 ad /*
109 1.1 ad * Paint a single character.
110 1.1 ad */
111 1.1 ad static void
112 1.1 ad rasops15_putchar(cookie, row, col, uc, attr)
113 1.1 ad void *cookie;
114 1.1 ad int row, col;
115 1.1 ad u_int uc;
116 1.1 ad long attr;
117 1.1 ad {
118 1.1 ad struct rasops_info *ri;
119 1.1 ad int fb, width, height, cnt, clr[2];
120 1.1 ad u_char *dp, *rp, *fr;
121 1.1 ad
122 1.1 ad ri = (struct rasops_info *)cookie;
123 1.1 ad
124 1.1 ad #ifdef RASOPS_CLIPPING
125 1.1 ad /* Catches 'row < 0' case too */
126 1.1 ad if ((unsigned)row >= (unsigned)ri->ri_rows)
127 1.1 ad return;
128 1.1 ad
129 1.1 ad if ((unsigned)col >= (unsigned)ri->ri_cols)
130 1.1 ad return;
131 1.1 ad #endif
132 1.1 ad
133 1.1 ad rp = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
134 1.1 ad height = ri->ri_font->fontheight;
135 1.1 ad width = ri->ri_font->fontwidth;
136 1.1 ad
137 1.1 ad clr[1] = ri->ri_devcmap[((u_int)attr >> 24) & 15];
138 1.1 ad clr[0] = ri->ri_devcmap[((u_int)attr >> 16) & 15];
139 1.1 ad
140 1.3 ad if (uc == ' ') {
141 1.3 ad while (height--) {
142 1.3 ad dp = rp;
143 1.3 ad rp += ri->ri_stride;
144 1.3 ad
145 1.3 ad for (cnt = width; cnt; cnt--) {
146 1.3 ad *(int16_t *)dp = (int16_t)clr[0];
147 1.3 ad dp += 2;
148 1.3 ad }
149 1.3 ad }
150 1.3 ad } else {
151 1.3 ad uc -= ri->ri_font->firstchar;
152 1.3 ad fr = (u_char *)ri->ri_font->data + uc * ri->ri_fontscale;
153 1.3 ad
154 1.3 ad while (height--) {
155 1.3 ad dp = rp;
156 1.3 ad fb = fr[3] | (fr[2] << 8) | (fr[1] << 16) | (fr[0] << 24);
157 1.3 ad fr += ri->ri_font->stride;
158 1.3 ad rp += ri->ri_stride;
159 1.1 ad
160 1.3 ad for (cnt = width; cnt; cnt--) {
161 1.3 ad *(int16_t *)dp = (int16_t)clr[(fb >> 31) & 1];
162 1.3 ad fb <<= 1;
163 1.3 ad dp += 2;
164 1.3 ad }
165 1.3 ad }
166 1.3 ad }
167 1.3 ad
168 1.3 ad /* Do underline */
169 1.3 ad if (attr & 1) {
170 1.3 ad rp -= ri->ri_stride << 1;
171 1.3 ad
172 1.3 ad while (width--) {
173 1.3 ad *(int16_t *)rp = clr[1];
174 1.3 ad rp += 2;
175 1.1 ad }
176 1.1 ad }
177 1.1 ad }
178 1.1 ad
179 1.1 ad
180 1.1 ad /*
181 1.1 ad * Recompute the (2x2)x1 blitting stamp.
182 1.1 ad */
183 1.1 ad static void
184 1.1 ad rasops15_makestamp(ri, attr)
185 1.1 ad struct rasops_info *ri;
186 1.1 ad long attr;
187 1.1 ad {
188 1.1 ad int32_t fg, bg;
189 1.1 ad int i;
190 1.1 ad
191 1.3 ad fg = ri->ri_devcmap[((u_int)attr >> 24) & 15] & 0xffff;
192 1.3 ad bg = ri->ri_devcmap[((u_int)attr >> 16) & 15] & 0xffff;
193 1.1 ad stamp_attr = attr;
194 1.1 ad
195 1.3 ad for (i = 0; i < 32; i += 2) {
196 1.1 ad #if BYTE_ORDER == LITTLE_ENDIAN
197 1.3 ad stamp[i] = (i & 16 ? fg : bg);
198 1.3 ad stamp[i] |= ((i & 8 ? fg : bg) << 16);
199 1.3 ad stamp[i + 1] = (i & 4 ? fg : bg);
200 1.3 ad stamp[i + 1] |= ((i & 2 ? fg : bg) << 16);
201 1.1 ad #else
202 1.3 ad stamp[i] = (i & 2 ? fg : bg);
203 1.3 ad stamp[i] |= ((i & 4 ? fg : bg) << 16);
204 1.3 ad stamp[i + 1] = (i & 8 ? fg : bg);
205 1.3 ad stamp[i + 1] |= ((i & 16 ? fg : bg) << 16);
206 1.1 ad #endif
207 1.1 ad }
208 1.1 ad }
209 1.1 ad
210 1.1 ad
211 1.1 ad /*
212 1.1 ad * Paint a single character. This is for 8-pixel wide fonts.
213 1.1 ad */
214 1.1 ad static void
215 1.1 ad rasops15_putchar8(cookie, row, col, uc, attr)
216 1.1 ad void *cookie;
217 1.1 ad int row, col;
218 1.1 ad u_int uc;
219 1.1 ad long attr;
220 1.1 ad {
221 1.1 ad struct rasops_info *ri;
222 1.1 ad int height, so, fs;
223 1.1 ad int32_t *rp;
224 1.1 ad u_char *fr;
225 1.1 ad
226 1.1 ad /* Can't risk remaking the stamp if it's already in use */
227 1.1 ad if (stamp_mutex++) {
228 1.1 ad stamp_mutex--;
229 1.1 ad rasops15_putchar(cookie, row, col, uc, attr);
230 1.1 ad return;
231 1.1 ad }
232 1.1 ad
233 1.1 ad ri = (struct rasops_info *)cookie;
234 1.1 ad
235 1.1 ad #ifdef RASOPS_CLIPPING
236 1.1 ad if ((unsigned)row >= (unsigned)ri->ri_rows) {
237 1.1 ad stamp_mutex--;
238 1.1 ad return;
239 1.1 ad }
240 1.1 ad
241 1.1 ad if ((unsigned)col >= (unsigned)ri->ri_cols) {
242 1.1 ad stamp_mutex--;
243 1.1 ad return;
244 1.1 ad }
245 1.1 ad #endif
246 1.1 ad
247 1.1 ad /* Recompute stamp? */
248 1.1 ad if (attr != stamp_attr)
249 1.1 ad rasops15_makestamp(ri, attr);
250 1.1 ad
251 1.1 ad rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
252 1.1 ad height = ri->ri_font->fontheight;
253 1.1 ad
254 1.1 ad if (uc == (u_int)-1) {
255 1.1 ad while (height--) {
256 1.1 ad rp[0] = stamp[0];
257 1.1 ad rp[1] = stamp[0];
258 1.1 ad rp[2] = stamp[0];
259 1.1 ad rp[3] = stamp[0];
260 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
261 1.1 ad }
262 1.1 ad } else {
263 1.1 ad uc -= ri->ri_font->firstchar;
264 1.1 ad fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
265 1.1 ad fs = ri->ri_font->stride;
266 1.1 ad
267 1.1 ad while (height--) {
268 1.1 ad so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
269 1.1 ad rp[0] = STAMP_READ(so);
270 1.1 ad rp[1] = STAMP_READ(so + 4);
271 1.1 ad
272 1.1 ad so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
273 1.1 ad rp[2] = STAMP_READ(so);
274 1.1 ad rp[3] = STAMP_READ(so + 4);
275 1.1 ad
276 1.1 ad fr += fs;
277 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
278 1.1 ad }
279 1.1 ad }
280 1.3 ad
281 1.3 ad /* Do underline */
282 1.3 ad if (attr & 1) {
283 1.3 ad DELTA(rp, -(ri->ri_stride << 1), int32_t *);
284 1.3 ad rp[0] = STAMP_READ(30);
285 1.3 ad rp[1] = STAMP_READ(30);
286 1.3 ad rp[2] = STAMP_READ(30);
287 1.3 ad rp[3] = STAMP_READ(30);
288 1.3 ad }
289 1.1 ad
290 1.1 ad stamp_mutex--;
291 1.1 ad }
292 1.1 ad
293 1.1 ad
294 1.1 ad /*
295 1.1 ad * Paint a single character. This is for 12-pixel wide fonts.
296 1.1 ad */
297 1.1 ad static void
298 1.1 ad rasops15_putchar12(cookie, row, col, uc, attr)
299 1.1 ad void *cookie;
300 1.1 ad int row, col;
301 1.1 ad u_int uc;
302 1.1 ad long attr;
303 1.1 ad {
304 1.1 ad struct rasops_info *ri;
305 1.1 ad int height, so, fs;
306 1.1 ad int32_t *rp;
307 1.1 ad u_char *fr;
308 1.1 ad
309 1.1 ad /* Can't risk remaking the stamp if it's already in use */
310 1.1 ad if (stamp_mutex++) {
311 1.1 ad stamp_mutex--;
312 1.1 ad rasops15_putchar(cookie, row, col, uc, attr);
313 1.1 ad return;
314 1.1 ad }
315 1.1 ad
316 1.1 ad ri = (struct rasops_info *)cookie;
317 1.1 ad
318 1.1 ad #ifdef RASOPS_CLIPPING
319 1.1 ad if ((unsigned)row >= (unsigned)ri->ri_rows) {
320 1.1 ad stamp_mutex--;
321 1.1 ad return;
322 1.1 ad }
323 1.1 ad
324 1.1 ad if ((unsigned)col >= (unsigned)ri->ri_cols) {
325 1.1 ad stamp_mutex--;
326 1.1 ad return;
327 1.1 ad }
328 1.1 ad #endif
329 1.1 ad
330 1.1 ad /* Recompute stamp? */
331 1.1 ad if (attr != stamp_attr)
332 1.1 ad rasops15_makestamp(ri, attr);
333 1.1 ad
334 1.1 ad rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
335 1.1 ad height = ri->ri_font->fontheight;
336 1.1 ad
337 1.1 ad if (uc == (u_int)-1) {
338 1.1 ad while (height--) {
339 1.1 ad rp[0] = stamp[0];
340 1.1 ad rp[1] = stamp[0];
341 1.1 ad rp[2] = stamp[0];
342 1.1 ad rp[3] = stamp[0];
343 1.1 ad rp[4] = stamp[0];
344 1.1 ad rp[5] = stamp[0];
345 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
346 1.1 ad }
347 1.1 ad } else {
348 1.1 ad uc -= ri->ri_font->firstchar;
349 1.1 ad fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
350 1.1 ad fs = ri->ri_font->stride;
351 1.1 ad
352 1.1 ad while (height--) {
353 1.1 ad so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
354 1.1 ad rp[0] = STAMP_READ(so);
355 1.1 ad rp[1] = STAMP_READ(so + 4);
356 1.1 ad
357 1.1 ad so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
358 1.1 ad rp[2] = STAMP_READ(so);
359 1.1 ad rp[3] = STAMP_READ(so + 4);
360 1.1 ad
361 1.1 ad so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
362 1.1 ad rp[4] = STAMP_READ(so);
363 1.1 ad rp[5] = STAMP_READ(so + 4);
364 1.1 ad
365 1.1 ad fr += fs;
366 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
367 1.1 ad }
368 1.1 ad }
369 1.3 ad
370 1.3 ad /* Do underline */
371 1.3 ad if (attr & 1) {
372 1.3 ad DELTA(rp, -(ri->ri_stride << 1), int32_t *);
373 1.3 ad rp[0] = STAMP_READ(30);
374 1.3 ad rp[1] = STAMP_READ(30);
375 1.3 ad rp[2] = STAMP_READ(30);
376 1.3 ad rp[3] = STAMP_READ(30);
377 1.3 ad rp[4] = STAMP_READ(30);
378 1.3 ad rp[5] = STAMP_READ(30);
379 1.3 ad }
380 1.1 ad
381 1.1 ad stamp_mutex--;
382 1.1 ad }
383 1.1 ad
384 1.1 ad
385 1.1 ad /*
386 1.1 ad * Paint a single character. This is for 16-pixel wide fonts.
387 1.1 ad */
388 1.1 ad static void
389 1.1 ad rasops15_putchar16(cookie, row, col, uc, attr)
390 1.1 ad void *cookie;
391 1.1 ad int row, col;
392 1.1 ad u_int uc;
393 1.1 ad long attr;
394 1.1 ad {
395 1.1 ad struct rasops_info *ri;
396 1.1 ad int height, so, fs;
397 1.1 ad int32_t *rp;
398 1.1 ad u_char *fr;
399 1.1 ad
400 1.1 ad /* Can't risk remaking the stamp if it's already in use */
401 1.1 ad if (stamp_mutex++) {
402 1.1 ad stamp_mutex--;
403 1.1 ad rasops15_putchar(cookie, row, col, uc, attr);
404 1.1 ad return;
405 1.1 ad }
406 1.1 ad
407 1.1 ad ri = (struct rasops_info *)cookie;
408 1.1 ad
409 1.1 ad #ifdef RASOPS_CLIPPING
410 1.1 ad if ((unsigned)row >= (unsigned)ri->ri_rows) {
411 1.1 ad stamp_mutex--;
412 1.1 ad return;
413 1.1 ad }
414 1.1 ad
415 1.1 ad if ((unsigned)col >= (unsigned)ri->ri_cols) {
416 1.1 ad stamp_mutex--;
417 1.1 ad return;
418 1.1 ad }
419 1.1 ad #endif
420 1.1 ad
421 1.1 ad /* Recompute stamp? */
422 1.1 ad if (attr != stamp_attr)
423 1.1 ad rasops15_makestamp(ri, attr);
424 1.1 ad
425 1.1 ad rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale);
426 1.1 ad height = ri->ri_font->fontheight;
427 1.1 ad
428 1.1 ad if (uc == (u_int)-1) {
429 1.1 ad while (height--) {
430 1.1 ad rp[0] = stamp[0];
431 1.1 ad rp[1] = stamp[0];
432 1.1 ad rp[2] = stamp[0];
433 1.1 ad rp[3] = stamp[0];
434 1.1 ad rp[4] = stamp[0];
435 1.1 ad rp[5] = stamp[0];
436 1.1 ad rp[6] = stamp[0];
437 1.1 ad rp[7] = stamp[0];
438 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
439 1.1 ad }
440 1.1 ad } else {
441 1.1 ad uc -= ri->ri_font->firstchar;
442 1.1 ad fr = (u_char *)ri->ri_font->data + uc*ri->ri_fontscale;
443 1.1 ad fs = ri->ri_font->stride;
444 1.1 ad
445 1.1 ad while (height--) {
446 1.1 ad so = STAMP_SHIFT(fr[0], 1) & STAMP_MASK;
447 1.1 ad rp[0] = STAMP_READ(so);
448 1.1 ad rp[1] = STAMP_READ(so + 4);
449 1.1 ad
450 1.1 ad so = STAMP_SHIFT(fr[0], 0) & STAMP_MASK;
451 1.1 ad rp[2] = STAMP_READ(so);
452 1.1 ad rp[3] = STAMP_READ(so + 4);
453 1.1 ad
454 1.1 ad so = STAMP_SHIFT(fr[1], 1) & STAMP_MASK;
455 1.1 ad rp[4] = STAMP_READ(so);
456 1.1 ad rp[5] = STAMP_READ(so + 4);
457 1.1 ad
458 1.1 ad so = STAMP_SHIFT(fr[1], 0) & STAMP_MASK;
459 1.1 ad rp[6] = STAMP_READ(so);
460 1.1 ad rp[7] = STAMP_READ(so + 4);
461 1.1 ad
462 1.1 ad DELTA(rp, ri->ri_stride, int32_t *);
463 1.1 ad fr += fs;
464 1.1 ad }
465 1.1 ad }
466 1.3 ad
467 1.3 ad /* Do underline */
468 1.3 ad if (attr & 1) {
469 1.3 ad DELTA(rp, -(ri->ri_stride << 1), int32_t *);
470 1.3 ad rp[0] = STAMP_READ(30);
471 1.3 ad rp[1] = STAMP_READ(30);
472 1.3 ad rp[2] = STAMP_READ(30);
473 1.3 ad rp[3] = STAMP_READ(30);
474 1.3 ad rp[4] = STAMP_READ(30);
475 1.3 ad rp[5] = STAMP_READ(30);
476 1.3 ad rp[6] = STAMP_READ(30);
477 1.3 ad rp[7] = STAMP_READ(30);
478 1.3 ad }
479 1.1 ad
480 1.1 ad stamp_mutex--;
481 1.1 ad }
482