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