1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 Copyright (C) 2009 Red Hat, Inc.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "red_pixmap_gdi.h"
24 #include "red_pixmap.h"
27 #include "pixels_source_p.h"
28 #include "platform_utils.h"
31 PixelsSource_p pixels_source_p;
35 RedPixmapGdi::RedPixmapGdi(int width, int height, RedDrawable::Format format, bool top_bottom)
36 : RedPixmap(width, height, format, top_bottom)
39 ASSERT(format == RedDrawable::ARGB32 || format == RedDrawable::RGB32
40 || format == RedDrawable::RGB16_555 || format == RedDrawable::RGB16_565
41 || format == RedDrawable::A1);
42 ASSERT(sizeof(RedPixmap_p) <= PIXELES_SOURCE_OPAQUE_SIZE);
49 memset(&bitmap_info, 0, sizeof(bitmap_info));
50 bitmap_info.inf.bmiHeader.biSize = sizeof(bitmap_info.inf.bmiHeader);
51 bitmap_info.inf.bmiHeader.biWidth = _width;
52 bitmap_info.inf.bmiHeader.biHeight = top_bottom ? -_height : _height;
54 bitmap_info.inf.bmiHeader.biPlanes = 1;
55 bitmap_info.inf.bmiHeader.biBitCount = RedDrawable::format_to_bpp(format);
56 if (format == RedDrawable::RGB16_565) {
57 bitmap_info.inf.bmiHeader.biCompression = BI_BITFIELDS;
60 bitmap_info.inf.bmiHeader.biCompression = BI_RGB;
64 bitmap_info.inf.bmiColors[0].rgbRed = 0;
65 bitmap_info.inf.bmiColors[0].rgbGreen = 0;
66 bitmap_info.inf.bmiColors[0].rgbBlue = 0;
68 // inf.bmiColors is [1] in mingw/include/wingdi.h
69 bitmap_info.inf.bmiColors[1].rgbRed = 0xff;
70 bitmap_info.inf.bmiColors[1].rgbGreen = 0xff;
71 bitmap_info.inf.bmiColors[1].rgbBlue = 0xff;
74 case RedDrawable::RGB16_565:
75 pixel_format = (DWORD *)bitmap_info.inf.bmiColors;
76 pixel_format[0] = 0xf800;
77 pixel_format[1] = 0x07e0;
78 pixel_format[2] = 0x001f;
80 case RedDrawable::ARGB32:
81 case RedDrawable::RGB32:
82 case RedDrawable::RGB16_555:
85 AutoDC dc(create_compatible_dc());
86 AutoGDIObject bitmap(CreateDIBSection(dc.get(), &bitmap_info.inf, 0,
87 (VOID **)&_data, NULL, 0));
88 if (!bitmap.valid()) {
89 THROW("create compatible bitmap failed");
92 ((RedPixmap_p*)get_opaque())->prev_bitmap = (HBITMAP)SelectObject(dc.get(), bitmap.release());
93 ((RedPixmap_p*)get_opaque())->pixels_source_p.dc = dc.release();
96 HDC RedPixmapGdi::get_dc()
98 return ((RedPixmap_p*)get_opaque())->pixels_source_p.dc;
101 void *RedPixmapGdi::get_memptr()
106 RedPixmapGdi::~RedPixmapGdi()
108 HDC dc = ((RedPixmap_p*)get_opaque())->pixels_source_p.dc;
110 HBITMAP prev_bitmap = ((RedPixmap_p*)get_opaque())->prev_bitmap;
111 HBITMAP bitmap = (HBITMAP)SelectObject(dc, prev_bitmap);
112 DeleteObject(bitmap);
117 RecurciveMutex& RedPixmapGdi::get_mutex()
119 RedPixmap_p* p_data = (RedPixmap_p*)get_opaque();
120 return *p_data->pixels_source_p._mutex;