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