src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga
Tue, 07 Feb 2006 06:59:48 +0000
changeset 1336 3692456e7b0f
parent 1312 c9b51268668f
child 1338 604d73db6802
permissions -rw-r--r--
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 
    22 	MGA CRTC2 support by Thomas Jarosch - tomj@simonv.com
    23 	CRTC2 support is inspired by mplayer's dfbmga driver
    24 	written by Ville Syrj��<syrjala@sci.fi>
    25 */
    26 
    27 /* DirectFB video driver implementation.
    28 */
    29 
    30 #include <stdlib.h>
    31 #include <string.h>
    32 #include <stdio.h>
    33 #include <fcntl.h>
    34 #include <unistd.h>
    35 #include <sys/mman.h>
    36 
    37 #include <directfb.h>
    38 #include <directfb_version.h>
    39 
    40 #include "SDL.h"
    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"
    50 
    51 /* The implementation dependent data for the window manager cursor */
    52 struct WMcursor {
    53 	int unused;
    54 };
    55 
    56 
    57 /* Initialization/Query functions */
    58 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
    59 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    60 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    61 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
    62 			 SDL_Color *colors);
    63 static void DirectFB_VideoQuit(_THIS);
    64 
    65 /* Hardware surface functions */
    66 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
    67 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
    68 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
    69 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
    70 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
    71 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
    72 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
    73                                 SDL_Surface *dst, SDL_Rect *dstrect);
    74 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
    75 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
    76 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
    77 static int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor);
    78 
    79 /* Various screen update functions available */
    80 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
    81 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
    82 
    83 /* This is the rect EnumModes2 uses */
    84 struct DirectFBEnumRect {
    85 	SDL_Rect r;
    86 	struct DirectFBEnumRect* next;
    87 };
    88 
    89 static struct DirectFBEnumRect *enumlist = NULL;
    90 
    91 
    92 /* DirectFB driver bootstrap functions */
    93 
    94 static int DirectFB_Available(void)
    95 {
    96   return 1;
    97 }
    98 
    99 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
   100 {
   101   SDL_free(device->hidden);
   102   SDL_free(device);
   103 }
   104 
   105 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
   106 {
   107   SDL_VideoDevice *device;
   108 
   109   /* Initialize all variables that we clean on shutdown */
   110   device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
   111   if (device)
   112     {
   113       memset (device, 0, (sizeof *device));
   114       device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
   115     }
   116   if (device == NULL  ||  device->hidden == NULL)
   117     {
   118       SDL_OutOfMemory();
   119       if (device)
   120         {
   121           free (device);
   122         }
   123       return(0);
   124     }
   125   memset (device->hidden, 0, sizeof (*device->hidden));
   126 
   127   /* Set the function pointers */
   128   device->VideoInit = DirectFB_VideoInit;
   129   device->ListModes = DirectFB_ListModes;
   130   device->SetVideoMode = DirectFB_SetVideoMode;
   131   device->SetColors = DirectFB_SetColors;
   132   device->UpdateRects = NULL;
   133   device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
   134   device->VideoQuit = DirectFB_VideoQuit;
   135   device->AllocHWSurface = DirectFB_AllocHWSurface;
   136   device->CheckHWBlit = DirectFB_CheckHWBlit;
   137   device->FillHWRect = DirectFB_FillHWRect;
   138   device->SetHWColorKey = DirectFB_SetHWColorKey;
   139   device->SetHWAlpha = DirectFB_SetHWAlpha;
   140   device->LockHWSurface = DirectFB_LockHWSurface;
   141   device->UnlockHWSurface = DirectFB_UnlockHWSurface;
   142   device->FlipHWSurface = DirectFB_FlipHWSurface;
   143   device->FreeHWSurface = DirectFB_FreeHWSurface;
   144   device->ShowWMCursor = DirectFB_ShowWMCursor;
   145   device->SetCaption = NULL;
   146   device->SetIcon = NULL;
   147   device->IconifyWindow = NULL;
   148   device->GrabInput = NULL;
   149   device->GetWMInfo = NULL;
   150   device->InitOSKeymap = DirectFB_InitOSKeymap;
   151   device->PumpEvents = DirectFB_PumpEvents;
   152 
   153   device->free = DirectFB_DeleteDevice;
   154 
   155   return device;
   156 }
   157 
   158 VideoBootStrap DirectFB_bootstrap = {
   159   "directfb", "DirectFB",
   160   DirectFB_Available, DirectFB_CreateDevice
   161 };
   162 
   163 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
   164 {
   165   DFBDisplayLayerConfig dlc;
   166   int                   bytes = (bpp + 7) / 8;
   167 
   168   layer->GetConfiguration (layer, &dlc);
   169 
   170   if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
   171     return dlc.pixelformat;
   172 
   173   switch (bytes)
   174     {
   175     case 1:
   176       return DSPF_LUT8;
   177     case 2:
   178       return DSPF_RGB16;
   179     case 3:
   180       return DSPF_RGB24;
   181     case 4:
   182       return DSPF_RGB32;
   183     }
   184 
   185   return DSPF_UNKNOWN;
   186 }
   187 
   188 static DFBEnumerationResult EnumModesCallback (int  width,
   189                                                int  height,
   190                                                int  bpp,
   191                                                void *data)
   192 {
   193   SDL_VideoDevice *this = (SDL_VideoDevice *)data;
   194   struct DirectFBEnumRect *enumrect;
   195 
   196   HIDDEN->nummodes++;
   197 
   198   enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
   199   if (!enumrect)
   200     {
   201       SDL_OutOfMemory();
   202       return DFENUM_CANCEL;
   203     }
   204 
   205   enumrect->r.w  = (Uint16)width;
   206   enumrect->r.h  = (Uint16)height;
   207   enumrect->next = enumlist;
   208 
   209   enumlist = enumrect;
   210 
   211   return DFENUM_OK;
   212 }
   213 
   214 struct private_hwdata {
   215   IDirectFBSurface *surface;
   216   IDirectFBPalette *palette;
   217 };
   218 
   219 void SetDirectFBerror (const char *function, DFBResult code)
   220 {
   221   const char *error = DirectFBErrorString (code);
   222 
   223   if (error)
   224     SDL_SetError("%s: %s", function, error);
   225   else
   226     SDL_SetError("Unknown error code from %s", function);
   227 }
   228 
   229 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
   230 {
   231   if (format->Rmask && format->Gmask && format->Bmask)
   232     {
   233       switch (format->BitsPerPixel)
   234         {
   235         case 8:
   236           return DSPF_LUT8;
   237           
   238         case 16:
   239           if (format->Rmask == 0xF800 &&
   240               format->Gmask == 0x07E0 &&
   241               format->Bmask == 0x001F)
   242             return DSPF_RGB16;
   243           /* fall through */
   244           
   245         case 15:
   246           if (format->Rmask == 0x7C00 &&
   247               format->Gmask == 0x03E0 &&
   248               format->Bmask == 0x001F)
   249             return DSPF_ARGB1555;
   250           break;
   251           
   252         case 24:
   253           if (format->Rmask == 0xFF0000 &&
   254               format->Gmask == 0x00FF00 &&
   255               format->Bmask == 0x0000FF)
   256             return DSPF_RGB24;
   257           break;
   258 
   259         case 32:
   260           if (format->Rmask == 0xFF0000 &&
   261               format->Gmask == 0x00FF00 &&
   262               format->Bmask == 0x0000FF)
   263             {
   264               if (format->Amask == 0xFF000000)
   265                 return DSPF_ARGB;
   266               else
   267                 return DSPF_RGB32;
   268             }
   269           break;
   270         }
   271     }
   272   else
   273     {
   274       switch (format->BitsPerPixel)
   275 	{
   276         case 8:
   277           return DSPF_LUT8;
   278 	case 15:
   279 	  return DSPF_ARGB1555;
   280 	case 16:
   281 	  return DSPF_RGB16;
   282 	case 24:
   283 	  return DSPF_RGB24;
   284 	case 32:
   285 	  return DSPF_RGB32;
   286 	}
   287     }
   288 
   289   return DSPF_UNKNOWN;
   290 }
   291 
   292 static SDL_Palette *AllocatePalette(int size)
   293 {
   294   SDL_Palette *palette;
   295   SDL_Color   *colors;
   296 
   297   palette = calloc (1, sizeof(SDL_Palette));
   298   if (!palette)
   299     {
   300       SDL_OutOfMemory();
   301       return NULL;
   302     }
   303 
   304   colors = calloc (size, sizeof(SDL_Color));
   305   if (!colors)
   306     {
   307       SDL_OutOfMemory();
   308       return NULL;
   309     }
   310 
   311   palette->ncolors = size;
   312   palette->colors  = colors;
   313 
   314   return palette;
   315 }
   316 
   317 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
   318 {
   319   format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
   320   format->BitsPerPixel = format->BytesPerPixel = 0;
   321 
   322   switch (pixelformat)
   323     {
   324     case DSPF_A8:
   325       format->Amask = 0x000000FF;
   326       break;
   327 
   328     case DSPF_ARGB1555:
   329       format->Rmask = 0x00007C00;
   330       format->Gmask = 0x000003E0;
   331       format->Bmask = 0x0000001F;
   332       break;
   333 
   334     case DSPF_RGB16:
   335       format->Rmask = 0x0000F800;
   336       format->Gmask = 0x000007E0;
   337       format->Bmask = 0x0000001F;
   338       break;
   339 
   340     case DSPF_ARGB:
   341       format->Amask = 0; /* apps don't seem to like that:  0xFF000000; */
   342       /* fall through */
   343     case DSPF_RGB24:
   344     case DSPF_RGB32:
   345       format->Rmask = 0x00FF0000;
   346       format->Gmask = 0x0000FF00;
   347       format->Bmask = 0x000000FF;
   348       break;
   349 
   350     case DSPF_LUT8:
   351       format->Rmask = 0x000000FF;
   352       format->Gmask = 0x000000FF;
   353       format->Bmask = 0x000000FF;
   354 
   355       if (!format->palette)
   356         format->palette = AllocatePalette(256);
   357       break;
   358 
   359     default:
   360       fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
   361       return -1;
   362     }
   363 
   364   format->BitsPerPixel  = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
   365   format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
   366 
   367   return 0;
   368 }
   369 
   370 
   371 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
   372 {
   373   int                      i;
   374   DFBResult                ret;
   375 #if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
   376   DFBCardCapabilities      caps;
   377 #else
   378   DFBGraphicsDeviceDescription caps;
   379 #endif
   380   DFBDisplayLayerConfig    dlc;
   381   struct DirectFBEnumRect *rect;
   382   IDirectFB               *dfb    = NULL;
   383   IDirectFBDisplayLayer   *layer  = NULL;
   384   IDirectFBEventBuffer    *events = NULL;
   385 
   386   HIDDEN->c2layer = NULL, HIDDEN->c2frame = NULL;
   387   HIDDEN->enable_mga_crtc2 = 0;
   388   HIDDEN->mga_crtc2_stretch_overscan = 1;
   389 
   390   ret = DirectFBInit (NULL, NULL);
   391   if (ret)
   392     {
   393       SetDirectFBerror ("DirectFBInit", ret);
   394       goto error;
   395     }
   396 
   397   ret = DirectFBCreate (&dfb);
   398   if (ret)
   399     {
   400       SetDirectFBerror ("DirectFBCreate", ret);
   401       goto error;
   402     }
   403 
   404   ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
   405   if (ret)
   406     {
   407       SetDirectFBerror ("dfb->GetDisplayLayer", ret);
   408       goto error;
   409     }
   410 
   411   ret = dfb->CreateInputEventBuffer (dfb, DICAPS_ALL, DFB_FALSE, &events);
   412   if (ret)
   413     {
   414       SetDirectFBerror ("dfb->CreateEventBuffer", ret);
   415       goto error;
   416     }
   417   
   418   layer->EnableCursor (layer, 1);
   419 
   420   /* Query layer configuration to determine the current mode and pixelformat */
   421   layer->GetConfiguration (layer, &dlc);
   422 
   423   /* If current format is not supported use LUT8 as the default */
   424   if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
   425     DFBToSDLPixelFormat (DSPF_LUT8, vformat);
   426 
   427   /* Enumerate the available fullscreen modes */
   428   ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
   429   if (ret)
   430     {
   431       SetDirectFBerror ("dfb->EnumVideoModes", ret);
   432       goto error;
   433     }
   434 
   435   HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
   436   if (!HIDDEN->modelist)
   437     {
   438       SDL_OutOfMemory();
   439       goto error;
   440     }
   441 
   442   for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
   443     {
   444       HIDDEN->modelist[i] = &rect->r;
   445     }
   446 
   447   HIDDEN->modelist[i] = NULL;
   448 
   449 
   450   /* Query card capabilities to get the video memory size */
   451 #if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
   452   dfb->GetCardCapabilities (dfb, &caps);
   453 #else
   454   dfb->GetDeviceDescription (dfb, &caps);
   455 #endif
   456 
   457   this->info.wm_available = 1;
   458   this->info.hw_available = 1;
   459   this->info.blit_hw      = 1;
   460   this->info.blit_hw_CC   = 1;
   461   this->info.blit_hw_A    = 1;
   462   this->info.blit_fill    = 1;
   463   this->info.video_mem    = caps.video_memory / 1024;
   464 
   465   HIDDEN->initialized = 1;
   466   HIDDEN->dfb         = dfb;
   467   HIDDEN->layer       = layer;
   468   HIDDEN->eventbuffer = events;
   469 
   470   if (SDL_getenv("SDL_DIRECTFB_MGA_CRTC2") != NULL)
   471     HIDDEN->enable_mga_crtc2 = 1;
   472   
   473   if (HIDDEN->enable_mga_crtc2)
   474     {
   475       DFBDisplayLayerConfig      dlc;
   476       DFBDisplayLayerConfigFlags failed;
   477 
   478       ret = dfb->GetDisplayLayer (dfb, 2, &HIDDEN->c2layer);
   479       if (ret)
   480         {
   481           SetDirectFBerror ("dfb->GetDisplayLayer(CRTC2)", ret);
   482           goto error;
   483         }
   484 
   485       ret = HIDDEN->layer->SetCooperativeLevel(HIDDEN->layer, DLSCL_EXCLUSIVE);
   486       if (ret)
   487         {
   488           SetDirectFBerror ("layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
   489           goto error;
   490         }
   491  
   492       ret = HIDDEN->c2layer->SetCooperativeLevel(HIDDEN->c2layer, DLSCL_EXCLUSIVE);
   493       if (ret)
   494         {
   495           SetDirectFBerror ("c2layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
   496           goto error;
   497         }
   498 
   499       HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0x0);
   500 
   501       /* Init the surface here as it got a fixed size */
   502       dlc.flags      = DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE;
   503       dlc.buffermode = DLBM_BACKVIDEO;
   504       dlc.pixelformat = DSPF_RGB32;
   505       
   506       ret = HIDDEN->c2layer->TestConfiguration( HIDDEN->c2layer, &dlc, &failed );
   507       if (ret)
   508         {
   509           SetDirectFBerror ("c2layer->TestConfiguration", ret);
   510           goto error;
   511         }
   512     
   513       ret = HIDDEN->c2layer->SetConfiguration( HIDDEN->c2layer, &dlc );
   514       if (ret)
   515         {
   516           SetDirectFBerror ("c2layer->SetConfiguration", ret);
   517           goto error;
   518         }
   519     
   520       ret = HIDDEN->c2layer->GetSurface( HIDDEN->c2layer, &HIDDEN->c2frame );
   521       if (ret)
   522         {
   523           SetDirectFBerror ("c2layer->GetSurface", ret);
   524           goto error;
   525         }
   526 
   527       HIDDEN->c2framesize.x = 0;
   528       HIDDEN->c2framesize.y = 0;
   529       HIDDEN->c2frame->GetSize( HIDDEN->c2frame, &HIDDEN->c2framesize.w, &HIDDEN->c2framesize.h);
   530 
   531       HIDDEN->c2frame->SetBlittingFlags( HIDDEN->c2frame, DSBLIT_NOFX );
   532       HIDDEN->c2frame->SetColor( HIDDEN->c2frame, 0, 0, 0, 0xff );
   533     
   534       /* Clear CRTC2 */
   535       HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
   536       HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
   537       HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
   538       HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
   539       HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
   540 
   541       HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0xFF );
   542     
   543       /* Check if overscan is possibly set */
   544       if (SDL_getenv("SDL_DIRECTFB_MGA_OVERSCAN") != NULL)
   545         {
   546 	    float overscan = 0;
   547 	    if (SDL_sscanf(SDL_getenv("SDL_DIRECTFB_MGA_OVERSCAN"), "%f", &overscan) == 1)
   548                if (overscan > 0 && overscan < 2)
   549 		  HIDDEN->mga_crtc2_stretch_overscan = overscan;
   550 	}
   551 
   552       #ifdef DIRECTFB_CRTC2_DEBUG
   553       printf("CRTC2 overscan: %f\n", HIDDEN->mga_crtc2_stretch_overscan);
   554       #endif
   555     }
   556 
   557   return 0;
   558 
   559  error:
   560   if (events)
   561     events->Release (events);
   562   
   563   if (HIDDEN->c2frame)
   564     HIDDEN->c2frame->Release (HIDDEN->c2frame);
   565 
   566   if (HIDDEN->c2layer)
   567     HIDDEN->c2layer->Release (HIDDEN->c2layer);
   568 
   569   if (layer)
   570     layer->Release (layer);
   571 
   572   if (dfb)
   573     dfb->Release (dfb);
   574 
   575   return -1;
   576 }
   577 
   578 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   579 {
   580   if (flags & SDL_FULLSCREEN)
   581     return HIDDEN->modelist;
   582   else
   583     if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
   584       return (SDL_Rect**) -1;
   585 
   586   return NULL;
   587 }
   588 
   589 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
   590 {
   591   DFBResult              ret;
   592   DFBSurfaceDescription  dsc;
   593   DFBSurfacePixelFormat  pixelformat;
   594   IDirectFBSurface      *surface;
   595 
   596   fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
   597            width, height, bpp, flags);
   598 
   599   flags |= SDL_FULLSCREEN;
   600 
   601   /* Release previous primary surface */
   602   if (current->hwdata && current->hwdata->surface)
   603     {
   604       current->hwdata->surface->Release (current->hwdata->surface);
   605       current->hwdata->surface = NULL;
   606 
   607       /* And its palette if present */
   608       if (current->hwdata->palette)
   609         {
   610           current->hwdata->palette->Release (current->hwdata->palette);
   611           current->hwdata->palette = NULL;
   612         }
   613     }
   614   else if (!current->hwdata)
   615     {
   616       /* Allocate the hardware acceleration data */
   617       current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
   618       if (!current->hwdata)
   619         {
   620           SDL_OutOfMemory();
   621           return NULL;
   622 	}
   623     }
   624 
   625   /* Set cooperative level depending on flag SDL_FULLSCREEN */
   626   if (flags & SDL_FULLSCREEN)
   627     {
   628       ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
   629       if (ret && !HIDDEN->enable_mga_crtc2)
   630         {
   631           DirectFBError ("dfb->SetCooperativeLevel", ret);
   632           flags &= ~SDL_FULLSCREEN;
   633         }
   634     }
   635   else
   636     HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   637 
   638   /* Set video mode */
   639   ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   640   if (ret)
   641     {
   642       if (flags & SDL_FULLSCREEN)
   643         {
   644           flags &= ~SDL_FULLSCREEN;
   645           HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   646           ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   647         }
   648 
   649       if (ret)
   650         {
   651           SetDirectFBerror ("dfb->SetVideoMode", ret);
   652           return NULL;
   653         }
   654     }
   655 
   656   /* Create primary surface */
   657   dsc.flags       = DSDESC_CAPS | DSDESC_PIXELFORMAT;
   658   dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
   659   dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
   660 
   661   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   662   if (ret && (flags & SDL_DOUBLEBUF))
   663     {
   664       /* Try without double buffering */
   665       dsc.caps &= ~DSCAPS_FLIPPING;
   666       ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   667     }
   668   if (ret)
   669     {
   670       SetDirectFBerror ("dfb->CreateSurface", ret);
   671       return NULL;
   672     }
   673 
   674   current->w     = width;
   675   current->h     = height;
   676   current->flags = SDL_HWSURFACE | SDL_PREALLOC;
   677 
   678   if (flags & SDL_FULLSCREEN)
   679     {
   680       current->flags |= SDL_FULLSCREEN;
   681       this->UpdateRects = DirectFB_DirectUpdate;
   682     }
   683   else
   684     this->UpdateRects = DirectFB_WindowedUpdate;
   685 
   686   if (dsc.caps & DSCAPS_FLIPPING)
   687     current->flags |= SDL_DOUBLEBUF;
   688 
   689   surface->GetPixelFormat (surface, &pixelformat);
   690 
   691   DFBToSDLPixelFormat (pixelformat, current->format);
   692 
   693   /* Get the surface palette (if supported) */
   694   if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
   695     {
   696       surface->GetPalette (surface, &current->hwdata->palette);
   697 
   698       current->flags |= SDL_HWPALETTE;
   699     }
   700 
   701   current->hwdata->surface = surface;
   702 
   703   /* MGA CRTC2 stuff */
   704   if (HIDDEN->enable_mga_crtc2)
   705     {
   706       /* no stretching if c2ssize == c2framesize */
   707       HIDDEN->c2ssize.x = 0, HIDDEN->c2ssize.y = 0;
   708       HIDDEN->c2ssize.w = width;
   709       HIDDEN->c2ssize.h = height;
   710 
   711       HIDDEN->c2dsize.x = 0, HIDDEN->c2dsize.y = 0;
   712       HIDDEN->c2dsize.w = width;
   713       HIDDEN->c2dsize.h = height;
   714 
   715       HIDDEN->mga_crtc2_stretch = 0;
   716 
   717       if (SDL_getenv("SDL_DIRECTFB_MGA_STRETCH") != NULL)
   718         {
   719 	    /* Normally assume a picture aspect ratio of 4:3 */
   720 	    int zoom_aspect_x = 4, zoom_aspect_y = 3, i, j;
   721 
   722 	    for (i = 1; i < 20; i++)
   723 	      {
   724 		for (j = 1; j < 10; j++)
   725 		  {
   726 		    if ((float)width/(float)i*(float)j == height) 
   727 		      {
   728 			zoom_aspect_x = i;
   729 			zoom_aspect_y = j;
   730 			
   731 			/* break the loop */
   732 			i = 21;
   733 			break;
   734 		      }
   735 		  }
   736 	      }
   737 	
   738             #ifdef DIRECTFB_CRTC2_DEBUG
   739             printf("Source resolution: X: %d, Y: %d, Aspect ratio: %d:%d\n", width, height, zoom_aspect_x, zoom_aspect_y);
   740             printf("CRTC2 resolution: X: %d, Y: %d\n", HIDDEN->c2framesize.w, HIDDEN->c2framesize.h);
   741             #endif
   742 	
   743           /* don't stretch only slightly smaller/larger images */
   744           if ((float)width < (float)HIDDEN->c2framesize.w*0.95 || (float)height < (float)HIDDEN->c2framesize.h*0.95)
   745             {
   746               while ((float)HIDDEN->c2dsize.w < (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan && (float)HIDDEN->c2dsize.h < (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
   747                 {
   748                    HIDDEN->c2dsize.w+=zoom_aspect_x;
   749                    HIDDEN->c2dsize.h+=zoom_aspect_y;
   750                 }
   751 
   752               /* one step down */
   753                 HIDDEN->c2dsize.w-=zoom_aspect_x;
   754                 HIDDEN->c2dsize.h-=zoom_aspect_y;
   755 
   756               #ifdef DIRECTFB_CRTC2_DEBUG
   757               printf("Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
   758               #endif
   759 
   760               HIDDEN->mga_crtc2_stretch = 1;
   761             } 
   762           else if ((float)width > (float)HIDDEN->c2framesize.w*0.95 || (float)height > (float)HIDDEN->c2framesize.h*0.95)
   763             {
   764                while ((float)HIDDEN->c2dsize.w > (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan || (float)HIDDEN->c2dsize.h > (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
   765                 {
   766               HIDDEN->c2dsize.w-=zoom_aspect_x;
   767               HIDDEN->c2dsize.h-=zoom_aspect_y;
   768                 }
   769               
   770               #ifdef DIRECTFB_CRTC2_DEBUG
   771               printf("Down-Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
   772               #endif
   773 
   774               HIDDEN->mga_crtc2_stretch = 1;
   775              } else {
   776           #ifdef DIRECTFB_CRTC2_DEBUG
   777           printf("Not stretching image\n");
   778           #endif
   779         }
   780 
   781       /* Panning */
   782       if (HIDDEN->c2framesize.w > HIDDEN->c2dsize.w)
   783         HIDDEN->c2dsize.x = (HIDDEN->c2framesize.w - HIDDEN->c2dsize.w)  / 2;
   784       else
   785         HIDDEN->c2dsize.x = (HIDDEN->c2dsize.w - HIDDEN->c2framesize.w)  / 2;
   786 
   787       if (HIDDEN->c2framesize.h > HIDDEN->c2dsize.h)
   788         HIDDEN->c2dsize.y = (HIDDEN->c2framesize.h - HIDDEN->c2dsize.h)  / 2;
   789       else
   790         HIDDEN->c2dsize.y = (HIDDEN->c2dsize.h - HIDDEN->c2framesize.h)  / 2;
   791 
   792       #ifdef DIRECTFB_CRTC2_DEBUG
   793     printf("CRTC2 position X: %d, Y: %d\n", HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
   794       #endif
   795    }
   796   }
   797 
   798   return current;
   799 }
   800 
   801 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
   802 {
   803   DFBResult             ret;
   804   DFBSurfaceDescription dsc;
   805 
   806   /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
   807       surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
   808 
   809   if (surface->w < 8 || surface->h < 8)
   810     return -1;
   811 
   812   /* fill surface description */
   813   dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
   814   dsc.width  = surface->w;
   815   dsc.height = surface->h;
   816   dsc.caps   = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
   817 
   818   /* find the right pixelformat */
   819   dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
   820   if (dsc.pixelformat == DSPF_UNKNOWN)
   821     return -1;
   822 
   823   /* Allocate the hardware acceleration data */
   824   surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
   825   if (surface->hwdata == NULL)
   826     {
   827       SDL_OutOfMemory();
   828       return -1;
   829     }
   830 
   831   /* Create the surface */
   832   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
   833   if (ret)
   834     {
   835       SetDirectFBerror ("dfb->CreateSurface", ret);
   836       free (surface->hwdata);
   837       surface->hwdata = NULL;
   838       return -1;
   839     }
   840 
   841   surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
   842 
   843   return 0;
   844 }
   845 
   846 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
   847 {
   848   if (surface->hwdata && HIDDEN->initialized)
   849     {
   850       surface->hwdata->surface->Release (surface->hwdata->surface);
   851       free (surface->hwdata);
   852       surface->hwdata = NULL;
   853     }
   854 }
   855 
   856 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
   857 {
   858   /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
   859       src->hwdata, dst->hwdata);*/
   860 
   861   if (!src->hwdata || !dst->hwdata)
   862     return 0;
   863 
   864   src->flags |= SDL_HWACCEL;
   865   src->map->hw_blit = DirectFB_HWAccelBlit;
   866 
   867   return 1;
   868 }
   869 
   870 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
   871                                 SDL_Surface *dst, SDL_Rect *dstrect)
   872 {
   873   DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
   874 
   875   DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
   876   DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
   877 
   878   IDirectFBSurface *surface = dst->hwdata->surface;
   879 
   880   if (src->flags & SDL_SRCCOLORKEY)
   881     {
   882       flags |= DSBLIT_SRC_COLORKEY;
   883       DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
   884     }
   885 
   886   if (src->flags & SDL_SRCALPHA)
   887     {
   888       flags |= DSBLIT_BLEND_COLORALPHA;
   889       surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
   890     }
   891 
   892   surface->SetBlittingFlags (surface, flags);
   893 
   894   if (sr.w == dr.w && sr.h == dr.h)
   895     surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
   896   else
   897     surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
   898 
   899   return 0;
   900 }
   901 
   902 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   903 {
   904   SDL_PixelFormat  *fmt     = dst->format;
   905   IDirectFBSurface *surface = dst->hwdata->surface;
   906 
   907   /* ugly */
   908   surface->SetColor (surface,
   909                      (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   910                      (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   911                      (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
   912   surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
   913 
   914   return 0;
   915 }
   916 
   917 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
   918 {
   919   SDL_PixelFormat  *fmt     = src->format;
   920   IDirectFBSurface *surface = src->hwdata->surface;
   921 
   922   if (fmt->BitsPerPixel == 8)
   923     surface->SetSrcColorKeyIndex (surface, key);
   924   else
   925     /* ugly */
   926     surface->SetSrcColorKey (surface,
   927                              (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   928                              (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   929                              (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   930 
   931   return 0;
   932 }
   933 
   934 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
   935 {
   936   return 0;
   937 }
   938 
   939 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
   940 {
   941   if (HIDDEN->enable_mga_crtc2)
   942     {
   943        int rtn = surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, 0);
   944        if (HIDDEN->mga_crtc2_stretch)
   945          HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
   946        else
   947          HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
   948      
   949        HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
   950        return rtn;
   951     } 
   952   else 
   953      return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
   954 }
   955 
   956 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
   957 {
   958   DFBResult  ret;
   959   void      *data;
   960   int        pitch;
   961 
   962   ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
   963                                         DSLF_WRITE, &data, &pitch);
   964   if (ret)
   965     {
   966       SetDirectFBerror ("surface->Lock", ret);
   967       return -1;
   968     }
   969 
   970   surface->pixels = data;
   971   surface->pitch  = pitch;
   972 
   973   return 0;
   974 }
   975 
   976 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
   977 {
   978   surface->hwdata->surface->Unlock (surface->hwdata->surface);
   979   surface->pixels = NULL;
   980 }
   981 
   982 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   983 {
   984   if (HIDDEN->enable_mga_crtc2)
   985     {
   986        if (HIDDEN->mga_crtc2_stretch)
   987          HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, this->screen->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize); 
   988        else
   989          HIDDEN->c2frame->Blit(HIDDEN->c2frame, this->screen->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y); 
   990 
   991        HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
   992     }
   993 }
   994 
   995 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
   996 {
   997   DFBRegion         region;
   998   int               i;
   999   int               region_valid = 0;
  1000   IDirectFBSurface *surface = this->screen->hwdata->surface;
  1001 
  1002   for (i=0; i<numrects; ++i)
  1003     {
  1004       int x2, y2;
  1005 
  1006       if ( ! rects[i].w ) /* Clipped? */
  1007         continue;
  1008 
  1009       x2 = rects[i].x + rects[i].w - 1;
  1010       y2 = rects[i].y + rects[i].h - 1;
  1011 
  1012       if (region_valid)
  1013         {
  1014           if (rects[i].x < region.x1)
  1015             region.x1 = rects[i].x;
  1016 
  1017           if (rects[i].y < region.y1)
  1018             region.y1 = rects[i].y;
  1019 
  1020           if (x2 > region.x2)
  1021             region.x2 = x2;
  1022 
  1023           if (y2 > region.y2)
  1024             region.y2 = y2;
  1025         }
  1026       else
  1027         {
  1028             region.x1 = rects[i].x;
  1029             region.y1 = rects[i].y;
  1030             region.x2 = x2;
  1031             region.y2 = y2;
  1032 
  1033             region_valid = 1;
  1034         }
  1035     }
  1036 
  1037   if (region_valid)
  1038     {
  1039       if (HIDDEN->enable_mga_crtc2)
  1040         {
  1041           if (HIDDEN->mga_crtc2_stretch)
  1042             HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
  1043           else
  1044             HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y); 
  1045       
  1046           HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
  1047         }
  1048       else 
  1049         surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
  1050     }
  1051 }
  1052 
  1053 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
  1054 {
  1055   IDirectFBPalette *palette = this->screen->hwdata->palette;
  1056 
  1057   if (!palette)
  1058     return 0;
  1059 
  1060   if (firstcolor > 255)
  1061     return 0;
  1062 
  1063   if (firstcolor + ncolors > 256)
  1064     ncolors = 256 - firstcolor;
  1065 
  1066   if (ncolors > 0)
  1067     {
  1068       int      i;
  1069       DFBColor entries[ncolors];
  1070 
  1071       for (i=0; i<ncolors; i++)
  1072         {
  1073           entries[i].a = 0xff;
  1074           entries[i].r = colors[i].r;
  1075           entries[i].g = colors[i].g;
  1076           entries[i].b = colors[i].b;
  1077         }
  1078 
  1079       palette->SetEntries (palette, entries, ncolors, firstcolor);
  1080     }
  1081 
  1082   return 1;
  1083 }
  1084 	
  1085 void DirectFB_VideoQuit(_THIS)
  1086 {
  1087   struct DirectFBEnumRect *rect    = enumlist;
  1088 
  1089   if (this->screen->hwdata)
  1090     {
  1091       IDirectFBSurface        *surface = this->screen->hwdata->surface;
  1092       IDirectFBPalette        *palette = this->screen->hwdata->palette;
  1093 
  1094       if (palette)
  1095         palette->Release (palette);
  1096 
  1097       if (surface)
  1098         surface->Release (surface);
  1099 
  1100       this->screen->hwdata->surface = NULL;
  1101       this->screen->hwdata->palette = NULL;
  1102     }
  1103 
  1104   if (HIDDEN->c2frame)
  1105     {
  1106       HIDDEN->c2frame->Release (HIDDEN->c2frame);
  1107       HIDDEN->c2frame = NULL;
  1108     }
  1109 
  1110   if (HIDDEN->eventbuffer)
  1111     {
  1112       HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
  1113       HIDDEN->eventbuffer = NULL;
  1114     }
  1115 
  1116   if (HIDDEN->c2layer)
  1117     {
  1118       HIDDEN->c2layer->Release (HIDDEN->c2layer);
  1119       HIDDEN->c2layer = NULL;
  1120     }
  1121 
  1122   if (HIDDEN->layer)
  1123     {
  1124       HIDDEN->layer->Release (HIDDEN->layer);
  1125       HIDDEN->layer = NULL;
  1126     }
  1127 
  1128   if (HIDDEN->dfb)
  1129     {
  1130       HIDDEN->dfb->Release (HIDDEN->dfb);
  1131       HIDDEN->dfb = NULL;
  1132     }
  1133 
  1134   /* Free video mode list */
  1135   if (HIDDEN->modelist)
  1136     {
  1137       free (HIDDEN->modelist);
  1138       HIDDEN->modelist = NULL;
  1139     }
  1140 
  1141   /* Free mode enumeration list */
  1142   while (rect)
  1143     {
  1144       struct DirectFBEnumRect *next = rect->next;
  1145       free (rect);
  1146       rect = next;
  1147     }
  1148   enumlist = NULL;
  1149 
  1150   HIDDEN->initialized = 0;
  1151 }
  1152 
  1153 
  1154 int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor)
  1155 {
  1156   /* We can only hide or show the default cursor */
  1157   if ( cursor == NULL )
  1158     {
  1159       HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0x00);
  1160     }
  1161     else
  1162     {
  1163       HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0xFF);
  1164     }
  1165   return 1;
  1166 }
  1167 
  1168 void DirectFB_FinalQuit(void) 
  1169 {
  1170 }