Windows QPA plugin: Add \internal to class documentation.
[profile/ivi/qtbase.git] / src / plugins / platforms / windows / qwindowsnativeimage.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qwindowsnativeimage.h"
43 #include "qwindowscontext.h"
44
45 #include <QtGui/private/qpaintengine_p.h>
46 #include <QtGui/private/qpaintengine_raster_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 typedef struct {
51     BITMAPINFOHEADER bmiHeader;
52     DWORD redMask;
53     DWORD greenMask;
54     DWORD blueMask;
55 } BITMAPINFO_MASK;
56
57 /*!
58     \class QWindowsNativeImage
59     \brief Windows Native image
60
61     Note that size can be 0 (widget autotests with zero size), which
62     causes CreateDIBSection() to fail.
63
64     \sa QWindowsBackingStore
65     \internal
66     \ingroup qt-lighthouse-win
67 */
68
69 static inline HDC createDC()
70 {
71     HDC display_dc = GetDC(0);
72     HDC hdc = CreateCompatibleDC(display_dc);
73     ReleaseDC(0, display_dc);
74     Q_ASSERT(hdc);
75     return hdc;
76 }
77
78 static inline HBITMAP createDIB(HDC hdc, int width, int height,
79                                 QImage::Format format,
80                                 uchar **bitsIn)
81 {
82     BITMAPINFO_MASK bmi;
83     memset(&bmi, 0, sizeof(bmi));
84     bmi.bmiHeader.biSize        = sizeof(BITMAPINFOHEADER);
85     bmi.bmiHeader.biWidth       = width;
86     bmi.bmiHeader.biHeight      = -height; // top-down.
87     bmi.bmiHeader.biPlanes      = 1;
88     bmi.bmiHeader.biSizeImage   = 0;
89
90     if (format == QImage::Format_RGB16) {
91         bmi.bmiHeader.biBitCount = 16;
92         bmi.bmiHeader.biCompression = BI_BITFIELDS;
93         bmi.redMask = 0xF800;
94         bmi.greenMask = 0x07E0;
95         bmi.blueMask = 0x001F;
96     } else {
97         bmi.bmiHeader.biBitCount    = 32;
98         bmi.bmiHeader.biCompression = BI_RGB;
99         bmi.redMask = 0;
100         bmi.greenMask = 0;
101         bmi.blueMask = 0;
102     }
103
104     void *bits = 0;
105     HBITMAP bitmap = CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO *>(&bmi),
106                                       DIB_RGB_COLORS, &bits, 0, 0);
107     if (!bitmap || !bits)
108         qFatal("%s: CreateDIBSection failed.", __FUNCTION__);
109
110     *bitsIn = (uchar*)bits;
111     return bitmap;
112 }
113
114 QWindowsNativeImage::QWindowsNativeImage(int width, int height,
115                                          QImage::Format format) :
116     m_hdc(createDC()),
117     m_bitmap(0),
118     m_null_bitmap(0)
119 {
120     if (width != 0 && height != 0) {
121         uchar *bits;
122         m_bitmap = createDIB(m_hdc, width, height, format, &bits);
123         m_null_bitmap = (HBITMAP)SelectObject(m_hdc, m_bitmap);
124         m_image = QImage(bits, width, height, format);
125         Q_ASSERT(m_image.paintEngine()->type() == QPaintEngine::Raster);
126         static_cast<QRasterPaintEngine *>(m_image.paintEngine())->setDC(m_hdc);
127     } else {
128         m_image = QImage(width, height, format);
129     }
130
131 #ifndef Q_OS_WINCE
132     GdiFlush();
133 #endif
134 }
135
136 QWindowsNativeImage::~QWindowsNativeImage()
137 {
138     if (m_hdc) {
139         if (m_bitmap) {
140             if (m_null_bitmap)
141                 SelectObject(m_hdc, m_null_bitmap);
142             DeleteObject(m_bitmap);
143         }
144         DeleteDC(m_hdc);
145     }
146 }
147
148 QImage::Format QWindowsNativeImage::systemFormat()
149 {
150     static const int depth = QWindowsContext::instance()->screenDepth();
151     return depth == 16 ? QImage::Format_RGB16 : QImage::Format_RGB32;
152 }
153
154 QT_END_NAMESPACE