2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* DirectFB video driver implementation.
41 #include "SDL_error.h"
42 #include "SDL_video.h"
43 #include "SDL_mouse.h"
44 #include "SDL_sysvideo.h"
45 #include "SDL_pixels_c.h"
46 #include "SDL_events_c.h"
47 #include "SDL_DirectFB_video.h"
48 #include "SDL_DirectFB_events.h"
49 #include "SDL_DirectFB_yuv.h"
52 /* Initialization/Query functions */
53 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
54 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
55 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
56 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
58 static void DirectFB_VideoQuit(_THIS);
60 /* Hardware surface functions */
61 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
62 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
63 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
64 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
65 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
66 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
67 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
68 SDL_Surface *dst, SDL_Rect *dstrect);
69 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
70 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
71 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
73 /* Various screen update functions available */
74 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
75 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
77 /* This is the rect EnumModes2 uses */
78 struct DirectFBEnumRect {
80 struct DirectFBEnumRect* next;
83 static struct DirectFBEnumRect *enumlist = NULL;
86 /* DirectFB driver bootstrap functions */
88 static int DirectFB_Available(void)
93 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
99 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
101 SDL_VideoDevice *device;
103 /* Initialize all variables that we clean on shutdown */
104 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
107 memset (device, 0, (sizeof *device));
108 device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
110 if (device == NULL || device->hidden == NULL)
119 memset (device->hidden, 0, sizeof (*device->hidden));
121 /* Set the function pointers */
122 device->VideoInit = DirectFB_VideoInit;
123 device->ListModes = DirectFB_ListModes;
124 device->SetVideoMode = DirectFB_SetVideoMode;
125 device->SetColors = DirectFB_SetColors;
126 device->UpdateRects = NULL;
127 device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
128 device->VideoQuit = DirectFB_VideoQuit;
129 device->AllocHWSurface = DirectFB_AllocHWSurface;
130 device->CheckHWBlit = DirectFB_CheckHWBlit;
131 device->FillHWRect = DirectFB_FillHWRect;
132 device->SetHWColorKey = DirectFB_SetHWColorKey;
133 device->SetHWAlpha = DirectFB_SetHWAlpha;
134 device->LockHWSurface = DirectFB_LockHWSurface;
135 device->UnlockHWSurface = DirectFB_UnlockHWSurface;
136 device->FlipHWSurface = DirectFB_FlipHWSurface;
137 device->FreeHWSurface = DirectFB_FreeHWSurface;
138 device->SetCaption = NULL;
139 device->SetIcon = NULL;
140 device->IconifyWindow = NULL;
141 device->GrabInput = NULL;
142 device->GetWMInfo = NULL;
143 device->InitOSKeymap = DirectFB_InitOSKeymap;
144 device->PumpEvents = DirectFB_PumpEvents;
146 device->free = DirectFB_DeleteDevice;
151 VideoBootStrap DirectFB_bootstrap = {
152 "directfb", "DirectFB",
153 DirectFB_Available, DirectFB_CreateDevice
156 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
158 DFBDisplayLayerConfig dlc;
159 int bytes = (bpp + 7) / 8;
161 layer->GetConfiguration (layer, &dlc);
163 if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
164 return dlc.pixelformat;
181 static DFBEnumerationResult EnumModesCallback (unsigned int width,
186 SDL_VideoDevice *this = (SDL_VideoDevice *)data;
187 struct DirectFBEnumRect *enumrect;
191 enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
195 return DFENUM_CANCEL;
198 enumrect->r.w = width;
199 enumrect->r.h = height;
200 enumrect->next = enumlist;
207 struct private_hwdata {
208 IDirectFBSurface *surface;
209 IDirectFBPalette *palette;
212 void SetDirectFBerror (const char *function, DFBResult code)
214 const char *error = DirectFBErrorString (code);
217 SDL_SetError("%s: %s", function, error);
219 SDL_SetError("Unknown error code from %s", function);
222 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
224 if (format->Rmask && format->Gmask && format->Bmask)
226 switch (format->BitsPerPixel)
232 if (format->Rmask == 0xF800 &&
233 format->Gmask == 0x07E0 &&
234 format->Bmask == 0x001F)
239 if (format->Rmask == 0x7C00 &&
240 format->Gmask == 0x03E0 &&
241 format->Bmask == 0x001F)
246 if (format->Rmask == 0xFF0000 &&
247 format->Gmask == 0x00FF00 &&
248 format->Bmask == 0x0000FF)
253 if (format->Rmask == 0xFF0000 &&
254 format->Gmask == 0x00FF00 &&
255 format->Bmask == 0x0000FF)
257 if (format->Amask == 0xFF000000)
267 switch (format->BitsPerPixel)
285 static SDL_Palette *AllocatePalette(int size)
287 SDL_Palette *palette;
290 palette = calloc (1, sizeof(SDL_Palette));
297 colors = calloc (size, sizeof(SDL_Color));
304 palette->ncolors = size;
305 palette->colors = colors;
310 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
312 format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
313 format->BitsPerPixel = format->BytesPerPixel = 0;
318 format->Amask = 0x000000FF;
322 format->Rmask = 0x00007C00;
323 format->Gmask = 0x000003E0;
324 format->Bmask = 0x0000001F;
328 format->Rmask = 0x0000F800;
329 format->Gmask = 0x000007E0;
330 format->Bmask = 0x0000001F;
334 format->Amask = 0; /* apps don't seem to like that: 0xFF000000; */
338 format->Rmask = 0x00FF0000;
339 format->Gmask = 0x0000FF00;
340 format->Bmask = 0x000000FF;
344 format->Rmask = 0x000000FF;
345 format->Gmask = 0x000000FF;
346 format->Bmask = 0x000000FF;
348 if (!format->palette)
349 format->palette = AllocatePalette(256);
353 fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
357 format->BitsPerPixel = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
358 format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
364 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
368 DFBCardCapabilities caps;
369 DFBDisplayLayerConfig dlc;
370 struct DirectFBEnumRect *rect;
371 IDirectFB *dfb = NULL;
372 IDirectFBDisplayLayer *layer = NULL;
373 IDirectFBEventBuffer *events = NULL;
376 ret = DirectFBInit (NULL, NULL);
379 SetDirectFBerror ("DirectFBInit", ret);
383 ret = DirectFBCreate (&dfb);
386 SetDirectFBerror ("DirectFBCreate", ret);
390 ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
393 SetDirectFBerror ("dfb->GetDisplayLayer", ret);
397 ret = dfb->CreateEventBuffer (dfb, DICAPS_ALL, &events);
400 SetDirectFBerror ("dfb->CreateEventBuffer", ret);
404 layer->EnableCursor (layer, 1);
406 /* Query layer configuration to determine the current mode and pixelformat */
407 layer->GetConfiguration (layer, &dlc);
409 /* If current format is not supported use LUT8 as the default */
410 if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
411 DFBToSDLPixelFormat (DSPF_LUT8, vformat);
413 /* Enumerate the available fullscreen modes */
414 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
417 SetDirectFBerror ("dfb->EnumVideoModes", ret);
421 HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
422 if (!HIDDEN->modelist)
428 for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
430 HIDDEN->modelist[i] = &rect->r;
433 HIDDEN->modelist[i] = NULL;
436 /* Query card capabilities to get the video memory size */
437 dfb->GetCardCapabilities (dfb, &caps);
439 this->info.wm_available = 1;
440 this->info.hw_available = 1;
441 this->info.blit_hw = 1;
442 this->info.blit_hw_CC = 1;
443 this->info.blit_hw_A = 1;
444 this->info.blit_fill = 1;
445 this->info.video_mem = caps.video_memory / 1024;
447 HIDDEN->initialized = 1;
449 HIDDEN->layer = layer;
450 HIDDEN->eventbuffer = events;
456 events->Release (events);
459 layer->Release (layer);
467 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
469 if (flags & SDL_FULLSCREEN)
470 return HIDDEN->modelist;
472 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
473 return (SDL_Rect**) -1;
478 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
481 DFBSurfaceDescription dsc;
482 DFBSurfacePixelFormat pixelformat;
483 IDirectFBSurface *surface;
485 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
486 width, height, bpp, flags);
488 flags |= SDL_FULLSCREEN;
490 /* Release previous primary surface */
491 if (current->hwdata && current->hwdata->surface)
493 current->hwdata->surface->Release (current->hwdata->surface);
494 current->hwdata->surface = NULL;
496 /* And its palette if present */
497 if (current->hwdata->palette)
499 current->hwdata->palette->Release (current->hwdata->palette);
500 current->hwdata->palette = NULL;
503 else if (!current->hwdata)
505 /* Allocate the hardware acceleration data */
506 current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
507 if (!current->hwdata)
514 /* Set cooperative level depending on flag SDL_FULLSCREEN */
515 if (flags & SDL_FULLSCREEN)
517 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
520 DirectFBError ("dfb->SetCooperativeLevel", ret);
521 flags &= ~SDL_FULLSCREEN;
525 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
528 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
531 if (flags & SDL_FULLSCREEN)
533 flags &= ~SDL_FULLSCREEN;
534 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
535 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
540 SetDirectFBerror ("dfb->SetVideoMode", ret);
545 /* Create primary surface */
546 dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
547 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
548 dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
550 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
551 if (ret && (flags & SDL_DOUBLEBUF))
553 /* Try without double buffering */
554 dsc.caps &= ~DSCAPS_FLIPPING;
555 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
559 SetDirectFBerror ("dfb->CreateSurface", ret);
565 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
567 if (flags & SDL_FULLSCREEN)
569 current->flags |= SDL_FULLSCREEN;
570 this->UpdateRects = DirectFB_DirectUpdate;
573 this->UpdateRects = DirectFB_WindowedUpdate;
575 if (dsc.caps & DSCAPS_FLIPPING)
576 current->flags |= SDL_DOUBLEBUF;
578 surface->GetPixelFormat (surface, &pixelformat);
580 DFBToSDLPixelFormat (pixelformat, current->format);
582 /* Get the surface palette (if supported) */
583 if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
585 surface->GetPalette (surface, ¤t->hwdata->palette);
587 current->flags |= SDL_HWPALETTE;
590 current->hwdata->surface = surface;
595 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
598 DFBSurfaceDescription dsc;
600 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
601 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
603 if (surface->w < 8 || surface->h < 8)
606 /* fill surface description */
607 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
608 dsc.width = surface->w;
609 dsc.height = surface->h;
610 dsc.caps = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
612 /* find the right pixelformat */
613 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
614 if (dsc.pixelformat == DSPF_UNKNOWN)
617 /* Allocate the hardware acceleration data */
618 surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
619 if (surface->hwdata == NULL)
625 /* Create the surface */
626 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
629 SetDirectFBerror ("dfb->CreateSurface", ret);
630 free (surface->hwdata);
631 surface->hwdata = NULL;
635 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
640 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
642 if (surface->hwdata && HIDDEN->initialized)
644 surface->hwdata->surface->Release (surface->hwdata->surface);
645 free (surface->hwdata);
646 surface->hwdata = NULL;
650 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
652 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
653 src->hwdata, dst->hwdata);*/
655 if (!src->hwdata || !dst->hwdata)
658 src->flags |= SDL_HWACCEL;
659 src->map->hw_blit = DirectFB_HWAccelBlit;
664 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
665 SDL_Surface *dst, SDL_Rect *dstrect)
667 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
669 DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
670 DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
672 IDirectFBSurface *surface = dst->hwdata->surface;
674 if (src->flags & SDL_SRCCOLORKEY)
676 flags |= DSBLIT_SRC_COLORKEY;
677 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
680 if (src->flags & SDL_SRCALPHA)
682 flags |= DSBLIT_BLEND_COLORALPHA;
683 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
686 surface->SetBlittingFlags (surface, flags);
688 if (sr.w == dr.w && sr.h == dr.h)
689 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
691 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
696 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
698 SDL_PixelFormat *fmt = dst->format;
699 IDirectFBSurface *surface = dst->hwdata->surface;
702 surface->SetColor (surface,
703 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
704 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
705 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
706 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
711 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
713 SDL_PixelFormat *fmt = src->format;
714 IDirectFBSurface *surface = src->hwdata->surface;
716 if (fmt->BitsPerPixel == 8)
717 surface->SetSrcColorKeyIndex (surface, key);
720 surface->SetSrcColorKey (surface,
721 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
722 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
723 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
728 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
733 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
735 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
738 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
744 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
745 DSLF_WRITE, &data, &pitch);
748 SetDirectFBerror ("surface->Lock", ret);
752 surface->pixels = data;
753 surface->pitch = pitch;
758 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
760 surface->hwdata->surface->Unlock (surface->hwdata->surface);
761 surface->pixels = NULL;
764 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
768 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
772 int region_valid = 0;
773 IDirectFBSurface *surface = this->screen->hwdata->surface;
775 for (i=0; i<numrects; ++i)
779 if ( ! rects[i].w ) /* Clipped? */
782 x2 = rects[i].x + rects[i].w - 1;
783 y2 = rects[i].y + rects[i].h - 1;
787 if (rects[i].x < region.x1)
788 region.x1 = rects[i].x;
790 if (rects[i].y < region.y1)
791 region.y1 = rects[i].y;
801 region.x1 = rects[i].x;
802 region.y1 = rects[i].y;
811 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
814 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
816 IDirectFBPalette *palette = this->screen->hwdata->palette;
821 if (firstcolor > 255)
824 if (firstcolor + ncolors > 256)
825 ncolors = 256 - firstcolor;
830 DFBColor entries[ncolors];
832 for (i=0; i<ncolors; i++)
835 entries[i].r = colors[i].r;
836 entries[i].g = colors[i].g;
837 entries[i].b = colors[i].b;
840 palette->SetEntries (palette, entries, ncolors, firstcolor);
846 void DirectFB_VideoQuit(_THIS)
848 struct DirectFBEnumRect *rect = enumlist;
849 IDirectFBSurface *surface = this->screen->hwdata->surface;
850 IDirectFBPalette *palette = this->screen->hwdata->palette;
853 palette->Release (palette);
856 surface->Release (surface);
858 this->screen->hwdata->surface = NULL;
859 this->screen->hwdata->palette = NULL;
861 if (HIDDEN->eventbuffer)
863 HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
864 HIDDEN->eventbuffer = NULL;
869 HIDDEN->layer->Release (HIDDEN->layer);
870 HIDDEN->layer = NULL;
875 HIDDEN->dfb->Release (HIDDEN->dfb);
879 /* Free video mode list */
880 if (HIDDEN->modelist)
882 free (HIDDEN->modelist);
883 HIDDEN->modelist = NULL;
886 /* Free mode enumeration list */
889 struct DirectFBEnumRect *next = rect->next;
895 HIDDEN->initialized = 0;
898 void DirectFB_FinalQuit(void)