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