Commit | Line | Data |
---|---|---|
ef016f83 MF |
1 | /* Blackfin GUI (SDL) helper code |
2 | ||
88b9d363 | 3 | Copyright (C) 2010-2022 Free Software Foundation, Inc. |
ef016f83 MF |
4 | Contributed by Analog Devices, Inc. |
5 | ||
6 | This file is part of simulators. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
20 | ||
6df01ab8 MF |
21 | /* This must come before any other includes. */ |
22 | #include "defs.h" | |
ef016f83 MF |
23 | |
24 | #ifdef HAVE_SDL | |
25 | # include <SDL.h> | |
26 | #endif | |
27 | #ifdef HAVE_DLFCN_H | |
28 | # include <dlfcn.h> | |
29 | #endif | |
30 | ||
31 | #include "libiberty.h" | |
32 | #include "gui.h" | |
33 | ||
34 | #ifdef HAVE_SDL | |
35 | ||
36 | static struct { | |
37 | void *handle; | |
38 | int (*Init) (Uint32 flags); | |
39 | void (*Quit) (void); | |
40 | SDL_Surface *(*SetVideoMode) (int width, int height, int bpp, Uint32 flags); | |
41 | void (*WM_SetCaption) (const char *title, const char *icon); | |
42 | int (*ShowCursor) (int toggle); | |
43 | int (*LockSurface) (SDL_Surface *surface); | |
44 | void (*UnlockSurface) (SDL_Surface *surface); | |
45 | void (*GetRGB) (Uint32 pixel, const SDL_PixelFormat * const fmt, Uint8 *r, Uint8 *g, Uint8 *b); | |
46 | Uint32 (*MapRGB) (const SDL_PixelFormat * const format, const Uint8 r, const Uint8 g, const Uint8 b); | |
47 | void (*UpdateRect) (SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h); | |
48 | } sdl; | |
49 | ||
990d19fd MF |
50 | static const char * const sdl_syms[] = |
51 | { | |
ef016f83 MF |
52 | "SDL_Init", |
53 | "SDL_Quit", | |
54 | "SDL_SetVideoMode", | |
55 | "SDL_WM_SetCaption", | |
56 | "SDL_ShowCursor", | |
57 | "SDL_LockSurface", | |
58 | "SDL_UnlockSurface", | |
59 | "SDL_GetRGB", | |
60 | "SDL_MapRGB", | |
61 | "SDL_UpdateRect", | |
62 | }; | |
63 | ||
64 | struct gui_state { | |
65 | SDL_Surface *screen; | |
66 | const SDL_PixelFormat *format; | |
67 | int throttle, throttle_limit; | |
68 | enum gui_color color; | |
69 | int curr_line; | |
70 | }; | |
71 | ||
72 | /* Load the SDL lib on the fly to avoid hard linking against it. */ | |
73 | static int | |
74 | bfin_gui_sdl_setup (void) | |
75 | { | |
76 | int i; | |
77 | uintptr_t **funcs; | |
78 | ||
79 | if (sdl.handle) | |
80 | return 0; | |
81 | ||
82 | sdl.handle = dlopen ("libSDL-1.2.so.0", RTLD_LAZY); | |
83 | if (sdl.handle == NULL) | |
84 | return -1; | |
85 | ||
86 | funcs = (void *) &sdl.Init; | |
87 | for (i = 0; i < ARRAY_SIZE (sdl_syms); ++i) | |
88 | { | |
89 | funcs[i] = dlsym (sdl.handle, sdl_syms[i]); | |
90 | if (funcs[i] == NULL) | |
91 | { | |
92 | dlclose (sdl.handle); | |
93 | sdl.handle = NULL; | |
94 | return -1; | |
95 | } | |
96 | } | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
101 | static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color); | |
102 | ||
103 | void * | |
104 | bfin_gui_setup (void *state, int enabled, int width, int height, | |
105 | enum gui_color color) | |
106 | { | |
107 | if (bfin_gui_sdl_setup ()) | |
108 | return NULL; | |
109 | ||
110 | /* Create an SDL window if enabled and we don't have one yet. */ | |
111 | if (enabled && !state) | |
112 | { | |
113 | struct gui_state *gui = xmalloc (sizeof (*gui)); | |
114 | if (!gui) | |
115 | return NULL; | |
116 | ||
117 | if (sdl.Init (SDL_INIT_VIDEO)) | |
118 | goto error; | |
119 | ||
120 | gui->color = color; | |
121 | gui->format = bfin_gui_color_format (gui->color); | |
122 | gui->screen = sdl.SetVideoMode (width, height, 32, | |
123 | SDL_ANYFORMAT|SDL_HWSURFACE); | |
124 | if (!gui->screen) | |
125 | { | |
126 | sdl.Quit(); | |
127 | goto error; | |
128 | } | |
129 | ||
130 | sdl.WM_SetCaption ("GDB Blackfin Simulator", NULL); | |
131 | sdl.ShowCursor (0); | |
132 | gui->curr_line = 0; | |
133 | gui->throttle = 0; | |
134 | gui->throttle_limit = 0xf; /* XXX: let people control this ? */ | |
135 | return gui; | |
136 | ||
137 | error: | |
138 | free (gui); | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | /* Else break down a window if disabled and we had one. */ | |
143 | else if (!enabled && state) | |
144 | { | |
145 | sdl.Quit(); | |
146 | free (state); | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | /* Retain existing state, whatever that may be. */ | |
151 | return state; | |
152 | } | |
153 | ||
154 | static int | |
155 | SDL_ConvertBlitLineFrom (const Uint8 *src, const SDL_PixelFormat * const format, | |
156 | SDL_Surface *dst, int dsty) | |
157 | { | |
158 | Uint8 r, g, b; | |
159 | Uint32 *pixels; | |
160 | unsigned i, j; | |
161 | ||
162 | if (SDL_MUSTLOCK (dst)) | |
163 | if (sdl.LockSurface (dst)) | |
164 | return 1; | |
165 | ||
166 | pixels = dst->pixels; | |
167 | pixels += (dsty * dst->pitch / 4); | |
168 | ||
169 | for (i = 0; i < dst->w; ++i) | |
170 | { | |
171 | /* Exract the packed source pixel; RGB or BGR. */ | |
172 | Uint32 pix = 0; | |
173 | for (j = 0; j < format->BytesPerPixel; ++j) | |
174 | if (format->Rshift) | |
175 | pix = (pix << 8) | src[j]; | |
176 | else | |
177 | pix = pix | ((Uint32)src[j] << (j * 8)); | |
178 | ||
179 | /* Unpack the source pixel into its components. */ | |
180 | sdl.GetRGB (pix, format, &r, &g, &b); | |
181 | /* Translate into the screen pixel format. */ | |
182 | *pixels++ = sdl.MapRGB (dst->format, r, g, b); | |
183 | ||
184 | src += format->BytesPerPixel; | |
185 | } | |
186 | ||
187 | if (SDL_MUSTLOCK (dst)) | |
188 | sdl.UnlockSurface (dst); | |
189 | ||
190 | sdl.UpdateRect (dst, 0, dsty, dst->w, 1); | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | unsigned | |
196 | bfin_gui_update (void *state, const void *source, unsigned nr_bytes) | |
197 | { | |
198 | struct gui_state *gui = state; | |
199 | int ret; | |
200 | ||
201 | if (!gui) | |
202 | return 0; | |
203 | ||
204 | /* XXX: Make this an option ? */ | |
205 | gui->throttle = (gui->throttle + 1) & gui->throttle_limit; | |
206 | if (gui->throttle) | |
207 | return 0; | |
208 | ||
209 | ret = SDL_ConvertBlitLineFrom (source, gui->format, gui->screen, | |
210 | gui->curr_line); | |
211 | if (ret) | |
212 | return 0; | |
213 | ||
214 | gui->curr_line = (gui->curr_line + 1) % gui->screen->h; | |
215 | ||
216 | return nr_bytes; | |
217 | } | |
218 | ||
219 | #define FMASK(cnt, shift) (((1 << (cnt)) - 1) << (shift)) | |
220 | #define _FORMAT(bpp, rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \ | |
221 | NULL, bpp, (bpp)/8, 8-(rcnt), 8-(gcnt), 8-(bcnt), 8-(acnt), rsh, gsh, bsh, ash, \ | |
222 | FMASK (rcnt, rsh), FMASK (gcnt, gsh), FMASK (bcnt, bsh), FMASK (acnt, ash), | |
223 | #define FORMAT(rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \ | |
224 | _FORMAT(((((rcnt) + (gcnt) + (bcnt) + (acnt)) + 7) / 8) * 8, \ | |
225 | rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) | |
226 | ||
990d19fd MF |
227 | static const SDL_PixelFormat sdl_rgb_565 = |
228 | { | |
ef016f83 MF |
229 | FORMAT (5, 6, 5, 0, 11, 5, 0, 0) |
230 | }; | |
990d19fd MF |
231 | static const SDL_PixelFormat sdl_bgr_565 = |
232 | { | |
ef016f83 MF |
233 | FORMAT (5, 6, 5, 0, 0, 5, 11, 0) |
234 | }; | |
990d19fd MF |
235 | static const SDL_PixelFormat sdl_rgb_888 = |
236 | { | |
ef016f83 MF |
237 | FORMAT (8, 8, 8, 0, 16, 8, 0, 0) |
238 | }; | |
990d19fd MF |
239 | static const SDL_PixelFormat sdl_bgr_888 = |
240 | { | |
ef016f83 MF |
241 | FORMAT (8, 8, 8, 0, 0, 8, 16, 0) |
242 | }; | |
990d19fd MF |
243 | static const SDL_PixelFormat sdl_rgba_8888 = |
244 | { | |
ef016f83 MF |
245 | FORMAT (8, 8, 8, 8, 24, 16, 8, 0) |
246 | }; | |
247 | ||
248 | static const struct { | |
249 | const char *name; | |
250 | const SDL_PixelFormat *format; | |
251 | enum gui_color color; | |
252 | } color_spaces[] = { | |
253 | { "rgb565", &sdl_rgb_565, GUI_COLOR_RGB_565, }, | |
254 | { "bgr565", &sdl_bgr_565, GUI_COLOR_BGR_565, }, | |
255 | { "rgb888", &sdl_rgb_888, GUI_COLOR_RGB_888, }, | |
256 | { "bgr888", &sdl_bgr_888, GUI_COLOR_BGR_888, }, | |
257 | { "rgba8888", &sdl_rgba_8888, GUI_COLOR_RGBA_8888, }, | |
258 | }; | |
259 | ||
260 | enum gui_color bfin_gui_color (const char *color) | |
261 | { | |
262 | int i; | |
263 | ||
264 | if (!color) | |
265 | goto def; | |
266 | ||
267 | for (i = 0; i < ARRAY_SIZE (color_spaces); ++i) | |
268 | if (!strcmp (color, color_spaces[i].name)) | |
269 | return color_spaces[i].color; | |
270 | ||
271 | /* Pick a random default. */ | |
272 | def: | |
273 | return GUI_COLOR_RGB_888; | |
274 | } | |
275 | ||
276 | static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color) | |
277 | { | |
278 | int i; | |
279 | ||
280 | for (i = 0; i < ARRAY_SIZE (color_spaces); ++i) | |
281 | if (color == color_spaces[i].color) | |
282 | return color_spaces[i].format; | |
283 | ||
284 | return NULL; | |
285 | } | |
286 | ||
287 | int bfin_gui_color_depth (enum gui_color color) | |
288 | { | |
289 | const SDL_PixelFormat *format = bfin_gui_color_format (color); | |
290 | return format ? format->BitsPerPixel : 0; | |
291 | } | |
292 | ||
293 | #endif |