src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga
Sat, 31 Aug 2002 04:13:28 +0000
changeset 478 f8482d7c9595
parent 477 22581630aab7
child 546 0009aadb3d01
permissions -rw-r--r--
Date: Tue, 27 Aug 2002 19:07:38 +0200
From: Denis Oliver Kropp
Subject: YUV support

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