grfabs.c revision 1.16 1 /* $NetBSD: grfabs.c,v 1.16 2009/03/18 16:00:10 cegger Exp $ */
2
3 /*
4 * Copyright (c) 1995 Leo Weppelman.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Leo Weppelman.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * atari abstract graphics driver.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: grfabs.c,v 1.16 2009/03/18 16:00:10 cegger Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44
45 #include <machine/cpu.h>
46 #include <machine/iomap.h>
47 #include <machine/video.h>
48 #include <machine/mfp.h>
49 #include <atari/dev/grfabs_reg.h>
50
51 /*
52 * Function decls
53 */
54 static dmode_t *get_best_display_mode(dimen_t *, int, dmode_t *);
55
56 /*
57 * List of available graphic modes
58 */
59 static MODES modes;
60
61 /*
62 * Ugh.. Stuff needed to allocate console structures before the VM-system
63 * is running. There is no malloc() available at that time.
64 * Decision to use these: atari_realconfig == 0
65 */
66 view_t gra_con_view;
67 colormap_t gra_con_cmap;
68 long gra_con_colors[MAX_CENTRIES];
69
70 /*
71 * Default colors.....
72 * Currently the TT-low (256 colors) just uses 16 times the 16-color default.
73 * If you have a sensible 256 scale, feel free to add.....
74 * The first 2 colors in all maps are {black,white}, so ite (text) displays
75 * are initially readable. Also, this enables me to supply only 1 map. The
76 * 4 color mode uses the first four entries of the 16 color mode thus giving
77 * a gray scale display. (Maybe we can add an intensity bit to the ite...)
78 */
79 u_long gra_def_color16[16] = {
80 0x00000000, /* black */
81 0x00ffffff, /* white */
82 0x000c0c0c, /* light gray */
83 0x00808008, /* gray */
84 0x0000000c, /* blue */
85 0x00000c00, /* green */
86 0x00000c0c, /* cyan */
87 0x00c00000, /* red */
88 0x00c0000c, /* magenta */
89 0x00c00c00, /* brown */
90 0x000000ff, /* light blue */
91 0x0000ff00, /* light green */
92 0x0000ffff, /* light cyan */
93 0x00ff0000, /* light red */
94 0x00ff00ff, /* light magenta */
95 0x00ffff00 /* light brown */
96 };
97
98 /*
99 * XXX: called from ite console init routine.
100 * Initialize list of possible video modes.
101 */
102 int
103 grfabs_probe(grf_probe_t probe_fun)
104 {
105 static int inited = 0;
106
107 if (!inited) {
108 LIST_INIT(&modes);
109 inited = 1;
110 }
111 (*probe_fun)(&modes);
112
113 return ((modes.lh_first == NULL) ? 0 : 1);
114 }
115
116 view_t *
117 grf_alloc_view(dmode_t *d, dimen_t *dim, u_char depth)
118 {
119 if (!d)
120 d = get_best_display_mode(dim, depth, NULL);
121 if (d)
122 return ((d->grfabs_funcs->alloc_view)(d, dim, depth));
123 return(NULL);
124 }
125
126 dmode_t *
127 grf_get_best_mode(dimen_t *dim, u_char depth)
128 {
129 return (get_best_display_mode(dim, depth, NULL));
130 }
131
132 void grf_display_view(v)
133 view_t *v;
134 {
135 (v->mode->grfabs_funcs->display_view)(v);
136 }
137
138 void grf_remove_view(v)
139 view_t *v;
140 {
141 (v->mode->grfabs_funcs->remove_view)(v);
142 }
143
144 void grf_save_view(v)
145 view_t *v;
146 {
147 (v->mode->grfabs_funcs->save_view)(v);
148 }
149
150 void grf_free_view(v)
151 view_t *v;
152 {
153 (v->mode->grfabs_funcs->free_view)(v);
154 }
155
156 int
157 grf_get_colormap(view_t *v, colormap_t *cm)
158 {
159 colormap_t *gcm;
160 int i, n;
161 u_long *sv_entry;
162
163 gcm = v->colormap;
164 n = cm->size < gcm->size ? cm->size : gcm->size;
165
166 /*
167 * Copy struct from view but be carefull to keep 'entry'
168 */
169 sv_entry = cm->entry;
170 *cm = *gcm;
171 cm->entry = sv_entry;
172
173 /*
174 * Copy the colors
175 */
176 memset(cm->entry, 0, cm->size * sizeof(long));
177 for (i = 0; i < n; i++)
178 cm->entry[i] = gcm->entry[i];
179 return (0);
180 }
181
182 int
183 grf_use_colormap(view_t *v, colormap_t *cm)
184 {
185 return (v->mode->grfabs_funcs->use_colormap)(v, cm);
186 }
187
188 static dmode_t *
189 get_best_display_mode(dimen_t *dim, int depth, dmode_t *curr_mode)
190 {
191 dmode_t *save;
192 dmode_t *dm;
193 long dx, dy, dd, ct;
194 long size_diff, depth_diff;
195
196 save = NULL;
197 size_diff = 0;
198 depth_diff = 0;
199 dm = modes.lh_first;
200 while (dm != NULL) {
201 dx = abs(dm->size.width - dim->width);
202 dy = abs(dm->size.height - dim->height);
203 dd = abs(dm->depth - depth);
204 ct = dx + dy;
205
206 if ((save != NULL) && (size_diff == 0)) {
207 if (dd > depth_diff) {
208 dm = dm->link.le_next;
209 continue;
210 }
211 }
212 if ((save == NULL) || (ct <= size_diff)) {
213 save = dm;
214 size_diff = ct;
215 depth_diff = dd;
216 }
217 dm = dm->link.le_next;
218 }
219 /*
220 * Did we do better than the current mode?
221 */
222 if ((save != NULL) && (curr_mode != NULL)) {
223 dx = abs(curr_mode->size.width - dim->width);
224 dy = abs(curr_mode->size.height - dim->height);
225 dd = abs(curr_mode->depth - depth);
226 ct = dx + dy;
227 if ((ct <= size_diff) && (dd <= depth_diff))
228 return (NULL);
229 }
230 return (save);
231 }
232