gui.c 7.0 KB

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