2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 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.
40 #include "SDL_error.h"
41 #include "SDL_video.h"
42 #include "SDL_mouse.h"
43 #include "SDL_sysvideo.h"
44 #include "SDL_pixels_c.h"
45 #include "SDL_events_c.h"
46 #include "SDL_DirectFB_video.h"
47 #include "SDL_DirectFB_events.h"
50 /* Initialization/Query functions */
51 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
52 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
53 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
54 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
56 static void DirectFB_VideoQuit(_THIS);
58 /* Hardware surface functions */
59 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
60 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
61 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
62 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
63 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
64 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
65 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
66 SDL_Surface *dst, SDL_Rect *dstrect);
67 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
68 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
69 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
71 /* Various screen update functions available */
72 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
73 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
75 /* This is the rect EnumModes2 uses */
76 struct DirectFBEnumRect {
78 struct DirectFBEnumRect* next;
81 static struct DirectFBEnumRect *enumlists[NUM_MODELISTS];
84 /* DirectFB driver bootstrap functions */
86 static int DirectFB_Available(void)
91 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
97 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
99 SDL_VideoDevice *device;
101 /* Initialize all variables that we clean on shutdown */
102 device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
105 memset (device, 0, (sizeof *device));
106 device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
108 if (device == NULL || device->hidden == NULL)
117 memset (device->hidden, 0, sizeof (*device->hidden));
119 /* Set the function pointers */
120 device->VideoInit = DirectFB_VideoInit;
121 device->ListModes = DirectFB_ListModes;
122 device->SetVideoMode = DirectFB_SetVideoMode;
123 device->SetColors = DirectFB_SetColors;
124 device->UpdateRects = NULL;
125 device->VideoQuit = DirectFB_VideoQuit;
126 device->AllocHWSurface = DirectFB_AllocHWSurface;
127 device->CheckHWBlit = DirectFB_CheckHWBlit;
128 device->FillHWRect = DirectFB_FillHWRect;
129 device->SetHWColorKey = DirectFB_SetHWColorKey;
130 device->SetHWAlpha = DirectFB_SetHWAlpha;
131 device->LockHWSurface = DirectFB_LockHWSurface;
132 device->UnlockHWSurface = DirectFB_UnlockHWSurface;
133 device->FlipHWSurface = DirectFB_FlipHWSurface;
134 device->FreeHWSurface = DirectFB_FreeHWSurface;
135 device->SetCaption = NULL;
136 device->SetIcon = NULL;
137 device->IconifyWindow = NULL;
138 device->GrabInput = NULL;
139 device->GetWMInfo = NULL;
140 device->InitOSKeymap = DirectFB_InitOSKeymap;
141 device->PumpEvents = DirectFB_PumpEvents;
143 device->free = DirectFB_DeleteDevice;
148 VideoBootStrap DirectFB_bootstrap = {
149 "directfb", "DirectFB",
150 DirectFB_Available, DirectFB_CreateDevice
153 static DFBEnumerationResult EnumModesCallback (unsigned int width,
158 SDL_VideoDevice *this = (SDL_VideoDevice *)data;
159 struct DirectFBEnumRect *enumrect;
169 ++HIDDEN->SDL_nummodes[bpp];
170 enumrect = (struct DirectFBEnumRect*)malloc(sizeof(struct DirectFBEnumRect));
174 return DFENUM_CANCEL;
178 enumrect->r.w = width;
179 enumrect->r.h = height;
180 enumrect->next = enumlists[bpp];
181 enumlists[bpp] = enumrect;
188 struct private_hwdata {
189 IDirectFBSurface *surface;
192 void SetDirectFBerror (const char *function, DFBResult code)
194 const char *error = DirectFBErrorString (code);
197 SDL_SetError("%s: %s", function, error);
199 SDL_SetError("Unknown error code from %s", function);
202 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
204 if (format->Rmask && format->Gmask && format->Bmask)
206 switch (format->BitsPerPixel)
209 if (format->Rmask == 0xF800 &&
210 format->Gmask == 0x07E0 &&
211 format->Bmask == 0x001F)
216 if (format->Rmask == 0x7C00 &&
217 format->Gmask == 0x03E0 &&
218 format->Bmask == 0x001F)
223 if (format->Rmask == 0xFF0000 &&
224 format->Gmask == 0x00FF00 &&
225 format->Bmask == 0x0000FF)
230 if (format->Rmask == 0xFF0000 &&
231 format->Gmask == 0x00FF00 &&
232 format->Bmask == 0x0000FF)
234 if (format->Amask == 0xFF000000)
244 switch (format->BitsPerPixel)
260 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
262 format->BitsPerPixel = 0;
263 format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
268 format->Amask = 0x000000FF;
271 format->Rmask = 0x00007C00;
272 format->Gmask = 0x000003E0;
273 format->Bmask = 0x0000001F;
276 format->Rmask = 0x0000F800;
277 format->Gmask = 0x000007E0;
278 format->Bmask = 0x0000001F;
281 format->Amask = 0xFF000000;
285 format->Rmask = 0x00FF0000;
286 format->Gmask = 0x0000FF00;
287 format->Bmask = 0x000000FF;
293 format->BitsPerPixel = DFB_BITS_PER_PIXEL(pixelformat);
299 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
304 DFBCardCapabilities caps;
305 IDirectFBDisplayLayer *layer;
306 DFBDisplayLayerConfig dlc;
307 IDirectFBInputBuffer *inputbuffer;
310 ret = DirectFBInit (NULL, NULL);
313 SetDirectFBerror ("DirectFBInit", ret);
317 ret = DirectFBCreate (&dfb);
320 SetDirectFBerror ("DirectFBCreate", ret);
324 ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
327 SetDirectFBerror ("dfb->GetDisplayLayer", ret);
332 ret = dfb->CreateInputBuffer (dfb, DICAPS_BUTTONS | DICAPS_AXIS | DICAPS_KEYS,
336 SetDirectFBerror ("dfb->CreateInputBuffer", ret);
337 layer->Release (layer);
342 layer->EnableCursor (layer, 1);
344 /* Query layer configuration to determine the current mode and pixelformat */
345 layer->GetConfiguration (layer, &dlc);
347 if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
349 SDL_SetError ("Unsupported pixelformat");
350 layer->Release (layer);
355 /* Enumerate the available fullscreen modes */
356 for ( i=0; i<NUM_MODELISTS; ++i )
359 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
362 SetDirectFBerror ("dfb->EnumVideoModes", ret);
363 layer->Release (layer);
367 for ( i=0; i<NUM_MODELISTS; ++i )
369 struct DirectFBEnumRect *rect;
370 HIDDEN->SDL_modelist[i] = (SDL_Rect **) malloc
371 ((HIDDEN->SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
372 if ( HIDDEN->SDL_modelist[i] == NULL )
377 for ( j = 0, rect = enumlists[i]; rect; ++j, rect = rect->next )
379 HIDDEN->SDL_modelist[i][j]=(SDL_Rect *)rect;
381 HIDDEN->SDL_modelist[i][j] = NULL;
384 /* Query card capabilities to get the video memory size */
385 dfb->GetCardCapabilities (dfb, &caps);
387 this->info.wm_available = 1;
388 this->info.hw_available = 1;
389 this->info.blit_hw = 1;
390 this->info.blit_hw_CC = 1;
391 this->info.blit_hw_A = 1;
392 this->info.blit_fill = 1;
393 this->info.video_mem = caps.video_memory / 1024;
395 HIDDEN->initialized = 1;
397 HIDDEN->layer = layer;
398 HIDDEN->inputbuffer = inputbuffer;
403 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
405 if (flags & SDL_FULLSCREEN)
406 return HIDDEN->SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1];
408 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
409 return (SDL_Rect**) -1;
414 SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
417 DFBSurfaceDescription dsc;
418 DFBSurfacePixelFormat pixelformat;
420 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
421 width, height, bpp, flags);
423 flags |= SDL_FULLSCREEN;
425 /* Release previous primary surface */
426 if (current->hwdata && current->hwdata->surface)
428 current->hwdata->surface->Release (current->hwdata->surface);
429 current->hwdata->surface = NULL;
431 else if (!current->hwdata)
433 /* Allocate the hardware acceleration data */
434 current->hwdata = (struct private_hwdata *) malloc (sizeof(*current->hwdata));
435 if (!current->hwdata)
440 memset (current->hwdata, 0, sizeof(*current->hwdata));
443 /* Set cooperative level depending on flag SDL_FULLSCREEN */
444 if (flags & SDL_FULLSCREEN)
446 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
449 DirectFBError ("dfb->SetCooperativeLevel", ret);
450 flags &= ~SDL_FULLSCREEN;
454 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
457 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
460 if (flags & SDL_FULLSCREEN)
462 flags &= ~SDL_FULLSCREEN;
463 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
464 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
469 SetDirectFBerror ("dfb->SetVideoMode", ret);
474 /* Create primary surface */
475 dsc.flags = DSDESC_CAPS;
476 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
478 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
479 if (ret && (flags & SDL_DOUBLEBUF))
481 /* Try without double buffering */
482 dsc.caps &= ~DSCAPS_FLIPPING;
483 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
487 SetDirectFBerror ("dfb->CreateSurface", ret);
488 current->hwdata->surface = NULL;
494 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
496 if (flags & SDL_FULLSCREEN)
498 current->flags |= SDL_FULLSCREEN;
499 this->UpdateRects = DirectFB_DirectUpdate;
502 this->UpdateRects = DirectFB_WindowedUpdate;
504 if (dsc.caps & DSCAPS_FLIPPING)
505 current->flags |= SDL_DOUBLEBUF;
507 current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
508 DFBToSDLPixelFormat (pixelformat, current->format);
513 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
516 DFBSurfaceDescription dsc;
518 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
519 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
521 if (surface->w < 8 || surface->h < 8)
524 /* fill surface description */
525 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
526 dsc.width = surface->w;
527 dsc.height = surface->h;
528 dsc.caps = surface->flags & SDL_DOUBLEBUF ? DSCAPS_FLIPPING : 0;
530 /* find the right pixelformat */
531 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
532 if (dsc.pixelformat == DSPF_UNKNOWN)
535 /* Allocate the hardware acceleration data */
536 surface->hwdata = (struct private_hwdata *) malloc (sizeof(*surface->hwdata));
537 if (surface->hwdata == NULL)
543 /* Create the surface */
544 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
547 SetDirectFBerror ("dfb->CreateSurface", ret);
548 free (surface->hwdata);
549 surface->hwdata = NULL;
553 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
558 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
560 if (surface->hwdata && HIDDEN->initialized)
562 surface->hwdata->surface->Release (surface->hwdata->surface);
563 free (surface->hwdata);
564 surface->hwdata = NULL;
568 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
570 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
571 src->hwdata, dst->hwdata);*/
573 if (!src->hwdata || !dst->hwdata)
576 src->flags |= SDL_HWACCEL;
577 src->map->hw_blit = DirectFB_HWAccelBlit;
582 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
583 SDL_Surface *dst, SDL_Rect *dstrect)
586 IDirectFBSurface *surface;
587 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
599 surface = dst->hwdata->surface;
601 if (src->flags & SDL_SRCCOLORKEY)
603 flags |= DSBLIT_SRC_COLORKEY;
604 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
607 if (src->flags & SDL_SRCALPHA)
609 flags |= DSBLIT_BLEND_COLORALPHA;
610 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
613 surface->SetBlittingFlags (surface, flags);
615 if (sr.w == dr.w && sr.h == dr.h)
616 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
618 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
623 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
625 SDL_PixelFormat *fmt = dst->format;
626 IDirectFBSurface *surface = dst->hwdata->surface;
629 surface->SetColor (surface,
630 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
631 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
632 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
633 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
638 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
640 SDL_PixelFormat *fmt = src->format;
641 IDirectFBSurface *surface = src->hwdata->surface;
644 surface->SetSrcColorKey (surface,
645 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
646 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
647 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
652 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
657 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
659 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
662 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
668 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
669 DSLF_WRITE, &data, &pitch);
672 SetDirectFBerror ("surface->Lock", ret);
676 surface->pixels = data;
677 surface->pitch = pitch;
682 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
684 surface->hwdata->surface->Unlock (surface->hwdata->surface);
685 surface->pixels = NULL;
688 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
692 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
694 IDirectFBSurface *surface = this->screen->hwdata->surface;
695 DFBRegion region = { rects->x, rects->y,
696 rects->x + rects->w - 1,
697 rects->y + rects->h - 1 };
705 if (rects->x < region.x1)
706 region.x1 = rects->x;
708 if (rects->y < region.y1)
709 region.y1 = rects->y;
711 x2 = rects->x + rects->w - 1;
712 y2 = rects->y + rects->h - 1;
721 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
724 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
726 fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
730 void DirectFB_VideoQuit(_THIS)
734 HIDDEN->inputbuffer->Release (HIDDEN->inputbuffer);
735 HIDDEN->layer->Release (HIDDEN->layer);
736 HIDDEN->dfb->Release (HIDDEN->dfb);
738 /* Free video mode lists */
739 for ( i=0; i<NUM_MODELISTS; ++i )
741 if ( HIDDEN->SDL_modelist[i] != NULL )
743 for ( j=0; HIDDEN->SDL_modelist[i][j]; ++j )
744 free(HIDDEN->SDL_modelist[i][j]);
745 free(HIDDEN->SDL_modelist[i]);
746 HIDDEN->SDL_modelist[i] = NULL;
750 HIDDEN->initialized = 0;
753 void DirectFB_FinalQuit(void)