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"
51 /* Initialization/Query functions */
52 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
53 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
54 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
55 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
57 static void DirectFB_VideoQuit(_THIS);
59 /* Hardware surface functions */
60 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
61 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
62 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
63 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
64 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
65 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
66 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
67 SDL_Surface *dst, SDL_Rect *dstrect);
68 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
69 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
70 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
72 /* Various screen update functions available */
73 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
74 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
76 /* This is the rect EnumModes2 uses */
77 struct DirectFBEnumRect {
79 struct DirectFBEnumRect* next;
82 static struct DirectFBEnumRect *enumlist = NULL;
85 /* DirectFB driver bootstrap functions */
87 static int DirectFB_Available(void)
92 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
98 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
100 SDL_VideoDevice *device;
102 /* Initialize all variables that we clean on shutdown */
103 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
106 memset (device, 0, (sizeof *device));
107 device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
109 if (device == NULL || device->hidden == NULL)
118 memset (device->hidden, 0, sizeof (*device->hidden));
120 /* Set the function pointers */
121 device->VideoInit = DirectFB_VideoInit;
122 device->ListModes = DirectFB_ListModes;
123 device->SetVideoMode = DirectFB_SetVideoMode;
124 device->SetColors = DirectFB_SetColors;
125 device->UpdateRects = NULL;
126 device->VideoQuit = DirectFB_VideoQuit;
127 device->AllocHWSurface = DirectFB_AllocHWSurface;
128 device->CheckHWBlit = DirectFB_CheckHWBlit;
129 device->FillHWRect = DirectFB_FillHWRect;
130 device->SetHWColorKey = DirectFB_SetHWColorKey;
131 device->SetHWAlpha = DirectFB_SetHWAlpha;
132 device->LockHWSurface = DirectFB_LockHWSurface;
133 device->UnlockHWSurface = DirectFB_UnlockHWSurface;
134 device->FlipHWSurface = DirectFB_FlipHWSurface;
135 device->FreeHWSurface = DirectFB_FreeHWSurface;
136 device->SetCaption = NULL;
137 device->SetIcon = NULL;
138 device->IconifyWindow = NULL;
139 device->GrabInput = NULL;
140 device->GetWMInfo = NULL;
141 device->InitOSKeymap = DirectFB_InitOSKeymap;
142 device->PumpEvents = DirectFB_PumpEvents;
144 device->free = DirectFB_DeleteDevice;
149 VideoBootStrap DirectFB_bootstrap = {
150 "directfb", "DirectFB",
151 DirectFB_Available, DirectFB_CreateDevice
154 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
156 DFBDisplayLayerConfig dlc;
157 int bytes = (bpp + 7) / 8;
159 layer->GetConfiguration (layer, &dlc);
161 if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat))
162 return dlc.pixelformat;
179 static DFBEnumerationResult EnumModesCallback (unsigned int width,
184 SDL_VideoDevice *this = (SDL_VideoDevice *)data;
185 struct DirectFBEnumRect *enumrect;
189 enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
193 return DFENUM_CANCEL;
196 enumrect->r.w = width;
197 enumrect->r.h = height;
198 enumrect->next = enumlist;
205 struct private_hwdata {
206 IDirectFBSurface *surface;
209 void SetDirectFBerror (const char *function, DFBResult code)
211 const char *error = DirectFBErrorString (code);
214 SDL_SetError("%s: %s", function, error);
216 SDL_SetError("Unknown error code from %s", function);
219 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
221 if (format->Rmask && format->Gmask && format->Bmask)
223 switch (format->BitsPerPixel)
226 if (format->Rmask == 0xF800 &&
227 format->Gmask == 0x07E0 &&
228 format->Bmask == 0x001F)
233 if (format->Rmask == 0x7C00 &&
234 format->Gmask == 0x03E0 &&
235 format->Bmask == 0x001F)
240 if (format->Rmask == 0xE0 &&
241 format->Gmask == 0x1C &&
242 format->Bmask == 0x03)
247 if (format->Rmask == 0xFF0000 &&
248 format->Gmask == 0x00FF00 &&
249 format->Bmask == 0x0000FF)
254 if (format->Rmask == 0xFF0000 &&
255 format->Gmask == 0x00FF00 &&
256 format->Bmask == 0x0000FF)
258 if (format->Amask == 0xFF000000)
268 switch (format->BitsPerPixel)
286 static const __u8 lookup3to8[] = { 0x00, 0x24, 0x49, 0x6d, 0x92, 0xb6, 0xdb, 0xff };
287 static const __u8 lookup2to8[] = { 0x00, 0x55, 0xaa, 0xff };
289 static SDL_Palette *GenerateRGB332Palette()
292 SDL_Palette *palette;
295 palette = calloc (1, sizeof(SDL_Palette));
302 colors = calloc (256, sizeof(SDL_Color));
309 for (i=0; i<256; i++)
311 colors[i].r = lookup3to8[ i >> 5 ];
312 colors[i].g = lookup3to8[ (i >> 2) & 7 ];
313 colors[i].g = lookup2to8[ i & 3 ];
316 palette->ncolors = 256;
317 palette->colors = colors;
322 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
324 format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
325 format->BitsPerPixel = format->BytesPerPixel = 0;
330 format->Amask = 0x000000FF;
334 format->Rmask = 0x00007C00;
335 format->Gmask = 0x000003E0;
336 format->Bmask = 0x0000001F;
340 format->Rmask = 0x0000F800;
341 format->Gmask = 0x000007E0;
342 format->Bmask = 0x0000001F;
346 format->Amask = 0; /* apps don't seem to like that: 0xFF000000; */
350 format->Rmask = 0x00FF0000;
351 format->Gmask = 0x0000FF00;
352 format->Bmask = 0x000000FF;
356 format->Rmask = 0x000000E0;
357 format->Gmask = 0x0000001C;
358 format->Bmask = 0x00000003;
360 format->palette = GenerateRGB332Palette();
367 format->BitsPerPixel = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
368 format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
374 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
378 DFBCardCapabilities caps;
379 DFBDisplayLayerConfig dlc;
380 DFBSurfacePixelFormat format;
381 struct DirectFBEnumRect *rect;
382 IDirectFB *dfb = NULL;
383 IDirectFBDisplayLayer *layer = NULL;
384 IDirectFBEventBuffer *events = NULL;
387 ret = DirectFBInit (NULL, NULL);
390 SetDirectFBerror ("DirectFBInit", ret);
394 ret = DirectFBCreate (&dfb);
397 SetDirectFBerror ("DirectFBCreate", ret);
401 ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
404 SetDirectFBerror ("dfb->GetDisplayLayer", ret);
408 ret = dfb->CreateEventBuffer (dfb, DICAPS_ALL, &events);
411 SetDirectFBerror ("dfb->CreateEventBuffer", ret);
415 layer->EnableCursor (layer, 1);
417 /* Query layer configuration to determine the current mode and pixelformat */
418 layer->GetConfiguration (layer, &dlc);
420 /* FIXME: Returning RGB332 as the default mode doesn't work (everything is black) */
421 if ((format = dlc.pixelformat) == DSPF_RGB332)
424 if (DFBToSDLPixelFormat (format, vformat))
426 SDL_SetError ("Unsupported pixelformat");
430 /* Enumerate the available fullscreen modes */
431 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
434 SetDirectFBerror ("dfb->EnumVideoModes", ret);
438 HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
439 if (!HIDDEN->modelist)
445 for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
447 HIDDEN->modelist[i] = &rect->r;
450 HIDDEN->modelist[i] = NULL;
453 /* Query card capabilities to get the video memory size */
454 dfb->GetCardCapabilities (dfb, &caps);
456 this->info.wm_available = 1;
457 this->info.hw_available = 1;
458 this->info.blit_hw = 1;
459 this->info.blit_hw_CC = 1;
460 this->info.blit_hw_A = 1;
461 this->info.blit_fill = 1;
462 this->info.video_mem = caps.video_memory / 1024;
464 HIDDEN->initialized = 1;
466 HIDDEN->layer = layer;
467 HIDDEN->eventbuffer = events;
473 events->Release (events);
476 layer->Release (layer);
484 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
486 if (flags & SDL_FULLSCREEN)
487 return HIDDEN->modelist;
489 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
490 return (SDL_Rect**) -1;
495 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
498 DFBSurfaceDescription dsc;
499 DFBSurfacePixelFormat pixelformat;
501 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
502 width, height, bpp, flags);
504 flags |= SDL_FULLSCREEN;
506 /* Release previous primary surface */
507 if (current->hwdata && current->hwdata->surface)
509 current->hwdata->surface->Release (current->hwdata->surface);
510 current->hwdata->surface = NULL;
512 else if (!current->hwdata)
514 /* Allocate the hardware acceleration data */
515 current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
516 if (!current->hwdata)
523 /* Set cooperative level depending on flag SDL_FULLSCREEN */
524 if (flags & SDL_FULLSCREEN)
526 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
529 DirectFBError ("dfb->SetCooperativeLevel", ret);
530 flags &= ~SDL_FULLSCREEN;
534 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
537 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
540 if (flags & SDL_FULLSCREEN)
542 flags &= ~SDL_FULLSCREEN;
543 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
544 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
549 SetDirectFBerror ("dfb->SetVideoMode", ret);
554 /* Create primary surface */
555 dsc.flags = DSDESC_CAPS | DSDESC_PIXELFORMAT;
556 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
557 dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
559 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
560 if (ret && (flags & SDL_DOUBLEBUF))
562 /* Try without double buffering */
563 dsc.caps &= ~DSCAPS_FLIPPING;
564 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
568 SetDirectFBerror ("dfb->CreateSurface", ret);
569 current->hwdata->surface = NULL;
575 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
577 if (flags & SDL_FULLSCREEN)
579 current->flags |= SDL_FULLSCREEN;
580 this->UpdateRects = DirectFB_DirectUpdate;
583 this->UpdateRects = DirectFB_WindowedUpdate;
585 if (dsc.caps & DSCAPS_FLIPPING)
586 current->flags |= SDL_DOUBLEBUF;
588 current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
589 DFBToSDLPixelFormat (pixelformat, current->format);
594 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
597 DFBSurfaceDescription dsc;
599 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
600 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
602 if (surface->w < 8 || surface->h < 8)
605 /* fill surface description */
606 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
607 dsc.width = surface->w;
608 dsc.height = surface->h;
609 dsc.caps = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
611 /* find the right pixelformat */
612 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
613 if (dsc.pixelformat == DSPF_UNKNOWN)
616 /* Allocate the hardware acceleration data */
617 surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
618 if (surface->hwdata == NULL)
624 /* Create the surface */
625 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
628 SetDirectFBerror ("dfb->CreateSurface", ret);
629 free (surface->hwdata);
630 surface->hwdata = NULL;
634 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
639 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
641 if (surface->hwdata && HIDDEN->initialized)
643 surface->hwdata->surface->Release (surface->hwdata->surface);
644 free (surface->hwdata);
645 surface->hwdata = NULL;
649 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
651 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
652 src->hwdata, dst->hwdata);*/
654 if (!src->hwdata || !dst->hwdata)
657 src->flags |= SDL_HWACCEL;
658 src->map->hw_blit = DirectFB_HWAccelBlit;
663 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
664 SDL_Surface *dst, SDL_Rect *dstrect)
666 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
668 DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
669 DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
671 IDirectFBSurface *surface = dst->hwdata->surface;
673 if (src->flags & SDL_SRCCOLORKEY)
675 flags |= DSBLIT_SRC_COLORKEY;
676 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
679 if (src->flags & SDL_SRCALPHA)
681 flags |= DSBLIT_BLEND_COLORALPHA;
682 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
685 surface->SetBlittingFlags (surface, flags);
687 if (sr.w == dr.w && sr.h == dr.h)
688 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
690 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
695 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
697 SDL_PixelFormat *fmt = dst->format;
698 IDirectFBSurface *surface = dst->hwdata->surface;
701 surface->SetColor (surface,
702 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
703 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
704 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
705 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
710 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
712 SDL_PixelFormat *fmt = src->format;
713 IDirectFBSurface *surface = src->hwdata->surface;
716 surface->SetSrcColorKey (surface,
717 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
718 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
719 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
724 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
729 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
731 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
734 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
740 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
741 DSLF_WRITE, &data, &pitch);
744 SetDirectFBerror ("surface->Lock", ret);
748 surface->pixels = data;
749 surface->pitch = pitch;
754 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
756 surface->hwdata->surface->Unlock (surface->hwdata->surface);
757 surface->pixels = NULL;
760 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
764 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
768 int region_valid = 0;
769 IDirectFBSurface *surface = this->screen->hwdata->surface;
771 for (i=0; i<numrects; ++i)
775 if ( ! rects[i].w ) /* Clipped? */
778 x2 = rects[i].x + rects[i].w - 1;
779 y2 = rects[i].y + rects[i].h - 1;
783 if (rects[i].x < region.x1)
784 region.x1 = rects[i].x;
786 if (rects[i].y < region.y1)
787 region.y1 = rects[i].y;
797 region.x1 = rects[i].x;
798 region.y1 = rects[i].y;
807 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
810 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
812 fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
816 void DirectFB_VideoQuit(_THIS)
818 struct DirectFBEnumRect *rect = enumlist;
820 if (HIDDEN->eventbuffer)
822 HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
823 HIDDEN->eventbuffer = NULL;
828 HIDDEN->layer->Release (HIDDEN->layer);
829 HIDDEN->layer = NULL;
834 HIDDEN->dfb->Release (HIDDEN->dfb);
838 /* Free video mode list */
839 if (HIDDEN->modelist)
841 free (HIDDEN->modelist);
842 HIDDEN->modelist = NULL;
845 /* Free mode enumeration list */
848 struct DirectFBEnumRect *next = rect->next;
854 HIDDEN->initialized = 0;
857 void DirectFB_FinalQuit(void)