Use the exact format of the window if possible, for speed.
authorSam Lantinga <slouken@libsdl.org>
Fri, 04 Feb 2011 13:47:02 -0800
changeset 51766d37aa594ecd
parent 5175 9b2e99ebd099
child 5177 019942602496
Use the exact format of the window if possible, for speed.
src/video/windows/SDL_windowsframebuffer.c
     1.1 --- a/src/video/windows/SDL_windowsframebuffer.c	Fri Feb 04 12:29:58 2011 -0800
     1.2 +++ b/src/video/windows/SDL_windowsframebuffer.c	Fri Feb 04 13:47:02 2011 -0800
     1.3 @@ -23,11 +23,16 @@
     1.4  
     1.5  #include "SDL_windowsvideo.h"
     1.6  
     1.7 +#define HAVE_GETDIBITS
     1.8  
     1.9  int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
    1.10  {
    1.11      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    1.12 -    BITMAPINFO info;
    1.13 +    size_t size;
    1.14 +    LPBITMAPINFO info;
    1.15 +#ifdef HAVE_GETDIBITS
    1.16 +    HBITMAP hbm;
    1.17 +#endif
    1.18  
    1.19      /* Free the old framebuffer surface */
    1.20      if (data->mdc) {
    1.21 @@ -37,25 +42,63 @@
    1.22          DeleteObject(data->hbm);
    1.23      }
    1.24  
    1.25 -    /* We'll use RGB format for now */
    1.26 -    *format = SDL_PIXELFORMAT_RGB888;
    1.27 -    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
    1.28 +    /* Find out the format of the screen */
    1.29 +    size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
    1.30 +    info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
    1.31 +
    1.32 +#ifdef HAVE_GETDIBITS
    1.33 +    SDL_memset(info, 0, size);
    1.34 +    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    1.35 +
    1.36 +    /* The second call to GetDIBits() fills in the bitfields */
    1.37 +    hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
    1.38 +    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
    1.39 +    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
    1.40 +    DeleteObject(hbm);
    1.41 +
    1.42 +    *format = SDL_PIXELFORMAT_UNKNOWN;
    1.43 +    if (info->bmiHeader.biCompression == BI_BITFIELDS) {
    1.44 +        Uint32 *masks;
    1.45  
    1.46 -    /* Create a new one */
    1.47 -    info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    1.48 -    info.bmiHeader.biWidth = window->w;
    1.49 -    info.bmiHeader.biHeight = -window->h;	/* negative for topdown bitmap */
    1.50 -    info.bmiHeader.biPlanes = 1;
    1.51 -    info.bmiHeader.biBitCount = 32;
    1.52 -    info.bmiHeader.biCompression = BI_RGB;
    1.53 -    info.bmiHeader.biSizeImage = window->h * (*pitch);
    1.54 -    info.bmiHeader.biXPelsPerMeter = 0;
    1.55 -    info.bmiHeader.biYPelsPerMeter = 0;
    1.56 -    info.bmiHeader.biClrUsed = 0;
    1.57 -    info.bmiHeader.biClrImportant = 0;
    1.58 +        masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
    1.59 +        if (masks[0] == 0x00FF0000 && masks[2] == 0x000000FF) {
    1.60 +            *format = SDL_PIXELFORMAT_RGB888;
    1.61 +        } else if (masks[0] == 0x000000FF && masks[2] == 0x00FF0000) {
    1.62 +            *format = SDL_PIXELFORMAT_BGR888;
    1.63 +        } else if (masks[0] == 0xF800 && masks[2] == 0x001F) {
    1.64 +            *format = SDL_PIXELFORMAT_RGB565;
    1.65 +        } else if (masks[0] == 0x001F && masks[2] == 0xF800) {
    1.66 +            *format = SDL_PIXELFORMAT_BGR565;
    1.67 +        } else if (masks[0] == 0x7C00 && masks[2] == 0x001F) {
    1.68 +            *format = SDL_PIXELFORMAT_RGB555;
    1.69 +        } else if (masks[0] == 0x001F && masks[2] == 0x7C00) {
    1.70 +            *format = SDL_PIXELFORMAT_BGR555;
    1.71 +        }
    1.72 +    }
    1.73 +    if (*format == SDL_PIXELFORMAT_UNKNOWN)
    1.74 +#endif
    1.75 +    {
    1.76 +        /* We'll use RGB format for now */
    1.77 +        *format = SDL_PIXELFORMAT_RGB888;
    1.78 +
    1.79 +        /* Create a new one */
    1.80 +        SDL_memset(info, 0, size);
    1.81 +        info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    1.82 +        info->bmiHeader.biPlanes = 1;
    1.83 +        info->bmiHeader.biBitCount = 32;
    1.84 +        info->bmiHeader.biCompression = BI_RGB;
    1.85 +    }
    1.86 +
    1.87 +    /* Fill in the size information */
    1.88 +    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
    1.89 +    info->bmiHeader.biWidth = window->w;
    1.90 +    info->bmiHeader.biHeight = -window->h;	/* negative for topdown bitmap */
    1.91 +    info->bmiHeader.biSizeImage = window->h * (*pitch);
    1.92  
    1.93      data->mdc = CreateCompatibleDC(data->hdc);
    1.94 -    data->hbm = CreateDIBSection(data->hdc, &info, DIB_RGB_COLORS, pixels, NULL, 0);
    1.95 +    data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
    1.96 +    SDL_stack_free(info);
    1.97 +
    1.98      if (!data->hbm) {
    1.99          WIN_SetError("Unable to create DIB");
   1.100          return -1;