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