src/video/directfb/SDL_DirectFB_video.c
author Ryan C. Gordon
Wed, 05 Nov 2003 21:07:29 +0000
changeset 732 d18e2b224d0e
parent 728 5446a009107a
child 759 26ac9609211f
permissions -rw-r--r--
Date: Wed, 5 Nov 2003 21:27:47 +0100
From: Thomas Jarosch <xxx>
To: Ryan C. Gordon <xxx>
Subject: MGA CRTC2 update

Hi Ryan,

attached is a small update for the DirectFB MGA CRTC2 support:

- Video aspect ratio correct scaling (important for MAME)
- Small bugfix in previous scaling code
- Ability to control the TV picture overscan via the environment variable
SDL_DIRECTFB_OVERSCAN.

Sounds worth enough for applying it to SDL CVS ;-)

Cheers,
Thomas
     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       /* Check if overscan is possibly set */
   534       if (getenv("SDL_DIRECTFB_MGA_OVERSCAN") != NULL)
   535         {
   536 	    float overscan = 0;
   537 	    if (sscanf(getenv("SDL_DIRECTFB_MGA_OVERSCAN"), "%f", &overscan) == 1)
   538                if (overscan > 0 && overscan < 2)
   539 		  HIDDEN->mga_crtc2_stretch_overscan = overscan;
   540 	}
   541 
   542       #ifdef DIRECTFB_CRTC2_DEBUG
   543       printf("CRTC2 overscan: %f\n", HIDDEN->mga_crtc2_stretch_overscan);
   544       #endif
   545     }
   546 
   547   return 0;
   548 
   549  error:
   550   if (events)
   551     events->Release (events);
   552   
   553   if (HIDDEN->c2frame)
   554     HIDDEN->c2frame->Release (HIDDEN->c2frame);
   555 
   556   if (HIDDEN->c2layer)
   557     HIDDEN->c2layer->Release (HIDDEN->c2layer);
   558 
   559   if (layer)
   560     layer->Release (layer);
   561 
   562   if (dfb)
   563     dfb->Release (dfb);
   564 
   565   return -1;
   566 }
   567 
   568 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   569 {
   570   if (flags & SDL_FULLSCREEN)
   571     return HIDDEN->modelist;
   572   else
   573     if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
   574       return (SDL_Rect**) -1;
   575 
   576   return NULL;
   577 }
   578 
   579 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
   580 {
   581   DFBResult              ret;
   582   DFBSurfaceDescription  dsc;
   583   DFBSurfacePixelFormat  pixelformat;
   584   IDirectFBSurface      *surface;
   585 
   586   fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
   587            width, height, bpp, flags);
   588 
   589   flags |= SDL_FULLSCREEN;
   590 
   591   /* Release previous primary surface */
   592   if (current->hwdata && current->hwdata->surface)
   593     {
   594       current->hwdata->surface->Release (current->hwdata->surface);
   595       current->hwdata->surface = NULL;
   596 
   597       /* And its palette if present */
   598       if (current->hwdata->palette)
   599         {
   600           current->hwdata->palette->Release (current->hwdata->palette);
   601           current->hwdata->palette = NULL;
   602         }
   603     }
   604   else if (!current->hwdata)
   605     {
   606       /* Allocate the hardware acceleration data */
   607       current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
   608       if (!current->hwdata)
   609         {
   610           SDL_OutOfMemory();
   611           return NULL;
   612 	}
   613     }
   614 
   615   /* Set cooperative level depending on flag SDL_FULLSCREEN */
   616   if (flags & SDL_FULLSCREEN)
   617     {
   618       ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
   619       if (ret && !HIDDEN->enable_mga_crtc2)
   620         {
   621           DirectFBError ("dfb->SetCooperativeLevel", ret);
   622           flags &= ~SDL_FULLSCREEN;
   623         }
   624     }
   625   else
   626     HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   627 
   628   /* Set video mode */
   629   ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   630   if (ret)
   631     {
   632       if (flags & SDL_FULLSCREEN)
   633         {
   634           flags &= ~SDL_FULLSCREEN;
   635           HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   636           ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   637         }
   638 
   639       if (ret)
   640         {
   641           SetDirectFBerror ("dfb->SetVideoMode", ret);
   642           return NULL;
   643         }
   644     }
   645 
   646   /* Create primary surface */
   647   dsc.flags       = DSDESC_CAPS | DSDESC_PIXELFORMAT;
   648   dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
   649   dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
   650 
   651   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   652   if (ret && (flags & SDL_DOUBLEBUF))
   653     {
   654       /* Try without double buffering */
   655       dsc.caps &= ~DSCAPS_FLIPPING;
   656       ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   657     }
   658   if (ret)
   659     {
   660       SetDirectFBerror ("dfb->CreateSurface", ret);
   661       return NULL;
   662     }
   663 
   664   current->w     = width;
   665   current->h     = height;
   666   current->flags = SDL_HWSURFACE | SDL_PREALLOC;
   667 
   668   if (flags & SDL_FULLSCREEN)
   669     {
   670       current->flags |= SDL_FULLSCREEN;
   671       this->UpdateRects = DirectFB_DirectUpdate;
   672     }
   673   else
   674     this->UpdateRects = DirectFB_WindowedUpdate;
   675 
   676   if (dsc.caps & DSCAPS_FLIPPING)
   677     current->flags |= SDL_DOUBLEBUF;
   678 
   679   surface->GetPixelFormat (surface, &pixelformat);
   680 
   681   DFBToSDLPixelFormat (pixelformat, current->format);
   682 
   683   /* Get the surface palette (if supported) */
   684   if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
   685     {
   686       surface->GetPalette (surface, &current->hwdata->palette);
   687 
   688       current->flags |= SDL_HWPALETTE;
   689     }
   690 
   691   current->hwdata->surface = surface;
   692 
   693   /* MGA CRTC2 stuff */
   694   if (HIDDEN->enable_mga_crtc2)
   695     {
   696       /* no stretching if c2ssize == c2framesize */
   697       HIDDEN->c2ssize.x = 0, HIDDEN->c2ssize.y = 0;
   698       HIDDEN->c2ssize.w = width;
   699       HIDDEN->c2ssize.h = height;
   700 
   701       HIDDEN->c2dsize.x = 0, HIDDEN->c2dsize.y = 0;
   702       HIDDEN->c2dsize.w = width;
   703       HIDDEN->c2dsize.h = height;
   704 
   705       HIDDEN->mga_crtc2_stretch = 0;
   706 
   707       if (getenv("SDL_DIRECTFB_MGA_STRETCH") != NULL)
   708         {
   709 	    /* Normally assume a picture aspect ratio of 4:3 */
   710 	    int zoom_aspect_x = 4, zoom_aspect_y = 3, i, j;
   711 
   712 	    for (i = 1; i < 20; i++)
   713 	      {
   714 		for (j = 1; j < 10; j++)
   715 		  {
   716 		    if ((float)width/(float)i*(float)j == height) 
   717 		      {
   718 			zoom_aspect_x = i;
   719 			zoom_aspect_y = j;
   720 			
   721 			/* break the loop */
   722 			i = 21;
   723 			break;
   724 		      }
   725 		  }
   726 	      }
   727 	
   728             #ifdef DIRECTFB_CRTC2_DEBUG
   729             printf("Source resolution: X: %d, Y: %d, Aspect ratio: %d:%d\n", width, height, zoom_aspect_x, zoom_aspect_y);
   730             printf("CRTC2 resolution: X: %d, Y: %d\n", HIDDEN->c2framesize.w, HIDDEN->c2framesize.h);
   731             #endif
   732 	
   733           /* don't stretch only slightly smaller/larger images */
   734           if ((float)width < (float)HIDDEN->c2framesize.w*0.95 || (float)height < (float)HIDDEN->c2framesize.h*0.95)
   735             {
   736               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)
   737                 {
   738                    HIDDEN->c2dsize.w+=zoom_aspect_x;
   739                    HIDDEN->c2dsize.h+=zoom_aspect_y;
   740                 }
   741 
   742               /* one step down */
   743                 HIDDEN->c2dsize.w-=zoom_aspect_x;
   744                 HIDDEN->c2dsize.h-=zoom_aspect_y;
   745 
   746               #ifdef DIRECTFB_CRTC2_DEBUG
   747               printf("Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
   748               #endif
   749 
   750               HIDDEN->mga_crtc2_stretch = 1;
   751             } 
   752           else if ((float)width > (float)HIDDEN->c2framesize.w*0.95 || (float)height > (float)HIDDEN->c2framesize.h*0.95)
   753             {
   754                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)
   755                 {
   756               HIDDEN->c2dsize.w-=zoom_aspect_x;
   757               HIDDEN->c2dsize.h-=zoom_aspect_y;
   758                 }
   759               
   760               #ifdef DIRECTFB_CRTC2_DEBUG
   761               printf("Down-Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
   762               #endif
   763 
   764               HIDDEN->mga_crtc2_stretch = 1;
   765              } else {
   766           #ifdef DIRECTFB_CRTC2_DEBUG
   767           printf("Not stretching image\n");
   768           #endif
   769         }
   770 
   771       /* Panning */
   772       if (HIDDEN->c2framesize.w > HIDDEN->c2dsize.w)
   773         HIDDEN->c2dsize.x = (HIDDEN->c2framesize.w - HIDDEN->c2dsize.w)  / 2;
   774       else
   775         HIDDEN->c2dsize.x = (HIDDEN->c2dsize.w - HIDDEN->c2framesize.w)  / 2;
   776 
   777       if (HIDDEN->c2framesize.h > HIDDEN->c2dsize.h)
   778         HIDDEN->c2dsize.y = (HIDDEN->c2framesize.h - HIDDEN->c2dsize.h)  / 2;
   779       else
   780         HIDDEN->c2dsize.y = (HIDDEN->c2dsize.h - HIDDEN->c2framesize.h)  / 2;
   781 
   782       #ifdef DIRECTFB_CRTC2_DEBUG
   783     printf("CRTC2 position X: %d, Y: %d\n", HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
   784       #endif
   785    }
   786   }
   787 
   788   return current;
   789 }
   790 
   791 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
   792 {
   793   DFBResult             ret;
   794   DFBSurfaceDescription dsc;
   795 
   796   /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
   797       surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
   798 
   799   if (surface->w < 8 || surface->h < 8)
   800     return -1;
   801 
   802   /* fill surface description */
   803   dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
   804   dsc.width  = surface->w;
   805   dsc.height = surface->h;
   806   dsc.caps   = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
   807 
   808   /* find the right pixelformat */
   809   dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
   810   if (dsc.pixelformat == DSPF_UNKNOWN)
   811     return -1;
   812 
   813   /* Allocate the hardware acceleration data */
   814   surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
   815   if (surface->hwdata == NULL)
   816     {
   817       SDL_OutOfMemory();
   818       return -1;
   819     }
   820 
   821   /* Create the surface */
   822   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
   823   if (ret)
   824     {
   825       SetDirectFBerror ("dfb->CreateSurface", ret);
   826       free (surface->hwdata);
   827       surface->hwdata = NULL;
   828       return -1;
   829     }
   830 
   831   surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
   832 
   833   return 0;
   834 }
   835 
   836 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
   837 {
   838   if (surface->hwdata && HIDDEN->initialized)
   839     {
   840       surface->hwdata->surface->Release (surface->hwdata->surface);
   841       free (surface->hwdata);
   842       surface->hwdata = NULL;
   843     }
   844 }
   845 
   846 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
   847 {
   848   /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
   849       src->hwdata, dst->hwdata);*/
   850 
   851   if (!src->hwdata || !dst->hwdata)
   852     return 0;
   853 
   854   src->flags |= SDL_HWACCEL;
   855   src->map->hw_blit = DirectFB_HWAccelBlit;
   856 
   857   return 1;
   858 }
   859 
   860 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
   861                                 SDL_Surface *dst, SDL_Rect *dstrect)
   862 {
   863   DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
   864 
   865   DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
   866   DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
   867 
   868   IDirectFBSurface *surface = dst->hwdata->surface;
   869 
   870   if (src->flags & SDL_SRCCOLORKEY)
   871     {
   872       flags |= DSBLIT_SRC_COLORKEY;
   873       DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
   874     }
   875 
   876   if (src->flags & SDL_SRCALPHA)
   877     {
   878       flags |= DSBLIT_BLEND_COLORALPHA;
   879       surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
   880     }
   881 
   882   surface->SetBlittingFlags (surface, flags);
   883 
   884   if (sr.w == dr.w && sr.h == dr.h)
   885     surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
   886   else
   887     surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
   888 
   889   return 0;
   890 }
   891 
   892 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   893 {
   894   SDL_PixelFormat  *fmt     = dst->format;
   895   IDirectFBSurface *surface = dst->hwdata->surface;
   896 
   897   /* ugly */
   898   surface->SetColor (surface,
   899                      (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   900                      (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   901                      (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
   902   surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
   903 
   904   return 0;
   905 }
   906 
   907 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
   908 {
   909   SDL_PixelFormat  *fmt     = src->format;
   910   IDirectFBSurface *surface = src->hwdata->surface;
   911 
   912   if (fmt->BitsPerPixel == 8)
   913     surface->SetSrcColorKeyIndex (surface, key);
   914   else
   915     /* ugly */
   916     surface->SetSrcColorKey (surface,
   917                              (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   918                              (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   919                              (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   920 
   921   return 0;
   922 }
   923 
   924 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
   925 {
   926   return 0;
   927 }
   928 
   929 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
   930 {
   931   if (HIDDEN->enable_mga_crtc2)
   932     {
   933        int rtn = surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, 0);
   934        if (HIDDEN->mga_crtc2_stretch)
   935          HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
   936        else
   937          HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
   938      
   939        HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
   940        return rtn;
   941     } 
   942   else 
   943      return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
   944 }
   945 
   946 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
   947 {
   948   DFBResult  ret;
   949   void      *data;
   950   int        pitch;
   951 
   952   ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
   953                                         DSLF_WRITE, &data, &pitch);
   954   if (ret)
   955     {
   956       SetDirectFBerror ("surface->Lock", ret);
   957       return -1;
   958     }
   959 
   960   surface->pixels = data;
   961   surface->pitch  = pitch;
   962 
   963   return 0;
   964 }
   965 
   966 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
   967 {
   968   surface->hwdata->surface->Unlock (surface->hwdata->surface);
   969   surface->pixels = NULL;
   970 }
   971 
   972 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   973 {
   974   if (HIDDEN->enable_mga_crtc2)
   975     {
   976        if (HIDDEN->mga_crtc2_stretch)
   977          HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, this->screen->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize); 
   978        else
   979          HIDDEN->c2frame->Blit(HIDDEN->c2frame, this->screen->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y); 
   980 
   981        HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
   982     }
   983 }
   984 
   985 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
   986 {
   987   DFBRegion         region;
   988   int               i;
   989   int               region_valid = 0;
   990   IDirectFBSurface *surface = this->screen->hwdata->surface;
   991 
   992   for (i=0; i<numrects; ++i)
   993     {
   994       int x2, y2;
   995 
   996       if ( ! rects[i].w ) /* Clipped? */
   997         continue;
   998 
   999       x2 = rects[i].x + rects[i].w - 1;
  1000       y2 = rects[i].y + rects[i].h - 1;
  1001 
  1002       if (region_valid)
  1003         {
  1004           if (rects[i].x < region.x1)
  1005             region.x1 = rects[i].x;
  1006 
  1007           if (rects[i].y < region.y1)
  1008             region.y1 = rects[i].y;
  1009 
  1010           if (x2 > region.x2)
  1011             region.x2 = x2;
  1012 
  1013           if (y2 > region.y2)
  1014             region.y2 = y2;
  1015         }
  1016       else
  1017         {
  1018             region.x1 = rects[i].x;
  1019             region.y1 = rects[i].y;
  1020             region.x2 = x2;
  1021             region.y2 = y2;
  1022 
  1023             region_valid = 1;
  1024         }
  1025     }
  1026 
  1027   if (region_valid)
  1028     {
  1029       if (HIDDEN->enable_mga_crtc2)
  1030         {
  1031           if (HIDDEN->mga_crtc2_stretch)
  1032             HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
  1033           else
  1034             HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y); 
  1035       
  1036           HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
  1037         }
  1038       else 
  1039         surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
  1040     }
  1041 }
  1042 
  1043 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
  1044 {
  1045   IDirectFBPalette *palette = this->screen->hwdata->palette;
  1046 
  1047   if (!palette)
  1048     return 0;
  1049 
  1050   if (firstcolor > 255)
  1051     return 0;
  1052 
  1053   if (firstcolor + ncolors > 256)
  1054     ncolors = 256 - firstcolor;
  1055 
  1056   if (ncolors > 0)
  1057     {
  1058       int      i;
  1059       DFBColor entries[ncolors];
  1060 
  1061       for (i=0; i<ncolors; i++)
  1062         {
  1063           entries[i].a = 0xff;
  1064           entries[i].r = colors[i].r;
  1065           entries[i].g = colors[i].g;
  1066           entries[i].b = colors[i].b;
  1067         }
  1068 
  1069       palette->SetEntries (palette, entries, ncolors, firstcolor);
  1070     }
  1071 
  1072   return 1;
  1073 }
  1074 	
  1075 void DirectFB_VideoQuit(_THIS)
  1076 {
  1077   struct DirectFBEnumRect *rect    = enumlist;
  1078   IDirectFBSurface        *surface = this->screen->hwdata->surface;
  1079   IDirectFBPalette        *palette = this->screen->hwdata->palette;
  1080 
  1081   if (palette)
  1082     palette->Release (palette);
  1083 
  1084   if (surface)
  1085     surface->Release (surface);
  1086 
  1087   if (HIDDEN->c2frame)
  1088     {
  1089       HIDDEN->c2frame->Release (HIDDEN->c2frame);
  1090       HIDDEN->c2frame = NULL;
  1091     }
  1092 
  1093   this->screen->hwdata->surface = NULL;
  1094   this->screen->hwdata->palette = NULL;
  1095 
  1096   if (HIDDEN->eventbuffer)
  1097     {
  1098       HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
  1099       HIDDEN->eventbuffer = NULL;
  1100     }
  1101 
  1102   if (HIDDEN->c2layer)
  1103     {
  1104       HIDDEN->c2layer->Release (HIDDEN->c2layer);
  1105       HIDDEN->c2layer = NULL;
  1106     }
  1107 
  1108   if (HIDDEN->layer)
  1109     {
  1110       HIDDEN->layer->Release (HIDDEN->layer);
  1111       HIDDEN->layer = NULL;
  1112     }
  1113 
  1114   if (HIDDEN->dfb)
  1115     {
  1116       HIDDEN->dfb->Release (HIDDEN->dfb);
  1117       HIDDEN->dfb = NULL;
  1118     }
  1119 
  1120   /* Free video mode list */
  1121   if (HIDDEN->modelist)
  1122     {
  1123       free (HIDDEN->modelist);
  1124       HIDDEN->modelist = NULL;
  1125     }
  1126 
  1127   /* Free mode enumeration list */
  1128   while (rect)
  1129     {
  1130       struct DirectFBEnumRect *next = rect->next;
  1131       free (rect);
  1132       rect = next;
  1133     }
  1134   enumlist = NULL;
  1135 
  1136   HIDDEN->initialized = 0;
  1137 }
  1138 
  1139 void DirectFB_FinalQuit(void) 
  1140 {
  1141 }