1 | /* $NetBSD: rasops32.c,v 1.29 2013/09/15 09:39:47 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: rasops32.c,v 1.29 2013/09/15 09:39:47 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 | |
41 | #include <dev/wscons/wsdisplayvar.h> |
42 | #include <dev/wscons/wsconsio.h> |
43 | #include <dev/rasops/rasops.h> |
44 | |
45 | static void rasops32_putchar(void *, int, int, u_int, long attr); |
46 | static void rasops32_putchar_aa(void *, int, int, u_int, long attr); |
47 | |
48 | /* |
49 | * Initialize a 'rasops_info' descriptor for this depth. |
50 | */ |
51 | void |
52 | rasops32_init(struct rasops_info *ri) |
53 | { |
54 | |
55 | if (ri->ri_rnum == 0) { |
56 | ri->ri_rnum = 8; |
57 | ri->ri_rpos = 0; |
58 | ri->ri_gnum = 8; |
59 | ri->ri_gpos = 8; |
60 | ri->ri_bnum = 8; |
61 | ri->ri_bpos = 16; |
62 | } |
63 | |
64 | if (FONT_IS_ALPHA(ri->ri_font)) { |
65 | ri->ri_ops.putchar = rasops32_putchar_aa; |
66 | } else |
67 | ri->ri_ops.putchar = rasops32_putchar; |
68 | } |
69 | |
70 | /* |
71 | * Paint a single character. |
72 | */ |
73 | |
74 | static void |
75 | rasops32_putchar(void *cookie, int row, int col, u_int uc, long attr) |
76 | { |
77 | int width, height, cnt, fs, fb, clr[2]; |
78 | struct rasops_info *ri = (struct rasops_info *)cookie; |
79 | struct wsdisplay_font *font = PICK_FONT(ri, uc); |
80 | int32_t *dp, *rp, *hp, *hrp; |
81 | u_char *fr; |
82 | |
83 | hp = hrp = NULL; |
84 | |
85 | #ifdef RASOPS_CLIPPING |
86 | /* Catches 'row < 0' case too */ |
87 | if ((unsigned)row >= (unsigned)ri->ri_rows) |
88 | return; |
89 | |
90 | if ((unsigned)col >= (unsigned)ri->ri_cols) |
91 | return; |
92 | #endif |
93 | |
94 | /* check if character fits into font limits */ |
95 | if (!CHAR_IN_FONT(uc, font)) |
96 | return; |
97 | |
98 | rp = (int32_t *)(ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale); |
99 | if (ri->ri_hwbits) |
100 | hrp = (int32_t *)(ri->ri_hwbits + row*ri->ri_yscale + |
101 | col*ri->ri_xscale); |
102 | |
103 | height = font->fontheight; |
104 | width = font->fontwidth; |
105 | |
106 | clr[0] = ri->ri_devcmap[(attr >> 16) & 0xf]; |
107 | clr[1] = ri->ri_devcmap[(attr >> 24) & 0xf]; |
108 | |
109 | if (uc == ' ') { |
110 | while (height--) { |
111 | dp = rp; |
112 | DELTA(rp, ri->ri_stride, int32_t *); |
113 | if (ri->ri_hwbits) { |
114 | hp = hrp; |
115 | DELTA(hrp, ri->ri_stride, int32_t *); |
116 | } |
117 | |
118 | for (cnt = width; cnt; cnt--) { |
119 | *dp++ = clr[0]; |
120 | if (ri->ri_hwbits) |
121 | *hp++ = clr[0]; |
122 | } |
123 | } |
124 | } else { |
125 | fr = WSFONT_GLYPH(uc, font); |
126 | fs = font->stride; |
127 | |
128 | while (height--) { |
129 | dp = rp; |
130 | fb = fr[3] | (fr[2] << 8) | (fr[1] << 16) | |
131 | (fr[0] << 24); |
132 | fr += fs; |
133 | DELTA(rp, ri->ri_stride, int32_t *); |
134 | if (ri->ri_hwbits) { |
135 | hp = hrp; |
136 | DELTA(hrp, ri->ri_stride, int32_t *); |
137 | } |
138 | |
139 | for (cnt = width; cnt; cnt--) { |
140 | *dp++ = clr[(fb >> 31) & 1]; |
141 | if (ri->ri_hwbits) |
142 | *hp++ = clr[(fb >> 31) & 1]; |
143 | fb <<= 1; |
144 | } |
145 | } |
146 | } |
147 | |
148 | /* Do underline */ |
149 | if ((attr & 1) != 0) { |
150 | DELTA(rp, -(ri->ri_stride << 1), int32_t *); |
151 | if (ri->ri_hwbits) |
152 | DELTA(hrp, -(ri->ri_stride << 1), int32_t *); |
153 | |
154 | while (width--) { |
155 | *rp++ = clr[1]; |
156 | if (ri->ri_hwbits) |
157 | *hrp++ = clr[1]; |
158 | } |
159 | } |
160 | } |
161 | |
162 | static void |
163 | rasops32_putchar_aa(void *cookie, int row, int col, u_int uc, long attr) |
164 | { |
165 | int width, height, cnt, clr[2]; |
166 | struct rasops_info *ri = (struct rasops_info *)cookie; |
167 | struct wsdisplay_font *font = PICK_FONT(ri, uc); |
168 | int32_t *dp, *rp; |
169 | uint8_t *rrp; |
170 | u_char *fr; |
171 | uint32_t buffer[64]; /* XXX */ |
172 | int x, y, r, g, b, aval; |
173 | int r1, g1, b1, r0, g0, b0; |
174 | |
175 | #ifdef RASOPS_CLIPPING |
176 | /* Catches 'row < 0' case too */ |
177 | if ((unsigned)row >= (unsigned)ri->ri_rows) |
178 | return; |
179 | |
180 | if ((unsigned)col >= (unsigned)ri->ri_cols) |
181 | return; |
182 | #endif |
183 | |
184 | /* check if character fits into font limits */ |
185 | if (!CHAR_IN_FONT(uc, font)) |
186 | return; |
187 | |
188 | rrp = (ri->ri_bits + row*ri->ri_yscale + col*ri->ri_xscale); |
189 | rp = (int32_t *)rrp; |
190 | |
191 | height = font->fontheight; |
192 | width = font->fontwidth; |
193 | |
194 | clr[0] = ri->ri_devcmap[(attr >> 16) & 0xf]; |
195 | clr[1] = ri->ri_devcmap[(attr >> 24) & 0xf]; |
196 | |
197 | if (uc == ' ') { |
198 | for (cnt = 0; cnt < width; cnt++) |
199 | buffer[cnt] = clr[0]; |
200 | while (height--) { |
201 | dp = rp; |
202 | DELTA(rp, ri->ri_stride, int32_t *); |
203 | memcpy(dp, buffer, width << 2); |
204 | } |
205 | } else { |
206 | fr = WSFONT_GLYPH(uc, font); |
207 | |
208 | r0 = (clr[0] >> 16) & 0xff; |
209 | r1 = (clr[1] >> 16) & 0xff; |
210 | g0 = (clr[0] >> 8) & 0xff; |
211 | g1 = (clr[1] >> 8) & 0xff; |
212 | b0 = clr[0] & 0xff; |
213 | b1 = clr[1] & 0xff; |
214 | |
215 | for (y = 0; y < height; y++) { |
216 | dp = (uint32_t *)(rrp + ri->ri_stride * y); |
217 | for (x = 0; x < width; x++) { |
218 | aval = *fr; |
219 | if (aval == 0) { |
220 | buffer[x] = clr[0]; |
221 | } else if (aval == 255) { |
222 | buffer[x] = clr[1]; |
223 | } else { |
224 | r = aval * r1 + (255 - aval) * r0; |
225 | g = aval * g1 + (255 - aval) * g0; |
226 | b = aval * b1 + (255 - aval) * b0; |
227 | buffer[x] = (r & 0xff00) << 8 | |
228 | (g & 0xff00) | |
229 | (b & 0xff00) >> 8; |
230 | } |
231 | fr++; |
232 | } |
233 | memcpy(dp, buffer, width << 2); |
234 | } |
235 | } |
236 | |
237 | /* Do underline */ |
238 | if ((attr & 1) != 0) { |
239 | rp = (uint32_t *)rrp; |
240 | DELTA(rp, (ri->ri_stride * (height - 2)), int32_t *); |
241 | while (width--) |
242 | *rp++ = clr[1]; |
243 | } |
244 | } |
245 | |