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