src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 04 Sep 2001 22:53:46 +0000
changeset 167 cb384ef627f6
child 219 f928da36f0e9
permissions -rw-r--r--
Added support for DirectFB video on Linux (thanks Denis!)
slouken@167
     1
/*
slouken@167
     2
	SDL - Simple DirectMedia Layer
slouken@167
     3
	Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@167
     4
slouken@167
     5
	This library is free software; you can redistribute it and/or
slouken@167
     6
	modify it under the terms of the GNU Library General Public
slouken@167
     7
	License as published by the Free Software Foundation; either
slouken@167
     8
	version 2 of the License, or (at your option) any later version.
slouken@167
     9
slouken@167
    10
	This library is distributed in the hope that it will be useful,
slouken@167
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@167
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@167
    13
	Library General Public License for more details.
slouken@167
    14
slouken@167
    15
	You should have received a copy of the GNU Library General Public
slouken@167
    16
	License along with this library; if not, write to the Free
slouken@167
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@167
    18
slouken@167
    19
	Sam Lantinga
slouken@167
    20
	slouken@devolution.com
slouken@167
    21
*/
slouken@167
    22
slouken@167
    23
#ifdef SAVE_RCSID
slouken@167
    24
static char rcsid =
slouken@167
    25
 "@(#) $Id$";
slouken@167
    26
#endif
slouken@167
    27
slouken@167
    28
/* DirectFB video driver implementation.
slouken@167
    29
*/
slouken@167
    30
slouken@167
    31
#include <stdlib.h>
slouken@167
    32
#include <stdio.h>
slouken@167
    33
#include <fcntl.h>
slouken@167
    34
#include <unistd.h>
slouken@167
    35
#include <sys/mman.h>
slouken@167
    36
slouken@167
    37
#include <directfb.h>
slouken@167
    38
slouken@167
    39
#include "SDL.h"
slouken@167
    40
#include "SDL_error.h"
slouken@167
    41
#include "SDL_video.h"
slouken@167
    42
#include "SDL_mouse.h"
slouken@167
    43
#include "SDL_sysvideo.h"
slouken@167
    44
#include "SDL_pixels_c.h"
slouken@167
    45
#include "SDL_events_c.h"
slouken@167
    46
#include "SDL_DirectFB_video.h"
slouken@167
    47
#include "SDL_DirectFB_events.h"
slouken@167
    48
slouken@167
    49
slouken@167
    50
/* Initialization/Query functions */
slouken@167
    51
static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@167
    52
static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
slouken@167
    53
static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@167
    54
static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
slouken@167
    55
			 SDL_Color *colors);
slouken@167
    56
static void DirectFB_VideoQuit(_THIS);
slouken@167
    57
slouken@167
    58
/* Hardware surface functions */
slouken@167
    59
static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    60
static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
slouken@167
    61
static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    62
static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    63
static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    64
static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
slouken@167
    65
static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@167
    66
                                SDL_Surface *dst, SDL_Rect *dstrect);
slouken@167
    67
static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
slouken@167
    68
static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
slouken@167
    69
static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    70
slouken@167
    71
/* Various screen update functions available */
slouken@167
    72
static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@167
    73
static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@167
    74
slouken@167
    75
/* This is the rect EnumModes2 uses */
slouken@167
    76
struct DirectFBEnumRect {
slouken@167
    77
	SDL_Rect r;
slouken@167
    78
	struct DirectFBEnumRect* next;
slouken@167
    79
};
slouken@167
    80
slouken@167
    81
static struct DirectFBEnumRect *enumlists[NUM_MODELISTS];
slouken@167
    82
slouken@167
    83
slouken@167
    84
/* DirectFB driver bootstrap functions */
slouken@167
    85
slouken@167
    86
static int DirectFB_Available(void)
slouken@167
    87
{
slouken@167
    88
  return 1;
slouken@167
    89
}
slouken@167
    90
slouken@167
    91
static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
slouken@167
    92
{
slouken@167
    93
  free(device->hidden);
slouken@167
    94
  free(device);
slouken@167
    95
}
slouken@167
    96
slouken@167
    97
static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
slouken@167
    98
{
slouken@167
    99
  SDL_VideoDevice *device;
slouken@167
   100
slouken@167
   101
  /* Initialize all variables that we clean on shutdown */
slouken@167
   102
  device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@167
   103
  if (device)
slouken@167
   104
    {
slouken@167
   105
      memset (device, 0, (sizeof *device));
slouken@167
   106
      device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
slouken@167
   107
    }
slouken@167
   108
  if (device == NULL  ||  device->hidden == NULL)
slouken@167
   109
    {
slouken@167
   110
      SDL_OutOfMemory();
slouken@167
   111
      if (device)
slouken@167
   112
        {
slouken@167
   113
          free (device);
slouken@167
   114
        }
slouken@167
   115
      return(0);
slouken@167
   116
    }
slouken@167
   117
  memset (device->hidden, 0, sizeof (*device->hidden));
slouken@167
   118
slouken@167
   119
  /* Set the function pointers */
slouken@167
   120
  device->VideoInit = DirectFB_VideoInit;
slouken@167
   121
  device->ListModes = DirectFB_ListModes;
slouken@167
   122
  device->SetVideoMode = DirectFB_SetVideoMode;
slouken@167
   123
  device->SetColors = DirectFB_SetColors;
slouken@167
   124
  device->UpdateRects = NULL;
slouken@167
   125
  device->VideoQuit = DirectFB_VideoQuit;
slouken@167
   126
  device->AllocHWSurface = DirectFB_AllocHWSurface;
slouken@167
   127
  device->CheckHWBlit = DirectFB_CheckHWBlit;
slouken@167
   128
  device->FillHWRect = DirectFB_FillHWRect;
slouken@167
   129
  device->SetHWColorKey = DirectFB_SetHWColorKey;
slouken@167
   130
  device->SetHWAlpha = DirectFB_SetHWAlpha;
slouken@167
   131
  device->LockHWSurface = DirectFB_LockHWSurface;
slouken@167
   132
  device->UnlockHWSurface = DirectFB_UnlockHWSurface;
slouken@167
   133
  device->FlipHWSurface = DirectFB_FlipHWSurface;
slouken@167
   134
  device->FreeHWSurface = DirectFB_FreeHWSurface;
slouken@167
   135
  device->SetCaption = NULL;
slouken@167
   136
  device->SetIcon = NULL;
slouken@167
   137
  device->IconifyWindow = NULL;
slouken@167
   138
  device->GrabInput = NULL;
slouken@167
   139
  device->GetWMInfo = NULL;
slouken@167
   140
  device->InitOSKeymap = DirectFB_InitOSKeymap;
slouken@167
   141
  device->PumpEvents = DirectFB_PumpEvents;
slouken@167
   142
slouken@167
   143
  device->free = DirectFB_DeleteDevice;
slouken@167
   144
slouken@167
   145
  return device;
slouken@167
   146
}
slouken@167
   147
slouken@167
   148
VideoBootStrap DirectFB_bootstrap = {
slouken@167
   149
  "directfb", "DirectFB",
slouken@167
   150
  DirectFB_Available, DirectFB_CreateDevice
slouken@167
   151
};
slouken@167
   152
slouken@167
   153
static DFBEnumerationResult EnumModesCallback (unsigned int  width,
slouken@167
   154
                                               unsigned int  height,
slouken@167
   155
                                               unsigned int  bpp,
slouken@167
   156
                                               void         *data)
slouken@167
   157
{
slouken@167
   158
  SDL_VideoDevice *this = (SDL_VideoDevice *)data;
slouken@167
   159
  struct DirectFBEnumRect *enumrect;
slouken@167
   160
slouken@167
   161
  switch (bpp)
slouken@167
   162
    {
slouken@167
   163
    case 8:
slouken@167
   164
    case 15:
slouken@167
   165
    case 16:
slouken@167
   166
    case 24:
slouken@167
   167
    case 32:
slouken@167
   168
      bpp /= 8; --bpp;
slouken@167
   169
      ++HIDDEN->SDL_nummodes[bpp];
slouken@167
   170
      enumrect = (struct DirectFBEnumRect*)malloc(sizeof(struct DirectFBEnumRect));
slouken@167
   171
      if ( !enumrect )
slouken@167
   172
        {
slouken@167
   173
          SDL_OutOfMemory();
slouken@167
   174
          return DFENUM_CANCEL;
slouken@167
   175
        }
slouken@167
   176
      enumrect->r.x = 0;
slouken@167
   177
      enumrect->r.y = 0;
slouken@167
   178
      enumrect->r.w = width;
slouken@167
   179
      enumrect->r.h = height;
slouken@167
   180
      enumrect->next = enumlists[bpp];
slouken@167
   181
      enumlists[bpp] = enumrect;
slouken@167
   182
      break;
slouken@167
   183
    }
slouken@167
   184
slouken@167
   185
  return DFENUM_OK;
slouken@167
   186
}
slouken@167
   187
slouken@167
   188
struct private_hwdata {
slouken@167
   189
  IDirectFBSurface *surface;
slouken@167
   190
};
slouken@167
   191
slouken@167
   192
void SetDirectFBerror (const char *function, DFBResult code)
slouken@167
   193
{
slouken@167
   194
  const char *error = DirectFBErrorString (code);
slouken@167
   195
slouken@167
   196
  if (error)
slouken@167
   197
    SDL_SetError("%s: %s", function, error);
slouken@167
   198
  else
slouken@167
   199
    SDL_SetError("Unknown error code from %s", function);
slouken@167
   200
}
slouken@167
   201
slouken@167
   202
static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
slouken@167
   203
{
slouken@167
   204
  if (format->Rmask && format->Gmask && format->Bmask)
slouken@167
   205
    {
slouken@167
   206
      switch (format->BitsPerPixel)
slouken@167
   207
        {
slouken@167
   208
        case 16:
slouken@167
   209
          if (format->Rmask == 0xF800 &&
slouken@167
   210
              format->Gmask == 0x07E0 &&
slouken@167
   211
              format->Bmask == 0x001F)
slouken@167
   212
            return DSPF_RGB16;
slouken@167
   213
          /* fall through */
slouken@167
   214
          
slouken@167
   215
        case 15:
slouken@167
   216
          if (format->Rmask == 0x7C00 &&
slouken@167
   217
              format->Gmask == 0x03E0 &&
slouken@167
   218
              format->Bmask == 0x001F)
slouken@167
   219
            return DSPF_RGB15;
slouken@167
   220
          break;
slouken@167
   221
          
slouken@167
   222
        case 24:
slouken@167
   223
          if (format->Rmask == 0xFF0000 &&
slouken@167
   224
              format->Gmask == 0x00FF00 &&
slouken@167
   225
              format->Bmask == 0x0000FF)
slouken@167
   226
            return DSPF_RGB24;
slouken@167
   227
          break;
slouken@167
   228
          
slouken@167
   229
        case 32:
slouken@167
   230
          if (format->Rmask == 0xFF0000 &&
slouken@167
   231
              format->Gmask == 0x00FF00 &&
slouken@167
   232
              format->Bmask == 0x0000FF)
slouken@167
   233
            {
slouken@167
   234
              if (format->Amask == 0xFF000000)
slouken@167
   235
                return DSPF_ARGB;
slouken@167
   236
              else
slouken@167
   237
                return DSPF_RGB32;
slouken@167
   238
            }
slouken@167
   239
          break;
slouken@167
   240
        }
slouken@167
   241
    }
slouken@167
   242
  else
slouken@167
   243
    {
slouken@167
   244
      switch (format->BitsPerPixel)
slouken@167
   245
	{
slouken@167
   246
	case 15:
slouken@167
   247
	  return DSPF_RGB15;
slouken@167
   248
	case 16:
slouken@167
   249
	  return DSPF_RGB16;
slouken@167
   250
	case 24:
slouken@167
   251
	  return DSPF_RGB24;
slouken@167
   252
	case 32:
slouken@167
   253
	  return DSPF_RGB32;
slouken@167
   254
	}
slouken@167
   255
    }
slouken@167
   256
slouken@167
   257
  return DSPF_UNKNOWN;
slouken@167
   258
}
slouken@167
   259
slouken@167
   260
static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
slouken@167
   261
{
slouken@167
   262
  format->BitsPerPixel = 0;
slouken@167
   263
  format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
slouken@167
   264
slouken@167
   265
  switch (pixelformat)
slouken@167
   266
    {
slouken@167
   267
    case DSPF_A8:
slouken@167
   268
      format->Amask = 0x000000FF;
slouken@167
   269
      break;
slouken@167
   270
    case DSPF_RGB15:
slouken@167
   271
      format->Rmask = 0x00007C00;
slouken@167
   272
      format->Gmask = 0x000003E0;
slouken@167
   273
      format->Bmask = 0x0000001F;
slouken@167
   274
      break;
slouken@167
   275
    case DSPF_RGB16:
slouken@167
   276
      format->Rmask = 0x0000F800;
slouken@167
   277
      format->Gmask = 0x000007E0;
slouken@167
   278
      format->Bmask = 0x0000001F;
slouken@167
   279
      break;
slouken@167
   280
    case DSPF_ARGB:
slouken@167
   281
      format->Amask = 0xFF000000;
slouken@167
   282
      /* fall through */
slouken@167
   283
    case DSPF_RGB24:
slouken@167
   284
    case DSPF_RGB32:
slouken@167
   285
      format->Rmask = 0x00FF0000;
slouken@167
   286
      format->Gmask = 0x0000FF00;
slouken@167
   287
      format->Bmask = 0x000000FF;
slouken@167
   288
      break;
slouken@167
   289
    default:
slouken@167
   290
      return -1;
slouken@167
   291
    }
slouken@167
   292
slouken@167
   293
  format->BitsPerPixel = BITS_PER_PIXEL(pixelformat);
slouken@167
   294
slouken@167
   295
  return 0;
slouken@167
   296
}
slouken@167
   297
slouken@167
   298
slouken@167
   299
int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@167
   300
{
slouken@167
   301
  int                    i, j;
slouken@167
   302
  DFBResult              ret;
slouken@167
   303
  IDirectFB             *dfb;
slouken@167
   304
  DFBCardCapabilities    caps;
slouken@167
   305
  IDirectFBDisplayLayer *layer;
slouken@167
   306
  DFBDisplayLayerConfig  dlc;
slouken@167
   307
  IDirectFBInputBuffer  *inputbuffer;
slouken@167
   308
slouken@167
   309
slouken@167
   310
  ret = DirectFBInit (NULL, NULL);
slouken@167
   311
  if (ret)
slouken@167
   312
    {
slouken@167
   313
      SetDirectFBerror ("DirectFBInit", ret);
slouken@167
   314
      return -1;
slouken@167
   315
    }
slouken@167
   316
slouken@167
   317
  ret = DirectFBCreate (&dfb);
slouken@167
   318
  if (ret)
slouken@167
   319
    {
slouken@167
   320
      SetDirectFBerror ("DirectFBCreate", ret);
slouken@167
   321
      return -1;
slouken@167
   322
    }
slouken@167
   323
slouken@167
   324
  ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
slouken@167
   325
  if (ret)
slouken@167
   326
    {
slouken@167
   327
      SetDirectFBerror ("dfb->GetDisplayLayer", ret);
slouken@167
   328
      dfb->Release (dfb);
slouken@167
   329
      return -1;
slouken@167
   330
    }
slouken@167
   331
slouken@167
   332
  ret = dfb->CreateInputBuffer (dfb, DICAPS_BUTTONS | DICAPS_AXIS | DICAPS_KEYS,
slouken@167
   333
                                &inputbuffer);
slouken@167
   334
  if (ret)
slouken@167
   335
    {
slouken@167
   336
      SetDirectFBerror ("dfb->CreateInputBuffer", ret);
slouken@167
   337
      layer->Release (layer);
slouken@167
   338
      dfb->Release (dfb);
slouken@167
   339
      return -1;
slouken@167
   340
    }
slouken@167
   341
  
slouken@167
   342
  layer->EnableCursor (layer, 1);
slouken@167
   343
slouken@167
   344
  /* Query layer configuration to determine the current mode and pixelformat */
slouken@167
   345
  layer->GetConfiguration (layer, &dlc);
slouken@167
   346
slouken@167
   347
  if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
slouken@167
   348
    {
slouken@167
   349
      SDL_SetError ("Unsupported pixelformat");
slouken@167
   350
      layer->Release (layer);
slouken@167
   351
      dfb->Release (dfb);
slouken@167
   352
      return -1;
slouken@167
   353
    }
slouken@167
   354
slouken@167
   355
  /* Enumerate the available fullscreen modes */
slouken@167
   356
  for ( i=0; i<NUM_MODELISTS; ++i )
slouken@167
   357
    enumlists[i] = NULL;
slouken@167
   358
slouken@167
   359
  ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
slouken@167
   360
  if (ret)
slouken@167
   361
    {
slouken@167
   362
      SetDirectFBerror ("dfb->EnumVideoModes", ret);
slouken@167
   363
      layer->Release (layer);
slouken@167
   364
      dfb->Release (dfb);
slouken@167
   365
      return(-1);
slouken@167
   366
    }
slouken@167
   367
  for ( i=0; i<NUM_MODELISTS; ++i )
slouken@167
   368
    {
slouken@167
   369
      struct DirectFBEnumRect *rect;
slouken@167
   370
      HIDDEN->SDL_modelist[i] = (SDL_Rect **) malloc
slouken@167
   371
        ((HIDDEN->SDL_nummodes[i]+1)*sizeof(SDL_Rect *));
slouken@167
   372
      if ( HIDDEN->SDL_modelist[i] == NULL )
slouken@167
   373
        {
slouken@167
   374
          SDL_OutOfMemory();
slouken@167
   375
          return(-1);
slouken@167
   376
        }
slouken@167
   377
      for ( j = 0, rect = enumlists[i]; rect; ++j, rect = rect->next )
slouken@167
   378
        {
slouken@167
   379
          HIDDEN->SDL_modelist[i][j]=(SDL_Rect *)rect;
slouken@167
   380
        }
slouken@167
   381
      HIDDEN->SDL_modelist[i][j] = NULL;
slouken@167
   382
    }
slouken@167
   383
slouken@167
   384
  /* Query card capabilities to get the video memory size */
slouken@167
   385
  dfb->GetCardCapabilities (dfb, &caps);
slouken@167
   386
slouken@167
   387
  this->info.wm_available = 1;
slouken@167
   388
  this->info.hw_available = 1;
slouken@167
   389
  this->info.blit_hw      = 1;
slouken@167
   390
  this->info.blit_hw_CC   = 1;
slouken@167
   391
  this->info.blit_hw_A    = 1;
slouken@167
   392
  this->info.blit_fill    = 1;
slouken@167
   393
  this->info.video_mem    = caps.video_memory / 1024;
slouken@167
   394
slouken@167
   395
  HIDDEN->initialized = 1;
slouken@167
   396
  HIDDEN->dfb         = dfb;
slouken@167
   397
  HIDDEN->layer       = layer;
slouken@167
   398
  HIDDEN->inputbuffer = inputbuffer;
slouken@167
   399
slouken@167
   400
  return 0;
slouken@167
   401
}
slouken@167
   402
slouken@167
   403
static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@167
   404
{
slouken@167
   405
  if (flags & SDL_FULLSCREEN)
slouken@167
   406
    return HIDDEN->SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1];
slouken@167
   407
  else
slouken@167
   408
    if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
slouken@167
   409
      return (SDL_Rect**) -1;
slouken@167
   410
slouken@167
   411
  return NULL;
slouken@167
   412
}
slouken@167
   413
slouken@167
   414
SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
slouken@167
   415
{
slouken@167
   416
  DFBResult             ret;
slouken@167
   417
  DFBSurfaceDescription dsc;
slouken@167
   418
  DFBSurfacePixelFormat pixelformat;
slouken@167
   419
slouken@167
   420
  fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
slouken@167
   421
           width, height, bpp, flags);
slouken@167
   422
slouken@167
   423
  flags |= SDL_FULLSCREEN;
slouken@167
   424
slouken@167
   425
  /* Release previous primary surface */
slouken@167
   426
  if (current->hwdata && current->hwdata->surface)
slouken@167
   427
    {
slouken@167
   428
      current->hwdata->surface->Release (current->hwdata->surface);
slouken@167
   429
      current->hwdata->surface = NULL;
slouken@167
   430
    }
slouken@167
   431
  else if (!current->hwdata)
slouken@167
   432
    {
slouken@167
   433
      /* Allocate the hardware acceleration data */
slouken@167
   434
      current->hwdata = (struct private_hwdata *) malloc (sizeof(*current->hwdata));
slouken@167
   435
      if (!current->hwdata)
slouken@167
   436
        {
slouken@167
   437
          SDL_OutOfMemory();
slouken@167
   438
          return NULL;
slouken@167
   439
	}
slouken@167
   440
      memset (current->hwdata, 0, sizeof(*current->hwdata));
slouken@167
   441
    }
slouken@167
   442
slouken@167
   443
  /* Set cooperative level depending on flag SDL_FULLSCREEN */
slouken@167
   444
  if (flags & SDL_FULLSCREEN)
slouken@167
   445
    {
slouken@167
   446
      ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
slouken@167
   447
      if (ret)
slouken@167
   448
        {
slouken@167
   449
          DirectFBError ("dfb->SetCooperativeLevel", ret);
slouken@167
   450
          flags &= ~SDL_FULLSCREEN;
slouken@167
   451
        }
slouken@167
   452
    }
slouken@167
   453
  else
slouken@167
   454
    HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
slouken@167
   455
slouken@167
   456
  /* Set video mode */
slouken@167
   457
  ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
slouken@167
   458
  if (ret)
slouken@167
   459
    {
slouken@167
   460
      if (flags & SDL_FULLSCREEN)
slouken@167
   461
        {
slouken@167
   462
          flags &= ~SDL_FULLSCREEN;
slouken@167
   463
          HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
slouken@167
   464
          ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
slouken@167
   465
        }
slouken@167
   466
slouken@167
   467
      if (ret)
slouken@167
   468
        {
slouken@167
   469
          SetDirectFBerror ("dfb->SetVideoMode", ret);
slouken@167
   470
          return NULL;
slouken@167
   471
        }
slouken@167
   472
    }
slouken@167
   473
slouken@167
   474
  /* Create primary surface */
slouken@167
   475
  dsc.flags = DSDESC_CAPS;
slouken@167
   476
  dsc.caps  = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
slouken@167
   477
slouken@167
   478
  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
slouken@167
   479
  if (ret && (flags & SDL_DOUBLEBUF))
slouken@167
   480
    {
slouken@167
   481
      /* Try without double buffering */
slouken@167
   482
      dsc.caps &= ~DSCAPS_FLIPPING;
slouken@167
   483
      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
slouken@167
   484
    }
slouken@167
   485
  if (ret)
slouken@167
   486
    {
slouken@167
   487
      SetDirectFBerror ("dfb->CreateSurface", ret);
slouken@167
   488
      current->hwdata->surface = NULL;
slouken@167
   489
      return NULL;
slouken@167
   490
    }
slouken@167
   491
slouken@167
   492
  current->w     = width;
slouken@167
   493
  current->h     = height;
slouken@167
   494
  current->flags = SDL_HWSURFACE | SDL_PREALLOC;
slouken@167
   495
slouken@167
   496
  if (flags & SDL_FULLSCREEN)
slouken@167
   497
    {
slouken@167
   498
      current->flags |= SDL_FULLSCREEN;
slouken@167
   499
      this->UpdateRects = DirectFB_DirectUpdate;
slouken@167
   500
    }
slouken@167
   501
  else
slouken@167
   502
    this->UpdateRects = DirectFB_WindowedUpdate;
slouken@167
   503
slouken@167
   504
  if (dsc.caps & DSCAPS_FLIPPING)
slouken@167
   505
    current->flags |= SDL_DOUBLEBUF;
slouken@167
   506
slouken@167
   507
  current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
slouken@167
   508
  DFBToSDLPixelFormat (pixelformat, current->format);
slouken@167
   509
slouken@167
   510
  return current;
slouken@167
   511
}
slouken@167
   512
slouken@167
   513
static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   514
{
slouken@167
   515
  DFBResult             ret;
slouken@167
   516
  DFBSurfaceDescription dsc;
slouken@167
   517
slouken@167
   518
  /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
slouken@167
   519
      surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
slouken@167
   520
slouken@167
   521
  if (surface->w < 8 || surface->h < 8)
slouken@167
   522
    return -1;
slouken@167
   523
slouken@167
   524
  /* fill surface description */
slouken@167
   525
  dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
slouken@167
   526
  dsc.width  = surface->w;
slouken@167
   527
  dsc.height = surface->h;
slouken@167
   528
  dsc.caps   = surface->flags & SDL_DOUBLEBUF ? DSCAPS_FLIPPING : 0;
slouken@167
   529
slouken@167
   530
  /* find the right pixelformat */
slouken@167
   531
  dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
slouken@167
   532
  if (dsc.pixelformat == DSPF_UNKNOWN)
slouken@167
   533
    return -1;
slouken@167
   534
slouken@167
   535
  /* Allocate the hardware acceleration data */
slouken@167
   536
  surface->hwdata = (struct private_hwdata *) malloc (sizeof(*surface->hwdata));
slouken@167
   537
  if (surface->hwdata == NULL)
slouken@167
   538
    {
slouken@167
   539
      SDL_OutOfMemory();
slouken@167
   540
      return -1;
slouken@167
   541
    }
slouken@167
   542
slouken@167
   543
  /* Create the surface */
slouken@167
   544
  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
slouken@167
   545
  if (ret)
slouken@167
   546
    {
slouken@167
   547
      SetDirectFBerror ("dfb->CreateSurface", ret);
slouken@167
   548
      free (surface->hwdata);
slouken@167
   549
      surface->hwdata = NULL;
slouken@167
   550
      return -1;
slouken@167
   551
    }
slouken@167
   552
slouken@167
   553
  surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
slouken@167
   554
slouken@167
   555
  return 0;
slouken@167
   556
}
slouken@167
   557
slouken@167
   558
static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   559
{
slouken@167
   560
  if (surface->hwdata && HIDDEN->initialized)
slouken@167
   561
    {
slouken@167
   562
      surface->hwdata->surface->Release (surface->hwdata->surface);
slouken@167
   563
      free (surface->hwdata);
slouken@167
   564
      surface->hwdata = NULL;
slouken@167
   565
    }
slouken@167
   566
}
slouken@167
   567
slouken@167
   568
static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
slouken@167
   569
{
slouken@167
   570
  /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
slouken@167
   571
      src->hwdata, dst->hwdata);*/
slouken@167
   572
slouken@167
   573
  if (!src->hwdata || !dst->hwdata)
slouken@167
   574
    return 0;
slouken@167
   575
slouken@167
   576
  src->flags |= SDL_HWACCEL;
slouken@167
   577
  src->map->hw_blit = DirectFB_HWAccelBlit;
slouken@167
   578
slouken@167
   579
  return 1;
slouken@167
   580
}
slouken@167
   581
slouken@167
   582
static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@167
   583
                                SDL_Surface *dst, SDL_Rect *dstrect)
slouken@167
   584
{
slouken@167
   585
  DFBRectangle             sr, dr;
slouken@167
   586
  IDirectFBSurface        *surface;
slouken@167
   587
  DFBSurfaceBlittingFlags  flags = DSBLIT_NOFX;
slouken@167
   588
slouken@167
   589
  sr.x = srcrect->x;
slouken@167
   590
  sr.y = srcrect->y;
slouken@167
   591
  sr.w = srcrect->w;
slouken@167
   592
  sr.h = srcrect->h;
slouken@167
   593
slouken@167
   594
  dr.x = dstrect->x;
slouken@167
   595
  dr.y = dstrect->y;
slouken@167
   596
  dr.w = dstrect->w;
slouken@167
   597
  dr.h = dstrect->h;
slouken@167
   598
slouken@167
   599
  surface = dst->hwdata->surface;
slouken@167
   600
slouken@167
   601
  if (src->flags & SDL_SRCCOLORKEY)
slouken@167
   602
    {
slouken@167
   603
      flags |= DSBLIT_SRC_COLORKEY;
slouken@167
   604
      surface->SetSrcColorKey (surface, src->format->colorkey);
slouken@167
   605
    }
slouken@167
   606
slouken@167
   607
  if (src->flags & SDL_SRCALPHA)
slouken@167
   608
    {
slouken@167
   609
      flags |= DSBLIT_BLEND_COLORALPHA;
slouken@167
   610
      surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
slouken@167
   611
    }
slouken@167
   612
slouken@167
   613
  surface->SetBlittingFlags (surface, flags);
slouken@167
   614
slouken@167
   615
  if (sr.w == dr.w && sr.h == dr.h)
slouken@167
   616
    surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
slouken@167
   617
  else
slouken@167
   618
    surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
slouken@167
   619
slouken@167
   620
  return 0;
slouken@167
   621
}
slouken@167
   622
slouken@167
   623
static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
slouken@167
   624
{
slouken@167
   625
  SDL_PixelFormat  *fmt     = dst->format;
slouken@167
   626
  IDirectFBSurface *surface = dst->hwdata->surface;
slouken@167
   627
slouken@167
   628
  /* ugly */
slouken@167
   629
  surface->SetColor (surface,
slouken@167
   630
                     (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
slouken@167
   631
                     (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
slouken@167
   632
                     (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
slouken@167
   633
  surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
slouken@167
   634
slouken@167
   635
  return 0;
slouken@167
   636
}
slouken@167
   637
slouken@167
   638
static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key)
slouken@167
   639
{
slouken@167
   640
  return 0;
slouken@167
   641
}
slouken@167
   642
slouken@167
   643
static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
slouken@167
   644
{
slouken@167
   645
  return 0;
slouken@167
   646
}
slouken@167
   647
slouken@167
   648
static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   649
{
slouken@167
   650
  return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
slouken@167
   651
}
slouken@167
   652
slouken@167
   653
static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   654
{
slouken@167
   655
  DFBResult  ret;
slouken@167
   656
  void      *data;
slouken@167
   657
  int        pitch;
slouken@167
   658
slouken@167
   659
  ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
slouken@167
   660
                                        DSLF_WRITE, &data, &pitch);
slouken@167
   661
  if (ret)
slouken@167
   662
    {
slouken@167
   663
      SetDirectFBerror ("surface->Lock", ret);
slouken@167
   664
      return -1;
slouken@167
   665
    }
slouken@167
   666
slouken@167
   667
  surface->pixels = data;
slouken@167
   668
  surface->pitch  = pitch;
slouken@167
   669
slouken@167
   670
  return 0;
slouken@167
   671
}
slouken@167
   672
slouken@167
   673
static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   674
{
slouken@167
   675
  surface->hwdata->surface->Unlock (surface->hwdata->surface);
slouken@167
   676
  surface->pixels = NULL;
slouken@167
   677
}
slouken@167
   678
slouken@167
   679
static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@167
   680
{
slouken@167
   681
}
slouken@167
   682
slouken@167
   683
static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@167
   684
{
slouken@167
   685
  IDirectFBSurface *surface = this->screen->hwdata->surface;
slouken@167
   686
  DFBRegion region = { rects->x, rects->y,
slouken@167
   687
                       rects->x + rects->w - 1,
slouken@167
   688
                       rects->y + rects->h - 1 };
slouken@167
   689
slouken@167
   690
  while (--numrects)
slouken@167
   691
    {
slouken@167
   692
      int x2, y2;
slouken@167
   693
slouken@167
   694
      rects++;
slouken@167
   695
slouken@167
   696
      if (rects->x < region.x1)
slouken@167
   697
        region.x1 = rects->x;
slouken@167
   698
slouken@167
   699
      if (rects->y < region.y1)
slouken@167
   700
        region.y1 = rects->y;
slouken@167
   701
slouken@167
   702
      x2 = rects->x + rects->w - 1;
slouken@167
   703
      y2 = rects->y + rects->h - 1;
slouken@167
   704
slouken@167
   705
      if (x2 > region.x2)
slouken@167
   706
        region.x2 = x2;
slouken@167
   707
slouken@167
   708
      if (y2 > region.y2)
slouken@167
   709
        region.y2 = y2;
slouken@167
   710
    }
slouken@167
   711
slouken@167
   712
  surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
slouken@167
   713
}
slouken@167
   714
slouken@167
   715
int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@167
   716
{
slouken@167
   717
  fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
slouken@167
   718
  return 0;
slouken@167
   719
}
slouken@167
   720
	
slouken@167
   721
void DirectFB_VideoQuit(_THIS)
slouken@167
   722
{
slouken@167
   723
  int i, j;
slouken@167
   724
slouken@167
   725
  HIDDEN->inputbuffer->Release (HIDDEN->inputbuffer);
slouken@167
   726
  HIDDEN->layer->Release (HIDDEN->layer);
slouken@167
   727
  HIDDEN->dfb->Release (HIDDEN->dfb);
slouken@167
   728
slouken@167
   729
  /* Free video mode lists */
slouken@167
   730
  for ( i=0; i<NUM_MODELISTS; ++i )
slouken@167
   731
    {
slouken@167
   732
      if ( HIDDEN->SDL_modelist[i] != NULL )
slouken@167
   733
        {
slouken@167
   734
          for ( j=0; HIDDEN->SDL_modelist[i][j]; ++j )
slouken@167
   735
            free(HIDDEN->SDL_modelist[i][j]);
slouken@167
   736
          free(HIDDEN->SDL_modelist[i]);
slouken@167
   737
          HIDDEN->SDL_modelist[i] = NULL;
slouken@167
   738
        }
slouken@167
   739
    }
slouken@167
   740
slouken@167
   741
  HIDDEN->initialized = 0;
slouken@167
   742
}
slouken@167
   743
slouken@167
   744
void DirectFB_FinalQuit(void) 
slouken@167
   745
{
slouken@167
   746
}