From 5961e500eb20e3697c657ce0375081b2a47544f8 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 4 Feb 2011 13:47:02 -0800 Subject: [PATCH] Use the exact format of the window if possible, for speed. --- src/video/windows/SDL_windowsframebuffer.c | 77 +++++++++++++++++----- 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/src/video/windows/SDL_windowsframebuffer.c b/src/video/windows/SDL_windowsframebuffer.c index f9f2c5aa2..8b59e273a 100755 --- a/src/video/windows/SDL_windowsframebuffer.c +++ b/src/video/windows/SDL_windowsframebuffer.c @@ -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 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi 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; + + 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; + } - /* 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; + /* 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;