Date: Tue, 27 Aug 2002 16:14:11 +0200
authorSam Lantinga <slouken@libsdl.org>
Sat, 31 Aug 2002 04:06:37 +0000
changeset 47722581630aab7
parent 476 a7129c0083f4
child 478 f8482d7c9595
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.
configure.in
src/video/directfb/SDL_DirectFB_video.c
     1.1 --- a/configure.in	Sat Aug 31 04:01:19 2002 +0000
     1.2 +++ b/configure.in	Sat Aug 31 04:06:37 2002 +0000
     1.3 @@ -779,11 +779,11 @@
     1.4          else
     1.5              AC_MSG_CHECKING(for DirectFB support)
     1.6  
     1.7 -            if ! $PKG_CONFIG --atleast-pkgconfig-version 0.5 ; then
     1.8 -                AC_MSG_ERROR([*** pkg-config too old; version 0.5 or better required.])
     1.9 +            if ! $PKG_CONFIG --atleast-pkgconfig-version 0.7 ; then
    1.10 +                AC_MSG_ERROR([*** pkg-config too old; version 0.7 or better required.])
    1.11              fi
    1.12  
    1.13 -            DIRECTFB_REQUIRED_VERSION=0.9.11
    1.14 +            DIRECTFB_REQUIRED_VERSION=0.9.14
    1.15  
    1.16              if $PKG_CONFIG --atleast-version $DIRECTFB_REQUIRED_VERSION directfb ; then
    1.17                  DIRECTFB_CFLAGS=`$PKG_CONFIG --cflags directfb`
     2.1 --- a/src/video/directfb/SDL_DirectFB_video.c	Sat Aug 31 04:01:19 2002 +0000
     2.2 +++ b/src/video/directfb/SDL_DirectFB_video.c	Sat Aug 31 04:06:37 2002 +0000
     2.3 @@ -158,13 +158,13 @@
     2.4  
     2.5    layer->GetConfiguration (layer, &dlc);
     2.6  
     2.7 -  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat))
     2.8 +  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
     2.9      return dlc.pixelformat;
    2.10  
    2.11    switch (bytes)
    2.12      {
    2.13      case 1:
    2.14 -      return DSPF_RGB332;
    2.15 +      return DSPF_LUT8;
    2.16      case 2:
    2.17        return DSPF_RGB16;
    2.18      case 3:
    2.19 @@ -204,6 +204,7 @@
    2.20  
    2.21  struct private_hwdata {
    2.22    IDirectFBSurface *surface;
    2.23 +  IDirectFBPalette *palette;
    2.24  };
    2.25  
    2.26  void SetDirectFBerror (const char *function, DFBResult code)
    2.27 @@ -222,6 +223,9 @@
    2.28      {
    2.29        switch (format->BitsPerPixel)
    2.30          {
    2.31 +        case 8:
    2.32 +          return DSPF_LUT8;
    2.33 +          
    2.34          case 16:
    2.35            if (format->Rmask == 0xF800 &&
    2.36                format->Gmask == 0x07E0 &&
    2.37 @@ -236,13 +240,6 @@
    2.38              return DSPF_RGB15;
    2.39            break;
    2.40            
    2.41 -        case 8:
    2.42 -          if (format->Rmask == 0xE0 &&
    2.43 -              format->Gmask == 0x1C &&
    2.44 -              format->Bmask == 0x03)
    2.45 -            return DSPF_RGB332;
    2.46 -          break;
    2.47 -          
    2.48          case 24:
    2.49            if (format->Rmask == 0xFF0000 &&
    2.50                format->Gmask == 0x00FF00 &&
    2.51 @@ -268,7 +265,7 @@
    2.52        switch (format->BitsPerPixel)
    2.53  	{
    2.54          case 8:
    2.55 -          return DSPF_RGB332;
    2.56 +          return DSPF_LUT8;
    2.57  	case 15:
    2.58  	  return DSPF_RGB15;
    2.59  	case 16:
    2.60 @@ -283,12 +280,8 @@
    2.61    return DSPF_UNKNOWN;
    2.62  }
    2.63  
    2.64 -static const __u8 lookup3to8[] = { 0x00, 0x24, 0x49, 0x6d, 0x92, 0xb6, 0xdb, 0xff };
    2.65 -static const __u8 lookup2to8[] = { 0x00, 0x55, 0xaa, 0xff };
    2.66 -
    2.67 -static SDL_Palette *GenerateRGB332Palette()
    2.68 +static SDL_Palette *AllocatePalette(int size)
    2.69  {
    2.70 -  int          i;
    2.71    SDL_Palette *palette;
    2.72    SDL_Color   *colors;
    2.73  
    2.74 @@ -299,21 +292,14 @@
    2.75        return NULL;
    2.76      }
    2.77  
    2.78 -  colors = calloc (256, sizeof(SDL_Color));
    2.79 +  colors = calloc (size, sizeof(SDL_Color));
    2.80    if (!colors)
    2.81      {
    2.82        SDL_OutOfMemory();
    2.83        return NULL;
    2.84      }
    2.85  
    2.86 -  for (i=0; i<256; i++)
    2.87 -    {
    2.88 -      colors[i].r = lookup3to8[ i >> 5 ];
    2.89 -      colors[i].g = lookup3to8[ (i >> 2) & 7 ];
    2.90 -      colors[i].g = lookup2to8[ i & 3 ];
    2.91 -    }
    2.92 -
    2.93 -  palette->ncolors = 256;
    2.94 +  palette->ncolors = size;
    2.95    palette->colors  = colors;
    2.96  
    2.97    return palette;
    2.98 @@ -352,15 +338,17 @@
    2.99        format->Bmask = 0x000000FF;
   2.100        break;
   2.101  
   2.102 -    case DSPF_RGB332:
   2.103 -      format->Rmask = 0x000000E0;
   2.104 -      format->Gmask = 0x0000001C;
   2.105 -      format->Bmask = 0x00000003;
   2.106 +    case DSPF_LUT8:
   2.107 +      format->Rmask = 0x000000FF;
   2.108 +      format->Gmask = 0x000000FF;
   2.109 +      format->Bmask = 0x000000FF;
   2.110  
   2.111 -      format->palette = GenerateRGB332Palette();
   2.112 +      if (!format->palette)
   2.113 +        format->palette = AllocatePalette(256);
   2.114        break;
   2.115  
   2.116      default:
   2.117 +      fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
   2.118        return -1;
   2.119      }
   2.120  
   2.121 @@ -377,7 +365,6 @@
   2.122    DFBResult                ret;
   2.123    DFBCardCapabilities      caps;
   2.124    DFBDisplayLayerConfig    dlc;
   2.125 -  DFBSurfacePixelFormat    format;
   2.126    struct DirectFBEnumRect *rect;
   2.127    IDirectFB               *dfb    = NULL;
   2.128    IDirectFBDisplayLayer   *layer  = NULL;
   2.129 @@ -417,15 +404,9 @@
   2.130    /* Query layer configuration to determine the current mode and pixelformat */
   2.131    layer->GetConfiguration (layer, &dlc);
   2.132  
   2.133 -  /* FIXME: Returning RGB332 as the default mode doesn't work (everything is black) */
   2.134 -  if ((format = dlc.pixelformat) == DSPF_RGB332)
   2.135 -    format = DSPF_RGB16;
   2.136 -
   2.137 -  if (DFBToSDLPixelFormat (format, vformat))
   2.138 -    {
   2.139 -      SDL_SetError ("Unsupported pixelformat");
   2.140 -      goto error;
   2.141 -    }
   2.142 +  /* If current format is not supported use LUT8 as the default */
   2.143 +  if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
   2.144 +    DFBToSDLPixelFormat (DSPF_LUT8, vformat);
   2.145  
   2.146    /* Enumerate the available fullscreen modes */
   2.147    ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
   2.148 @@ -494,9 +475,10 @@
   2.149  
   2.150  static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
   2.151  {
   2.152 -  DFBResult             ret;
   2.153 -  DFBSurfaceDescription dsc;
   2.154 -  DFBSurfacePixelFormat pixelformat;
   2.155 +  DFBResult              ret;
   2.156 +  DFBSurfaceDescription  dsc;
   2.157 +  DFBSurfacePixelFormat  pixelformat;
   2.158 +  IDirectFBSurface      *surface;
   2.159  
   2.160    fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
   2.161             width, height, bpp, flags);
   2.162 @@ -508,6 +490,13 @@
   2.163      {
   2.164        current->hwdata->surface->Release (current->hwdata->surface);
   2.165        current->hwdata->surface = NULL;
   2.166 +
   2.167 +      /* And its palette if present */
   2.168 +      if (current->hwdata->palette)
   2.169 +        {
   2.170 +          current->hwdata->palette->Release (current->hwdata->palette);
   2.171 +          current->hwdata->palette = NULL;
   2.172 +        }
   2.173      }
   2.174    else if (!current->hwdata)
   2.175      {
   2.176 @@ -556,17 +545,16 @@
   2.177    dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
   2.178    dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
   2.179  
   2.180 -  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
   2.181 +  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   2.182    if (ret && (flags & SDL_DOUBLEBUF))
   2.183      {
   2.184        /* Try without double buffering */
   2.185        dsc.caps &= ~DSCAPS_FLIPPING;
   2.186 -      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
   2.187 +      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   2.188      }
   2.189    if (ret)
   2.190      {
   2.191        SetDirectFBerror ("dfb->CreateSurface", ret);
   2.192 -      current->hwdata->surface = NULL;
   2.193        return NULL;
   2.194      }
   2.195  
   2.196 @@ -585,9 +573,20 @@
   2.197    if (dsc.caps & DSCAPS_FLIPPING)
   2.198      current->flags |= SDL_DOUBLEBUF;
   2.199  
   2.200 -  current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
   2.201 +  surface->GetPixelFormat (surface, &pixelformat);
   2.202 +
   2.203    DFBToSDLPixelFormat (pixelformat, current->format);
   2.204  
   2.205 +  /* Get the surface palette (if supported) */
   2.206 +  if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
   2.207 +    {
   2.208 +      surface->GetPalette (surface, &current->hwdata->palette);
   2.209 +
   2.210 +      current->flags |= SDL_HWPALETTE;
   2.211 +    }
   2.212 +
   2.213 +  current->hwdata->surface = surface;
   2.214 +
   2.215    return current;
   2.216  }
   2.217  
   2.218 @@ -712,11 +711,14 @@
   2.219    SDL_PixelFormat  *fmt     = src->format;
   2.220    IDirectFBSurface *surface = src->hwdata->surface;
   2.221  
   2.222 -  /* ugly */
   2.223 -  surface->SetSrcColorKey (surface,
   2.224 -                           (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   2.225 -                           (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   2.226 -                           (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   2.227 +  if (fmt->BitsPerPixel == 8)
   2.228 +    surface->SetSrcColorKeyIndex (surface, key);
   2.229 +  else
   2.230 +    /* ugly */
   2.231 +    surface->SetSrcColorKey (surface,
   2.232 +                             (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   2.233 +                             (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   2.234 +                             (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   2.235  
   2.236    return 0;
   2.237  }
   2.238 @@ -809,13 +811,50 @@
   2.239  
   2.240  int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   2.241  {
   2.242 -  fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
   2.243 -  return -1;
   2.244 +  IDirectFBPalette *palette = this->screen->hwdata->palette;
   2.245 +
   2.246 +  if (!palette)
   2.247 +    return 0;
   2.248 +
   2.249 +  if (firstcolor > 255)
   2.250 +    return 0;
   2.251 +
   2.252 +  if (firstcolor + ncolors > 256)
   2.253 +    ncolors = 256 - firstcolor;
   2.254 +
   2.255 +  if (ncolors > 0)
   2.256 +    {
   2.257 +      int      i;
   2.258 +      DFBColor entries[ncolors];
   2.259 +
   2.260 +      for (i=0; i<ncolors; i++)
   2.261 +        {
   2.262 +          entries[i].a = 0xff;
   2.263 +          entries[i].r = colors[i].r;
   2.264 +          entries[i].g = colors[i].g;
   2.265 +          entries[i].b = colors[i].b;
   2.266 +        }
   2.267 +
   2.268 +      palette->SetEntries (palette, entries, ncolors, firstcolor);
   2.269 +    }
   2.270 +
   2.271 +  return 1;
   2.272  }
   2.273  	
   2.274  void DirectFB_VideoQuit(_THIS)
   2.275  {
   2.276 -  struct DirectFBEnumRect *rect = enumlist;
   2.277 +  struct DirectFBEnumRect *rect    = enumlist;
   2.278 +  IDirectFBSurface        *surface = this->screen->hwdata->surface;
   2.279 +  IDirectFBPalette        *palette = this->screen->hwdata->palette;
   2.280 +
   2.281 +  if (palette)
   2.282 +    palette->Release (palette);
   2.283 +
   2.284 +  if (surface)
   2.285 +    surface->Release (surface);
   2.286 +
   2.287 +  this->screen->hwdata->surface = NULL;
   2.288 +  this->screen->hwdata->palette = NULL;
   2.289  
   2.290    if (HIDDEN->eventbuffer)
   2.291      {