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