video_subr.c revision 1.5 1 1.5 uwe /* $NetBSD: video_subr.c,v 1.5 2003/08/02 22:14:13 uwe Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.4 lukem
39 1.4 lukem #include <sys/cdefs.h>
40 1.5 uwe __KERNEL_RCSID(0, "$NetBSD: video_subr.c,v 1.5 2003/08/02 22:14:13 uwe Exp $");
41 1.1 uch
42 1.1 uch #include <sys/param.h>
43 1.1 uch #include <sys/systm.h>
44 1.1 uch #include <sys/malloc.h>
45 1.1 uch
46 1.1 uch #include <machine/bootinfo.h>
47 1.1 uch
48 1.1 uch #include <dev/hpc/video_subr.h>
49 1.1 uch
50 1.1 uch #define BPP2 ({ \
51 1.1 uch u_int8_t bitmap; \
52 1.1 uch bitmap = *(volatile u_int8_t*)addr; \
53 1.1 uch *(volatile u_int8_t*)addr = \
54 1.1 uch (bitmap & ~(0x3 << ((3 - (x % 4)) * 2))); \
55 1.1 uch })
56 1.1 uch
57 1.1 uch #define BPP4 ({ \
58 1.1 uch u_int8_t bitmap; \
59 1.1 uch bitmap = *(volatile u_int8_t*)addr; \
60 1.1 uch *(volatile u_int8_t*)addr = \
61 1.1 uch (bitmap & ~(0xf << ((1 - (x % 2)) * 4))); \
62 1.1 uch })
63 1.1 uch
64 1.1 uch #define BPP8 ({ \
65 1.1 uch *(volatile u_int8_t*)addr = 0xff; \
66 1.1 uch })
67 1.1 uch
68 1.1 uch #define BRESENHAM(a, b, c, d, func) ({ \
69 1.1 uch u_int32_t fbaddr = vc->vc_fbvaddr; \
70 1.1 uch u_int32_t fbwidth = vc->vc_fbwidth; \
71 1.1 uch u_int32_t fbdepth = vc->vc_fbdepth; \
72 1.1 uch len = a, step = b -1; \
73 1.1 uch if (step == 0) \
74 1.1 uch return; \
75 1.1 uch kstep = len == 0 ? 0 : 1; \
76 1.1 uch for (i = k = 0, j = step / 2; i <= step; i++) { \
77 1.1 uch x = xbase c; \
78 1.1 uch y = ybase d; \
79 1.1 uch addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3); \
80 1.1 uch func; \
81 1.1 uch j -= len; \
82 1.1 uch while (j < 0) { \
83 1.1 uch j += step; \
84 1.1 uch k += kstep; \
85 1.1 uch } \
86 1.1 uch } \
87 1.1 uch })
88 1.1 uch
89 1.1 uch #define DRAWLINE(func) ({ \
90 1.1 uch if (x < 0) { \
91 1.1 uch if (y < 0) { \
92 1.1 uch if (_y < _x) { \
93 1.1 uch BRESENHAM(_y, _x, -i, -k, func); \
94 1.1 uch } else { \
95 1.1 uch BRESENHAM(_x, _y, -k, -i, func); \
96 1.1 uch } \
97 1.1 uch } else { \
98 1.1 uch if (_y < _x) { \
99 1.1 uch BRESENHAM(_y, _x, -i, +k, func); \
100 1.1 uch } else { \
101 1.1 uch BRESENHAM(_x, _y, -k, +i, func); \
102 1.1 uch } \
103 1.1 uch } \
104 1.1 uch } else { \
105 1.1 uch if (y < 0) { \
106 1.1 uch if (_y < _x) { \
107 1.1 uch BRESENHAM(_y, _x, +i, -k, func); \
108 1.1 uch } else { \
109 1.1 uch BRESENHAM(_x, _y, +k, -i, func); \
110 1.1 uch } \
111 1.1 uch } else { \
112 1.1 uch if (_y < _x) { \
113 1.1 uch BRESENHAM(_y, _x, +i, +k, func); \
114 1.1 uch } else { \
115 1.1 uch BRESENHAM(_x, _y, +k, +i, func); \
116 1.1 uch } \
117 1.1 uch } \
118 1.1 uch } \
119 1.1 uch })
120 1.1 uch
121 1.1 uch #define LINEFUNC(b) \
122 1.1 uch static void linebpp##b (struct video_chip *, int, int, int, int); \
123 1.1 uch static void \
124 1.5 uwe linebpp##b(vc, x0, y0, x1, y1) \
125 1.1 uch struct video_chip *vc; \
126 1.1 uch int x0, y0, x1, y1; \
127 1.1 uch { \
128 1.1 uch u_int32_t addr; \
129 1.1 uch int i, j, k, len, step, kstep; \
130 1.1 uch int x, _x, y, _y; \
131 1.1 uch int xbase, ybase; \
132 1.1 uch x = x1 - x0; \
133 1.1 uch y = y1 - y0; \
134 1.1 uch _x = abs(x); \
135 1.1 uch _y = abs(y); \
136 1.1 uch xbase = x0; \
137 1.1 uch ybase = y0; \
138 1.5 uwe DRAWLINE(BPP##b); \
139 1.1 uch }
140 1.1 uch
141 1.1 uch #define DOTFUNC(b) \
142 1.1 uch static void dotbpp##b (struct video_chip *, int, int); \
143 1.1 uch static void \
144 1.5 uwe dotbpp##b(vc, x, y) \
145 1.1 uch struct video_chip *vc; \
146 1.1 uch int x, y; \
147 1.1 uch { \
148 1.1 uch u_int32_t addr; \
149 1.1 uch addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) * \
150 1.1 uch vc->vc_fbdepth) >> 3); \
151 1.1 uch BPP##b; \
152 1.1 uch }
153 1.1 uch
154 1.1 uch LINEFUNC(2)
155 1.1 uch LINEFUNC(4)
156 1.1 uch LINEFUNC(8)
157 1.1 uch DOTFUNC(2)
158 1.1 uch DOTFUNC(4)
159 1.1 uch DOTFUNC(8)
160 1.1 uch static void linebpp_unimpl(struct video_chip *, int, int, int, int);
161 1.1 uch static void dotbpp_unimpl(struct video_chip *, int, int);
162 1.1 uch
163 1.1 uch int
164 1.1 uch cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
165 1.2 uch int cnt)
166 1.1 uch {
167 1.3 uch KASSERT(LEGAL_CLUT_INDEX(cnt - 1));
168 1.1 uch
169 1.3 uch #define ALLOC_BUF(x, bit) \
170 1.3 uch if (x) { \
171 1.3 uch *x = malloc(cnt * sizeof(u_int ## bit ## _t), \
172 1.3 uch M_DEVBUF, M_WAITOK); \
173 1.3 uch if (*x == 0) \
174 1.3 uch goto errout; \
175 1.3 uch }
176 1.3 uch ALLOC_BUF(r, 8);
177 1.3 uch ALLOC_BUF(g, 8);
178 1.3 uch ALLOC_BUF(b, 8);
179 1.3 uch ALLOC_BUF(rgb, 32);
180 1.3 uch #undef ALLOCBUF
181 1.3 uch
182 1.3 uch return (0);
183 1.3 uch errout:
184 1.3 uch cmap_work_free(*r, *g, *b, *rgb);
185 1.1 uch
186 1.3 uch return (1);
187 1.1 uch }
188 1.1 uch
189 1.1 uch void
190 1.1 uch cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
191 1.1 uch {
192 1.1 uch if (r)
193 1.1 uch free(r, M_DEVBUF);
194 1.1 uch if (g)
195 1.1 uch free(g, M_DEVBUF);
196 1.1 uch if (b)
197 1.1 uch free(b, M_DEVBUF);
198 1.1 uch if (rgb)
199 1.1 uch free(rgb, M_DEVBUF);
200 1.1 uch }
201 1.1 uch
202 1.1 uch void
203 1.1 uch rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
204 1.1 uch {
205 1.1 uch int i;
206 1.1 uch KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
207 1.1 uch
208 1.1 uch for (i = 0; i < cnt; i++) {
209 1.1 uch *rgb24++ = RGB24(r[i], g[i], b[i]);
210 1.1 uch }
211 1.1 uch }
212 1.1 uch
213 1.1 uch void
214 1.1 uch rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
215 1.2 uch int cnt)
216 1.1 uch {
217 1.1 uch int i;
218 1.1 uch KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
219 1.1 uch
220 1.1 uch for (i = 0; i < cnt; i++) {
221 1.1 uch u_int32_t rgb = *rgb24++;
222 1.1 uch *r++ = (rgb >> 16) & 0xff;
223 1.1 uch *g++ = (rgb >> 8) & 0xff;
224 1.1 uch *b++ = rgb & 0xff;
225 1.1 uch }
226 1.1 uch }
227 1.1 uch
228 1.1 uch /*
229 1.1 uch * Debug routines.
230 1.1 uch */
231 1.1 uch void
232 1.1 uch video_calibration_pattern(struct video_chip *vc)
233 1.1 uch {
234 1.1 uch int x, y;
235 1.1 uch
236 1.1 uch x = vc->vc_fbwidth - 40;
237 1.1 uch y = vc->vc_fbheight - 40;
238 1.1 uch video_line(vc, 40, 40, x , 40);
239 1.1 uch video_line(vc, x , 40, x , y );
240 1.1 uch video_line(vc, x , y , 40, y );
241 1.1 uch video_line(vc, 40, y , 40, 40);
242 1.1 uch video_line(vc, 40, 40, x , y );
243 1.1 uch video_line(vc, x, 40, 40, y );
244 1.1 uch }
245 1.1 uch
246 1.1 uch static void
247 1.1 uch linebpp_unimpl(struct video_chip *vc, int x0, int y0, int x1, int y1)
248 1.1 uch {
249 1.1 uch return;
250 1.1 uch }
251 1.1 uch
252 1.1 uch static void
253 1.1 uch dotbpp_unimpl(struct video_chip *vc, int x, int y)
254 1.1 uch {
255 1.1 uch return;
256 1.1 uch }
257 1.1 uch
258 1.1 uch void
259 1.1 uch video_attach_drawfunc(struct video_chip *vc)
260 1.1 uch {
261 1.1 uch switch (vc->vc_fbdepth) {
262 1.1 uch default:
263 1.1 uch vc->vc_drawline = linebpp_unimpl;
264 1.1 uch vc->vc_drawdot = dotbpp_unimpl;
265 1.1 uch break;
266 1.1 uch case 8:
267 1.1 uch vc->vc_drawline = linebpp8;
268 1.1 uch vc->vc_drawdot = dotbpp8;
269 1.1 uch break;
270 1.1 uch case 4:
271 1.1 uch vc->vc_drawline = linebpp4;
272 1.1 uch vc->vc_drawdot = dotbpp4;
273 1.1 uch break;
274 1.1 uch case 2:
275 1.1 uch vc->vc_drawline = linebpp2;
276 1.1 uch vc->vc_drawdot = dotbpp2;
277 1.1 uch break;
278 1.1 uch }
279 1.1 uch }
280 1.1 uch
281 1.1 uch void
282 1.1 uch video_line(struct video_chip *vc, int x0, int y0, int x1, int y1)
283 1.1 uch {
284 1.1 uch if (vc->vc_drawline)
285 1.1 uch vc->vc_drawline(vc, x0, y0, x1, y1);
286 1.1 uch }
287 1.1 uch
288 1.1 uch void
289 1.1 uch video_dot(struct video_chip *vc, int x, int y)
290 1.1 uch {
291 1.1 uch if (vc->vc_drawdot)
292 1.1 uch vc->vc_drawdot(vc, x, y);
293 1.1 uch }
294 1.1 uch
295 1.1 uch int
296 1.1 uch video_reverse_color()
297 1.1 uch {
298 1.1 uch struct {
299 1.1 uch int reverse, normal;
300 1.1 uch } ctype[] = {
301 1.1 uch { BIFB_D2_M2L_3, BIFB_D2_M2L_0 },
302 1.1 uch { BIFB_D2_M2L_3x2, BIFB_D2_M2L_0x2 },
303 1.1 uch { BIFB_D8_FF, BIFB_D8_00 },
304 1.1 uch { BIFB_D16_FFFF, BIFB_D16_0000, },
305 1.1 uch { -1, -1 } /* terminator */
306 1.1 uch }, *ctypep;
307 1.1 uch u_int16_t fbtype;
308 1.1 uch
309 1.1 uch /* check reverse color */
310 1.1 uch fbtype = bootinfo->fb_type;
311 1.1 uch for (ctypep = ctype; ctypep->normal != -1 ; ctypep++) {
312 1.1 uch if (fbtype == ctypep->normal) {
313 1.1 uch return (0);
314 1.1 uch } else if (fbtype == ctypep->reverse) {
315 1.1 uch return (1);
316 1.1 uch }
317 1.1 uch }
318 1.1 uch printf(": WARNING unknown frame buffer type 0x%04x.\n", fbtype);
319 1.1 uch return (0);
320 1.1 uch }
321