changeset 5179:6d37aa594ecd

Use the exact format of the window if possible, for speed.
author Sam Lantinga <slouken@libsdl.org>
date Fri, 04 Feb 2011 13:47:02 -0800
parents 9b2e99ebd099
children 019942602496
files src/video/windows/SDL_windowsframebuffer.c
diffstat 1 files changed, 60 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/video/windows/SDL_windowsframebuffer.c	Fri Feb 04 12:29:58 2011 -0800
+++ b/src/video/windows/SDL_windowsframebuffer.c	Fri Feb 04 13:47:02 2011 -0800
@@ -23,11 +23,16 @@
 
 #include "SDL_windowsvideo.h"
 
+#define HAVE_GETDIBITS
 
 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
 {
     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
-    BITMAPINFO info;
+    size_t size;
+    LPBITMAPINFO info;
+#ifdef HAVE_GETDIBITS
+    HBITMAP hbm;
+#endif
 
     /* Free the old framebuffer surface */
     if (data->mdc) {
@@ -37,25 +42,63 @@
         DeleteObject(data->hbm);
     }
 
-    /* We'll use RGB format for now */
-    *format = SDL_PIXELFORMAT_RGB888;
-    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
+    /* Find out the format of the screen */
+    size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
+    info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
+
+#ifdef HAVE_GETDIBITS
+    SDL_memset(info, 0, size);
+    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+
+    /* The second call to GetDIBits() fills in the bitfields */
+    hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
+    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
+    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
+    DeleteObject(hbm);
+
+    *format = SDL_PIXELFORMAT_UNKNOWN;
+    if (info->bmiHeader.biCompression == BI_BITFIELDS) {
+        Uint32 *masks;
 
-    /* Create a new one */
-    info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
-    info.bmiHeader.biWidth = window->w;
-    info.bmiHeader.biHeight = -window->h;	/* negative for topdown bitmap */
-    info.bmiHeader.biPlanes = 1;
-    info.bmiHeader.biBitCount = 32;
-    info.bmiHeader.biCompression = BI_RGB;
-    info.bmiHeader.biSizeImage = window->h * (*pitch);
-    info.bmiHeader.biXPelsPerMeter = 0;
-    info.bmiHeader.biYPelsPerMeter = 0;
-    info.bmiHeader.biClrUsed = 0;
-    info.bmiHeader.biClrImportant = 0;
+        masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
+        if (masks[0] == 0x00FF0000 && masks[2] == 0x000000FF) {
+            *format = SDL_PIXELFORMAT_RGB888;
+        } else if (masks[0] == 0x000000FF && masks[2] == 0x00FF0000) {
+            *format = SDL_PIXELFORMAT_BGR888;
+        } else if (masks[0] == 0xF800 && masks[2] == 0x001F) {
+            *format = SDL_PIXELFORMAT_RGB565;
+        } else if (masks[0] == 0x001F && masks[2] == 0xF800) {
+            *format = SDL_PIXELFORMAT_BGR565;
+        } else if (masks[0] == 0x7C00 && masks[2] == 0x001F) {
+            *format = SDL_PIXELFORMAT_RGB555;
+        } else if (masks[0] == 0x001F && masks[2] == 0x7C00) {
+            *format = SDL_PIXELFORMAT_BGR555;
+        }
+    }
+    if (*format == SDL_PIXELFORMAT_UNKNOWN)
+#endif
+    {
+        /* We'll use RGB format for now */
+        *format = SDL_PIXELFORMAT_RGB888;
+
+        /* Create a new one */
+        SDL_memset(info, 0, size);
+        info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
+        info->bmiHeader.biPlanes = 1;
+        info->bmiHeader.biBitCount = 32;
+        info->bmiHeader.biCompression = BI_RGB;
+    }
+
+    /* Fill in the size information */
+    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
+    info->bmiHeader.biWidth = window->w;
+    info->bmiHeader.biHeight = -window->h;	/* negative for topdown bitmap */
+    info->bmiHeader.biSizeImage = window->h * (*pitch);
 
     data->mdc = CreateCompatibleDC(data->hdc);
-    data->hbm = CreateDIBSection(data->hdc, &info, DIB_RGB_COLORS, pixels, NULL, 0);
+    data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
+    SDL_stack_free(info);
+
     if (!data->hbm) {
         WIN_SetError("Unable to create DIB");
         return -1;