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