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.
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 IDirectFBEventBuffer *eventbuffer;
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->CreateEventBuffer (dfb, DICAPS_ALL, &eventbuffer);
335 SetDirectFBerror ("dfb->CreateEventBuffer", ret);
336 layer->Release (layer);
341 layer->EnableCursor (layer, 1);
343 /* Query layer configuration to determine the current mode and pixelformat */
344 layer->GetConfiguration (layer, &dlc);
346 if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
348 SDL_SetError ("Unsupported pixelformat");
349 layer->Release (layer);
354 /* Enumerate the available fullscreen modes */
355 for ( i=0; i<NUM_MODELISTS; ++i )
358 ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
361 SetDirectFBerror ("dfb->EnumVideoModes", ret);
362 layer->Release (layer);
366 for ( i=0; i<NUM_MODELISTS; ++i )
368 struct DirectFBEnumRect *rect;
369 HIDDEN->SDL_modelist[i] = (SDL_Rect **) malloc
370 ((HIDDEN->SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
371 if ( HIDDEN->SDL_modelist[i] == NULL )
376 for ( j = 0, rect = enumlists[i]; rect; ++j, rect = rect->next )
378 HIDDEN->SDL_modelist[i][j]=(SDL_Rect *)rect;
380 HIDDEN->SDL_modelist[i][j] = NULL;
383 /* Query card capabilities to get the video memory size */
384 dfb->GetCardCapabilities (dfb, &caps);
386 this->info.wm_available = 1;
387 this->info.hw_available = 1;
388 this->info.blit_hw = 1;
389 this->info.blit_hw_CC = 1;
390 this->info.blit_hw_A = 1;
391 this->info.blit_fill = 1;
392 this->info.video_mem = caps.video_memory / 1024;
394 HIDDEN->initialized = 1;
396 HIDDEN->layer = layer;
397 HIDDEN->eventbuffer = eventbuffer;
402 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
404 if (flags & SDL_FULLSCREEN)
405 return HIDDEN->SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1];
407 if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
408 return (SDL_Rect**) -1;
413 SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
416 DFBSurfaceDescription dsc;
417 DFBSurfacePixelFormat pixelformat;
419 fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
420 width, height, bpp, flags);
422 flags |= SDL_FULLSCREEN;
424 /* Release previous primary surface */
425 if (current->hwdata && current->hwdata->surface)
427 current->hwdata->surface->Release (current->hwdata->surface);
428 current->hwdata->surface = NULL;
430 else if (!current->hwdata)
432 /* Allocate the hardware acceleration data */
433 current->hwdata = (struct private_hwdata *) malloc (sizeof(*current->hwdata));
434 if (!current->hwdata)
439 memset (current->hwdata, 0, sizeof(*current->hwdata));
442 /* Set cooperative level depending on flag SDL_FULLSCREEN */
443 if (flags & SDL_FULLSCREEN)
445 ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
448 DirectFBError ("dfb->SetCooperativeLevel", ret);
449 flags &= ~SDL_FULLSCREEN;
453 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
456 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
459 if (flags & SDL_FULLSCREEN)
461 flags &= ~SDL_FULLSCREEN;
462 HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
463 ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
468 SetDirectFBerror ("dfb->SetVideoMode", ret);
473 /* Create primary surface */
474 dsc.flags = DSDESC_CAPS;
475 dsc.caps = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
477 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
478 if (ret && (flags & SDL_DOUBLEBUF))
480 /* Try without double buffering */
481 dsc.caps &= ~DSCAPS_FLIPPING;
482 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, ¤t->hwdata->surface);
486 SetDirectFBerror ("dfb->CreateSurface", ret);
487 current->hwdata->surface = NULL;
493 current->flags = SDL_HWSURFACE | SDL_PREALLOC;
495 if (flags & SDL_FULLSCREEN)
497 current->flags |= SDL_FULLSCREEN;
498 this->UpdateRects = DirectFB_DirectUpdate;
501 this->UpdateRects = DirectFB_WindowedUpdate;
503 if (dsc.caps & DSCAPS_FLIPPING)
504 current->flags |= SDL_DOUBLEBUF;
506 current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
507 DFBToSDLPixelFormat (pixelformat, current->format);
512 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
515 DFBSurfaceDescription dsc;
517 /* fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
518 surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
520 if (surface->w < 8 || surface->h < 8)
523 /* fill surface description */
524 dsc.flags = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
525 dsc.width = surface->w;
526 dsc.height = surface->h;
527 dsc.caps = surface->flags & SDL_DOUBLEBUF ? DSCAPS_FLIPPING : 0;
529 /* find the right pixelformat */
530 dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
531 if (dsc.pixelformat == DSPF_UNKNOWN)
534 /* Allocate the hardware acceleration data */
535 surface->hwdata = (struct private_hwdata *) malloc (sizeof(*surface->hwdata));
536 if (surface->hwdata == NULL)
542 /* Create the surface */
543 ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
546 SetDirectFBerror ("dfb->CreateSurface", ret);
547 free (surface->hwdata);
548 surface->hwdata = NULL;
552 surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
557 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
559 if (surface->hwdata && HIDDEN->initialized)
561 surface->hwdata->surface->Release (surface->hwdata->surface);
562 free (surface->hwdata);
563 surface->hwdata = NULL;
567 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
569 /* fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
570 src->hwdata, dst->hwdata);*/
572 if (!src->hwdata || !dst->hwdata)
575 src->flags |= SDL_HWACCEL;
576 src->map->hw_blit = DirectFB_HWAccelBlit;
581 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
582 SDL_Surface *dst, SDL_Rect *dstrect)
585 IDirectFBSurface *surface;
586 DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
598 surface = dst->hwdata->surface;
600 if (src->flags & SDL_SRCCOLORKEY)
602 flags |= DSBLIT_SRC_COLORKEY;
603 DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
606 if (src->flags & SDL_SRCALPHA)
608 flags |= DSBLIT_BLEND_COLORALPHA;
609 surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
612 surface->SetBlittingFlags (surface, flags);
614 if (sr.w == dr.w && sr.h == dr.h)
615 surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
617 surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
622 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
624 SDL_PixelFormat *fmt = dst->format;
625 IDirectFBSurface *surface = dst->hwdata->surface;
628 surface->SetColor (surface,
629 (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
630 (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
631 (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
632 surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
637 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
639 SDL_PixelFormat *fmt = src->format;
640 IDirectFBSurface *surface = src->hwdata->surface;
643 surface->SetSrcColorKey (surface,
644 (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
645 (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
646 (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
651 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
656 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
658 return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
661 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
667 ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
668 DSLF_WRITE, &data, &pitch);
671 SetDirectFBerror ("surface->Lock", ret);
675 surface->pixels = data;
676 surface->pitch = pitch;
681 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
683 surface->hwdata->surface->Unlock (surface->hwdata->surface);
684 surface->pixels = NULL;
687 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
691 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
695 int region_valid = 0;
696 IDirectFBSurface *surface = this->screen->hwdata->surface;
698 for (i=0; i<numrects; ++i)
702 if ( ! rects[i].w ) /* Clipped? */
705 x2 = rects[i].x + rects[i].w - 1;
706 y2 = rects[i].y + rects[i].h - 1;
710 if (rects[i].x < region.x1)
711 region.x1 = rects[i].x;
713 if (rects[i].y < region.y1)
714 region.y1 = rects[i].y;
724 region.x1 = rects[i].x;
725 region.y1 = rects[i].y;
734 surface->Flip (surface, ®ion, DSFLIP_WAITFORSYNC);
737 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
739 fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
743 void DirectFB_VideoQuit(_THIS)
747 HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
748 HIDDEN->layer->Release (HIDDEN->layer);
749 HIDDEN->dfb->Release (HIDDEN->dfb);
751 /* Free video mode lists */
752 for ( i=0; i<NUM_MODELISTS; ++i )
754 if ( HIDDEN->SDL_modelist[i] != NULL )
756 for ( j=0; HIDDEN->SDL_modelist[i][j]; ++j )
757 free(HIDDEN->SDL_modelist[i][j]);
758 free(HIDDEN->SDL_modelist[i]);
759 HIDDEN->SDL_modelist[i] = NULL;
763 HIDDEN->initialized = 0;
766 void DirectFB_FinalQuit(void)