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