SDL_DirectFB_render.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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_window.h"
  21. #include "SDL_DirectFB_modes.h"
  22. #include "SDL_syswm.h"
  23. #include "SDL_DirectFB_shape.h"
  24. #include "../SDL_sysvideo.h"
  25. #include "../../render/SDL_sysrender.h"
  26. #ifndef DFB_VERSION_ATLEAST
  27. #define DFB_VERSIONNUM(X, Y, Z) \
  28. ((X)*1000 + (Y)*100 + (Z))
  29. #define DFB_COMPILEDVERSION \
  30. DFB_VERSIONNUM(DIRECTFB_MAJOR_VERSION, DIRECTFB_MINOR_VERSION, DIRECTFB_MICRO_VERSION)
  31. #define DFB_VERSION_ATLEAST(X, Y, Z) \
  32. (DFB_COMPILEDVERSION >= DFB_VERSIONNUM(X, Y, Z))
  33. #define SDL_DFB_CHECK(x) x
  34. #endif
  35. /* the following is not yet tested ... */
  36. #define USE_DISPLAY_PALETTE (0)
  37. #define SDL_DFB_RENDERERDATA(rend) DirectFB_RenderData *renddata = ((rend) ? (DirectFB_RenderData *) (rend)->driverdata : NULL)
  38. #define SDL_DFB_WINDOWSURFACE(win) IDirectFBSurface *destsurf = ((DFB_WindowData *) ((win)->driverdata))->surface;
  39. typedef struct
  40. {
  41. SDL_Window *window;
  42. DFBSurfaceFlipFlags flipflags;
  43. int size_changed;
  44. int lastBlendMode;
  45. DFBSurfaceBlittingFlags blitFlags;
  46. DFBSurfaceDrawingFlags drawFlags;
  47. IDirectFBSurface* target;
  48. } DirectFB_RenderData;
  49. typedef struct
  50. {
  51. IDirectFBSurface *surface;
  52. Uint32 format;
  53. void *pixels;
  54. int pitch;
  55. IDirectFBPalette *palette;
  56. int isDirty;
  57. SDL_VideoDisplay *display; /* only for yuv textures */
  58. #if (DFB_VERSION_ATLEAST(1,2,0))
  59. DFBSurfaceRenderOptions render_options;
  60. #endif
  61. } DirectFB_TextureData;
  62. static SDL_INLINE void
  63. SDLtoDFBRect(const SDL_Rect * sr, DFBRectangle * dr)
  64. {
  65. dr->x = sr->x;
  66. dr->y = sr->y;
  67. dr->h = sr->h;
  68. dr->w = sr->w;
  69. }
  70. static SDL_INLINE void
  71. SDLtoDFBRect_Float(const SDL_FRect * sr, DFBRectangle * dr)
  72. {
  73. dr->x = sr->x;
  74. dr->y = sr->y;
  75. dr->h = sr->h;
  76. dr->w = sr->w;
  77. }
  78. static int
  79. TextureHasAlpha(DirectFB_TextureData * data)
  80. {
  81. /* Drawing primitive ? */
  82. if (!data)
  83. return 0;
  84. return (DFB_PIXELFORMAT_HAS_ALPHA(DirectFB_SDLToDFBPixelFormat(data->format)) ? 1 : 0);
  85. #if 0
  86. switch (data->format) {
  87. case SDL_PIXELFORMAT_INDEX4LSB:
  88. case SDL_PIXELFORMAT_INDEX4MSB:
  89. case SDL_PIXELFORMAT_ARGB4444:
  90. case SDL_PIXELFORMAT_ARGB1555:
  91. case SDL_PIXELFORMAT_ARGB8888:
  92. case SDL_PIXELFORMAT_RGBA8888:
  93. case SDL_PIXELFORMAT_ABGR8888:
  94. case SDL_PIXELFORMAT_BGRA8888:
  95. case SDL_PIXELFORMAT_ARGB2101010:
  96. return 1;
  97. default:
  98. return 0;
  99. }
  100. #endif
  101. }
  102. static SDL_INLINE IDirectFBSurface *get_dfb_surface(SDL_Window *window)
  103. {
  104. SDL_SysWMinfo wm_info;
  105. SDL_memset(&wm_info, 0, sizeof(SDL_SysWMinfo));
  106. SDL_VERSION(&wm_info.version);
  107. if (!SDL_GetWindowWMInfo(window, &wm_info)) {
  108. return NULL;
  109. }
  110. return wm_info.info.dfb.surface;
  111. }
  112. static SDL_INLINE IDirectFBWindow *get_dfb_window(SDL_Window *window)
  113. {
  114. SDL_SysWMinfo wm_info;
  115. SDL_memset(&wm_info, 0, sizeof(SDL_SysWMinfo));
  116. SDL_VERSION(&wm_info.version);
  117. if (!SDL_GetWindowWMInfo(window, &wm_info)) {
  118. return NULL;
  119. }
  120. return wm_info.info.dfb.window;
  121. }
  122. static void
  123. SetBlendMode(DirectFB_RenderData * data, int blendMode,
  124. DirectFB_TextureData * source)
  125. {
  126. IDirectFBSurface *destsurf = data->target;
  127. /* FIXME: check for format change */
  128. if (1 || data->lastBlendMode != blendMode) {
  129. switch (blendMode) {
  130. case SDL_BLENDMODE_NONE:
  131. /**< No blending */
  132. data->blitFlags = DSBLIT_NOFX;
  133. data->drawFlags = DSDRAW_NOFX;
  134. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
  135. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ZERO));
  136. break;
  137. #if 0
  138. case SDL_BLENDMODE_MASK:
  139. data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
  140. data->drawFlags = DSDRAW_BLEND;
  141. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
  142. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
  143. break;
  144. #endif
  145. case SDL_BLENDMODE_BLEND:
  146. data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
  147. data->drawFlags = DSDRAW_BLEND;
  148. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
  149. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
  150. break;
  151. case SDL_BLENDMODE_ADD:
  152. data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
  153. data->drawFlags = DSDRAW_BLEND;
  154. /* FIXME: SRCALPHA kills performance on radeon ...
  155. * It will be cheaper to copy the surface to a temporary surface and premultiply
  156. */
  157. if (source && TextureHasAlpha(source))
  158. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_SRCALPHA));
  159. else
  160. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ONE));
  161. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_ONE));
  162. break;
  163. case SDL_BLENDMODE_MOD:
  164. data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
  165. data->drawFlags = DSDRAW_BLEND;
  166. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_ZERO));
  167. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_SRCCOLOR));
  168. break;
  169. case SDL_BLENDMODE_MUL:
  170. data->blitFlags = DSBLIT_BLEND_ALPHACHANNEL;
  171. data->drawFlags = DSDRAW_BLEND;
  172. SDL_DFB_CHECK(destsurf->SetSrcBlendFunction(destsurf, DSBF_DESTCOLOR));
  173. SDL_DFB_CHECK(destsurf->SetDstBlendFunction(destsurf, DSBF_INVSRCALPHA));
  174. break;
  175. }
  176. data->lastBlendMode = blendMode;
  177. }
  178. }
  179. static int
  180. PrepareDraw(SDL_Renderer * renderer, const SDL_RenderCommand *cmd)
  181. {
  182. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  183. IDirectFBSurface *destsurf = data->target;
  184. Uint8 r = cmd->data.draw.r;
  185. Uint8 g = cmd->data.draw.g;
  186. Uint8 b = cmd->data.draw.b;
  187. Uint8 a = cmd->data.draw.a;
  188. SetBlendMode(data, cmd->data.draw.blend, NULL);
  189. SDL_DFB_CHECKERR(destsurf->SetDrawingFlags(destsurf, data->drawFlags));
  190. switch (renderer->blendMode) {
  191. case SDL_BLENDMODE_NONE:
  192. /* case SDL_BLENDMODE_MASK: */
  193. case SDL_BLENDMODE_BLEND:
  194. break;
  195. case SDL_BLENDMODE_ADD:
  196. case SDL_BLENDMODE_MOD:
  197. case SDL_BLENDMODE_MUL:
  198. r = ((int) r * (int) a) / 255;
  199. g = ((int) g * (int) a) / 255;
  200. b = ((int) b * (int) a) / 255;
  201. a = 255;
  202. break;
  203. case SDL_BLENDMODE_INVALID: break;
  204. }
  205. SDL_DFB_CHECKERR(destsurf->SetColor(destsurf, r, g, b, a));
  206. return 0;
  207. error:
  208. return -1;
  209. }
  210. static void
  211. DirectFB_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  212. {
  213. SDL_DFB_RENDERERDATA(renderer);
  214. if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  215. /* Rebind the context to the window area and update matrices */
  216. /* SDL_CurrentContext = NULL; */
  217. /* data->updateSize = SDL_TRUE; */
  218. renddata->size_changed = SDL_TRUE;
  219. }
  220. }
  221. static void
  222. DirectFB_ActivateRenderer(SDL_Renderer * renderer)
  223. {
  224. SDL_DFB_RENDERERDATA(renderer);
  225. if (renddata->size_changed /* || windata->wm_needs_redraw */) {
  226. renddata->size_changed = SDL_FALSE;
  227. }
  228. }
  229. static int
  230. DirectFB_AcquireVidLayer(SDL_Renderer * renderer, SDL_Texture * texture)
  231. {
  232. SDL_Window *window = renderer->window;
  233. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  234. SDL_DFB_DEVICEDATA(display->device);
  235. DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
  236. DirectFB_TextureData *data = texture->driverdata;
  237. DFBDisplayLayerConfig layconf;
  238. DFBResult ret;
  239. if (devdata->use_yuv_direct && (dispdata->vidID >= 0)
  240. && (!dispdata->vidIDinuse)
  241. && SDL_ISPIXELFORMAT_FOURCC(data->format)) {
  242. layconf.flags =
  243. DLCONF_WIDTH | DLCONF_HEIGHT | DLCONF_PIXELFORMAT |
  244. DLCONF_SURFACE_CAPS;
  245. layconf.width = texture->w;
  246. layconf.height = texture->h;
  247. layconf.pixelformat = DirectFB_SDLToDFBPixelFormat(data->format);
  248. layconf.surface_caps = DSCAPS_VIDEOONLY | DSCAPS_DOUBLE;
  249. SDL_DFB_CHECKERR(devdata->dfb->GetDisplayLayer(devdata->dfb,
  250. dispdata->vidID,
  251. &dispdata->vidlayer));
  252. SDL_DFB_CHECKERR(dispdata->
  253. vidlayer->SetCooperativeLevel(dispdata->vidlayer,
  254. DLSCL_EXCLUSIVE));
  255. if (devdata->use_yuv_underlays) {
  256. ret = dispdata->vidlayer->SetLevel(dispdata->vidlayer, -1);
  257. if (ret != DFB_OK)
  258. SDL_DFB_DEBUG("Underlay Setlevel not supported\n");
  259. }
  260. SDL_DFB_CHECKERR(dispdata->
  261. vidlayer->SetConfiguration(dispdata->vidlayer,
  262. &layconf));
  263. SDL_DFB_CHECKERR(dispdata->
  264. vidlayer->GetSurface(dispdata->vidlayer,
  265. &data->surface));
  266. dispdata->vidIDinuse = 1;
  267. data->display = display;
  268. return 0;
  269. }
  270. return 1;
  271. error:
  272. if (dispdata->vidlayer) {
  273. SDL_DFB_RELEASE(data->surface);
  274. SDL_DFB_CHECKERR(dispdata->
  275. vidlayer->SetCooperativeLevel(dispdata->vidlayer,
  276. DLSCL_ADMINISTRATIVE));
  277. SDL_DFB_RELEASE(dispdata->vidlayer);
  278. }
  279. return 1;
  280. }
  281. static int
  282. DirectFB_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  283. {
  284. SDL_Window *window = renderer->window;
  285. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  286. SDL_DFB_DEVICEDATA(display->device);
  287. DirectFB_TextureData *data;
  288. DFBSurfaceDescription dsc;
  289. DFBSurfacePixelFormat pixelformat;
  290. DirectFB_ActivateRenderer(renderer);
  291. SDL_DFB_ALLOC_CLEAR(data, sizeof(*data));
  292. texture->driverdata = data;
  293. /* find the right pixelformat */
  294. pixelformat = DirectFB_SDLToDFBPixelFormat(texture->format);
  295. if (pixelformat == DSPF_UNKNOWN) {
  296. SDL_SetError("Unknown pixel format %d", data->format);
  297. goto error;
  298. }
  299. data->format = texture->format;
  300. data->pitch = texture->w * DFB_BYTES_PER_PIXEL(pixelformat);
  301. if (DirectFB_AcquireVidLayer(renderer, texture) != 0) {
  302. /* fill surface description */
  303. dsc.flags =
  304. DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
  305. dsc.width = texture->w;
  306. dsc.height = texture->h;
  307. if(texture->format == SDL_PIXELFORMAT_YV12 ||
  308. texture->format == SDL_PIXELFORMAT_IYUV) {
  309. /* dfb has problems with odd sizes -make them even internally */
  310. dsc.width += (dsc.width % 2);
  311. dsc.height += (dsc.height % 2);
  312. }
  313. /* <1.2 Never use DSCAPS_VIDEOONLY here. It kills performance
  314. * No DSCAPS_SYSTEMONLY either - let dfb decide
  315. * 1.2: DSCAPS_SYSTEMONLY boosts performance by factor ~8
  316. * Depends on other settings as well. Let dfb decide.
  317. */
  318. dsc.caps = DSCAPS_PREMULTIPLIED;
  319. #if 0
  320. if (texture->access == SDL_TEXTUREACCESS_STREAMING)
  321. dsc.caps |= DSCAPS_SYSTEMONLY;
  322. else
  323. dsc.caps |= DSCAPS_VIDEOONLY;
  324. #endif
  325. dsc.pixelformat = pixelformat;
  326. data->pixels = NULL;
  327. /* Create the surface */
  328. SDL_DFB_CHECKERR(devdata->dfb->CreateSurface(devdata->dfb, &dsc,
  329. &data->surface));
  330. if (SDL_ISPIXELFORMAT_INDEXED(data->format)
  331. && !SDL_ISPIXELFORMAT_FOURCC(data->format)) {
  332. #if 1
  333. SDL_DFB_CHECKERR(data->surface->GetPalette(data->surface, &data->palette));
  334. #else
  335. /* DFB has issues with blitting LUT8 surfaces.
  336. * Creating a new palette does not help.
  337. */
  338. DFBPaletteDescription pal_desc;
  339. pal_desc.flags = DPDESC_SIZE; /* | DPDESC_ENTRIES */
  340. pal_desc.size = 256;
  341. SDL_DFB_CHECKERR(devdata->dfb->CreatePalette(devdata->dfb, &pal_desc,&data->palette));
  342. SDL_DFB_CHECKERR(data->surface->SetPalette(data->surface, data->palette));
  343. #endif
  344. }
  345. }
  346. #if (DFB_VERSION_ATLEAST(1,2,0))
  347. data->render_options = DSRO_NONE;
  348. #endif
  349. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  350. /* 3 plane YUVs return 1 bpp, but we need more space for other planes */
  351. if(texture->format == SDL_PIXELFORMAT_YV12 ||
  352. texture->format == SDL_PIXELFORMAT_IYUV) {
  353. SDL_DFB_ALLOC_CLEAR(data->pixels, (texture->h * data->pitch + ((texture->h + texture->h % 2) * (data->pitch + data->pitch % 2) * 2) / 4));
  354. } else {
  355. SDL_DFB_ALLOC_CLEAR(data->pixels, texture->h * data->pitch);
  356. }
  357. }
  358. return 0;
  359. error:
  360. SDL_DFB_RELEASE(data->palette);
  361. SDL_DFB_RELEASE(data->surface);
  362. SDL_DFB_FREE(texture->driverdata);
  363. return -1;
  364. }
  365. #if 0
  366. static int
  367. DirectFB_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
  368. {
  369. #if (DFB_VERSION_ATLEAST(1,2,0))
  370. DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
  371. switch (texture->scaleMode) {
  372. case SDL_SCALEMODE_NONE:
  373. case SDL_SCALEMODE_FAST:
  374. data->render_options = DSRO_NONE;
  375. break;
  376. case SDL_SCALEMODE_SLOW:
  377. data->render_options = DSRO_SMOOTH_UPSCALE | DSRO_SMOOTH_DOWNSCALE;
  378. break;
  379. case SDL_SCALEMODE_BEST:
  380. data->render_options =
  381. DSRO_SMOOTH_UPSCALE | DSRO_SMOOTH_DOWNSCALE | DSRO_ANTIALIAS;
  382. break;
  383. default:
  384. data->render_options = DSRO_NONE;
  385. texture->scaleMode = SDL_SCALEMODE_NONE;
  386. return SDL_Unsupported();
  387. }
  388. #endif
  389. return 0;
  390. }
  391. #endif
  392. static int
  393. DirectFB_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  394. const SDL_Rect * rect, const void *pixels, int pitch)
  395. {
  396. DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
  397. Uint8 *dpixels;
  398. int dpitch;
  399. Uint8 *src, *dst;
  400. int row;
  401. size_t length;
  402. int bpp = DFB_BYTES_PER_PIXEL(DirectFB_SDLToDFBPixelFormat(texture->format));
  403. /* FIXME: SDL_BYTESPERPIXEL(texture->format) broken for yuv yv12 3 planes */
  404. DirectFB_ActivateRenderer(renderer);
  405. if ((texture->format == SDL_PIXELFORMAT_YV12) ||
  406. (texture->format == SDL_PIXELFORMAT_IYUV)) {
  407. bpp = 1;
  408. }
  409. SDL_DFB_CHECKERR(data->surface->Lock(data->surface,
  410. DSLF_WRITE | DSLF_READ,
  411. ((void **) &dpixels), &dpitch));
  412. src = (Uint8 *) pixels;
  413. dst = (Uint8 *) dpixels + rect->y * dpitch + rect->x * bpp;
  414. length = rect->w * bpp;
  415. for (row = 0; row < rect->h; ++row) {
  416. SDL_memcpy(dst, src, length);
  417. src += pitch;
  418. dst += dpitch;
  419. }
  420. /* copy other planes for 3 plane formats */
  421. if ((texture->format == SDL_PIXELFORMAT_YV12) ||
  422. (texture->format == SDL_PIXELFORMAT_IYUV)) {
  423. src = (Uint8 *) pixels + texture->h * pitch;
  424. dst = (Uint8 *) dpixels + texture->h * dpitch + rect->y * dpitch / 4 + rect->x * bpp / 2;
  425. for (row = 0; row < rect->h / 2 + (rect->h & 1); ++row) {
  426. SDL_memcpy(dst, src, length / 2);
  427. src += pitch / 2;
  428. dst += dpitch / 2;
  429. }
  430. src = (Uint8 *) pixels + texture->h * pitch + texture->h * pitch / 4;
  431. dst = (Uint8 *) dpixels + texture->h * dpitch + texture->h * dpitch / 4 + rect->y * dpitch / 4 + rect->x * bpp / 2;
  432. for (row = 0; row < rect->h / 2 + (rect->h & 1); ++row) {
  433. SDL_memcpy(dst, src, length / 2);
  434. src += pitch / 2;
  435. dst += dpitch / 2;
  436. }
  437. }
  438. SDL_DFB_CHECKERR(data->surface->Unlock(data->surface));
  439. data->isDirty = 0;
  440. return 0;
  441. error:
  442. return 1;
  443. }
  444. static int
  445. DirectFB_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  446. const SDL_Rect * rect, void **pixels, int *pitch)
  447. {
  448. DirectFB_TextureData *texturedata =
  449. (DirectFB_TextureData *) texture->driverdata;
  450. DirectFB_ActivateRenderer(renderer);
  451. #if 0
  452. if (markDirty) {
  453. SDL_AddDirtyRect(&texturedata->dirty, rect);
  454. }
  455. #endif
  456. if (texturedata->display) {
  457. void *fdata;
  458. int fpitch;
  459. SDL_DFB_CHECKERR(texturedata->surface->Lock(texturedata->surface,
  460. DSLF_WRITE | DSLF_READ,
  461. &fdata, &fpitch));
  462. *pitch = fpitch;
  463. *pixels = fdata;
  464. } else {
  465. *pixels =
  466. (void *) ((Uint8 *) texturedata->pixels +
  467. rect->y * texturedata->pitch +
  468. rect->x * DFB_BYTES_PER_PIXEL(DirectFB_SDLToDFBPixelFormat(texture->format)));
  469. *pitch = texturedata->pitch;
  470. texturedata->isDirty = 1;
  471. }
  472. return 0;
  473. error:
  474. return -1;
  475. }
  476. static void
  477. DirectFB_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  478. {
  479. DirectFB_TextureData *texturedata =
  480. (DirectFB_TextureData *) texture->driverdata;
  481. DirectFB_ActivateRenderer(renderer);
  482. if (texturedata->display) {
  483. SDL_DFB_CHECK(texturedata->surface->Unlock(texturedata->surface));
  484. texturedata->pixels = NULL;
  485. }
  486. }
  487. static void
  488. DirectFB_SetTextureScaleMode()
  489. {
  490. }
  491. #if 0
  492. static void
  493. DirectFB_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  494. int numrects, const SDL_Rect * rects)
  495. {
  496. DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
  497. int i;
  498. for (i = 0; i < numrects; ++i) {
  499. SDL_AddDirtyRect(&data->dirty, &rects[i]);
  500. }
  501. }
  502. #endif
  503. static int DirectFB_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  504. {
  505. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  506. DirectFB_TextureData *tex_data = NULL;
  507. DirectFB_ActivateRenderer(renderer);
  508. if (texture) {
  509. tex_data = (DirectFB_TextureData *) texture->driverdata;
  510. data->target = tex_data->surface;
  511. } else {
  512. data->target = get_dfb_surface(data->window);
  513. }
  514. data->lastBlendMode = 0;
  515. return 0;
  516. }
  517. static int
  518. DirectFB_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
  519. {
  520. return 0; /* nothing to do in this backend. */
  521. }
  522. static int
  523. DirectFB_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
  524. {
  525. const size_t len = count * sizeof (SDL_FPoint);
  526. SDL_FPoint *verts = (SDL_FPoint *) SDL_AllocateRenderVertices(renderer, len, 0, &cmd->data.draw.first);
  527. if (!verts) {
  528. return -1;
  529. }
  530. cmd->data.draw.count = count;
  531. SDL_memcpy(verts, points, len);
  532. return 0;
  533. }
  534. static int
  535. DirectFB_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
  536. {
  537. const size_t len = count * sizeof (SDL_FRect);
  538. SDL_FRect *verts = (SDL_FRect *) SDL_AllocateRenderVertices(renderer, len, 0, &cmd->data.draw.first);
  539. if (!verts) {
  540. return -1;
  541. }
  542. cmd->data.draw.count = count;
  543. SDL_memcpy(verts, rects, len);
  544. return 0;
  545. }
  546. static int
  547. DirectFB_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  548. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  549. {
  550. DFBRectangle *verts = (DFBRectangle *) SDL_AllocateRenderVertices(renderer, 2 * sizeof (DFBRectangle), 0, &cmd->data.draw.first);
  551. if (!verts) {
  552. return -1;
  553. }
  554. cmd->data.draw.count = 1;
  555. SDLtoDFBRect(srcrect, verts++);
  556. SDLtoDFBRect_Float(dstrect, verts);
  557. return 0;
  558. }
  559. static int
  560. DirectFB_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  561. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  562. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  563. {
  564. return SDL_Unsupported();
  565. }
  566. static int
  567. DirectFB_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  568. {
  569. /* !!! FIXME: there are probably some good optimization wins in here if someone wants to look it over. */
  570. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  571. IDirectFBSurface *destsurf = data->target;
  572. DFBRegion clip_region;
  573. size_t i;
  574. DirectFB_ActivateRenderer(renderer);
  575. SDL_zero(clip_region); /* in theory, this always gets set before use. */
  576. while (cmd) {
  577. switch (cmd->command) {
  578. case SDL_RENDERCMD_SETDRAWCOLOR:
  579. break; /* not used here */
  580. case SDL_RENDERCMD_SETVIEWPORT: {
  581. const SDL_Rect *viewport = &cmd->data.viewport.rect;
  582. clip_region.x1 = viewport->x;
  583. clip_region.y1 = viewport->y;
  584. clip_region.x2 = clip_region.x1 + viewport->w - 1;
  585. clip_region.y2 = clip_region.y1 + viewport->h - 1;
  586. destsurf->SetClip(destsurf, &clip_region);
  587. break;
  588. }
  589. case SDL_RENDERCMD_SETCLIPRECT: {
  590. /* !!! FIXME: how does this SetClip interact with the one in SETVIEWPORT? */
  591. if (cmd->data.cliprect.enabled) {
  592. const SDL_Rect *rect = &cmd->data.cliprect.rect;
  593. clip_region.x1 = rect->x;
  594. clip_region.x2 = rect->x + rect->w;
  595. clip_region.y1 = rect->y;
  596. clip_region.y2 = rect->y + rect->h;
  597. destsurf->SetClip(destsurf, &clip_region);
  598. }
  599. break;
  600. }
  601. case SDL_RENDERCMD_CLEAR: {
  602. const Uint8 r = cmd->data.color.r;
  603. const Uint8 g = cmd->data.color.g;
  604. const Uint8 b = cmd->data.color.b;
  605. const Uint8 a = cmd->data.color.a;
  606. destsurf->Clear(destsurf, r, g, b, a);
  607. break;
  608. }
  609. case SDL_RENDERCMD_DRAW_POINTS: {
  610. const size_t count = cmd->data.draw.count;
  611. const SDL_FPoint *points = (SDL_FPoint *) (((Uint8 *) vertices) + cmd->data.draw.first);
  612. PrepareDraw(renderer, cmd);
  613. for (i = 0; i < count; i++) {
  614. const int x = points[i].x + clip_region.x1;
  615. const int y = points[i].y + clip_region.y1;
  616. destsurf->DrawLine(destsurf, x, y, x, y);
  617. }
  618. break;
  619. }
  620. case SDL_RENDERCMD_DRAW_LINES: {
  621. const SDL_FPoint *points = (SDL_FPoint *) (((Uint8 *) vertices) + cmd->data.draw.first);
  622. const size_t count = cmd->data.draw.count;
  623. PrepareDraw(renderer, cmd);
  624. #if (DFB_VERSION_ATLEAST(1,2,0)) /* !!! FIXME: should this be set once, somewhere else? */
  625. destsurf->SetRenderOptions(destsurf, DSRO_ANTIALIAS);
  626. #endif
  627. for (i = 0; i < count - 1; i++) {
  628. const int x1 = points[i].x + clip_region.x1;
  629. const int y1 = points[i].y + clip_region.y1;
  630. const int x2 = points[i + 1].x + clip_region.x1;
  631. const int y2 = points[i + 1].y + clip_region.y1;
  632. destsurf->DrawLine(destsurf, x1, y1, x2, y2);
  633. }
  634. break;
  635. }
  636. case SDL_RENDERCMD_FILL_RECTS: {
  637. const SDL_FRect *rects = (SDL_FRect *) (((Uint8 *) vertices) + cmd->data.draw.first);
  638. const size_t count = cmd->data.draw.count;
  639. PrepareDraw(renderer, cmd);
  640. for (i = 0; i < count; i++, rects++) {
  641. destsurf->FillRectangle(destsurf, rects->x + clip_region.x1, rects->y + clip_region.y1, rects->w, rects->h);
  642. }
  643. break;
  644. }
  645. case SDL_RENDERCMD_COPY: {
  646. SDL_Texture *texture = cmd->data.draw.texture;
  647. const Uint8 r = cmd->data.draw.r;
  648. const Uint8 g = cmd->data.draw.g;
  649. const Uint8 b = cmd->data.draw.b;
  650. const Uint8 a = cmd->data.draw.a;
  651. DFBRectangle *verts = (DFBRectangle *) (((Uint8 *) vertices) + cmd->data.draw.first);
  652. DirectFB_TextureData *texturedata = (DirectFB_TextureData *) texture->driverdata;
  653. DFBRectangle *sr = verts++;
  654. DFBRectangle *dr = verts;
  655. dr->x += clip_region.x1;
  656. dr->y += clip_region.y1;
  657. if (texturedata->display) {
  658. int px, py;
  659. SDL_Window *window = renderer->window;
  660. IDirectFBWindow *dfbwin = get_dfb_window(window);
  661. SDL_DFB_WINDOWDATA(window);
  662. SDL_VideoDisplay *display = texturedata->display;
  663. DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
  664. dispdata->vidlayer->SetSourceRectangle(dispdata->vidlayer, sr->x, sr->y, sr->w, sr->h);
  665. dfbwin->GetPosition(dfbwin, &px, &py);
  666. px += windata->client.x;
  667. py += windata->client.y;
  668. dispdata->vidlayer->SetScreenRectangle(dispdata->vidlayer, px + dr->x, py + dr->y, dr->w, dr->h);
  669. } else {
  670. DFBSurfaceBlittingFlags flags = 0;
  671. if (texturedata->isDirty) {
  672. const SDL_Rect rect = { 0, 0, texture->w, texture->h };
  673. DirectFB_UpdateTexture(renderer, texture, &rect, texturedata->pixels, texturedata->pitch);
  674. }
  675. if (a != 0xFF) {
  676. flags |= DSBLIT_BLEND_COLORALPHA;
  677. }
  678. if ((r & g & b) != 0xFF) {
  679. flags |= DSBLIT_COLORIZE;
  680. }
  681. destsurf->SetColor(destsurf, r, g, b, a);
  682. /* ???? flags |= DSBLIT_SRC_PREMULTCOLOR; */
  683. SetBlendMode(data, texture->blendMode, texturedata);
  684. destsurf->SetBlittingFlags(destsurf, data->blitFlags | flags);
  685. #if (DFB_VERSION_ATLEAST(1,2,0))
  686. destsurf->SetRenderOptions(destsurf, texturedata->render_options);
  687. #endif
  688. if (sr->w == dr->w && sr->h == dr->h) {
  689. destsurf->Blit(destsurf, texturedata->surface, sr, dr->x, dr->y);
  690. } else {
  691. destsurf->StretchBlit(destsurf, texturedata->surface, sr, dr);
  692. }
  693. }
  694. break;
  695. }
  696. case SDL_RENDERCMD_COPY_EX:
  697. break; /* unsupported */
  698. case SDL_RENDERCMD_NO_OP:
  699. break;
  700. }
  701. cmd = cmd->next;
  702. }
  703. return 0;
  704. }
  705. static void
  706. DirectFB_RenderPresent(SDL_Renderer * renderer)
  707. {
  708. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  709. SDL_Window *window = renderer->window;
  710. SDL_DFB_WINDOWDATA(window);
  711. SDL_ShapeData *shape_data = (window->shaper ? window->shaper->driverdata : NULL);
  712. DirectFB_ActivateRenderer(renderer);
  713. if (shape_data && shape_data->surface) {
  714. /* saturate the window surface alpha channel */
  715. SDL_DFB_CHECK(windata->window_surface->SetSrcBlendFunction(windata->window_surface, DSBF_ONE));
  716. SDL_DFB_CHECK(windata->window_surface->SetDstBlendFunction(windata->window_surface, DSBF_ONE));
  717. SDL_DFB_CHECK(windata->window_surface->SetDrawingFlags(windata->window_surface, DSDRAW_BLEND));
  718. SDL_DFB_CHECK(windata->window_surface->SetColor(windata->window_surface, 0, 0, 0, 0xff));
  719. SDL_DFB_CHECK(windata->window_surface->FillRectangle(windata->window_surface, 0,0, windata->size.w, windata->size.h));
  720. /* blit the mask */
  721. SDL_DFB_CHECK(windata->surface->SetSrcBlendFunction(windata->surface, DSBF_DESTCOLOR));
  722. SDL_DFB_CHECK(windata->surface->SetDstBlendFunction(windata->surface, DSBF_ZERO));
  723. SDL_DFB_CHECK(windata->surface->SetBlittingFlags(windata->surface, DSBLIT_BLEND_ALPHACHANNEL));
  724. #if (DFB_VERSION_ATLEAST(1,2,0))
  725. SDL_DFB_CHECK(windata->surface->SetRenderOptions(windata->surface, DSRO_NONE));
  726. #endif
  727. SDL_DFB_CHECK(windata->surface->Blit(windata->surface, shape_data->surface, NULL, 0, 0));
  728. }
  729. /* Send the data to the display */
  730. SDL_DFB_CHECK(windata->window_surface->Flip(windata->window_surface, NULL,
  731. data->flipflags));
  732. }
  733. static void
  734. DirectFB_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  735. {
  736. DirectFB_TextureData *data = (DirectFB_TextureData *) texture->driverdata;
  737. DirectFB_ActivateRenderer(renderer);
  738. if (!data) {
  739. return;
  740. }
  741. SDL_DFB_RELEASE(data->palette);
  742. SDL_DFB_RELEASE(data->surface);
  743. if (data->display) {
  744. DFB_DisplayData *dispdata =
  745. (DFB_DisplayData *) data->display->driverdata;
  746. dispdata->vidIDinuse = 0;
  747. /* FIXME: Shouldn't we reset the cooperative level */
  748. SDL_DFB_CHECK(dispdata->vidlayer->SetCooperativeLevel(dispdata->vidlayer,
  749. DLSCL_ADMINISTRATIVE));
  750. SDL_DFB_RELEASE(dispdata->vidlayer);
  751. }
  752. SDL_DFB_FREE(data->pixels);
  753. SDL_free(data);
  754. texture->driverdata = NULL;
  755. }
  756. static void
  757. DirectFB_DestroyRenderer(SDL_Renderer * renderer)
  758. {
  759. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  760. #if 0
  761. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(data->window);
  762. if (display->palette) {
  763. SDL_DelPaletteWatch(display->palette, DisplayPaletteChanged, data);
  764. }
  765. #endif
  766. SDL_free(data);
  767. SDL_free(renderer);
  768. }
  769. static int
  770. DirectFB_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  771. Uint32 format, void * pixels, int pitch)
  772. {
  773. Uint32 sdl_format;
  774. unsigned char* laypixels;
  775. int laypitch;
  776. DFBSurfacePixelFormat dfb_format;
  777. DirectFB_RenderData *data = (DirectFB_RenderData *) renderer->driverdata;
  778. IDirectFBSurface *winsurf = data->target;
  779. DirectFB_ActivateRenderer(renderer);
  780. winsurf->GetPixelFormat(winsurf, &dfb_format);
  781. sdl_format = DirectFB_DFBToSDLPixelFormat(dfb_format);
  782. winsurf->Lock(winsurf, DSLF_READ, (void **) &laypixels, &laypitch);
  783. laypixels += (rect->y * laypitch + rect->x * SDL_BYTESPERPIXEL(sdl_format) );
  784. SDL_ConvertPixels(rect->w, rect->h,
  785. sdl_format, laypixels, laypitch,
  786. format, pixels, pitch);
  787. winsurf->Unlock(winsurf);
  788. return 0;
  789. }
  790. #if 0
  791. static int
  792. DirectFB_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  793. Uint32 format, const void * pixels, int pitch)
  794. {
  795. SDL_Window *window = renderer->window;
  796. SDL_DFB_WINDOWDATA(window);
  797. Uint32 sdl_format;
  798. unsigned char* laypixels;
  799. int laypitch;
  800. DFBSurfacePixelFormat dfb_format;
  801. SDL_DFB_CHECK(windata->surface->GetPixelFormat(windata->surface, &dfb_format));
  802. sdl_format = DirectFB_DFBToSDLPixelFormat(dfb_format);
  803. SDL_DFB_CHECK(windata->surface->Lock(windata->surface, DSLF_WRITE, (void **) &laypixels, &laypitch));
  804. laypixels += (rect->y * laypitch + rect->x * SDL_BYTESPERPIXEL(sdl_format) );
  805. SDL_ConvertPixels(rect->w, rect->h,
  806. format, pixels, pitch,
  807. sdl_format, laypixels, laypitch);
  808. SDL_DFB_CHECK(windata->surface->Unlock(windata->surface));
  809. return 0;
  810. }
  811. #endif
  812. SDL_Renderer *
  813. DirectFB_CreateRenderer(SDL_Window * window, Uint32 flags)
  814. {
  815. IDirectFBSurface *winsurf = get_dfb_surface(window);
  816. /*SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);*/
  817. SDL_Renderer *renderer = NULL;
  818. DirectFB_RenderData *data = NULL;
  819. DFBSurfaceCapabilities scaps;
  820. if (!winsurf) {
  821. return NULL;
  822. }
  823. SDL_DFB_ALLOC_CLEAR(renderer, sizeof(*renderer));
  824. SDL_DFB_ALLOC_CLEAR(data, sizeof(*data));
  825. renderer->WindowEvent = DirectFB_WindowEvent;
  826. renderer->CreateTexture = DirectFB_CreateTexture;
  827. renderer->UpdateTexture = DirectFB_UpdateTexture;
  828. renderer->LockTexture = DirectFB_LockTexture;
  829. renderer->UnlockTexture = DirectFB_UnlockTexture;
  830. renderer->SetTextureScaleMode = DirectFB_SetTextureScaleMode;
  831. renderer->QueueSetViewport = DirectFB_QueueSetViewport;
  832. renderer->QueueSetDrawColor = DirectFB_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
  833. renderer->QueueDrawPoints = DirectFB_QueueDrawPoints;
  834. renderer->QueueDrawLines = DirectFB_QueueDrawPoints; /* lines and points queue vertices the same way. */
  835. renderer->QueueFillRects = DirectFB_QueueFillRects;
  836. renderer->QueueCopy = DirectFB_QueueCopy;
  837. renderer->QueueCopyEx = DirectFB_QueueCopyEx;
  838. renderer->RunCommandQueue = DirectFB_RunCommandQueue;
  839. renderer->RenderPresent = DirectFB_RenderPresent;
  840. /* FIXME: Yet to be tested */
  841. renderer->RenderReadPixels = DirectFB_RenderReadPixels;
  842. /* renderer->RenderWritePixels = DirectFB_RenderWritePixels; */
  843. renderer->DestroyTexture = DirectFB_DestroyTexture;
  844. renderer->DestroyRenderer = DirectFB_DestroyRenderer;
  845. renderer->SetRenderTarget = DirectFB_SetRenderTarget;
  846. renderer->info = DirectFB_RenderDriver.info;
  847. renderer->window = window; /* SDL window */
  848. renderer->driverdata = data;
  849. renderer->info.flags =
  850. SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE;
  851. data->window = window;
  852. data->target = winsurf;
  853. data->flipflags = DSFLIP_PIPELINE | DSFLIP_BLIT;
  854. if (flags & SDL_RENDERER_PRESENTVSYNC) {
  855. data->flipflags |= DSFLIP_WAITFORSYNC | DSFLIP_ONSYNC;
  856. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  857. } else
  858. data->flipflags |= DSFLIP_ONSYNC;
  859. SDL_DFB_CHECKERR(winsurf->GetCapabilities(winsurf, &scaps));
  860. #if 0
  861. if (scaps & DSCAPS_DOUBLE)
  862. renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
  863. else if (scaps & DSCAPS_TRIPLE)
  864. renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
  865. else
  866. renderer->info.flags |= SDL_RENDERER_SINGLEBUFFER;
  867. #endif
  868. DirectFB_SetSupportedPixelFormats(&renderer->info);
  869. #if 0
  870. /* Set up a palette watch on the display palette */
  871. if (display-> palette) {
  872. SDL_AddPaletteWatch(display->palette, DisplayPaletteChanged, data);
  873. }
  874. #endif
  875. return renderer;
  876. error:
  877. SDL_DFB_FREE(renderer);
  878. SDL_DFB_FREE(data);
  879. return NULL;
  880. }
  881. SDL_RenderDriver DirectFB_RenderDriver = {
  882. DirectFB_CreateRenderer,
  883. {
  884. "directfb",
  885. (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
  886. /* (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
  887. SDL_TEXTUREMODULATE_ALPHA),
  888. (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND |
  889. SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
  890. (SDL_SCALEMODE_NONE | SDL_SCALEMODE_FAST |
  891. SDL_SCALEMODE_SLOW | SDL_SCALEMODE_BEST), */
  892. 0,
  893. {
  894. /* formats filled in later */
  895. },
  896. 0,
  897. 0}
  898. };
  899. #endif /* SDL_VIDEO_DRIVER_DIRECTFB */
  900. /* vi: set ts=4 sw=4 expandtab: */