video_subr.c revision 1.3 1 1.3 uch /* $NetBSD: video_subr.c,v 1.3 2001/06/05 15:02:12 uch 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.1 uch
39 1.1 uch #include <sys/param.h>
40 1.1 uch #include <sys/systm.h>
41 1.1 uch #include <sys/malloc.h>
42 1.1 uch
43 1.1 uch #include <machine/bootinfo.h>
44 1.1 uch
45 1.1 uch #include <dev/hpc/video_subr.h>
46 1.1 uch
47 1.1 uch #define BPP2 ({ \
48 1.1 uch u_int8_t bitmap; \
49 1.1 uch bitmap = *(volatile u_int8_t*)addr; \
50 1.1 uch *(volatile u_int8_t*)addr = \
51 1.1 uch (bitmap & ~(0x3 << ((3 - (x % 4)) * 2))); \
52 1.1 uch })
53 1.1 uch
54 1.1 uch #define BPP4 ({ \
55 1.1 uch u_int8_t bitmap; \
56 1.1 uch bitmap = *(volatile u_int8_t*)addr; \
57 1.1 uch *(volatile u_int8_t*)addr = \
58 1.1 uch (bitmap & ~(0xf << ((1 - (x % 2)) * 4))); \
59 1.1 uch })
60 1.1 uch
61 1.1 uch #define BPP8 ({ \
62 1.1 uch *(volatile u_int8_t*)addr = 0xff; \
63 1.1 uch })
64 1.1 uch
65 1.1 uch #define BRESENHAM(a, b, c, d, func) ({ \
66 1.1 uch u_int32_t fbaddr = vc->vc_fbvaddr; \
67 1.1 uch u_int32_t fbwidth = vc->vc_fbwidth; \
68 1.1 uch u_int32_t fbdepth = vc->vc_fbdepth; \
69 1.1 uch len = a, step = b -1; \
70 1.1 uch if (step == 0) \
71 1.1 uch return; \
72 1.1 uch kstep = len == 0 ? 0 : 1; \
73 1.1 uch for (i = k = 0, j = step / 2; i <= step; i++) { \
74 1.1 uch x = xbase c; \
75 1.1 uch y = ybase d; \
76 1.1 uch addr = fbaddr + (((y * fbwidth + x) * fbdepth) >> 3); \
77 1.1 uch func; \
78 1.1 uch j -= len; \
79 1.1 uch while (j < 0) { \
80 1.1 uch j += step; \
81 1.1 uch k += kstep; \
82 1.1 uch } \
83 1.1 uch } \
84 1.1 uch })
85 1.1 uch
86 1.1 uch #define DRAWLINE(func) ({ \
87 1.1 uch if (x < 0) { \
88 1.1 uch if (y < 0) { \
89 1.1 uch if (_y < _x) { \
90 1.1 uch BRESENHAM(_y, _x, -i, -k, func); \
91 1.1 uch } else { \
92 1.1 uch BRESENHAM(_x, _y, -k, -i, func); \
93 1.1 uch } \
94 1.1 uch } else { \
95 1.1 uch if (_y < _x) { \
96 1.1 uch BRESENHAM(_y, _x, -i, +k, func); \
97 1.1 uch } else { \
98 1.1 uch BRESENHAM(_x, _y, -k, +i, func); \
99 1.1 uch } \
100 1.1 uch } \
101 1.1 uch } else { \
102 1.1 uch if (y < 0) { \
103 1.1 uch if (_y < _x) { \
104 1.1 uch BRESENHAM(_y, _x, +i, -k, func); \
105 1.1 uch } else { \
106 1.1 uch BRESENHAM(_x, _y, +k, -i, func); \
107 1.1 uch } \
108 1.1 uch } else { \
109 1.1 uch if (_y < _x) { \
110 1.1 uch BRESENHAM(_y, _x, +i, +k, func); \
111 1.1 uch } else { \
112 1.1 uch BRESENHAM(_x, _y, +k, +i, func); \
113 1.1 uch } \
114 1.1 uch } \
115 1.1 uch } \
116 1.1 uch })
117 1.1 uch
118 1.1 uch #define LINEFUNC(b) \
119 1.1 uch static void linebpp##b (struct video_chip *, int, int, int, int); \
120 1.1 uch static void \
121 1.1 uch linebpp##b##(vc, x0, y0, x1, y1) \
122 1.1 uch struct video_chip *vc; \
123 1.1 uch int x0, y0, x1, y1; \
124 1.1 uch { \
125 1.1 uch u_int32_t addr; \
126 1.1 uch int i, j, k, len, step, kstep; \
127 1.1 uch int x, _x, y, _y; \
128 1.1 uch int xbase, ybase; \
129 1.1 uch x = x1 - x0; \
130 1.1 uch y = y1 - y0; \
131 1.1 uch _x = abs(x); \
132 1.1 uch _y = abs(y); \
133 1.1 uch xbase = x0; \
134 1.1 uch ybase = y0; \
135 1.1 uch DRAWLINE(BPP##b##); \
136 1.1 uch }
137 1.1 uch
138 1.1 uch #define DOTFUNC(b) \
139 1.1 uch static void dotbpp##b (struct video_chip *, int, int); \
140 1.1 uch static void \
141 1.1 uch dotbpp##b##(vc, x, y) \
142 1.1 uch struct video_chip *vc; \
143 1.1 uch int x, y; \
144 1.1 uch { \
145 1.1 uch u_int32_t addr; \
146 1.1 uch addr = vc->vc_fbvaddr + (((y * vc->vc_fbwidth + x) * \
147 1.1 uch vc->vc_fbdepth) >> 3); \
148 1.1 uch BPP##b; \
149 1.1 uch }
150 1.1 uch
151 1.1 uch LINEFUNC(2)
152 1.1 uch LINEFUNC(4)
153 1.1 uch LINEFUNC(8)
154 1.1 uch DOTFUNC(2)
155 1.1 uch DOTFUNC(4)
156 1.1 uch DOTFUNC(8)
157 1.1 uch static void linebpp_unimpl(struct video_chip *, int, int, int, int);
158 1.1 uch static void dotbpp_unimpl(struct video_chip *, int, int);
159 1.1 uch
160 1.1 uch int
161 1.1 uch cmap_work_alloc(u_int8_t **r, u_int8_t **g, u_int8_t **b, u_int32_t **rgb,
162 1.2 uch int cnt)
163 1.1 uch {
164 1.3 uch KASSERT(LEGAL_CLUT_INDEX(cnt - 1));
165 1.1 uch
166 1.3 uch #define ALLOC_BUF(x, bit) \
167 1.3 uch if (x) { \
168 1.3 uch *x = malloc(cnt * sizeof(u_int ## bit ## _t), \
169 1.3 uch M_DEVBUF, M_WAITOK); \
170 1.3 uch if (*x == 0) \
171 1.3 uch goto errout; \
172 1.3 uch }
173 1.3 uch ALLOC_BUF(r, 8);
174 1.3 uch ALLOC_BUF(g, 8);
175 1.3 uch ALLOC_BUF(b, 8);
176 1.3 uch ALLOC_BUF(rgb, 32);
177 1.3 uch #undef ALLOCBUF
178 1.3 uch
179 1.3 uch return (0);
180 1.3 uch errout:
181 1.3 uch cmap_work_free(*r, *g, *b, *rgb);
182 1.1 uch
183 1.3 uch return (1);
184 1.1 uch }
185 1.1 uch
186 1.1 uch void
187 1.1 uch cmap_work_free(u_int8_t *r, u_int8_t *g, u_int8_t *b, u_int32_t *rgb)
188 1.1 uch {
189 1.1 uch if (r)
190 1.1 uch free(r, M_DEVBUF);
191 1.1 uch if (g)
192 1.1 uch free(g, M_DEVBUF);
193 1.1 uch if (b)
194 1.1 uch free(b, M_DEVBUF);
195 1.1 uch if (rgb)
196 1.1 uch free(rgb, M_DEVBUF);
197 1.1 uch }
198 1.1 uch
199 1.1 uch void
200 1.1 uch rgb24_compose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b, int cnt)
201 1.1 uch {
202 1.1 uch int i;
203 1.1 uch KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
204 1.1 uch
205 1.1 uch for (i = 0; i < cnt; i++) {
206 1.1 uch *rgb24++ = RGB24(r[i], g[i], b[i]);
207 1.1 uch }
208 1.1 uch }
209 1.1 uch
210 1.1 uch void
211 1.1 uch rgb24_decompose(u_int32_t *rgb24, u_int8_t *r, u_int8_t *g, u_int8_t *b,
212 1.2 uch int cnt)
213 1.1 uch {
214 1.1 uch int i;
215 1.1 uch KASSERT(rgb24 && r && g && b && LEGAL_CLUT_INDEX(cnt - 1));
216 1.1 uch
217 1.1 uch for (i = 0; i < cnt; i++) {
218 1.1 uch u_int32_t rgb = *rgb24++;
219 1.1 uch *r++ = (rgb >> 16) & 0xff;
220 1.1 uch *g++ = (rgb >> 8) & 0xff;
221 1.1 uch *b++ = rgb & 0xff;
222 1.1 uch }
223 1.1 uch }
224 1.1 uch
225 1.1 uch /*
226 1.1 uch * Debug routines.
227 1.1 uch */
228 1.1 uch void
229 1.1 uch video_calibration_pattern(struct video_chip *vc)
230 1.1 uch {
231 1.1 uch int x, y;
232 1.1 uch
233 1.1 uch x = vc->vc_fbwidth - 40;
234 1.1 uch y = vc->vc_fbheight - 40;
235 1.1 uch video_line(vc, 40, 40, x , 40);
236 1.1 uch video_line(vc, x , 40, x , y );
237 1.1 uch video_line(vc, x , y , 40, y );
238 1.1 uch video_line(vc, 40, y , 40, 40);
239 1.1 uch video_line(vc, 40, 40, x , y );
240 1.1 uch video_line(vc, x, 40, 40, y );
241 1.1 uch }
242 1.1 uch
243 1.1 uch static void
244 1.1 uch linebpp_unimpl(struct video_chip *vc, int x0, int y0, int x1, int y1)
245 1.1 uch {
246 1.1 uch return;
247 1.1 uch }
248 1.1 uch
249 1.1 uch static void
250 1.1 uch dotbpp_unimpl(struct video_chip *vc, int x, int y)
251 1.1 uch {
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.1 uch
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