src/video/directfb/SDL_DirectFB_video.c
changeset 477 22581630aab7
parent 464 1c4be4a16410
child 478 f8482d7c9595
     1.1 --- a/src/video/directfb/SDL_DirectFB_video.c	Sat Aug 31 04:01:19 2002 +0000
     1.2 +++ b/src/video/directfb/SDL_DirectFB_video.c	Sat Aug 31 04:06:37 2002 +0000
     1.3 @@ -158,13 +158,13 @@
     1.4  
     1.5    layer->GetConfiguration (layer, &dlc);
     1.6  
     1.7 -  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat))
     1.8 +  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
     1.9      return dlc.pixelformat;
    1.10  
    1.11    switch (bytes)
    1.12      {
    1.13      case 1:
    1.14 -      return DSPF_RGB332;
    1.15 +      return DSPF_LUT8;
    1.16      case 2:
    1.17        return DSPF_RGB16;
    1.18      case 3:
    1.19 @@ -204,6 +204,7 @@
    1.20  
    1.21  struct private_hwdata {
    1.22    IDirectFBSurface *surface;
    1.23 +  IDirectFBPalette *palette;
    1.24  };
    1.25  
    1.26  void SetDirectFBerror (const char *function, DFBResult code)
    1.27 @@ -222,6 +223,9 @@
    1.28      {
    1.29        switch (format->BitsPerPixel)
    1.30          {
    1.31 +        case 8:
    1.32 +          return DSPF_LUT8;
    1.33 +          
    1.34          case 16:
    1.35            if (format->Rmask == 0xF800 &&
    1.36                format->Gmask == 0x07E0 &&
    1.37 @@ -236,13 +240,6 @@
    1.38              return DSPF_RGB15;
    1.39            break;
    1.40            
    1.41 -        case 8:
    1.42 -          if (format->Rmask == 0xE0 &&
    1.43 -              format->Gmask == 0x1C &&
    1.44 -              format->Bmask == 0x03)
    1.45 -            return DSPF_RGB332;
    1.46 -          break;
    1.47 -          
    1.48          case 24:
    1.49            if (format->Rmask == 0xFF0000 &&
    1.50                format->Gmask == 0x00FF00 &&
    1.51 @@ -268,7 +265,7 @@
    1.52        switch (format->BitsPerPixel)
    1.53  	{
    1.54          case 8:
    1.55 -          return DSPF_RGB332;
    1.56 +          return DSPF_LUT8;
    1.57  	case 15:
    1.58  	  return DSPF_RGB15;
    1.59  	case 16:
    1.60 @@ -283,12 +280,8 @@
    1.61    return DSPF_UNKNOWN;
    1.62  }
    1.63  
    1.64 -static const __u8 lookup3to8[] = { 0x00, 0x24, 0x49, 0x6d, 0x92, 0xb6, 0xdb, 0xff };
    1.65 -static const __u8 lookup2to8[] = { 0x00, 0x55, 0xaa, 0xff };
    1.66 -
    1.67 -static SDL_Palette *GenerateRGB332Palette()
    1.68 +static SDL_Palette *AllocatePalette(int size)
    1.69  {
    1.70 -  int          i;
    1.71    SDL_Palette *palette;
    1.72    SDL_Color   *colors;
    1.73  
    1.74 @@ -299,21 +292,14 @@
    1.75        return NULL;
    1.76      }
    1.77  
    1.78 -  colors = calloc (256, sizeof(SDL_Color));
    1.79 +  colors = calloc (size, sizeof(SDL_Color));
    1.80    if (!colors)
    1.81      {
    1.82        SDL_OutOfMemory();
    1.83        return NULL;
    1.84      }
    1.85  
    1.86 -  for (i=0; i<256; i++)
    1.87 -    {
    1.88 -      colors[i].r = lookup3to8[ i >> 5 ];
    1.89 -      colors[i].g = lookup3to8[ (i >> 2) & 7 ];
    1.90 -      colors[i].g = lookup2to8[ i & 3 ];
    1.91 -    }
    1.92 -
    1.93 -  palette->ncolors = 256;
    1.94 +  palette->ncolors = size;
    1.95    palette->colors  = colors;
    1.96  
    1.97    return palette;
    1.98 @@ -352,15 +338,17 @@
    1.99        format->Bmask = 0x000000FF;
   1.100        break;
   1.101  
   1.102 -    case DSPF_RGB332:
   1.103 -      format->Rmask = 0x000000E0;
   1.104 -      format->Gmask = 0x0000001C;
   1.105 -      format->Bmask = 0x00000003;
   1.106 +    case DSPF_LUT8:
   1.107 +      format->Rmask = 0x000000FF;
   1.108 +      format->Gmask = 0x000000FF;
   1.109 +      format->Bmask = 0x000000FF;
   1.110  
   1.111 -      format->palette = GenerateRGB332Palette();
   1.112 +      if (!format->palette)
   1.113 +        format->palette = AllocatePalette(256);
   1.114        break;
   1.115  
   1.116      default:
   1.117 +      fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
   1.118        return -1;
   1.119      }
   1.120  
   1.121 @@ -377,7 +365,6 @@
   1.122    DFBResult                ret;
   1.123    DFBCardCapabilities      caps;
   1.124    DFBDisplayLayerConfig    dlc;
   1.125 -  DFBSurfacePixelFormat    format;
   1.126    struct DirectFBEnumRect *rect;
   1.127    IDirectFB               *dfb    = NULL;
   1.128    IDirectFBDisplayLayer   *layer  = NULL;
   1.129 @@ -417,15 +404,9 @@
   1.130    /* Query layer configuration to determine the current mode and pixelformat */
   1.131    layer->GetConfiguration (layer, &dlc);
   1.132  
   1.133 -  /* FIXME: Returning RGB332 as the default mode doesn't work (everything is black) */
   1.134 -  if ((format = dlc.pixelformat) == DSPF_RGB332)
   1.135 -    format = DSPF_RGB16;
   1.136 -
   1.137 -  if (DFBToSDLPixelFormat (format, vformat))
   1.138 -    {
   1.139 -      SDL_SetError ("Unsupported pixelformat");
   1.140 -      goto error;
   1.141 -    }
   1.142 +  /* If current format is not supported use LUT8 as the default */
   1.143 +  if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
   1.144 +    DFBToSDLPixelFormat (DSPF_LUT8, vformat);
   1.145  
   1.146    /* Enumerate the available fullscreen modes */
   1.147    ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
   1.148 @@ -494,9 +475,10 @@
   1.149  
   1.150  static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
   1.151  {
   1.152 -  DFBResult             ret;
   1.153 -  DFBSurfaceDescription dsc;
   1.154 -  DFBSurfacePixelFormat pixelformat;
   1.155 +  DFBResult              ret;
   1.156 +  DFBSurfaceDescription  dsc;
   1.157 +  DFBSurfacePixelFormat  pixelformat;
   1.158 +  IDirectFBSurface      *surface;
   1.159  
   1.160    fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
   1.161             width, height, bpp, flags);
   1.162 @@ -508,6 +490,13 @@
   1.163      {
   1.164        current->hwdata->surface->Release (current->hwdata->surface);
   1.165        current->hwdata->surface = NULL;
   1.166 +
   1.167 +      /* And its palette if present */
   1.168 +      if (current->hwdata->palette)
   1.169 +        {
   1.170 +          current->hwdata->palette->Release (current->hwdata->palette);
   1.171 +          current->hwdata->palette = NULL;
   1.172 +        }
   1.173      }
   1.174    else if (!current->hwdata)
   1.175      {
   1.176 @@ -556,17 +545,16 @@
   1.177    dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
   1.178    dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
   1.179  
   1.180 -  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
   1.181 +  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   1.182    if (ret && (flags & SDL_DOUBLEBUF))
   1.183      {
   1.184        /* Try without double buffering */
   1.185        dsc.caps &= ~DSCAPS_FLIPPING;
   1.186 -      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
   1.187 +      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
   1.188      }
   1.189    if (ret)
   1.190      {
   1.191        SetDirectFBerror ("dfb->CreateSurface", ret);
   1.192 -      current->hwdata->surface = NULL;
   1.193        return NULL;
   1.194      }
   1.195  
   1.196 @@ -585,9 +573,20 @@
   1.197    if (dsc.caps & DSCAPS_FLIPPING)
   1.198      current->flags |= SDL_DOUBLEBUF;
   1.199  
   1.200 -  current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
   1.201 +  surface->GetPixelFormat (surface, &pixelformat);
   1.202 +
   1.203    DFBToSDLPixelFormat (pixelformat, current->format);
   1.204  
   1.205 +  /* Get the surface palette (if supported) */
   1.206 +  if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
   1.207 +    {
   1.208 +      surface->GetPalette (surface, &current->hwdata->palette);
   1.209 +
   1.210 +      current->flags |= SDL_HWPALETTE;
   1.211 +    }
   1.212 +
   1.213 +  current->hwdata->surface = surface;
   1.214 +
   1.215    return current;
   1.216  }
   1.217  
   1.218 @@ -712,11 +711,14 @@
   1.219    SDL_PixelFormat  *fmt     = src->format;
   1.220    IDirectFBSurface *surface = src->hwdata->surface;
   1.221  
   1.222 -  /* ugly */
   1.223 -  surface->SetSrcColorKey (surface,
   1.224 -                           (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   1.225 -                           (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   1.226 -                           (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   1.227 +  if (fmt->BitsPerPixel == 8)
   1.228 +    surface->SetSrcColorKeyIndex (surface, key);
   1.229 +  else
   1.230 +    /* ugly */
   1.231 +    surface->SetSrcColorKey (surface,
   1.232 +                             (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
   1.233 +                             (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
   1.234 +                             (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
   1.235  
   1.236    return 0;
   1.237  }
   1.238 @@ -809,13 +811,50 @@
   1.239  
   1.240  int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   1.241  {
   1.242 -  fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
   1.243 -  return -1;
   1.244 +  IDirectFBPalette *palette = this->screen->hwdata->palette;
   1.245 +
   1.246 +  if (!palette)
   1.247 +    return 0;
   1.248 +
   1.249 +  if (firstcolor > 255)
   1.250 +    return 0;
   1.251 +
   1.252 +  if (firstcolor + ncolors > 256)
   1.253 +    ncolors = 256 - firstcolor;
   1.254 +
   1.255 +  if (ncolors > 0)
   1.256 +    {
   1.257 +      int      i;
   1.258 +      DFBColor entries[ncolors];
   1.259 +
   1.260 +      for (i=0; i<ncolors; i++)
   1.261 +        {
   1.262 +          entries[i].a = 0xff;
   1.263 +          entries[i].r = colors[i].r;
   1.264 +          entries[i].g = colors[i].g;
   1.265 +          entries[i].b = colors[i].b;
   1.266 +        }
   1.267 +
   1.268 +      palette->SetEntries (palette, entries, ncolors, firstcolor);
   1.269 +    }
   1.270 +
   1.271 +  return 1;
   1.272  }
   1.273  	
   1.274  void DirectFB_VideoQuit(_THIS)
   1.275  {
   1.276 -  struct DirectFBEnumRect *rect = enumlist;
   1.277 +  struct DirectFBEnumRect *rect    = enumlist;
   1.278 +  IDirectFBSurface        *surface = this->screen->hwdata->surface;
   1.279 +  IDirectFBPalette        *palette = this->screen->hwdata->palette;
   1.280 +
   1.281 +  if (palette)
   1.282 +    palette->Release (palette);
   1.283 +
   1.284 +  if (surface)
   1.285 +    surface->Release (surface);
   1.286 +
   1.287 +  this->screen->hwdata->surface = NULL;
   1.288 +  this->screen->hwdata->palette = NULL;
   1.289  
   1.290    if (HIDDEN->eventbuffer)
   1.291      {