SDL_DirectFB_modes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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_internal.h"
  19. #if SDL_VIDEO_DRIVER_DIRECTFB
  20. #include "SDL_DirectFB_video.h"
  21. #include "SDL_DirectFB_modes.h"
  22. #define DFB_MAX_MODES 200
  23. struct screen_callback_t
  24. {
  25. int numscreens;
  26. DFBScreenID screenid[DFB_MAX_SCREENS];
  27. DFBDisplayLayerID gralayer[DFB_MAX_SCREENS];
  28. DFBDisplayLayerID vidlayer[DFB_MAX_SCREENS];
  29. int aux; /* auxiliary integer for callbacks */
  30. };
  31. struct modes_callback_t
  32. {
  33. int nummodes;
  34. SDL_DisplayMode *modelist;
  35. };
  36. static DFBEnumerationResult EnumModesCallback(int width, int height, int bpp, void *data)
  37. {
  38. struct modes_callback_t *modedata = (struct modes_callback_t *) data;
  39. SDL_DisplayMode mode;
  40. mode.w = width;
  41. mode.h = height;
  42. mode.refresh_rate = 0;
  43. mode.driverdata = NULL;
  44. mode.format = SDL_PIXELFORMAT_UNKNOWN;
  45. if (modedata->nummodes < DFB_MAX_MODES) {
  46. modedata->modelist[modedata->nummodes++] = mode;
  47. }
  48. return DFENUM_OK;
  49. }
  50. static DFBEnumerationResult EnumScreensCallback(DFBScreenID screen_id, DFBScreenDescription desc, void *callbackdata)
  51. {
  52. struct screen_callback_t *devdata = (struct screen_callback_t *) callbackdata;
  53. devdata->screenid[devdata->numscreens++] = screen_id;
  54. return DFENUM_OK;
  55. }
  56. static DFBEnumerationResult EnumLayersCallback(DFBDisplayLayerID layer_id, DFBDisplayLayerDescription desc, void *callbackdata)
  57. {
  58. struct screen_callback_t *devdata = (struct screen_callback_t *) callbackdata;
  59. if (desc.caps & DLCAPS_SURFACE) {
  60. if ((desc.type & DLTF_GRAPHICS) && (desc.type & DLTF_VIDEO)) {
  61. if (devdata->vidlayer[devdata->aux] == -1)
  62. devdata->vidlayer[devdata->aux] = layer_id;
  63. } else if (desc.type & DLTF_GRAPHICS) {
  64. if (devdata->gralayer[devdata->aux] == -1)
  65. devdata->gralayer[devdata->aux] = layer_id;
  66. }
  67. }
  68. return DFENUM_OK;
  69. }
  70. static void CheckSetDisplayMode(_THIS, SDL_VideoDisplay * display, DFB_DisplayData * data, SDL_DisplayMode * mode)
  71. {
  72. SDL_DFB_DEVICEDATA(_this);
  73. DFBDisplayLayerConfig config;
  74. DFBDisplayLayerConfigFlags failed;
  75. SDL_DFB_CHECKERR(data->layer->SetCooperativeLevel(data->layer,
  76. DLSCL_ADMINISTRATIVE));
  77. config.width = mode->w;
  78. config.height = mode->h;
  79. config.pixelformat = DirectFB_SDLToDFBPixelFormat(mode->format);
  80. config.flags = DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT;
  81. if (devdata->use_yuv_underlays) {
  82. config.flags |= DLCONF_OPTIONS;
  83. config.options = DLOP_ALPHACHANNEL;
  84. }
  85. failed = 0;
  86. data->layer->TestConfiguration(data->layer, &config, &failed);
  87. SDL_DFB_CHECKERR(data->layer->SetCooperativeLevel(data->layer,
  88. DLSCL_SHARED));
  89. if (failed == 0)
  90. {
  91. SDL_AddDisplayMode(display, mode);
  92. SDL_DFB_LOG("Mode %d x %d Added\n", mode->w, mode->h);
  93. }
  94. else
  95. SDL_DFB_ERR("Mode %d x %d not available: %x\n", mode->w,
  96. mode->h, failed);
  97. return;
  98. error:
  99. return;
  100. }
  101. void DirectFB_SetContext(_THIS, SDL_Window *window)
  102. {
  103. #if (DFB_VERSION_ATLEAST(1,0,0))
  104. /* FIXME: does not work on 1.0/1.2 with radeon driver
  105. * the approach did work with the matrox driver
  106. * This has simply no effect.
  107. */
  108. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  109. DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
  110. /* FIXME: should we handle the error */
  111. if (dispdata->vidIDinuse)
  112. SDL_DFB_CHECK(dispdata->vidlayer->SwitchContext(dispdata->vidlayer,
  113. DFB_TRUE));
  114. #endif
  115. }
  116. void DirectFB_InitModes(_THIS)
  117. {
  118. SDL_DFB_DEVICEDATA(_this);
  119. IDirectFBDisplayLayer *layer = NULL;
  120. SDL_VideoDisplay display;
  121. DFB_DisplayData *dispdata = NULL;
  122. SDL_DisplayMode mode;
  123. DFBGraphicsDeviceDescription caps;
  124. DFBDisplayLayerConfig dlc;
  125. struct screen_callback_t *screencbdata;
  126. int tcw[DFB_MAX_SCREENS];
  127. int tch[DFB_MAX_SCREENS];
  128. int i;
  129. DFBResult ret;
  130. SDL_DFB_ALLOC_CLEAR(screencbdata, sizeof(*screencbdata));
  131. screencbdata->numscreens = 0;
  132. for (i = 0; i < DFB_MAX_SCREENS; i++) {
  133. screencbdata->gralayer[i] = -1;
  134. screencbdata->vidlayer[i] = -1;
  135. }
  136. SDL_DFB_CHECKERR(devdata->dfb->EnumScreens(devdata->dfb, &EnumScreensCallback,
  137. screencbdata));
  138. for (i = 0; i < screencbdata->numscreens; i++) {
  139. IDirectFBScreen *screen;
  140. SDL_DFB_CHECKERR(devdata->dfb->GetScreen(devdata->dfb,
  141. screencbdata->screenid
  142. [i], &screen));
  143. screencbdata->aux = i;
  144. SDL_DFB_CHECKERR(screen->EnumDisplayLayers(screen, &EnumLayersCallback,
  145. screencbdata));
  146. screen->GetSize(screen, &tcw[i], &tch[i]);
  147. screen->Release(screen);
  148. }
  149. /* Query card capabilities */
  150. devdata->dfb->GetDeviceDescription(devdata->dfb, &caps);
  151. for (i = 0; i < screencbdata->numscreens; i++) {
  152. SDL_DFB_CHECKERR(devdata->dfb->GetDisplayLayer(devdata->dfb,
  153. screencbdata->gralayer
  154. [i], &layer));
  155. SDL_DFB_CHECKERR(layer->SetCooperativeLevel(layer,
  156. DLSCL_ADMINISTRATIVE));
  157. layer->EnableCursor(layer, 1);
  158. SDL_DFB_CHECKERR(layer->SetCursorOpacity(layer, 0xC0));
  159. if (devdata->use_yuv_underlays) {
  160. dlc.flags = DLCONF_PIXELFORMAT | DLCONF_OPTIONS;
  161. dlc.pixelformat = DSPF_ARGB;
  162. dlc.options = DLOP_ALPHACHANNEL;
  163. ret = layer->SetConfiguration(layer, &dlc);
  164. if (ret != DFB_OK) {
  165. /* try AiRGB if the previous failed */
  166. dlc.pixelformat = DSPF_AiRGB;
  167. SDL_DFB_CHECKERR(layer->SetConfiguration(layer, &dlc));
  168. }
  169. }
  170. /* Query layer configuration to determine the current mode and pixelformat */
  171. dlc.flags = DLCONF_ALL;
  172. SDL_DFB_CHECKERR(layer->GetConfiguration(layer, &dlc));
  173. mode.format = DirectFB_DFBToSDLPixelFormat(dlc.pixelformat);
  174. if (mode.format == SDL_PIXELFORMAT_UNKNOWN) {
  175. SDL_DFB_ERR("Unknown dfb pixelformat %x !\n", dlc.pixelformat);
  176. goto error;
  177. }
  178. mode.w = dlc.width;
  179. mode.h = dlc.height;
  180. mode.refresh_rate = 0;
  181. mode.driverdata = NULL;
  182. SDL_DFB_ALLOC_CLEAR(dispdata, sizeof(*dispdata));
  183. dispdata->layer = layer;
  184. dispdata->pixelformat = dlc.pixelformat;
  185. dispdata->cw = tcw[i];
  186. dispdata->ch = tch[i];
  187. /* YUV - Video layer */
  188. dispdata->vidID = screencbdata->vidlayer[i];
  189. dispdata->vidIDinuse = 0;
  190. SDL_zero(display);
  191. display.desktop_mode = mode;
  192. display.current_mode = mode;
  193. display.driverdata = dispdata;
  194. #if (DFB_VERSION_ATLEAST(1,2,0))
  195. dlc.flags =
  196. DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT |
  197. DLCONF_OPTIONS;
  198. ret = layer->SetConfiguration(layer, &dlc);
  199. #endif
  200. SDL_DFB_CHECKERR(layer->SetCooperativeLevel(layer, DLSCL_SHARED));
  201. SDL_AddVideoDisplay(&display, SDL_FALSE);
  202. }
  203. SDL_DFB_FREE(screencbdata);
  204. return;
  205. error:
  206. /* FIXME: Cleanup not complete, Free existing displays */
  207. SDL_DFB_FREE(dispdata);
  208. SDL_DFB_RELEASE(layer);
  209. return;
  210. }
  211. void DirectFB_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
  212. {
  213. SDL_DFB_DEVICEDATA(_this);
  214. DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
  215. SDL_DisplayMode mode;
  216. struct modes_callback_t data;
  217. int i;
  218. data.nummodes = 0;
  219. /* Enumerate the available fullscreen modes */
  220. SDL_DFB_CALLOC(data.modelist, DFB_MAX_MODES, sizeof(SDL_DisplayMode));
  221. SDL_DFB_CHECKERR(devdata->dfb->EnumVideoModes(devdata->dfb,
  222. EnumModesCallback, &data));
  223. for (i = 0; i < data.nummodes; ++i) {
  224. mode = data.modelist[i];
  225. mode.format = SDL_PIXELFORMAT_ARGB8888;
  226. CheckSetDisplayMode(_this, display, dispdata, &mode);
  227. mode.format = SDL_PIXELFORMAT_RGB888;
  228. CheckSetDisplayMode(_this, display, dispdata, &mode);
  229. mode.format = SDL_PIXELFORMAT_RGB24;
  230. CheckSetDisplayMode(_this, display, dispdata, &mode);
  231. mode.format = SDL_PIXELFORMAT_RGB565;
  232. CheckSetDisplayMode(_this, display, dispdata, &mode);
  233. mode.format = SDL_PIXELFORMAT_INDEX8;
  234. CheckSetDisplayMode(_this, display, dispdata, &mode);
  235. }
  236. SDL_DFB_FREE(data.modelist);
  237. error:
  238. return;
  239. }
  240. int DirectFB_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
  241. {
  242. /*
  243. * FIXME: video mode switch is currently broken for 1.2.0
  244. *
  245. */
  246. SDL_DFB_DEVICEDATA(_this);
  247. DFB_DisplayData *data = (DFB_DisplayData *) display->driverdata;
  248. DFBDisplayLayerConfig config, rconfig;
  249. DFBDisplayLayerConfigFlags fail = 0;
  250. SDL_DFB_CHECKERR(data->layer->SetCooperativeLevel(data->layer,
  251. DLSCL_ADMINISTRATIVE));
  252. SDL_DFB_CHECKERR(data->layer->GetConfiguration(data->layer, &config));
  253. config.flags = DLCONF_WIDTH | DLCONF_HEIGHT;
  254. if (mode->format != SDL_PIXELFORMAT_UNKNOWN) {
  255. config.flags |= DLCONF_PIXELFORMAT;
  256. config.pixelformat = DirectFB_SDLToDFBPixelFormat(mode->format);
  257. data->pixelformat = config.pixelformat;
  258. }
  259. config.width = mode->w;
  260. config.height = mode->h;
  261. if (devdata->use_yuv_underlays) {
  262. config.flags |= DLCONF_OPTIONS;
  263. config.options = DLOP_ALPHACHANNEL;
  264. }
  265. data->layer->TestConfiguration(data->layer, &config, &fail);
  266. if (fail &
  267. (DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT |
  268. DLCONF_OPTIONS)) {
  269. SDL_DFB_ERR("Error setting mode %dx%d-%x\n", mode->w, mode->h,
  270. mode->format);
  271. return -1;
  272. }
  273. config.flags &= ~fail;
  274. SDL_DFB_CHECKERR(data->layer->SetConfiguration(data->layer, &config));
  275. #if (DFB_VERSION_ATLEAST(1,2,0))
  276. /* Need to call this twice ! */
  277. SDL_DFB_CHECKERR(data->layer->SetConfiguration(data->layer, &config));
  278. #endif
  279. /* Double check */
  280. SDL_DFB_CHECKERR(data->layer->GetConfiguration(data->layer, &rconfig));
  281. SDL_DFB_CHECKERR(data->
  282. layer->SetCooperativeLevel(data->layer, DLSCL_SHARED));
  283. if ((config.width != rconfig.width) || (config.height != rconfig.height)
  284. || ((mode->format != SDL_PIXELFORMAT_UNKNOWN)
  285. && (config.pixelformat != rconfig.pixelformat))) {
  286. SDL_DFB_ERR("Error setting mode %dx%d-%x\n", mode->w, mode->h,
  287. mode->format);
  288. return -1;
  289. }
  290. data->pixelformat = rconfig.pixelformat;
  291. data->cw = config.width;
  292. data->ch = config.height;
  293. display->current_mode = *mode;
  294. return 0;
  295. error:
  296. return -1;
  297. }
  298. void DirectFB_QuitModes(_THIS)
  299. {
  300. SDL_DisplayMode tmode;
  301. int i;
  302. for (i = 0; i < _this->num_displays; ++i) {
  303. SDL_VideoDisplay *display = &_this->displays[i];
  304. DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
  305. SDL_GetDesktopDisplayMode(i, &tmode);
  306. tmode.format = SDL_PIXELFORMAT_UNKNOWN;
  307. DirectFB_SetDisplayMode(_this, display, &tmode);
  308. SDL_GetDesktopDisplayMode(i, &tmode);
  309. DirectFB_SetDisplayMode(_this, display, &tmode);
  310. if (dispdata->layer) {
  311. SDL_DFB_CHECK(dispdata->
  312. layer->SetCooperativeLevel(dispdata->layer,
  313. DLSCL_ADMINISTRATIVE));
  314. SDL_DFB_CHECK(dispdata->
  315. layer->SetCursorOpacity(dispdata->layer, 0x00));
  316. SDL_DFB_CHECK(dispdata->
  317. layer->SetCooperativeLevel(dispdata->layer,
  318. DLSCL_SHARED));
  319. }
  320. SDL_DFB_RELEASE(dispdata->layer);
  321. SDL_DFB_RELEASE(dispdata->vidlayer);
  322. }
  323. }
  324. #endif /* SDL_VIDEO_DRIVER_DIRECTFB */
  325. /* vi: set ts=4 sw=4 expandtab: */