src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Aug 2002 04:06:37 +0000
changeset 477 22581630aab7
parent 464 1c4be4a16410
child 478 f8482d7c9595
permissions -rw-r--r--
Date: Tue, 27 Aug 2002 16:14:11 +0200
From: Denis Oliver Kropp
Subject: Palette support and 8bit color keying fix

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