Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/kernel/qplatformgraphicsbufferhelper.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 QtPlatformSupport 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 <QtGui/qpa/qplatformgraphicsbuffer.h> | - | ||||||||||||
41 | - | |||||||||||||
42 | #include "qplatformgraphicsbufferhelper.h" | - | ||||||||||||
43 | #include <QtCore/QDebug> | - | ||||||||||||
44 | #include <QtGui/qopengl.h> | - | ||||||||||||
45 | #include <QtGui/QImage> | - | ||||||||||||
46 | #include <QtGui/QOpenGLContext> | - | ||||||||||||
47 | #include <QtGui/QOpenGLFunctions> | - | ||||||||||||
48 | - | |||||||||||||
49 | #ifndef GL_RGB10_A2 | - | ||||||||||||
50 | #define GL_RGB10_A2 0x8059 | - | ||||||||||||
51 | #endif | - | ||||||||||||
52 | - | |||||||||||||
53 | #ifndef GL_UNSIGNED_INT_2_10_10_10_REV | - | ||||||||||||
54 | #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368 | - | ||||||||||||
55 | #endif | - | ||||||||||||
56 | - | |||||||||||||
57 | QT_BEGIN_NAMESPACE | - | ||||||||||||
58 | - | |||||||||||||
59 | /*! | - | ||||||||||||
60 | Convenience function to both lock and bind the buffer to a texture. This | - | ||||||||||||
61 | function will first try and lock with texture read and texture write | - | ||||||||||||
62 | access. If this succeeds it will use the bindToTexture function to bind the | - | ||||||||||||
63 | content to the currently bound texture. If this fail it will try and lock | - | ||||||||||||
64 | with SWReadAccess and then use the bindSWToTexture convenience function. | - | ||||||||||||
65 | - | |||||||||||||
66 | \a swizzle is suppose to be used by the caller to figure out if the Red and | - | ||||||||||||
67 | Blue color channels need to be swizzled when rendering. | - | ||||||||||||
68 | - | |||||||||||||
69 | \a rect is the subrect which is desired to be bounded to the texture. This | - | ||||||||||||
70 | argument has a no less than semantic, meaning more (if not all) of the buffer | - | ||||||||||||
71 | can be bounded to the texture. An empty QRect is interpreted as entire buffer | - | ||||||||||||
72 | should be bound. | - | ||||||||||||
73 | - | |||||||||||||
74 | The user should use the AccessTypes returned by isLocked to figure out what | - | ||||||||||||
75 | lock has been obtained. | - | ||||||||||||
76 | - | |||||||||||||
77 | returns true if the buffer has successfully been bound to the currently | - | ||||||||||||
78 | bound texture, otherwise returns false. | - | ||||||||||||
79 | */ | - | ||||||||||||
80 | bool QPlatformGraphicsBufferHelper::lockAndBindToTexture(QPlatformGraphicsBuffer *graphicsBuffer, | - | ||||||||||||
81 | bool *swizzle, bool *premultiplied, | - | ||||||||||||
82 | const QRect &rect) | - | ||||||||||||
83 | { | - | ||||||||||||
84 | if (graphicsBuffer->lock(QPlatformGraphicsBuffer::TextureAccess)) {
| 0 | ||||||||||||
85 | if (!graphicsBuffer->bindToTexture(rect)) {
| 0 | ||||||||||||
86 | qWarning("Failed to bind %sgraphicsbuffer to texture", ""); | - | ||||||||||||
87 | return false; never executed: return false; | 0 | ||||||||||||
88 | } | - | ||||||||||||
89 | if (swizzle)
| 0 | ||||||||||||
90 | *swizzle = false; never executed: *swizzle = false; | 0 | ||||||||||||
91 | if (premultiplied)
| 0 | ||||||||||||
92 | *premultiplied = false; never executed: *premultiplied = false; | 0 | ||||||||||||
93 | } else if (graphicsBuffer->lock(QPlatformGraphicsBuffer::SWReadAccess)) { never executed: end of block
| 0 | ||||||||||||
94 | if (!bindSWToTexture(graphicsBuffer, swizzle, premultiplied, rect)) {
| 0 | ||||||||||||
95 | qWarning("Failed to bind %sgraphicsbuffer to texture", "SW "); | - | ||||||||||||
96 | return false; never executed: return false; | 0 | ||||||||||||
97 | } | - | ||||||||||||
98 | } else { never executed: end of block | 0 | ||||||||||||
99 | qWarning("Failed to lock"); | - | ||||||||||||
100 | return false; never executed: return false; | 0 | ||||||||||||
101 | } | - | ||||||||||||
102 | return true; never executed: return true; | 0 | ||||||||||||
103 | } | - | ||||||||||||
104 | - | |||||||||||||
105 | /*! | - | ||||||||||||
106 | Convenience function that uploads the current raster content to the currently bound texture. | - | ||||||||||||
107 | - | |||||||||||||
108 | \a swizzleRandB is suppose to be used by the caller to figure out if the Red and | - | ||||||||||||
109 | Blue color channels need to be swizzled when rendering. This is an | - | ||||||||||||
110 | optimization. Qt often renders to software buffers interpreting pixels as | - | ||||||||||||
111 | unsigned ints. When these buffers are uploaded to textures and each color | - | ||||||||||||
112 | channel per pixel is interpreted as a byte (read sequentially), then the | - | ||||||||||||
113 | Red and Blue channels are swapped. Conveniently the Alpha buffer will be | - | ||||||||||||
114 | correct since Qt historically has had the alpha channel as the first | - | ||||||||||||
115 | channel, while OpenGL typically expects the alpha channel to be the last | - | ||||||||||||
116 | channel. | - | ||||||||||||
117 | - | |||||||||||||
118 | \a subRect is the subrect which is desired to be bounded to the texture. This | - | ||||||||||||
119 | argument has a no less than semantic, meaning more (if not all) of the buffer | - | ||||||||||||
120 | can be bounded to the texture. An empty QRect is interpreted as entire buffer | - | ||||||||||||
121 | should be bound. | - | ||||||||||||
122 | - | |||||||||||||
123 | This function fails for buffers not capable of locking to SWAccess. | - | ||||||||||||
124 | - | |||||||||||||
125 | Returns true on success, otherwise false. | - | ||||||||||||
126 | */ | - | ||||||||||||
127 | bool QPlatformGraphicsBufferHelper::bindSWToTexture(const QPlatformGraphicsBuffer *graphicsBuffer, | - | ||||||||||||
128 | bool *swizzleRandB, bool *premultipliedB, | - | ||||||||||||
129 | const QRect &subRect) | - | ||||||||||||
130 | { | - | ||||||||||||
131 | #ifndef QT_NO_OPENGL | - | ||||||||||||
132 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); | - | ||||||||||||
133 | if (!ctx)
| 0 | ||||||||||||
134 | return false; never executed: return false; | 0 | ||||||||||||
135 | - | |||||||||||||
136 | if (!(graphicsBuffer->isLocked() & QPlatformGraphicsBuffer::SWReadAccess))
| 0 | ||||||||||||
137 | return false; never executed: return false; | 0 | ||||||||||||
138 | - | |||||||||||||
139 | QSize size = graphicsBuffer->size(); | - | ||||||||||||
140 | - | |||||||||||||
141 | Q_ASSERT(subRect.isEmpty() || QRect(QPoint(0,0), size).contains(subRect)); | - | ||||||||||||
142 | - | |||||||||||||
143 | GLenum internalFormat = GL_RGBA; | - | ||||||||||||
144 | GLuint pixelType = GL_UNSIGNED_BYTE; | - | ||||||||||||
145 | - | |||||||||||||
146 | bool needsConversion = false; | - | ||||||||||||
147 | bool swizzle = false; | - | ||||||||||||
148 | bool premultiplied = false; | - | ||||||||||||
149 | QImage::Format imageformat = QImage::toImageFormat(graphicsBuffer->format()); | - | ||||||||||||
150 | QImage image(graphicsBuffer->data(), size.width(), size.height(), graphicsBuffer->bytesPerLine(), imageformat); | - | ||||||||||||
151 | if (graphicsBuffer->bytesPerLine() != (size.width() * 4)) {
| 0 | ||||||||||||
152 | needsConversion = true; | - | ||||||||||||
153 | } else { never executed: end of block | 0 | ||||||||||||
154 | switch (imageformat) { | - | ||||||||||||
155 | case QImage::Format_ARGB32_Premultiplied: never executed: case QImage::Format_ARGB32_Premultiplied: | 0 | ||||||||||||
156 | premultiplied = true; | - | ||||||||||||
157 | // no break | - | ||||||||||||
158 | case QImage::Format_RGB32: code before this statement never executed: case QImage::Format_RGB32: never executed: case QImage::Format_RGB32: | 0 | ||||||||||||
159 | case QImage::Format_ARGB32: never executed: case QImage::Format_ARGB32: | 0 | ||||||||||||
160 | swizzle = true; | - | ||||||||||||
161 | break; never executed: break; | 0 | ||||||||||||
162 | case QImage::Format_RGBA8888_Premultiplied: never executed: case QImage::Format_RGBA8888_Premultiplied: | 0 | ||||||||||||
163 | premultiplied = true; | - | ||||||||||||
164 | // no break | - | ||||||||||||
165 | case QImage::Format_RGBX8888: code before this statement never executed: case QImage::Format_RGBX8888: never executed: case QImage::Format_RGBX8888: | 0 | ||||||||||||
166 | case QImage::Format_RGBA8888: never executed: case QImage::Format_RGBA8888: | 0 | ||||||||||||
167 | break; never executed: break; | 0 | ||||||||||||
168 | case QImage::Format_BGR30: never executed: case QImage::Format_BGR30: | 0 | ||||||||||||
169 | case QImage::Format_A2BGR30_Premultiplied: never executed: case QImage::Format_A2BGR30_Premultiplied: | 0 | ||||||||||||
170 | if (!ctx->isOpenGLES() || ctx->format().majorVersion() >= 3) {
| 0 | ||||||||||||
171 | pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; | - | ||||||||||||
172 | internalFormat = GL_RGB10_A2; | - | ||||||||||||
173 | premultiplied = true; | - | ||||||||||||
174 | } else { never executed: end of block | 0 | ||||||||||||
175 | needsConversion = true; | - | ||||||||||||
176 | } never executed: end of block | 0 | ||||||||||||
177 | break; never executed: break; | 0 | ||||||||||||
178 | case QImage::Format_RGB30: never executed: case QImage::Format_RGB30: | 0 | ||||||||||||
179 | case QImage::Format_A2RGB30_Premultiplied: never executed: case QImage::Format_A2RGB30_Premultiplied: | 0 | ||||||||||||
180 | if (!ctx->isOpenGLES() || ctx->format().majorVersion() >= 3) {
| 0 | ||||||||||||
181 | pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; | - | ||||||||||||
182 | internalFormat = GL_RGB10_A2; | - | ||||||||||||
183 | premultiplied = true; | - | ||||||||||||
184 | swizzle = true; | - | ||||||||||||
185 | } else { never executed: end of block | 0 | ||||||||||||
186 | needsConversion = true; | - | ||||||||||||
187 | } never executed: end of block | 0 | ||||||||||||
188 | break; never executed: break; | 0 | ||||||||||||
189 | default: never executed: default: | 0 | ||||||||||||
190 | needsConversion = true; | - | ||||||||||||
191 | break; never executed: break; | 0 | ||||||||||||
192 | } | - | ||||||||||||
193 | } | - | ||||||||||||
194 | if (needsConversion)
| 0 | ||||||||||||
195 | image = image.convertToFormat(QImage::Format_RGBA8888); never executed: image = image.convertToFormat(QImage::Format_RGBA8888); | 0 | ||||||||||||
196 | - | |||||||||||||
197 | QOpenGLFunctions *funcs = ctx->functions(); | - | ||||||||||||
198 | - | |||||||||||||
199 | QRect rect = subRect; | - | ||||||||||||
200 | if (rect.isNull() || rect == QRect(QPoint(0,0),size)) {
| 0 | ||||||||||||
201 | funcs->glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, size.width(), size.height(), 0, GL_RGBA, pixelType, image.constBits()); | - | ||||||||||||
202 | } else { never executed: end of block | 0 | ||||||||||||
203 | #ifndef QT_OPENGL_ES_2 | - | ||||||||||||
204 | if (!ctx->isOpenGLES()) {
| 0 | ||||||||||||
205 | funcs->glPixelStorei(GL_UNPACK_ROW_LENGTH, image.width()); | - | ||||||||||||
206 | funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x(), rect.y(), rect.width(), rect.height(), GL_RGBA, pixelType, | - | ||||||||||||
207 | image.constScanLine(rect.y()) + rect.x() * 4); | - | ||||||||||||
208 | funcs->glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); | - | ||||||||||||
209 | } else never executed: end of block | 0 | ||||||||||||
210 | #endif | - | ||||||||||||
211 | { | - | ||||||||||||
212 | // if the rect is wide enough it's cheaper to just | - | ||||||||||||
213 | // extend it instead of doing an image copy | - | ||||||||||||
214 | if (rect.width() >= size.width() / 2) {
| 0 | ||||||||||||
215 | rect.setX(0); | - | ||||||||||||
216 | rect.setWidth(size.width()); | - | ||||||||||||
217 | } never executed: end of block | 0 | ||||||||||||
218 | - | |||||||||||||
219 | // if the sub-rect is full-width we can pass the image data directly to | - | ||||||||||||
220 | // OpenGL instead of copying, since there's no gap between scanlines | - | ||||||||||||
221 | - | |||||||||||||
222 | if (rect.width() == size.width()) {
| 0 | ||||||||||||
223 | funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, rect.y(), rect.width(), rect.height(), GL_RGBA, pixelType, | - | ||||||||||||
224 | image.constScanLine(rect.y())); | - | ||||||||||||
225 | } else { never executed: end of block | 0 | ||||||||||||
226 | funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x(), rect.y(), rect.width(), rect.height(), GL_RGBA, pixelType, | - | ||||||||||||
227 | image.copy(rect).constBits()); | - | ||||||||||||
228 | } never executed: end of block | 0 | ||||||||||||
229 | } | - | ||||||||||||
230 | } | - | ||||||||||||
231 | if (swizzleRandB)
| 0 | ||||||||||||
232 | *swizzleRandB = swizzle; never executed: *swizzleRandB = swizzle; | 0 | ||||||||||||
233 | if (premultipliedB)
| 0 | ||||||||||||
234 | *premultipliedB = premultiplied; never executed: *premultipliedB = premultiplied; | 0 | ||||||||||||
235 | - | |||||||||||||
236 | return true; never executed: return true; | 0 | ||||||||||||
237 | - | |||||||||||||
238 | #else | - | ||||||||||||
239 | Q_UNUSED(graphicsBuffer) | - | ||||||||||||
240 | Q_UNUSED(swizzleRandB) | - | ||||||||||||
241 | Q_UNUSED(premultipliedB) | - | ||||||||||||
242 | Q_UNUSED(subRect) | - | ||||||||||||
243 | return false; | - | ||||||||||||
244 | #endif // QT_NO_OPENGL | - | ||||||||||||
245 | } | - | ||||||||||||
246 | - | |||||||||||||
247 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |