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