SDL_DirectFB_render.c 42 KB

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