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