/src/qtbase/src/gui/image/qplatformpixmap.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /**************************************************************************** |
2 | | ** |
3 | | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | | ** Contact: https://www.qt.io/licensing/ |
5 | | ** |
6 | | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | | ** |
8 | | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | | ** Commercial License Usage |
10 | | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | | ** accordance with the commercial license agreement provided with the |
12 | | ** Software or, alternatively, in accordance with the terms contained in |
13 | | ** a written agreement between you and The Qt Company. For licensing terms |
14 | | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | | ** information use the contact form at https://www.qt.io/contact-us. |
16 | | ** |
17 | | ** GNU Lesser General Public License Usage |
18 | | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | | ** General Public License version 3 as published by the Free Software |
20 | | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | | ** packaging of this file. Please review the following information to |
22 | | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | | ** |
25 | | ** GNU General Public License Usage |
26 | | ** Alternatively, this file may be used under the terms of the GNU |
27 | | ** General Public License version 2.0 or (at your option) the GNU General |
28 | | ** Public license version 3 or any later version approved by the KDE Free |
29 | | ** Qt Foundation. The licenses are as published by the Free Software |
30 | | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | | ** included in the packaging of this file. Please review the following |
32 | | ** information to ensure the GNU General Public License requirements will |
33 | | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | | ** |
36 | | ** $QT_END_LICENSE$ |
37 | | ** |
38 | | ****************************************************************************/ |
39 | | |
40 | | #include "qplatformpixmap.h" |
41 | | #include <qpa/qplatformintegration.h> |
42 | | #include <QtCore/qbuffer.h> |
43 | | #include <QtGui/qbitmap.h> |
44 | | #include <QtGui/qimagereader.h> |
45 | | #include <private/qguiapplication_p.h> |
46 | | #include <private/qimagepixmapcleanuphooks_p.h> |
47 | | |
48 | | QT_BEGIN_NAMESPACE |
49 | | |
50 | | /*! |
51 | | \class QPlatformPixmap |
52 | | \since 5.0 |
53 | | \internal |
54 | | \preliminary |
55 | | \ingroup qpa |
56 | | |
57 | | \brief The QPlatformPixmap class provides an abstraction for native pixmaps. |
58 | | */ |
59 | | QPlatformPixmap *QPlatformPixmap::create(int w, int h, PixelType type) |
60 | 0 | { |
61 | 0 | if (Q_UNLIKELY(!QGuiApplicationPrivate::platformIntegration())) |
62 | 0 | qFatal("QPlatformPixmap: QGuiApplication required"); |
63 | |
|
64 | 0 | QPlatformPixmap *data = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(static_cast<QPlatformPixmap::PixelType>(type)); |
65 | 0 | data->resize(w, h); |
66 | 0 | return data; |
67 | 0 | } |
68 | | |
69 | | |
70 | | QPlatformPixmap::QPlatformPixmap(PixelType pixelType, int objectId) |
71 | 0 | : w(0), |
72 | 0 | h(0), |
73 | 0 | d(0), |
74 | 0 | is_null(true), |
75 | 0 | ref(0), |
76 | 0 | detach_no(0), |
77 | 0 | type(pixelType), |
78 | 0 | id(objectId), |
79 | 0 | ser_no(0), |
80 | 0 | is_cached(false) |
81 | 0 | { |
82 | 0 | } |
83 | | |
84 | | QPlatformPixmap::~QPlatformPixmap() |
85 | 0 | { |
86 | | // Sometimes the pixmap cleanup hooks will be called from derrived classes, which will |
87 | | // then set is_cached to false. For example, on X11 Qt GUI needs to delete the GLXPixmap |
88 | | // or EGL Pixmap Surface for a given pixmap _before_ the native X11 pixmap is deleted, |
89 | | // otherwise some drivers will leak the GL surface. In this case, QX11PlatformPixmap will |
90 | | // call the cleanup hooks itself before deleting the native pixmap and set is_cached to |
91 | | // false. |
92 | 0 | if (is_cached) { |
93 | 0 | QImagePixmapCleanupHooks::executePlatformPixmapDestructionHooks(this); |
94 | 0 | is_cached = false; |
95 | 0 | } |
96 | 0 | } |
97 | | |
98 | | QPlatformPixmap *QPlatformPixmap::createCompatiblePlatformPixmap() const |
99 | 0 | { |
100 | 0 | QPlatformPixmap *d = QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(pixelType()); |
101 | 0 | return d; |
102 | 0 | } |
103 | | |
104 | | static QImage makeBitmapCompliantIfNeeded(QPlatformPixmap *d, const QImage &image, Qt::ImageConversionFlags flags) |
105 | 0 | { |
106 | 0 | if (d->pixelType() == QPlatformPixmap::BitmapType) { |
107 | 0 | QImage img = image.convertToFormat(QImage::Format_MonoLSB, flags); |
108 | | |
109 | | // make sure image.color(0) == Qt::color0 (white) |
110 | | // and image.color(1) == Qt::color1 (black) |
111 | 0 | const QRgb c0 = QColor(Qt::black).rgb(); |
112 | 0 | const QRgb c1 = QColor(Qt::white).rgb(); |
113 | 0 | if (img.color(0) == c0 && img.color(1) == c1) { |
114 | 0 | img.invertPixels(); |
115 | 0 | img.setColor(0, c1); |
116 | 0 | img.setColor(1, c0); |
117 | 0 | } |
118 | 0 | return img; |
119 | 0 | } |
120 | | |
121 | 0 | return image; |
122 | 0 | } |
123 | | |
124 | | void QPlatformPixmap::fromImageReader(QImageReader *imageReader, |
125 | | Qt::ImageConversionFlags flags) |
126 | 0 | { |
127 | 0 | const QImage image = imageReader->read(); |
128 | 0 | fromImage(image, flags); |
129 | 0 | } |
130 | | |
131 | | bool QPlatformPixmap::fromFile(const QString &fileName, const char *format, |
132 | | Qt::ImageConversionFlags flags) |
133 | 0 | { |
134 | 0 | QImage image = QImageReader(fileName, format).read(); |
135 | 0 | if (image.isNull()) |
136 | 0 | return false; |
137 | 0 | fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags); |
138 | 0 | return !isNull(); |
139 | 0 | } |
140 | | |
141 | | bool QPlatformPixmap::fromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags) |
142 | 0 | { |
143 | 0 | QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(buf), len); |
144 | 0 | QBuffer b(&a); |
145 | 0 | b.open(QIODevice::ReadOnly); |
146 | 0 | QImage image = QImageReader(&b, format).read(); |
147 | 0 | if (image.isNull()) |
148 | 0 | return false; |
149 | 0 | fromImage(makeBitmapCompliantIfNeeded(this, image, flags), flags); |
150 | 0 | return !isNull(); |
151 | 0 | } |
152 | | |
153 | | void QPlatformPixmap::copy(const QPlatformPixmap *data, const QRect &rect) |
154 | 0 | { |
155 | 0 | fromImage(data->toImage(rect), Qt::NoOpaqueDetection); |
156 | 0 | } |
157 | | |
158 | | bool QPlatformPixmap::scroll(int dx, int dy, const QRect &rect) |
159 | 0 | { |
160 | 0 | Q_UNUSED(dx); |
161 | 0 | Q_UNUSED(dy); |
162 | 0 | Q_UNUSED(rect); |
163 | 0 | return false; |
164 | 0 | } |
165 | | |
166 | | QBitmap QPlatformPixmap::mask() const |
167 | 0 | { |
168 | 0 | if (!hasAlphaChannel()) |
169 | 0 | return QBitmap(); |
170 | | |
171 | 0 | const QImage img = toImage(); |
172 | 0 | bool shouldConvert = (img.format() != QImage::Format_ARGB32 && img.format() != QImage::Format_ARGB32_Premultiplied); |
173 | 0 | const QImage image = (shouldConvert ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img); |
174 | 0 | const int w = image.width(); |
175 | 0 | const int h = image.height(); |
176 | |
|
177 | 0 | QImage mask(w, h, QImage::Format_MonoLSB); |
178 | 0 | if (mask.isNull()) // allocation failed |
179 | 0 | return QBitmap(); |
180 | | |
181 | 0 | mask.setDevicePixelRatio(devicePixelRatio()); |
182 | 0 | mask.setColorCount(2); |
183 | 0 | mask.setColor(0, QColor(Qt::color0).rgba()); |
184 | 0 | mask.setColor(1, QColor(Qt::color1).rgba()); |
185 | |
|
186 | 0 | const int bpl = mask.bytesPerLine(); |
187 | |
|
188 | 0 | for (int y = 0; y < h; ++y) { |
189 | 0 | const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y)); |
190 | 0 | uchar *dest = mask.scanLine(y); |
191 | 0 | memset(dest, 0, bpl); |
192 | 0 | for (int x = 0; x < w; ++x) { |
193 | 0 | if (qAlpha(*src) > 0) |
194 | 0 | dest[x >> 3] |= (1 << (x & 7)); |
195 | 0 | ++src; |
196 | 0 | } |
197 | 0 | } |
198 | |
|
199 | 0 | return QBitmap::fromImage(mask); |
200 | 0 | } |
201 | | |
202 | | void QPlatformPixmap::setMask(const QBitmap &mask) |
203 | 0 | { |
204 | 0 | QImage image = toImage(); |
205 | 0 | if (mask.size().isEmpty()) { |
206 | 0 | if (image.depth() != 1) { // hw: ???? |
207 | 0 | image = image.convertToFormat(QImage::Format_RGB32); |
208 | 0 | } |
209 | 0 | } else { |
210 | 0 | const int w = image.width(); |
211 | 0 | const int h = image.height(); |
212 | |
|
213 | 0 | switch (image.depth()) { |
214 | 0 | case 1: { |
215 | 0 | const QImage imageMask = mask.toImage().convertToFormat(image.format()); |
216 | 0 | for (int y = 0; y < h; ++y) { |
217 | 0 | const uchar *mscan = imageMask.scanLine(y); |
218 | 0 | uchar *tscan = image.scanLine(y); |
219 | 0 | int bytesPerLine = image.bytesPerLine(); |
220 | 0 | for (int i = 0; i < bytesPerLine; ++i) |
221 | 0 | tscan[i] &= mscan[i]; |
222 | 0 | } |
223 | 0 | break; |
224 | 0 | } |
225 | 0 | default: { |
226 | 0 | const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB); |
227 | 0 | image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); |
228 | 0 | for (int y = 0; y < h; ++y) { |
229 | 0 | const uchar *mscan = imageMask.scanLine(y); |
230 | 0 | QRgb *tscan = (QRgb *)image.scanLine(y); |
231 | 0 | for (int x = 0; x < w; ++x) { |
232 | 0 | if (!(mscan[x>>3] & (1 << (x&7)))) |
233 | 0 | tscan[x] = 0; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | break; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | } |
240 | 0 | fromImage(image, Qt::AutoColor); |
241 | 0 | } |
242 | | |
243 | | QPixmap QPlatformPixmap::transformed(const QTransform &matrix, |
244 | | Qt::TransformationMode mode) const |
245 | 0 | { |
246 | 0 | return QPixmap::fromImage(toImage().transformed(matrix, mode)); |
247 | 0 | } |
248 | | |
249 | | void QPlatformPixmap::setSerialNumber(int serNo) |
250 | 0 | { |
251 | 0 | ser_no = serNo; |
252 | 0 | } |
253 | | |
254 | | void QPlatformPixmap::setDetachNumber(int detNo) |
255 | 0 | { |
256 | 0 | detach_no = detNo; |
257 | 0 | } |
258 | | |
259 | | QImage QPlatformPixmap::toImage(const QRect &rect) const |
260 | 0 | { |
261 | 0 | if (rect.contains(QRect(0, 0, w, h))) |
262 | 0 | return toImage(); |
263 | 0 | else |
264 | 0 | return toImage().copy(rect); |
265 | 0 | } |
266 | | |
267 | | QImage* QPlatformPixmap::buffer() |
268 | 0 | { |
269 | 0 | return nullptr; |
270 | 0 | } |
271 | | |
272 | | |
273 | | QT_END_NAMESPACE |