src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga
Sat, 31 Aug 2002 04:13:28 +0000
changeset 478 f8482d7c9595
parent 477 22581630aab7
child 546 0009aadb3d01
permissions -rw-r--r--
Date: Tue, 27 Aug 2002 19:07:38 +0200
From: Denis Oliver Kropp
Subject: YUV support

I just added YUV overlay support to the DirectFB backend.
     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 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* DirectFB video driver implementation.
    29 */
    30 
    31 #include <stdlib.h>
    32 #include <string.h>
    33 #include <stdio.h>
    34 #include <fcntl.h>
    35 #include <unistd.h>
    36 #include <sys/mman.h>
    37 
    38 #include <directfb.h>
    39 
    40 #include "SDL.h"
    41 #include "SDL_error.h"
    42 #include "SDL_video.h"
    43 #include "SDL_mouse.h"
    44 #include "SDL_sysvideo.h"
    45 #include "SDL_pixels_c.h"
    46 #include "SDL_events_c.h"
    47 #include "SDL_DirectFB_video.h"
    48 #include "SDL_DirectFB_events.h"
    49 #include "SDL_DirectFB_yuv.h"
    50 
    51 
    52 /* Initialization/Query functions */
    53 static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
    54 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    55 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    56 static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
    57 			 SDL_Color *colors);
    58 static void DirectFB_VideoQuit(_THIS);
    59 
    60 /* Hardware surface functions */
    61 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
    62 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
    63 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
    64 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
    65 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
    66 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
    67 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
    68                                 SDL_Surface *dst, SDL_Rect *dstrect);
    69 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
    70 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
    71 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
    72 
    73 /* Various screen update functions available */
    74 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
    75 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
    76 
    77 /* This is the rect EnumModes2 uses */
    78 struct DirectFBEnumRect {
    79 	SDL_Rect r;
    80 	struct DirectFBEnumRect* next;
    81 };
    82 
    83 static struct DirectFBEnumRect *enumlist = NULL;
    84 
    85 
    86 /* DirectFB driver bootstrap functions */
    87 
    88 static int DirectFB_Available(void)
    89 {
    90   return 1;
    91 }
    92 
    93 static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
    94 {
    95   free(device->hidden);
    96   free(device);
    97 }
    98 
    99 static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
   100 {
   101   SDL_VideoDevice *device;
   102 
   103   /* Initialize all variables that we clean on shutdown */
   104   device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
   105   if (device)
   106     {
   107       memset (device, 0, (sizeof *device));
   108       device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
   109     }
   110   if (device == NULL  ||  device->hidden == NULL)
   111     {
   112       SDL_OutOfMemory();
   113       if (device)
   114         {
   115           free (device);
   116         }
   117       return(0);
   118     }
   119   memset (device->hidden, 0, sizeof (*device->hidden));
   120 
   121   /* Set the function pointers */
   122   device->VideoInit = DirectFB_VideoInit;
   123   device->ListModes = DirectFB_ListModes;
   124   device->SetVideoMode = DirectFB_SetVideoMode;
   125   device->SetColors = DirectFB_SetColors;
   126   device->UpdateRects = NULL;
   127   device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
   128   device->VideoQuit = DirectFB_VideoQuit;
   129   device->AllocHWSurface = DirectFB_AllocHWSurface;
   130   device->CheckHWBlit = DirectFB_CheckHWBlit;
   131   device->FillHWRect = DirectFB_FillHWRect;
   132   device->SetHWColorKey = DirectFB_SetHWColorKey;
   133   device->SetHWAlpha = DirectFB_SetHWAlpha;
   134   device->LockHWSurface = DirectFB_LockHWSurface;
   135   device->UnlockHWSurface = DirectFB_UnlockHWSurface;
   136   device->FlipHWSurface = DirectFB_FlipHWSurface;
   137   device->FreeHWSurface = DirectFB_FreeHWSurface;
   138   device->SetCaption = NULL;
   139   device->SetIcon = NULL;
   140   device->IconifyWindow = NULL;
   141   device->GrabInput = NULL;
   142   device->GetWMInfo = NULL;
   143   device->InitOSKeymap = DirectFB_InitOSKeymap;
   144   device->PumpEvents = DirectFB_PumpEvents;
   145 
   146   device->free = DirectFB_DeleteDevice;
   147 
   148   return device;
   149 }
   150 
   151 VideoBootStrap DirectFB_bootstrap = {
   152   "directfb", "DirectFB",
   153   DirectFB_Available, DirectFB_CreateDevice
   154 };
   155 
   156 static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
   157 {
   158   DFBDisplayLayerConfig dlc;
   159   int                   bytes = (bpp + 7) / 8;
   160 
   161   layer->GetConfiguration (layer, &dlc);
   162 
   163   if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
   164     return dlc.pixelformat;
   165 
   166   switch (bytes)
   167     {
   168     case 1:
   169       return DSPF_LUT8;
   170     case 2:
   171       return DSPF_RGB16;
   172     case 3:
   173       return DSPF_RGB24;
   174     case 4:
   175       return DSPF_RGB32;
   176     }
   177 
   178   return DSPF_UNKNOWN;
   179 }
   180 
   181 static DFBEnumerationResult EnumModesCallback (unsigned int  width,
   182                                                unsigned int  height,
   183                                                unsigned int  bpp,
   184                                                void         *data)
   185 {
   186   SDL_VideoDevice *this = (SDL_VideoDevice *)data;
   187   struct DirectFBEnumRect *enumrect;
   188 
   189   HIDDEN->nummodes++;
   190 
   191   enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
   192   if (!enumrect)
   193     {
   194       SDL_OutOfMemory();
   195       return DFENUM_CANCEL;
   196     }
   197 
   198   enumrect->r.w  = width;
   199   enumrect->r.h  = height;
   200   enumrect->next = enumlist;
   201 
   202   enumlist = enumrect;
   203 
   204   return DFENUM_OK;
   205 }
   206 
   207 struct private_hwdata {
   208   IDirectFBSurface *surface;
   209   IDirectFBPalette *palette;
   210 };
   211 
   212 void SetDirectFBerror (const char *function, DFBResult code)
   213 {
   214   const char *error = DirectFBErrorString (code);
   215 
   216   if (error)
   217     SDL_SetError("%s: %s", function, error);
   218   else
   219     SDL_SetError("Unknown error code from %s", function);
   220 }
   221 
   222 static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
   223 {
   224   if (format->Rmask && format->Gmask && format->Bmask)
   225     {
   226       switch (format->BitsPerPixel)
   227         {
   228         case 8:
   229           return DSPF_LUT8;
   230           
   231         case 16:
   232           if (format->Rmask == 0xF800 &&
   233               format->Gmask == 0x07E0 &&
   234               format->Bmask == 0x001F)
   235             return DSPF_RGB16;
   236           /* fall through */
   237           
   238         case 15:
   239           if (format->Rmask == 0x7C00 &&
   240               format->Gmask == 0x03E0 &&
   241               format->Bmask == 0x001F)
   242             return DSPF_RGB15;
   243           break;
   244           
   245         case 24:
   246           if (format->Rmask == 0xFF0000 &&
   247               format->Gmask == 0x00FF00 &&
   248               format->Bmask == 0x0000FF)
   249             return DSPF_RGB24;
   250           break;
   251 
   252         case 32:
   253           if (format->Rmask == 0xFF0000 &&
   254               format->Gmask == 0x00FF00 &&
   255               format->Bmask == 0x0000FF)
   256             {
   257               if (format->Amask == 0xFF000000)
   258                 return DSPF_ARGB;
   259               else
   260                 return DSPF_RGB32;
   261             }
   262           break;
   263         }
   264     }
   265   else
   266     {
   267       switch (format->BitsPerPixel)
   268 	{
   269         case 8:
   270           return DSPF_LUT8;
   271 	case 15:
   272 	  return DSPF_RGB15;
   273 	case 16:
   274 	  return DSPF_RGB16;
   275 	case 24:
   276 	  return DSPF_RGB24;
   277 	case 32:
   278 	  return DSPF_RGB32;
   279 	}
   280     }
   281 
   282   return DSPF_UNKNOWN;
   283 }
   284 
   285 static SDL_Palette *AllocatePalette(int size)
   286 {
   287   SDL_Palette *palette;
   288   SDL_Color   *colors;
   289 
   290   palette = calloc (1, sizeof(SDL_Palette));
   291   if (!palette)
   292     {
   293       SDL_OutOfMemory();
   294       return NULL;
   295     }
   296 
   297   colors = calloc (size, sizeof(SDL_Color));
   298   if (!colors)
   299     {
   300       SDL_OutOfMemory();
   301       return NULL;
   302     }
   303 
   304   palette->ncolors = size;
   305   palette->colors  = colors;
   306 
   307   return palette;
   308 }
   309 
   310 static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
   311 {
   312   format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
   313   format->BitsPerPixel = format->BytesPerPixel = 0;
   314 
   315   switch (pixelformat)
   316     {
   317     case DSPF_A8:
   318       format->Amask = 0x000000FF;
   319       break;
   320 
   321     case DSPF_RGB15:
   322       format->Rmask = 0x00007C00;
   323       format->Gmask = 0x000003E0;
   324       format->Bmask = 0x0000001F;
   325       break;
   326 
   327     case DSPF_RGB16:
   328       format->Rmask = 0x0000F800;
   329       format->Gmask = 0x000007E0;
   330       format->Bmask = 0x0000001F;
   331       break;
   332 
   333     case DSPF_ARGB:
   334       format->Amask = 0; /* apps don't seem to like that:  0xFF000000; */
   335       /* fall through */
   336     case DSPF_RGB24:
   337     case DSPF_RGB32:
   338       format->Rmask = 0x00FF0000;
   339       format->Gmask = 0x0000FF00;
   340       format->Bmask = 0x000000FF;
   341       break;
   342 
   343     case DSPF_LUT8:
   344       format->Rmask = 0x000000FF;
   345       format->Gmask = 0x000000FF;
   346       format->Bmask = 0x000000FF;
   347 
   348       if (!format->palette)
   349         format->palette = AllocatePalette(256);
   350       break;
   351 
   352     default:
   353       fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
   354       return -1;
   355     }
   356 
   357   format->BitsPerPixel  = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
   358   format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
   359 
   360   return 0;
   361 }
   362 
   363 
   364 int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
   365 {
   366   int                      i;
   367   DFBResult                ret;
   368   DFBCardCapabilities      caps;
   369   DFBDisplayLayerConfig    dlc;
   370   struct DirectFBEnumRect *rect;
   371   IDirectFB               *dfb    = NULL;
   372   IDirectFBDisplayLayer   *layer  = NULL;
   373   IDirectFBEventBuffer    *events = NULL;
   374 
   375 
   376   ret = DirectFBInit (NULL, NULL);
   377   if (ret)
   378     {
   379       SetDirectFBerror ("DirectFBInit", ret);
   380       goto error;
   381     }
   382 
   383   ret = DirectFBCreate (&dfb);
   384   if (ret)
   385     {
   386       SetDirectFBerror ("DirectFBCreate", ret);
   387       goto error;
   388     }
   389 
   390   ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
   391   if (ret)
   392     {
   393       SetDirectFBerror ("dfb->GetDisplayLayer", ret);
   394       goto error;
   395     }
   396 
   397   ret = dfb->CreateEventBuffer (dfb, DICAPS_ALL, &events);
   398   if (ret)
   399     {
   400       SetDirectFBerror ("dfb->CreateEventBuffer", ret);
   401       goto error;
   402     }
   403   
   404   layer->EnableCursor (layer, 1);
   405 
   406   /* Query layer configuration to determine the current mode and pixelformat */
   407   layer->GetConfiguration (layer, &dlc);
   408 
   409   /* If current format is not supported use LUT8 as the default */
   410   if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
   411     DFBToSDLPixelFormat (DSPF_LUT8, vformat);
   412 
   413   /* Enumerate the available fullscreen modes */
   414   ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
   415   if (ret)
   416     {
   417       SetDirectFBerror ("dfb->EnumVideoModes", ret);
   418       goto error;
   419     }
   420 
   421   HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
   422   if (!HIDDEN->modelist)
   423     {
   424       SDL_OutOfMemory();
   425       goto error;
   426     }
   427 
   428   for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
   429     {
   430       HIDDEN->modelist[i] = &rect->r;
   431     }
   432 
   433   HIDDEN->modelist[i] = NULL;
   434 
   435 
   436   /* Query card capabilities to get the video memory size */
   437   dfb->GetCardCapabilities (dfb, &caps);
   438 
   439   this->info.wm_available = 1;
   440   this->info.hw_available = 1;
   441   this->info.blit_hw      = 1;
   442   this->info.blit_hw_CC   = 1;
   443   this->info.blit_hw_A    = 1;
   444   this->info.blit_fill    = 1;
   445   this->info.video_mem    = caps.video_memory / 1024;
   446 
   447   HIDDEN->initialized = 1;
   448   HIDDEN->dfb         = dfb;
   449   HIDDEN->layer       = layer;
   450   HIDDEN->eventbuffer = events;
   451 
   452   return 0;
   453 
   454  error:
   455   if (events)
   456     events->Release (events);
   457   
   458   if (layer)
   459     layer->Release (layer);
   460 
   461   if (dfb)
   462     dfb->Release (dfb);
   463 
   464   return -1;
   465 }
   466 
   467 static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
   468 {
   469   if (flags & SDL_FULLSCREEN)
   470     return HIDDEN->modelist;
   471   else
   472     if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
   473       return (SDL_Rect**) -1;
   474 
   475   return NULL;
   476 }
   477 
   478 static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
   479 {
   480   DFBResult              ret;
   481   DFBSurfaceDescription  dsc;
   482   DFBSurfacePixelFormat  pixelformat;
   483   IDirectFBSurface      *surface;
   484 
   485   fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
   486            width, height, bpp, flags);
   487 
   488   flags |= SDL_FULLSCREEN;
   489 
   490   /* Release previous primary surface */
   491   if (current->hwdata && current->hwdata->surface)
   492     {
   493       current->hwdata->surface->Release (current->hwdata->surface);
   494       current->hwdata->surface = NULL;
   495 
   496       /* And its palette if present */
   497       if (current->hwdata->palette)
   498         {
   499           current->hwdata->palette->Release (current->hwdata->palette);
   500           current->hwdata->palette = NULL;
   501         }
   502     }
   503   else if (!current->hwdata)
   504     {
   505       /* Allocate the hardware acceleration data */
   506       current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
   507       if (!current->hwdata)
   508         {
   509           SDL_OutOfMemory();
   510           return NULL;
   511 	}
   512     }
   513 
   514   /* Set cooperative level depending on flag SDL_FULLSCREEN */
   515   if (flags & SDL_FULLSCREEN)
   516     {
   517       ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
   518       if (ret)
   519         {
   520           DirectFBError ("dfb->SetCooperativeLevel", ret);
   521           flags &= ~SDL_FULLSCREEN;
   522         }
   523     }
   524   else
   525     HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   526 
   527   /* Set video mode */
   528   ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   529   if (ret)
   530     {
   531       if (flags & SDL_FULLSCREEN)
   532         {
   533           flags &= ~SDL_FULLSCREEN;
   534           HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
   535           ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
   536         }
   537 
   538       if (ret)
   539         {
   540           SetDirectFBerror ("dfb->SetVideoMode", ret);
   541           return NULL;
   542         }
   543     }
   544 
   545   /* Create primary surface */
   546   dsc.flags       = DSDESC_CAPS | DSDESC_PIXELFORMAT;
   547   dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
   548   dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
   549 
   550   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   551   if (ret && (flags & SDL_DOUBLEBUF))
   552     {
   553       /* Try without double buffering */
   554       dsc.caps &= ~DSCAPS_FLIPPING;
   555       ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   556     }
   557   if (ret)
   558     {
   559       SetDirectFBerror ("dfb->CreateSurface", ret);
   560       return NULL;
   561     }
   562 
   563   current->w     = width;
   564   current->h     = height;
   565   current->flags = SDL_HWSURFACE | SDL_PREALLOC;
   566 
   567   if (flags & SDL_FULLSCREEN)
   568     {
   569       current->flags |= SDL_FULLSCREEN;
   570       this->UpdateRects = DirectFB_DirectUpdate;
   571     }
   572   else
   573     this->UpdateRects = DirectFB_WindowedUpdate;
   574 
   575   if (dsc.caps & DSCAPS_FLIPPING)
   576     current->flags |= SDL_DOUBLEBUF;
   577 
   578   surface->GetPixelFormat (surface, &pixelformat);
   579 
   580   DFBToSDLPixelFormat (pixelformat, current->format);
   581 
   582   /* Get the surface palette (if supported) */
   583   if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
   584     {
   585       surface->GetPalette (surface, &current->hwdata->palette);
   586 
   587       current->flags |= SDL_HWPALETTE;
   588     }
   589 
   590   current->hwdata->surface = surface;
   591 
   592   return current;
   593 }
   594 
   595 static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
   596 {
   597   DFBResult             ret;
   598   DFBSurfaceDescription dsc;
   599 
   600   /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
   601       surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
   602 
   603   if (surface->w < 8 || surface->h < 8)
   604     return -1;
   605 
   606   /* fill surface description */
   607   dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
   608   dsc.width  = surface->w;
   609   dsc.height = surface->h;
   610   dsc.caps   = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
   611 
   612   /* find the right pixelformat */
   613   dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
   614   if (dsc.pixelformat == DSPF_UNKNOWN)
   615     return -1;
   616 
   617   /* Allocate the hardware acceleration data */
   618   surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
   619   if (surface->hwdata == NULL)
   620     {
   621       SDL_OutOfMemory();
   622       return -1;
   623     }
   624 
   625   /* Create the surface */
   626   ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
   627   if (ret)
   628     {
   629       SetDirectFBerror ("dfb->CreateSurface", ret);
   630       free (surface->hwdata);
   631       surface->hwdata = NULL;
   632       return -1;
   633     }
   634 
   635   surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
   636 
   637   return 0;
   638 }
   639 
   640 static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
   641 {
   642   if (surface->hwdata && HIDDEN->initialized)
   643     {
   644       surface->hwdata->surface->Release (surface->hwdata->surface);
   645       free (surface->hwdata);
   646       surface->hwdata = NULL;
   647     }
   648 }
   649 
   650 static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
   651 {
   652   /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
   653       src->hwdata, dst->hwdata);*/
   654 
   655   if (!src->hwdata || !dst->hwdata)
   656     return 0;
   657 
   658   src->flags |= SDL_HWACCEL;
   659   src->map->hw_blit = DirectFB_HWAccelBlit;
   660 
   661   return 1;
   662 }
   663 
   664 static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
   665                                 SDL_Surface *dst, SDL_Rect *dstrect)
   666 {
   667   DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
   668 
   669   DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
   670   DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
   671 
   672   IDirectFBSurface *surface = dst->hwdata->surface;
   673 
   674   if (src->flags & SDL_SRCCOLORKEY)
   675     {
   676       flags |= DSBLIT_SRC_COLORKEY;
   677       DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
   678     }
   679 
   680   if (src->flags & SDL_SRCALPHA)
   681     {
   682       flags |= DSBLIT_BLEND_COLORALPHA;
   683       surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
   684     }
   685 
   686   surface->SetBlittingFlags (surface, flags);
   687 
   688   if (sr.w == dr.w && sr.h == dr.h)
   689     surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
   690   else
   691     surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
   692 
   693   return 0;
   694 }
   695 
   696 static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   697 {
   698   SDL_PixelFormat  *fmt     = dst->format;
   699   IDirectFBSurface *surface = dst->hwdata->surface;
   700 
   701   /* ugly */
   702   surface->SetColor (surface,
   703                      (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   704                      (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   705                      (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
   706   surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
   707 
   708   return 0;
   709 }
   710 
   711 static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
   712 {
   713   SDL_PixelFormat  *fmt     = src->format;
   714   IDirectFBSurface *surface = src->hwdata->surface;
   715 
   716   if (fmt->BitsPerPixel == 8)
   717     surface->SetSrcColorKeyIndex (surface, key);
   718   else
   719     /* ugly */
   720     surface->SetSrcColorKey (surface,
   721                              (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   722                              (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   723                              (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   724 
   725   return 0;
   726 }
   727 
   728 static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
   729 {
   730   return 0;
   731 }
   732 
   733 static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
   734 {
   735   return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
   736 }
   737 
   738 static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
   739 {
   740   DFBResult  ret;
   741   void      *data;
   742   int        pitch;
   743 
   744   ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
   745                                         DSLF_WRITE, &data, &pitch);
   746   if (ret)
   747     {
   748       SetDirectFBerror ("surface->Lock", ret);
   749       return -1;
   750     }
   751 
   752   surface->pixels = data;
   753   surface->pitch  = pitch;
   754 
   755   return 0;
   756 }
   757 
   758 static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
   759 {
   760   surface->hwdata->surface->Unlock (surface->hwdata->surface);
   761   surface->pixels = NULL;
   762 }
   763 
   764 static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
   765 {
   766 }
   767 
   768 static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
   769 {
   770   DFBRegion         region;
   771   int               i;
   772   int               region_valid = 0;
   773   IDirectFBSurface *surface = this->screen->hwdata->surface;
   774 
   775   for (i=0; i<numrects; ++i)
   776     {
   777       int x2, y2;
   778 
   779       if ( ! rects[i].w ) /* Clipped? */
   780         continue;
   781 
   782       x2 = rects[i].x + rects[i].w - 1;
   783       y2 = rects[i].y + rects[i].h - 1;
   784 
   785       if (region_valid)
   786         {
   787           if (rects[i].x < region.x1)
   788             region.x1 = rects[i].x;
   789 
   790           if (rects[i].y < region.y1)
   791             region.y1 = rects[i].y;
   792 
   793           if (x2 > region.x2)
   794             region.x2 = x2;
   795 
   796           if (y2 > region.y2)
   797             region.y2 = y2;
   798         }
   799       else
   800         {
   801             region.x1 = rects[i].x;
   802             region.y1 = rects[i].y;
   803             region.x2 = x2;
   804             region.y2 = y2;
   805 
   806             region_valid = 1;
   807         }
   808     }
   809 
   810   if (region_valid)
   811     surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
   812 }
   813 
   814 int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   815 {
   816   IDirectFBPalette *palette = this->screen->hwdata->palette;
   817 
   818   if (!palette)
   819     return 0;
   820 
   821   if (firstcolor > 255)
   822     return 0;
   823 
   824   if (firstcolor + ncolors > 256)
   825     ncolors = 256 - firstcolor;
   826 
   827   if (ncolors > 0)
   828     {
   829       int      i;
   830       DFBColor entries[ncolors];
   831 
   832       for (i=0; i<ncolors; i++)
   833         {
   834           entries[i].a = 0xff;
   835           entries[i].r = colors[i].r;
   836           entries[i].g = colors[i].g;
   837           entries[i].b = colors[i].b;
   838         }
   839 
   840       palette->SetEntries (palette, entries, ncolors, firstcolor);
   841     }
   842 
   843   return 1;
   844 }
   845 	
   846 void DirectFB_VideoQuit(_THIS)
   847 {
   848   struct DirectFBEnumRect *rect    = enumlist;
   849   IDirectFBSurface        *surface = this->screen->hwdata->surface;
   850   IDirectFBPalette        *palette = this->screen->hwdata->palette;
   851 
   852   if (palette)
   853     palette->Release (palette);
   854 
   855   if (surface)
   856     surface->Release (surface);
   857 
   858   this->screen->hwdata->surface = NULL;
   859   this->screen->hwdata->palette = NULL;
   860 
   861   if (HIDDEN->eventbuffer)
   862     {
   863       HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
   864       HIDDEN->eventbuffer = NULL;
   865     }
   866 
   867   if (HIDDEN->layer)
   868     {
   869       HIDDEN->layer->Release (HIDDEN->layer);
   870       HIDDEN->layer = NULL;
   871     }
   872 
   873   if (HIDDEN->dfb)
   874     {
   875       HIDDEN->dfb->Release (HIDDEN->dfb);
   876       HIDDEN->dfb = NULL;
   877     }
   878 
   879   /* Free video mode list */
   880   if (HIDDEN->modelist)
   881     {
   882       free (HIDDEN->modelist);
   883       HIDDEN->modelist = NULL;
   884     }
   885 
   886   /* Free mode enumeration list */
   887   while (rect)
   888     {
   889       struct DirectFBEnumRect *next = rect->next;
   890       free (rect);
   891       rect = next;
   892     }
   893   enumlist = NULL;
   894 
   895   HIDDEN->initialized = 0;
   896 }
   897 
   898 void DirectFB_FinalQuit(void) 
   899 {
   900 }