/project/jni/sdl-1.3/src/video/directfb/SDL_DirectFB_video.c

https://github.com/aichunyu/FFPlayer · C · 423 lines · 304 code · 81 blank · 38 comment · 27 complexity · 7d787a4d385aa141b261fb27fd87cb65 MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_config.h"
  19. #if SDL_VIDEO_DRIVER_DIRECTFB
  20. #include "SDL_DirectFB_video.h"
  21. #include "SDL_DirectFB_events.h"
  22. /*
  23. * #include "SDL_DirectFB_keyboard.h"
  24. */
  25. #include "SDL_DirectFB_modes.h"
  26. #include "SDL_DirectFB_mouse.h"
  27. #include "SDL_DirectFB_opengl.h"
  28. #include "SDL_DirectFB_window.h"
  29. #include "SDL_DirectFB_WM.h"
  30. #include "SDL_config.h"
  31. /* DirectFB video driver implementation.
  32. */
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <sys/mman.h>
  36. #include <directfb.h>
  37. #include <directfb_version.h>
  38. #include <directfb_strings.h>
  39. #include "SDL_video.h"
  40. #include "SDL_mouse.h"
  41. #include "../SDL_sysvideo.h"
  42. #include "../SDL_pixels_c.h"
  43. #include "../../events/SDL_events_c.h"
  44. #include "SDL_DirectFB_video.h"
  45. #include "SDL_DirectFB_events.h"
  46. #include "SDL_DirectFB_render.h"
  47. #include "SDL_DirectFB_mouse.h"
  48. #include "SDL_DirectFB_shape.h"
  49. #include "SDL_DirectFB_dyn.h"
  50. /* Initialization/Query functions */
  51. static int DirectFB_VideoInit(_THIS);
  52. static void DirectFB_VideoQuit(_THIS);
  53. static int DirectFB_Available(void);
  54. static SDL_VideoDevice *DirectFB_CreateDevice(int devindex);
  55. VideoBootStrap DirectFB_bootstrap = {
  56. "directfb", "DirectFB",
  57. DirectFB_Available, DirectFB_CreateDevice
  58. };
  59. static const DirectFBSurfaceDrawingFlagsNames(drawing_flags);
  60. static const DirectFBSurfaceBlittingFlagsNames(blitting_flags);
  61. static const DirectFBAccelerationMaskNames(acceleration_mask);
  62. /* DirectFB driver bootstrap functions */
  63. static int
  64. DirectFB_Available(void)
  65. {
  66. if (!SDL_DirectFB_LoadLibrary())
  67. return 0;
  68. SDL_DirectFB_UnLoadLibrary();
  69. return 1;
  70. }
  71. static void
  72. DirectFB_DeleteDevice(SDL_VideoDevice * device)
  73. {
  74. SDL_DirectFB_UnLoadLibrary();
  75. SDL_DFB_FREE(device->driverdata);
  76. SDL_DFB_FREE(device);
  77. }
  78. static SDL_VideoDevice *
  79. DirectFB_CreateDevice(int devindex)
  80. {
  81. SDL_VideoDevice *device;
  82. if (!SDL_DirectFB_LoadLibrary())
  83. return NULL;
  84. /* Initialize all variables that we clean on shutdown */
  85. SDL_DFB_ALLOC_CLEAR(device, sizeof(SDL_VideoDevice));
  86. /* Set the function pointers */
  87. /* Set the function pointers */
  88. device->VideoInit = DirectFB_VideoInit;
  89. device->VideoQuit = DirectFB_VideoQuit;
  90. device->GetDisplayModes = DirectFB_GetDisplayModes;
  91. device->SetDisplayMode = DirectFB_SetDisplayMode;
  92. device->PumpEvents = DirectFB_PumpEventsWindow;
  93. device->CreateWindow = DirectFB_CreateWindow;
  94. device->CreateWindowFrom = DirectFB_CreateWindowFrom;
  95. device->SetWindowTitle = DirectFB_SetWindowTitle;
  96. device->SetWindowIcon = DirectFB_SetWindowIcon;
  97. device->SetWindowPosition = DirectFB_SetWindowPosition;
  98. device->SetWindowSize = DirectFB_SetWindowSize;
  99. device->ShowWindow = DirectFB_ShowWindow;
  100. device->HideWindow = DirectFB_HideWindow;
  101. device->RaiseWindow = DirectFB_RaiseWindow;
  102. device->MaximizeWindow = DirectFB_MaximizeWindow;
  103. device->MinimizeWindow = DirectFB_MinimizeWindow;
  104. device->RestoreWindow = DirectFB_RestoreWindow;
  105. device->SetWindowGrab = DirectFB_SetWindowGrab;
  106. device->DestroyWindow = DirectFB_DestroyWindow;
  107. device->GetWindowWMInfo = DirectFB_GetWindowWMInfo;
  108. #if SDL_DIRECTFB_OPENGL
  109. device->GL_LoadLibrary = DirectFB_GL_LoadLibrary;
  110. device->GL_GetProcAddress = DirectFB_GL_GetProcAddress;
  111. device->GL_MakeCurrent = DirectFB_GL_MakeCurrent;
  112. device->GL_CreateContext = DirectFB_GL_CreateContext;
  113. device->GL_SetSwapInterval = DirectFB_GL_SetSwapInterval;
  114. device->GL_GetSwapInterval = DirectFB_GL_GetSwapInterval;
  115. device->GL_SwapWindow = DirectFB_GL_SwapWindow;
  116. device->GL_DeleteContext = DirectFB_GL_DeleteContext;
  117. #endif
  118. /* Shaped window support */
  119. device->shape_driver.CreateShaper = DirectFB_CreateShaper;
  120. device->shape_driver.SetWindowShape = DirectFB_SetWindowShape;
  121. device->shape_driver.ResizeWindowShape = DirectFB_ResizeWindowShape;
  122. device->free = DirectFB_DeleteDevice;
  123. return device;
  124. error:
  125. if (device)
  126. free(device);
  127. return (0);
  128. }
  129. static void
  130. DirectFB_DeviceInformation(IDirectFB * dfb)
  131. {
  132. DFBGraphicsDeviceDescription desc;
  133. int n;
  134. dfb->GetDeviceDescription(dfb, &desc);
  135. SDL_DFB_LOG( "DirectFB Device Information");
  136. SDL_DFB_LOG( "===========================");
  137. SDL_DFB_LOG( "Name: %s", desc.name);
  138. SDL_DFB_LOG( "Vendor: %s", desc.vendor);
  139. SDL_DFB_LOG( "Driver Name: %s", desc.driver.name);
  140. SDL_DFB_LOG( "Driver Vendor: %s", desc.driver.vendor);
  141. SDL_DFB_LOG( "Driver Version: %d.%d", desc.driver.major,
  142. desc.driver.minor);
  143. SDL_DFB_LOG( "Video memoory: %d", desc.video_memory);
  144. SDL_DFB_LOG( "Blitting flags:");
  145. for (n = 0; blitting_flags[n].flag; n++) {
  146. if (desc.blitting_flags & blitting_flags[n].flag)
  147. SDL_DFB_LOG( " %s", blitting_flags[n].name);
  148. }
  149. SDL_DFB_LOG( "Drawing flags:");
  150. for (n = 0; drawing_flags[n].flag; n++) {
  151. if (desc.drawing_flags & drawing_flags[n].flag)
  152. SDL_DFB_LOG( " %s", drawing_flags[n].name);
  153. }
  154. SDL_DFB_LOG( "Acceleration flags:");
  155. for (n = 0; acceleration_mask[n].mask; n++) {
  156. if (desc.acceleration_mask & acceleration_mask[n].mask)
  157. SDL_DFB_LOG( " %s", acceleration_mask[n].name);
  158. }
  159. }
  160. static int readBoolEnv(const char *env_name, int def_val)
  161. {
  162. char *stemp;
  163. stemp = SDL_getenv(env_name);
  164. if (stemp)
  165. return atoi(stemp);
  166. else
  167. return def_val;
  168. }
  169. static int
  170. DirectFB_VideoInit(_THIS)
  171. {
  172. IDirectFB *dfb = NULL;
  173. DFB_DeviceData *devdata = NULL;
  174. DFBResult ret;
  175. SDL_DFB_ALLOC_CLEAR(devdata, sizeof(*devdata));
  176. SDL_DFB_CHECKERR(DirectFBInit(NULL, NULL));
  177. /* avoid switching to the framebuffer when we
  178. * are running X11 */
  179. ret = readBoolEnv(DFBENV_USE_X11_CHECK , 1);
  180. if (ret) {
  181. if (SDL_getenv("DISPLAY"))
  182. DirectFBSetOption("system", "x11");
  183. else
  184. DirectFBSetOption("disable-module", "x11input");
  185. }
  186. /* FIXME: Reenable as default once multi kbd/mouse interface is sorted out */
  187. devdata->use_linux_input = readBoolEnv(DFBENV_USE_LINUX_INPUT, 0); /* default: on */
  188. if (!devdata->use_linux_input)
  189. {
  190. SDL_DFB_LOG("Disabling linxu input\n");
  191. DirectFBSetOption("disable-module", "linux_input");
  192. }
  193. SDL_DFB_CHECKERR(DirectFBCreate(&dfb));
  194. DirectFB_DeviceInformation(dfb);
  195. devdata->use_yuv_underlays = readBoolEnv(DFBENV_USE_YUV_UNDERLAY, 0); /* default: off */
  196. devdata->use_yuv_direct = readBoolEnv(DFBENV_USE_YUV_DIRECT, 0); /* default is off! */
  197. /* Create global Eventbuffer for axis events */
  198. if (devdata->use_linux_input) {
  199. SDL_DFB_CHECKERR(dfb->CreateInputEventBuffer(dfb, DICAPS_ALL,
  200. DFB_TRUE,
  201. &devdata->events));
  202. } else {
  203. SDL_DFB_CHECKERR(dfb->CreateInputEventBuffer(dfb, DICAPS_AXES
  204. /*DICAPS_ALL */ ,
  205. DFB_TRUE,
  206. &devdata->events));
  207. }
  208. /* simple window manager support */
  209. devdata->has_own_wm = readBoolEnv(DFBENV_USE_WM, 0);
  210. devdata->initialized = 1;
  211. devdata->dfb = dfb;
  212. devdata->firstwin = NULL;
  213. devdata->grabbed_window = NULL;
  214. _this->driverdata = devdata;
  215. DirectFB_InitModes(_this);
  216. #if SDL_DIRECTFB_OPENGL
  217. DirectFB_GL_Initialize(_this);
  218. #endif
  219. DirectFB_InitMouse(_this);
  220. DirectFB_InitKeyboard(_this);
  221. return 0;
  222. error:
  223. SDL_DFB_FREE(devdata);
  224. SDL_DFB_RELEASE(dfb);
  225. return -1;
  226. }
  227. static void
  228. DirectFB_VideoQuit(_THIS)
  229. {
  230. DFB_DeviceData *devdata = (DFB_DeviceData *) _this->driverdata;
  231. DirectFB_QuitModes(_this);
  232. DirectFB_QuitKeyboard(_this);
  233. DirectFB_QuitMouse(_this);
  234. devdata->events->Reset(devdata->events);
  235. SDL_DFB_RELEASE(devdata->events);
  236. SDL_DFB_RELEASE(devdata->dfb);
  237. #if SDL_DIRECTFB_OPENGL
  238. DirectFB_GL_Shutdown(_this);
  239. #endif
  240. devdata->initialized = 0;
  241. }
  242. /* DirectFB driver general support functions */
  243. static const struct {
  244. DFBSurfacePixelFormat dfb;
  245. Uint32 sdl;
  246. } pixelformat_tab[] =
  247. {
  248. { DSPF_RGB32, SDL_PIXELFORMAT_RGB888 }, /* 24 bit RGB (4 byte, nothing@24, red 8@16, green 8@8, blue 8@0) */
  249. { DSPF_ARGB, SDL_PIXELFORMAT_ARGB8888 }, /* 32 bit ARGB (4 byte, alpha 8@24, red 8@16, green 8@8, blue 8@0) */
  250. { DSPF_RGB16, SDL_PIXELFORMAT_RGB565 }, /* 16 bit RGB (2 byte, red 5@11, green 6@5, blue 5@0) */
  251. { DSPF_RGB332, SDL_PIXELFORMAT_RGB332 }, /* 8 bit RGB (1 byte, red 3@5, green 3@2, blue 2@0) */
  252. { DSPF_ARGB4444, SDL_PIXELFORMAT_ARGB4444 }, /* 16 bit ARGB (2 byte, alpha 4@12, red 4@8, green 4@4, blue 4@0) */
  253. { DSPF_ARGB1555, SDL_PIXELFORMAT_ARGB1555 }, /* 16 bit ARGB (2 byte, alpha 1@15, red 5@10, green 5@5, blue 5@0) */
  254. { DSPF_RGB24, SDL_PIXELFORMAT_RGB24 }, /* 24 bit RGB (3 byte, red 8@16, green 8@8, blue 8@0) */
  255. { DSPF_RGB444, SDL_PIXELFORMAT_RGB444 }, /* 16 bit RGB (2 byte, nothing @12, red 4@8, green 4@4, blue 4@0) */
  256. { DSPF_YV12, SDL_PIXELFORMAT_YV12 }, /* 12 bit YUV (8 bit Y plane followed by 8 bit quarter size V/U planes) */
  257. { DSPF_I420,SDL_PIXELFORMAT_IYUV }, /* 12 bit YUV (8 bit Y plane followed by 8 bit quarter size U/V planes) */
  258. { DSPF_YUY2, SDL_PIXELFORMAT_YUY2 }, /* 16 bit YUV (4 byte/ 2 pixel, macropixel contains CbYCrY [31:0]) */
  259. { DSPF_UYVY, SDL_PIXELFORMAT_UYVY }, /* 16 bit YUV (4 byte/ 2 pixel, macropixel contains YCbYCr [31:0]) */
  260. { DSPF_RGB555, SDL_PIXELFORMAT_RGB555 }, /* 16 bit RGB (2 byte, nothing @15, red 5@10, green 5@5, blue 5@0) */
  261. #if (ENABLE_LUT8)
  262. { DSPF_LUT8, SDL_PIXELFORMAT_INDEX8 }, /* 8 bit LUT (8 bit color and alpha lookup from palette) */
  263. #endif
  264. #if (DFB_VERSION_ATLEAST(1,2,0))
  265. { DSPF_BGR555, SDL_PIXELFORMAT_BGR555 }, /* 16 bit BGR (2 byte, nothing @15, blue 5@10, green 5@5, red 5@0) */
  266. #else
  267. { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR555 },
  268. #endif
  269. /* Pfff ... nonmatching formats follow */
  270. { DSPF_ALUT44, SDL_PIXELFORMAT_UNKNOWN }, /* 8 bit ALUT (1 byte, alpha 4@4, color lookup 4@0) */
  271. { DSPF_A8, SDL_PIXELFORMAT_UNKNOWN }, /* 8 bit alpha (1 byte, alpha 8@0), e.g. anti-aliased glyphs */
  272. { DSPF_AiRGB, SDL_PIXELFORMAT_UNKNOWN }, /* 32 bit ARGB (4 byte, inv. alpha 8@24, red 8@16, green 8@8, blue 8@0) */
  273. { DSPF_A1, SDL_PIXELFORMAT_UNKNOWN }, /* 1 bit alpha (1 byte/ 8 pixel, most significant bit used first) */
  274. { DSPF_NV12, SDL_PIXELFORMAT_UNKNOWN }, /* 12 bit YUV (8 bit Y plane followed by one 16 bit quarter size CbCr [15:0] plane) */
  275. { DSPF_NV16, SDL_PIXELFORMAT_UNKNOWN }, /* 16 bit YUV (8 bit Y plane followed by one 16 bit half width CbCr [15:0] plane) */
  276. { DSPF_ARGB2554, SDL_PIXELFORMAT_UNKNOWN }, /* 16 bit ARGB (2 byte, alpha 2@14, red 5@9, green 5@4, blue 4@0) */
  277. { DSPF_NV21, SDL_PIXELFORMAT_UNKNOWN }, /* 12 bit YUV (8 bit Y plane followed by one 16 bit quarter size CrCb [15:0] plane) */
  278. { DSPF_AYUV, SDL_PIXELFORMAT_UNKNOWN }, /* 32 bit AYUV (4 byte, alpha 8@24, Y 8@16, Cb 8@8, Cr 8@0) */
  279. { DSPF_A4, SDL_PIXELFORMAT_UNKNOWN }, /* 4 bit alpha (1 byte/ 2 pixel, more significant nibble used first) */
  280. { DSPF_ARGB1666, SDL_PIXELFORMAT_UNKNOWN }, /* 1 bit alpha (3 byte/ alpha 1@18, red 6@16, green 6@6, blue 6@0) */
  281. { DSPF_ARGB6666, SDL_PIXELFORMAT_UNKNOWN }, /* 6 bit alpha (3 byte/ alpha 6@18, red 6@16, green 6@6, blue 6@0) */
  282. { DSPF_RGB18, SDL_PIXELFORMAT_UNKNOWN }, /* 6 bit RGB (3 byte/ red 6@16, green 6@6, blue 6@0) */
  283. { DSPF_LUT2, SDL_PIXELFORMAT_UNKNOWN }, /* 2 bit LUT (1 byte/ 4 pixel, 2 bit color and alpha lookup from palette) */
  284. #if (DFB_VERSION_ATLEAST(1,3,0))
  285. { DSPF_RGBA4444, SDL_PIXELFORMAT_UNKNOWN }, /* 16 bit RGBA (2 byte, red 4@12, green 4@8, blue 4@4, alpha 4@0) */
  286. #endif
  287. #if (DFB_VERSION_ATLEAST(1,4,3))
  288. { DSPF_RGBA5551, SDL_PIXELFORMAT_UNKNOWN }, /* 16 bit RGBA (2 byte, red 5@11, green 5@6, blue 5@1, alpha 1@0) */
  289. { DSPF_YUV444P, SDL_PIXELFORMAT_UNKNOWN }, /* 24 bit full YUV planar (8 bit Y plane followed by an 8 bit Cb and an 8 bit Cr plane) */
  290. { DSPF_ARGB8565, SDL_PIXELFORMAT_UNKNOWN }, /* 24 bit ARGB (3 byte, alpha 8@16, red 5@11, green 6@5, blue 5@0) */
  291. { DSPF_AVYU, SDL_PIXELFORMAT_UNKNOWN }, /* 32 bit AVYU 4:4:4 (4 byte, alpha 8@24, Cr 8@16, Y 8@8, Cb 8@0) */
  292. { DSPF_VYU, SDL_PIXELFORMAT_UNKNOWN }, /* 24 bit VYU 4:4:4 (3 byte, Cr 8@16, Y 8@8, Cb 8@0) */
  293. #endif
  294. { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX1LSB },
  295. { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX1MSB },
  296. { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX4LSB },
  297. { DSPF_UNKNOWN, SDL_PIXELFORMAT_INDEX4MSB },
  298. { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR24 },
  299. { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR888 },
  300. { DSPF_UNKNOWN, SDL_PIXELFORMAT_RGBA8888 },
  301. { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR8888 },
  302. { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGRA8888 },
  303. { DSPF_UNKNOWN, SDL_PIXELFORMAT_ARGB2101010 },
  304. { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR4444 },
  305. { DSPF_UNKNOWN, SDL_PIXELFORMAT_ABGR1555 },
  306. { DSPF_UNKNOWN, SDL_PIXELFORMAT_BGR565 },
  307. { DSPF_UNKNOWN, SDL_PIXELFORMAT_YVYU }, /**< Packed mode: Y0+V0+Y1+U0 (1 pla */
  308. };
  309. Uint32
  310. DirectFB_DFBToSDLPixelFormat(DFBSurfacePixelFormat pixelformat)
  311. {
  312. int i;
  313. for (i=0; pixelformat_tab[i].dfb != DSPF_UNKNOWN; i++)
  314. if (pixelformat_tab[i].dfb == pixelformat)
  315. {
  316. return pixelformat_tab[i].sdl;
  317. }
  318. return SDL_PIXELFORMAT_UNKNOWN;
  319. }
  320. DFBSurfacePixelFormat
  321. DirectFB_SDLToDFBPixelFormat(Uint32 format)
  322. {
  323. int i;
  324. for (i=0; pixelformat_tab[i].dfb != DSPF_UNKNOWN; i++)
  325. if (pixelformat_tab[i].sdl == format)
  326. {
  327. return pixelformat_tab[i].dfb;
  328. }
  329. return DSPF_UNKNOWN;
  330. }
  331. void DirectFB_SetSupportedPixelFormats(SDL_RendererInfo* ri)
  332. {
  333. int i, j;
  334. for (i=0, j=0; pixelformat_tab[i].dfb != DSPF_UNKNOWN; i++)
  335. if (pixelformat_tab[i].sdl != SDL_PIXELFORMAT_UNKNOWN)
  336. ri->texture_formats[j++] = pixelformat_tab[i].sdl;
  337. ri->num_texture_formats = j;
  338. }
  339. #endif /* SDL_VIDEO_DRIVER_DIRECTFB */