1 /* Blackfin GUI (SDL) helper code
3 Copyright (C) 2010-2022 Free Software Foundation, Inc.
4 Contributed by Analog Devices, Inc.
6 This file is part of simulators.
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.
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.
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/>. */
21 /* This must come before any other includes. */
31 #include "libiberty.h"
38 int (*Init) (Uint32 flags);
40 int (*ShowCursor) (int toggle);
41 int (*LockSurface) (SDL_Surface *surface);
42 void (*UnlockSurface) (SDL_Surface *surface);
44 void (*GetRGB) (Uint32 pixel, const SDL_PixelFormat * const fmt, Uint8 *r, Uint8 *g, Uint8 *b);
45 Uint32 (*MapRGB) (const SDL_PixelFormat * const format, const Uint8 r, const Uint8 g, const Uint8 b);
46 SDL_Surface *(*SetVideoMode) (int width, int height, int bpp, Uint32 flags);
47 void (*WM_SetCaption) (const char *title, const char *icon);
48 void (*UpdateRect) (SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h);
50 void (*GetRGB) (Uint32 pixel, const SDL_PixelFormat *fmt, Uint8 *r, Uint8 *g, Uint8 *b);
51 Uint32 (*MapRGB) (const SDL_PixelFormat *format, Uint8 r, Uint8 g, Uint8 b);
52 SDL_Window *(*CreateWindow) (const char *title, int x, int y, int w, int h, Uint32 flags);
53 SDL_Surface *(*GetWindowSurface) (SDL_Window *window);
54 SDL_PixelFormat *(*AllocFormat) (Uint32 pixel_format);
55 int (*UpdateWindowSurfaceRects) (SDL_Window *window, const SDL_Rect *rects, int numrects);
59 static const char * const sdl_syms[] =
74 "SDL_GetWindowSurface",
76 "SDL_UpdateWindowSurfaceRects",
85 const SDL_PixelFormat *format;
86 int throttle, throttle_limit;
92 static const char bfin_gui_window_title[] = "Blackfin GNU Simulator";
94 /* Load the SDL lib on the fly to avoid hard linking against it. */
96 bfin_gui_sdl_setup (void)
98 static const char libsdl_soname[] =
110 sdl.handle = dlopen (libsdl_soname, RTLD_LAZY);
111 if (sdl.handle == NULL)
114 funcs = (void *) &sdl.Init;
115 for (i = 0; i < ARRAY_SIZE (sdl_syms); ++i)
117 funcs[i] = dlsym (sdl.handle, sdl_syms[i]);
118 if (funcs[i] == NULL)
120 dlclose (sdl.handle);
129 static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color,
130 int *bytes_per_pixel);
133 bfin_gui_setup (void *state, int enabled, int width, int height,
134 enum gui_color color)
136 if (bfin_gui_sdl_setup ())
139 /* Create an SDL window if enabled and we don't have one yet. */
140 if (enabled && !state)
142 struct gui_state *gui = xmalloc (sizeof (*gui));
146 if (sdl.Init (SDL_INIT_VIDEO))
150 gui->format = bfin_gui_color_format (gui->color, &gui->bytes_per_pixel);
152 sdl.WM_SetCaption (bfin_gui_window_title, NULL);
153 gui->screen = sdl.SetVideoMode (width, height, 32,
154 SDL_ANYFORMAT|SDL_HWSURFACE);
156 gui->window = sdl.CreateWindow (
157 bfin_gui_window_title, SDL_WINDOWPOS_CENTERED,
158 SDL_WINDOWPOS_CENTERED, width, height, 0);
165 gui->screen = sdl.GetWindowSurface(gui->window);
176 gui->throttle_limit = 0xf; /* XXX: let people control this ? */
184 /* Else break down a window if disabled and we had one. */
185 else if (!enabled && state)
192 /* Retain existing state, whatever that may be. */
197 SDL_ConvertBlitLineFrom (const Uint8 *src, const SDL_PixelFormat * const format,
201 SDL_Surface *dst, int dsty, int bytes_per_pixel)
207 if (SDL_MUSTLOCK (dst))
208 if (sdl.LockSurface (dst))
211 pixels = dst->pixels;
212 pixels += (dsty * dst->pitch / 4);
214 for (i = 0; i < dst->w; ++i)
216 /* Exract the packed source pixel; RGB or BGR. */
218 for (j = 0; j < bytes_per_pixel; ++j)
220 pix = (pix << 8) | src[j];
222 pix = pix | ((Uint32)src[j] << (j * 8));
224 /* Unpack the source pixel into its components. */
225 sdl.GetRGB (pix, format, &r, &g, &b);
226 /* Translate into the screen pixel format. */
227 *pixels++ = sdl.MapRGB (dst->format, r, g, b);
229 src += bytes_per_pixel;
232 if (SDL_MUSTLOCK (dst))
233 sdl.UnlockSurface (dst);
236 sdl.UpdateRect (dst, 0, dsty, dst->w, 1);
246 sdl.UpdateWindowSurfaceRects (win, &rect, 1);
254 bfin_gui_update (void *state, const void *source, unsigned nr_bytes)
256 struct gui_state *gui = state;
262 /* XXX: Make this an option ? */
263 gui->throttle = (gui->throttle + 1) & gui->throttle_limit;
267 ret = SDL_ConvertBlitLineFrom (source, gui->format,
271 gui->screen, gui->curr_line,
272 gui->bytes_per_pixel);
276 gui->curr_line = (gui->curr_line + 1) % gui->screen->h;
283 #define FMASK(cnt, shift) (((1 << (cnt)) - 1) << (shift))
284 #define _FORMAT(bpp, rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \
285 NULL, bpp, (bpp)/8, 8-(rcnt), 8-(gcnt), 8-(bcnt), 8-(acnt), rsh, gsh, bsh, ash, \
286 FMASK (rcnt, rsh), FMASK (gcnt, gsh), FMASK (bcnt, bsh), FMASK (acnt, ash),
287 #define FORMAT(rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash) \
288 _FORMAT(((((rcnt) + (gcnt) + (bcnt) + (acnt)) + 7) / 8) * 8, \
289 rcnt, gcnt, bcnt, acnt, rsh, gsh, bsh, ash)
291 static const SDL_PixelFormat sdl_rgb_565 =
293 FORMAT (5, 6, 5, 0, 11, 5, 0, 0)
295 #define SDL_PIXELFORMAT_RGB565 &sdl_rgb_565
297 static const SDL_PixelFormat sdl_bgr_565 =
299 FORMAT (5, 6, 5, 0, 0, 5, 11, 0)
301 #define SDL_PIXELFORMAT_BGR565 &sdl_bgr_565
303 static const SDL_PixelFormat sdl_rgb_888 =
305 FORMAT (8, 8, 8, 0, 16, 8, 0, 0)
307 #define SDL_PIXELFORMAT_RGB888 &sdl_rgb_888
309 static const SDL_PixelFormat sdl_bgr_888 =
311 FORMAT (8, 8, 8, 0, 0, 8, 16, 0)
313 #define SDL_PIXELFORMAT_BGR888 &sdl_bgr_888
315 static const SDL_PixelFormat sdl_rgba_8888 =
317 FORMAT (8, 8, 8, 8, 24, 16, 8, 0)
319 #define SDL_PIXELFORMAT_RGBA8888 &sdl_rgba_8888
323 static const struct {
325 /* Since we declare the pixel formats above for SDL 1, we have the bpp
326 setting, but SDL 2 internally uses larger values for its own memory, so
327 we can't assume the Blackfin pixel format sizes always match SDL. */
330 const SDL_PixelFormat *format;
334 enum gui_color color;
336 { "rgb565", 2, SDL_PIXELFORMAT_RGB565, GUI_COLOR_RGB_565, },
337 { "bgr565", 2, SDL_PIXELFORMAT_BGR565, GUI_COLOR_BGR_565, },
338 { "rgb888", 3, SDL_PIXELFORMAT_RGB888, GUI_COLOR_RGB_888, },
339 { "bgr888", 3, SDL_PIXELFORMAT_BGR888, GUI_COLOR_BGR_888, },
340 { "rgba8888", 4, SDL_PIXELFORMAT_RGBA8888, GUI_COLOR_RGBA_8888, },
343 enum gui_color bfin_gui_color (const char *color)
350 for (i = 0; i < ARRAY_SIZE (color_spaces); ++i)
351 if (!strcmp (color, color_spaces[i].name))
352 return color_spaces[i].color;
354 /* Pick a random default. */
356 return GUI_COLOR_RGB_888;
359 static const SDL_PixelFormat *bfin_gui_color_format (enum gui_color color,
360 int *bytes_per_pixel)
364 for (i = 0; i < ARRAY_SIZE (color_spaces); ++i)
365 if (color == color_spaces[i].color)
367 *bytes_per_pixel = color_spaces[i].bytes_per_pixel;
369 return color_spaces[i].format;
371 return sdl.AllocFormat (color_spaces[i].format);
378 int bfin_gui_color_depth (enum gui_color color)
381 const SDL_PixelFormat *format = bfin_gui_color_format (color,
383 return format ? bytes_per_pixel * 8 : 0;