1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*
|
---|
43 | When the active program changes, we need to update it's uniforms.
|
---|
44 | We could track state for each program and only update stale uniforms
|
---|
45 | - Could lead to lots of overhead if there's a lot of programs
|
---|
46 | We could update all the uniforms when the program changes
|
---|
47 | - Could end up updating lots of uniforms which don't need updating
|
---|
48 |
|
---|
49 | Updating uniforms should be cheap, so the overhead of updating up-to-date
|
---|
50 | uniforms should be minimal. It's also less complex.
|
---|
51 |
|
---|
52 | Things which _may_ cause a different program to be used:
|
---|
53 | - Change in brush/pen style
|
---|
54 | - Change in painter opacity
|
---|
55 | - Change in composition mode
|
---|
56 |
|
---|
57 | Whenever we set a mode on the shader manager - it needs to tell us if it had
|
---|
58 | to switch to a different program.
|
---|
59 |
|
---|
60 | The shader manager should only switch when we tell it to. E.g. if we set a new
|
---|
61 | brush style and then switch to transparent painter, we only want it to compile
|
---|
62 | and use the correct program when we really need it.
|
---|
63 | */
|
---|
64 |
|
---|
65 | // #define QT_OPENGL_CACHE_AS_VBOS
|
---|
66 |
|
---|
67 | #include "qglgradientcache_p.h"
|
---|
68 | #include "qpaintengineex_opengl2_p.h"
|
---|
69 |
|
---|
70 | #include <string.h> //for memcpy
|
---|
71 | #include <qmath.h>
|
---|
72 |
|
---|
73 | #include <private/qgl_p.h>
|
---|
74 | #include <private/qmath_p.h>
|
---|
75 | #include <private/qpaintengineex_p.h>
|
---|
76 | #include <QPaintEngine>
|
---|
77 | #include <private/qpainter_p.h>
|
---|
78 | #include <private/qfontengine_p.h>
|
---|
79 | #include <private/qpixmapdata_gl_p.h>
|
---|
80 | #include <private/qdatabuffer_p.h>
|
---|
81 | #include <private/qstatictext_p.h>
|
---|
82 | #include <private/qtriangulator_p.h>
|
---|
83 |
|
---|
84 | #include "qglengineshadermanager_p.h"
|
---|
85 | #include "qgl2pexvertexarray_p.h"
|
---|
86 | #include "qtriangulatingstroker_p.h"
|
---|
87 | #include "qtextureglyphcache_gl_p.h"
|
---|
88 |
|
---|
89 | #include <QDebug>
|
---|
90 |
|
---|
91 | QT_BEGIN_NAMESPACE
|
---|
92 |
|
---|
93 | #if defined(Q_OS_SYMBIAN)
|
---|
94 | #define QT_GL_NO_SCISSOR_TEST
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | #if defined(Q_WS_WIN)
|
---|
98 | extern Q_GUI_EXPORT bool qt_cleartype_enabled;
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | #ifdef Q_WS_MAC
|
---|
102 | extern bool qt_applefontsmoothing_enabled;
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | Q_DECL_IMPORT extern QImage qt_imageForBrush(int brushStyle, bool invert);
|
---|
106 |
|
---|
107 | ////////////////////////////////// Private Methods //////////////////////////////////////////
|
---|
108 |
|
---|
109 | QGL2PaintEngineExPrivate::~QGL2PaintEngineExPrivate()
|
---|
110 | {
|
---|
111 | delete shaderManager;
|
---|
112 |
|
---|
113 | while (pathCaches.size()) {
|
---|
114 | QVectorPath::CacheEntry *e = *(pathCaches.constBegin());
|
---|
115 | e->cleanup(e->engine, e->data);
|
---|
116 | e->data = 0;
|
---|
117 | e->engine = 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (elementIndicesVBOId != 0) {
|
---|
121 | glDeleteBuffers(1, &elementIndicesVBOId);
|
---|
122 | elementIndicesVBOId = 0;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | void QGL2PaintEngineExPrivate::updateTextureFilter(GLenum target, GLenum wrapMode, bool smoothPixmapTransform, GLuint id)
|
---|
127 | {
|
---|
128 | // glActiveTexture(GL_TEXTURE0 + QT_BRUSH_TEXTURE_UNIT); //### Is it always this texture unit?
|
---|
129 | if (id != GLuint(-1) && id == lastTextureUsed)
|
---|
130 | return;
|
---|
131 |
|
---|
132 | lastTextureUsed = id;
|
---|
133 |
|
---|
134 | if (smoothPixmapTransform) {
|
---|
135 | glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
136 | glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
137 | } else {
|
---|
138 | glTexParameterf(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
139 | glTexParameterf(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
140 | }
|
---|
141 | glTexParameterf(target, GL_TEXTURE_WRAP_S, wrapMode);
|
---|
142 | glTexParameterf(target, GL_TEXTURE_WRAP_T, wrapMode);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | inline QColor qt_premultiplyColor(QColor c, GLfloat opacity)
|
---|
147 | {
|
---|
148 | qreal alpha = c.alphaF() * opacity;
|
---|
149 | c.setAlphaF(alpha);
|
---|
150 | c.setRedF(c.redF() * alpha);
|
---|
151 | c.setGreenF(c.greenF() * alpha);
|
---|
152 | c.setBlueF(c.blueF() * alpha);
|
---|
153 | return c;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void QGL2PaintEngineExPrivate::setBrush(const QBrush& brush)
|
---|
158 | {
|
---|
159 | if (qbrush_fast_equals(currentBrush, brush))
|
---|
160 | return;
|
---|
161 |
|
---|
162 | const Qt::BrushStyle newStyle = qbrush_style(brush);
|
---|
163 | Q_ASSERT(newStyle != Qt::NoBrush);
|
---|
164 |
|
---|
165 | currentBrush = brush;
|
---|
166 | if (!currentBrushPixmap.isNull())
|
---|
167 | currentBrushPixmap = QPixmap();
|
---|
168 | brushUniformsDirty = true; // All brushes have at least one uniform
|
---|
169 |
|
---|
170 | if (newStyle > Qt::SolidPattern)
|
---|
171 | brushTextureDirty = true;
|
---|
172 |
|
---|
173 | if (currentBrush.style() == Qt::TexturePattern
|
---|
174 | && qHasPixmapTexture(brush) && brush.texture().isQBitmap())
|
---|
175 | {
|
---|
176 | shaderManager->setSrcPixelType(QGLEngineShaderManager::TextureSrcWithPattern);
|
---|
177 | } else {
|
---|
178 | shaderManager->setSrcPixelType(newStyle);
|
---|
179 | }
|
---|
180 | shaderManager->optimiseForBrushTransform(currentBrush.transform().type());
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | void QGL2PaintEngineExPrivate::useSimpleShader()
|
---|
185 | {
|
---|
186 | shaderManager->useSimpleProgram();
|
---|
187 |
|
---|
188 | if (matrixDirty)
|
---|
189 | updateMatrix();
|
---|
190 | }
|
---|
191 |
|
---|
192 | void QGL2PaintEngineExPrivate::updateBrushTexture()
|
---|
193 | {
|
---|
194 | Q_Q(QGL2PaintEngineEx);
|
---|
195 | // qDebug("QGL2PaintEngineExPrivate::updateBrushTexture()");
|
---|
196 | Qt::BrushStyle style = currentBrush.style();
|
---|
197 |
|
---|
198 | if ( (style >= Qt::Dense1Pattern) && (style <= Qt::DiagCrossPattern) ) {
|
---|
199 | // Get the image data for the pattern
|
---|
200 | QImage texImage = qt_imageForBrush(style, false);
|
---|
201 |
|
---|
202 | glActiveTexture(GL_TEXTURE0 + QT_BRUSH_TEXTURE_UNIT);
|
---|
203 | ctx->d_func()->bindTexture(texImage, GL_TEXTURE_2D, GL_RGBA, QGLContext::InternalBindOption);
|
---|
204 | updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, q->state()->renderHints & QPainter::SmoothPixmapTransform);
|
---|
205 | }
|
---|
206 | else if (style >= Qt::LinearGradientPattern && style <= Qt::ConicalGradientPattern) {
|
---|
207 | // Gradiant brush: All the gradiants use the same texture
|
---|
208 |
|
---|
209 | const QGradient* g = currentBrush.gradient();
|
---|
210 |
|
---|
211 | // We apply global opacity in the fragment shaders, so we always pass 1.0
|
---|
212 | // for opacity to the cache.
|
---|
213 | GLuint texId = QGL2GradientCache::cacheForContext(ctx)->getBuffer(*g, 1.0);
|
---|
214 |
|
---|
215 | glActiveTexture(GL_TEXTURE0 + QT_BRUSH_TEXTURE_UNIT);
|
---|
216 | glBindTexture(GL_TEXTURE_2D, texId);
|
---|
217 |
|
---|
218 | if (g->spread() == QGradient::RepeatSpread || g->type() == QGradient::ConicalGradient)
|
---|
219 | updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, q->state()->renderHints & QPainter::SmoothPixmapTransform);
|
---|
220 | else if (g->spread() == QGradient::ReflectSpread)
|
---|
221 | updateTextureFilter(GL_TEXTURE_2D, GL_MIRRORED_REPEAT_IBM, q->state()->renderHints & QPainter::SmoothPixmapTransform);
|
---|
222 | else
|
---|
223 | updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE, q->state()->renderHints & QPainter::SmoothPixmapTransform);
|
---|
224 | }
|
---|
225 | else if (style == Qt::TexturePattern) {
|
---|
226 | currentBrushPixmap = currentBrush.texture();
|
---|
227 |
|
---|
228 | int max_texture_size = ctx->d_func()->maxTextureSize();
|
---|
229 | if (currentBrushPixmap.width() > max_texture_size || currentBrushPixmap.height() > max_texture_size)
|
---|
230 | currentBrushPixmap = currentBrushPixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio);
|
---|
231 |
|
---|
232 | glActiveTexture(GL_TEXTURE0 + QT_BRUSH_TEXTURE_UNIT);
|
---|
233 | QGLTexture *tex = ctx->d_func()->bindTexture(currentBrushPixmap, GL_TEXTURE_2D, GL_RGBA,
|
---|
234 | QGLContext::InternalBindOption |
|
---|
235 | QGLContext::CanFlipNativePixmapBindOption);
|
---|
236 | updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, q->state()->renderHints & QPainter::SmoothPixmapTransform);
|
---|
237 | textureInvertedY = tex->options & QGLContext::InvertedYBindOption ? -1 : 1;
|
---|
238 | }
|
---|
239 | brushTextureDirty = false;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | void QGL2PaintEngineExPrivate::updateBrushUniforms()
|
---|
244 | {
|
---|
245 | // qDebug("QGL2PaintEngineExPrivate::updateBrushUniforms()");
|
---|
246 | Qt::BrushStyle style = currentBrush.style();
|
---|
247 |
|
---|
248 | if (style == Qt::NoBrush)
|
---|
249 | return;
|
---|
250 |
|
---|
251 | QTransform brushQTransform = currentBrush.transform();
|
---|
252 |
|
---|
253 | if (style == Qt::SolidPattern) {
|
---|
254 | QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity);
|
---|
255 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::FragmentColor), col);
|
---|
256 | }
|
---|
257 | else {
|
---|
258 | // All other brushes have a transform and thus need the translation point:
|
---|
259 | QPointF translationPoint;
|
---|
260 |
|
---|
261 | if (style <= Qt::DiagCrossPattern) {
|
---|
262 | QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity);
|
---|
263 |
|
---|
264 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col);
|
---|
265 |
|
---|
266 | QVector2D halfViewportSize(width*0.5, height*0.5);
|
---|
267 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::HalfViewportSize), halfViewportSize);
|
---|
268 | }
|
---|
269 | else if (style == Qt::LinearGradientPattern) {
|
---|
270 | const QLinearGradient *g = static_cast<const QLinearGradient *>(currentBrush.gradient());
|
---|
271 |
|
---|
272 | QPointF realStart = g->start();
|
---|
273 | QPointF realFinal = g->finalStop();
|
---|
274 | translationPoint = realStart;
|
---|
275 |
|
---|
276 | QPointF l = realFinal - realStart;
|
---|
277 |
|
---|
278 | QVector3D linearData(
|
---|
279 | l.x(),
|
---|
280 | l.y(),
|
---|
281 | 1.0f / (l.x() * l.x() + l.y() * l.y())
|
---|
282 | );
|
---|
283 |
|
---|
284 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::LinearData), linearData);
|
---|
285 |
|
---|
286 | QVector2D halfViewportSize(width*0.5, height*0.5);
|
---|
287 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::HalfViewportSize), halfViewportSize);
|
---|
288 | }
|
---|
289 | else if (style == Qt::ConicalGradientPattern) {
|
---|
290 | const QConicalGradient *g = static_cast<const QConicalGradient *>(currentBrush.gradient());
|
---|
291 | translationPoint = g->center();
|
---|
292 |
|
---|
293 | GLfloat angle = -(g->angle() * 2 * Q_PI) / 360.0;
|
---|
294 |
|
---|
295 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Angle), angle);
|
---|
296 |
|
---|
297 | QVector2D halfViewportSize(width*0.5, height*0.5);
|
---|
298 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::HalfViewportSize), halfViewportSize);
|
---|
299 | }
|
---|
300 | else if (style == Qt::RadialGradientPattern) {
|
---|
301 | const QRadialGradient *g = static_cast<const QRadialGradient *>(currentBrush.gradient());
|
---|
302 | QPointF realCenter = g->center();
|
---|
303 | QPointF realFocal = g->focalPoint();
|
---|
304 | qreal realRadius = g->radius();
|
---|
305 | translationPoint = realFocal;
|
---|
306 |
|
---|
307 | QPointF fmp = realCenter - realFocal;
|
---|
308 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Fmp), fmp);
|
---|
309 |
|
---|
310 | GLfloat fmp2_m_radius2 = -fmp.x() * fmp.x() - fmp.y() * fmp.y() + realRadius*realRadius;
|
---|
311 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Fmp2MRadius2), fmp2_m_radius2);
|
---|
312 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Inverse2Fmp2MRadius2),
|
---|
313 | GLfloat(1.0 / (2.0*fmp2_m_radius2)));
|
---|
314 |
|
---|
315 | QVector2D halfViewportSize(width*0.5, height*0.5);
|
---|
316 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::HalfViewportSize), halfViewportSize);
|
---|
317 | }
|
---|
318 | else if (style == Qt::TexturePattern) {
|
---|
319 | const QPixmap& texPixmap = currentBrush.texture();
|
---|
320 |
|
---|
321 | if (qHasPixmapTexture(currentBrush) && currentBrush.texture().isQBitmap()) {
|
---|
322 | QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity);
|
---|
323 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col);
|
---|
324 | }
|
---|
325 |
|
---|
326 | QSizeF invertedTextureSize(1.0 / texPixmap.width(), 1.0 / texPixmap.height());
|
---|
327 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::InvertedTextureSize), invertedTextureSize);
|
---|
328 |
|
---|
329 | QVector2D halfViewportSize(width*0.5, height*0.5);
|
---|
330 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::HalfViewportSize), halfViewportSize);
|
---|
331 | }
|
---|
332 | else
|
---|
333 | qWarning("QGL2PaintEngineEx: Unimplemented fill style");
|
---|
334 |
|
---|
335 | const QPointF &brushOrigin = q->state()->brushOrigin;
|
---|
336 | QTransform matrix = q->state()->matrix;
|
---|
337 | matrix.translate(brushOrigin.x(), brushOrigin.y());
|
---|
338 |
|
---|
339 | QTransform translate(1, 0, 0, 1, -translationPoint.x(), -translationPoint.y());
|
---|
340 | QTransform gl_to_qt(1, 0, 0, -1, 0, height);
|
---|
341 | QTransform inv_matrix;
|
---|
342 | if (style == Qt::TexturePattern && textureInvertedY == -1)
|
---|
343 | inv_matrix = gl_to_qt * (QTransform(1, 0, 0, -1, 0, currentBrush.texture().height()) * brushQTransform * matrix).inverted() * translate;
|
---|
344 | else
|
---|
345 | inv_matrix = gl_to_qt * (brushQTransform * matrix).inverted() * translate;
|
---|
346 |
|
---|
347 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::BrushTransform), inv_matrix);
|
---|
348 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::BrushTexture), QT_BRUSH_TEXTURE_UNIT);
|
---|
349 | }
|
---|
350 | brushUniformsDirty = false;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | // This assumes the shader manager has already setup the correct shader program
|
---|
355 | void QGL2PaintEngineExPrivate::updateMatrix()
|
---|
356 | {
|
---|
357 | // qDebug("QGL2PaintEngineExPrivate::updateMatrix()");
|
---|
358 |
|
---|
359 | const QTransform& transform = q->state()->matrix;
|
---|
360 |
|
---|
361 | // The projection matrix converts from Qt's coordinate system to GL's coordinate system
|
---|
362 | // * GL's viewport is 2x2, Qt's is width x height
|
---|
363 | // * GL has +y -> -y going from bottom -> top, Qt is the other way round
|
---|
364 | // * GL has [0,0] in the center, Qt has it in the top-left
|
---|
365 | //
|
---|
366 | // This results in the Projection matrix below, which is multiplied by the painter's
|
---|
367 | // transformation matrix, as shown below:
|
---|
368 | //
|
---|
369 | // Projection Matrix Painter Transform
|
---|
370 | // ------------------------------------------------ ------------------------
|
---|
371 | // | 2.0 / width | 0.0 | -1.0 | | m11 | m21 | dx |
|
---|
372 | // | 0.0 | -2.0 / height | 1.0 | * | m12 | m22 | dy |
|
---|
373 | // | 0.0 | 0.0 | 1.0 | | m13 | m23 | m33 |
|
---|
374 | // ------------------------------------------------ ------------------------
|
---|
375 | //
|
---|
376 | // NOTE: The resultant matrix is also transposed, as GL expects column-major matracies
|
---|
377 |
|
---|
378 | const GLfloat wfactor = 2.0f / width;
|
---|
379 | const GLfloat hfactor = -2.0f / height;
|
---|
380 | GLfloat dx = transform.dx();
|
---|
381 | GLfloat dy = transform.dy();
|
---|
382 |
|
---|
383 | // Non-integer translates can have strange effects for some rendering operations such as
|
---|
384 | // anti-aliased text rendering. In such cases, we snap the translate to the pixel grid.
|
---|
385 | if (snapToPixelGrid && transform.type() == QTransform::TxTranslate) {
|
---|
386 | // 0.50 needs to rounded down to 0.0 for consistency with raster engine:
|
---|
387 | dx = ceilf(dx - 0.5f);
|
---|
388 | dy = ceilf(dy - 0.5f);
|
---|
389 | }
|
---|
390 | #ifndef Q_OS_SYMBIAN
|
---|
391 | if (addOffset) {
|
---|
392 | dx += 0.49f;
|
---|
393 | dy += 0.49f;
|
---|
394 | }
|
---|
395 | #endif
|
---|
396 | pmvMatrix[0][0] = (wfactor * transform.m11()) - transform.m13();
|
---|
397 | pmvMatrix[1][0] = (wfactor * transform.m21()) - transform.m23();
|
---|
398 | pmvMatrix[2][0] = (wfactor * dx) - transform.m33();
|
---|
399 | pmvMatrix[0][1] = (hfactor * transform.m12()) + transform.m13();
|
---|
400 | pmvMatrix[1][1] = (hfactor * transform.m22()) + transform.m23();
|
---|
401 | pmvMatrix[2][1] = (hfactor * dy) + transform.m33();
|
---|
402 | pmvMatrix[0][2] = transform.m13();
|
---|
403 | pmvMatrix[1][2] = transform.m23();
|
---|
404 | pmvMatrix[2][2] = transform.m33();
|
---|
405 |
|
---|
406 | // 1/10000 == 0.0001, so we have good enough res to cover curves
|
---|
407 | // that span the entire widget...
|
---|
408 | inverseScale = qMax(1 / qMax( qMax(qAbs(transform.m11()), qAbs(transform.m22())),
|
---|
409 | qMax(qAbs(transform.m12()), qAbs(transform.m21())) ),
|
---|
410 | qreal(0.0001));
|
---|
411 |
|
---|
412 | matrixDirty = false;
|
---|
413 | matrixUniformDirty = true;
|
---|
414 |
|
---|
415 | // Set the PMV matrix attribute. As we use an attributes rather than uniforms, we only
|
---|
416 | // need to do this once for every matrix change and persists across all shader programs.
|
---|
417 | glVertexAttrib3fv(QT_PMV_MATRIX_1_ATTR, pmvMatrix[0]);
|
---|
418 | glVertexAttrib3fv(QT_PMV_MATRIX_2_ATTR, pmvMatrix[1]);
|
---|
419 | glVertexAttrib3fv(QT_PMV_MATRIX_3_ATTR, pmvMatrix[2]);
|
---|
420 |
|
---|
421 | dasher.setInvScale(inverseScale);
|
---|
422 | stroker.setInvScale(inverseScale);
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | void QGL2PaintEngineExPrivate::updateCompositionMode()
|
---|
427 | {
|
---|
428 | // NOTE: The entire paint engine works on pre-multiplied data - which is why some of these
|
---|
429 | // composition modes look odd.
|
---|
430 | // qDebug() << "QGL2PaintEngineExPrivate::updateCompositionMode() - Setting GL composition mode for " << q->state()->composition_mode;
|
---|
431 | switch(q->state()->composition_mode) {
|
---|
432 | case QPainter::CompositionMode_SourceOver:
|
---|
433 | glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
---|
434 | break;
|
---|
435 | case QPainter::CompositionMode_DestinationOver:
|
---|
436 | glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE);
|
---|
437 | break;
|
---|
438 | case QPainter::CompositionMode_Clear:
|
---|
439 | glBlendFunc(GL_ZERO, GL_ZERO);
|
---|
440 | break;
|
---|
441 | case QPainter::CompositionMode_Source:
|
---|
442 | glBlendFunc(GL_ONE, GL_ZERO);
|
---|
443 | break;
|
---|
444 | case QPainter::CompositionMode_Destination:
|
---|
445 | glBlendFunc(GL_ZERO, GL_ONE);
|
---|
446 | break;
|
---|
447 | case QPainter::CompositionMode_SourceIn:
|
---|
448 | glBlendFunc(GL_DST_ALPHA, GL_ZERO);
|
---|
449 | break;
|
---|
450 | case QPainter::CompositionMode_DestinationIn:
|
---|
451 | glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
|
---|
452 | break;
|
---|
453 | case QPainter::CompositionMode_SourceOut:
|
---|
454 | glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ZERO);
|
---|
455 | break;
|
---|
456 | case QPainter::CompositionMode_DestinationOut:
|
---|
457 | glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
|
---|
458 | break;
|
---|
459 | case QPainter::CompositionMode_SourceAtop:
|
---|
460 | glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
461 | break;
|
---|
462 | case QPainter::CompositionMode_DestinationAtop:
|
---|
463 | glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA);
|
---|
464 | break;
|
---|
465 | case QPainter::CompositionMode_Xor:
|
---|
466 | glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
467 | break;
|
---|
468 | case QPainter::CompositionMode_Plus:
|
---|
469 | glBlendFunc(GL_ONE, GL_ONE);
|
---|
470 | break;
|
---|
471 | default:
|
---|
472 | qWarning("Unsupported composition mode");
|
---|
473 | break;
|
---|
474 | }
|
---|
475 |
|
---|
476 | compositionModeDirty = false;
|
---|
477 | }
|
---|
478 |
|
---|
479 | static inline void setCoords(GLfloat *coords, const QGLRect &rect)
|
---|
480 | {
|
---|
481 | coords[0] = rect.left;
|
---|
482 | coords[1] = rect.top;
|
---|
483 | coords[2] = rect.right;
|
---|
484 | coords[3] = rect.top;
|
---|
485 | coords[4] = rect.right;
|
---|
486 | coords[5] = rect.bottom;
|
---|
487 | coords[6] = rect.left;
|
---|
488 | coords[7] = rect.bottom;
|
---|
489 | }
|
---|
490 |
|
---|
491 | void QGL2PaintEngineExPrivate::drawTexture(const QGLRect& dest, const QGLRect& src, const QSize &textureSize, bool opaque, bool pattern)
|
---|
492 | {
|
---|
493 | // Setup for texture drawing
|
---|
494 | currentBrush = noBrush;
|
---|
495 | shaderManager->setSrcPixelType(pattern ? QGLEngineShaderManager::PatternSrc : QGLEngineShaderManager::ImageSrc);
|
---|
496 |
|
---|
497 | if (addOffset) {
|
---|
498 | addOffset = false;
|
---|
499 | matrixDirty = true;
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (snapToPixelGrid) {
|
---|
503 | snapToPixelGrid = false;
|
---|
504 | matrixDirty = true;
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (prepareForDraw(opaque))
|
---|
508 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT);
|
---|
509 |
|
---|
510 | if (pattern) {
|
---|
511 | QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity);
|
---|
512 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col);
|
---|
513 | }
|
---|
514 |
|
---|
515 | GLfloat dx = 1.0 / textureSize.width();
|
---|
516 | GLfloat dy = 1.0 / textureSize.height();
|
---|
517 |
|
---|
518 | QGLRect srcTextureRect(src.left*dx, src.top*dy, src.right*dx, src.bottom*dy);
|
---|
519 |
|
---|
520 | setCoords(staticVertexCoordinateArray, dest);
|
---|
521 | setCoords(staticTextureCoordinateArray, srcTextureRect);
|
---|
522 |
|
---|
523 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
---|
524 | }
|
---|
525 |
|
---|
526 | void QGL2PaintEngineEx::beginNativePainting()
|
---|
527 | {
|
---|
528 | Q_D(QGL2PaintEngineEx);
|
---|
529 | ensureActive();
|
---|
530 | d->transferMode(BrushDrawingMode);
|
---|
531 |
|
---|
532 | d->nativePaintingActive = true;
|
---|
533 |
|
---|
534 | QGLContext *ctx = d->ctx;
|
---|
535 | glUseProgram(0);
|
---|
536 |
|
---|
537 | // Disable all the vertex attribute arrays:
|
---|
538 | for (int i = 0; i < QT_GL_VERTEX_ARRAY_TRACKED_COUNT; ++i)
|
---|
539 | glDisableVertexAttribArray(i);
|
---|
540 |
|
---|
541 | #ifndef QT_OPENGL_ES_2
|
---|
542 | // be nice to people who mix OpenGL 1.x code with QPainter commands
|
---|
543 | // by setting modelview and projection matrices to mirror the GL 1
|
---|
544 | // paint engine
|
---|
545 | const QTransform& mtx = state()->matrix;
|
---|
546 |
|
---|
547 | float mv_matrix[4][4] =
|
---|
548 | {
|
---|
549 | { float(mtx.m11()), float(mtx.m12()), 0, float(mtx.m13()) },
|
---|
550 | { float(mtx.m21()), float(mtx.m22()), 0, float(mtx.m23()) },
|
---|
551 | { 0, 0, 1, 0 },
|
---|
552 | { float(mtx.dx()), float(mtx.dy()), 0, float(mtx.m33()) }
|
---|
553 | };
|
---|
554 |
|
---|
555 | const QSize sz = d->device->size();
|
---|
556 |
|
---|
557 | glMatrixMode(GL_PROJECTION);
|
---|
558 | glLoadIdentity();
|
---|
559 | glOrtho(0, sz.width(), sz.height(), 0, -999999, 999999);
|
---|
560 |
|
---|
561 | glMatrixMode(GL_MODELVIEW);
|
---|
562 | glLoadMatrixf(&mv_matrix[0][0]);
|
---|
563 | #else
|
---|
564 | Q_UNUSED(ctx);
|
---|
565 | #endif
|
---|
566 |
|
---|
567 | d->lastTextureUsed = GLuint(-1);
|
---|
568 | d->dirtyStencilRegion = QRect(0, 0, d->width, d->height);
|
---|
569 | d->resetGLState();
|
---|
570 |
|
---|
571 | d->shaderManager->setDirty();
|
---|
572 |
|
---|
573 | d->needsSync = true;
|
---|
574 | }
|
---|
575 |
|
---|
576 | void QGL2PaintEngineExPrivate::resetGLState()
|
---|
577 | {
|
---|
578 | glDisable(GL_BLEND);
|
---|
579 | glActiveTexture(GL_TEXTURE0);
|
---|
580 | glDisable(GL_STENCIL_TEST);
|
---|
581 | glDisable(GL_DEPTH_TEST);
|
---|
582 | glDisable(GL_SCISSOR_TEST);
|
---|
583 | glDepthMask(true);
|
---|
584 | glDepthFunc(GL_LESS);
|
---|
585 | glClearDepth(1);
|
---|
586 | glStencilMask(0xff);
|
---|
587 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
|
---|
588 | glStencilFunc(GL_ALWAYS, 0, 0xff);
|
---|
589 | ctx->d_func()->setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, false);
|
---|
590 | ctx->d_func()->setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, false);
|
---|
591 | ctx->d_func()->setVertexAttribArrayEnabled(QT_OPACITY_ATTR, false);
|
---|
592 | #ifndef QT_OPENGL_ES_2
|
---|
593 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // color may have been changed by glVertexAttrib()
|
---|
594 | #endif
|
---|
595 | }
|
---|
596 |
|
---|
597 | void QGL2PaintEngineEx::endNativePainting()
|
---|
598 | {
|
---|
599 | Q_D(QGL2PaintEngineEx);
|
---|
600 | d->needsSync = true;
|
---|
601 | d->nativePaintingActive = false;
|
---|
602 | }
|
---|
603 |
|
---|
604 | void QGL2PaintEngineEx::invalidateState()
|
---|
605 | {
|
---|
606 | Q_D(QGL2PaintEngineEx);
|
---|
607 | d->needsSync = true;
|
---|
608 | }
|
---|
609 |
|
---|
610 | bool QGL2PaintEngineEx::isNativePaintingActive() const {
|
---|
611 | Q_D(const QGL2PaintEngineEx);
|
---|
612 | return d->nativePaintingActive;
|
---|
613 | }
|
---|
614 |
|
---|
615 | void QGL2PaintEngineExPrivate::transferMode(EngineMode newMode)
|
---|
616 | {
|
---|
617 | if (newMode == mode)
|
---|
618 | return;
|
---|
619 |
|
---|
620 | if (mode == TextDrawingMode || mode == ImageDrawingMode || mode == ImageArrayDrawingMode) {
|
---|
621 | lastTextureUsed = GLuint(-1);
|
---|
622 | }
|
---|
623 |
|
---|
624 | if (newMode == TextDrawingMode) {
|
---|
625 | shaderManager->setHasComplexGeometry(true);
|
---|
626 | } else {
|
---|
627 | shaderManager->setHasComplexGeometry(false);
|
---|
628 | }
|
---|
629 |
|
---|
630 | if (newMode == ImageDrawingMode) {
|
---|
631 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray);
|
---|
632 | setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, staticTextureCoordinateArray);
|
---|
633 | }
|
---|
634 |
|
---|
635 | if (newMode == ImageArrayDrawingMode) {
|
---|
636 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data());
|
---|
637 | setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data());
|
---|
638 | setVertexAttributePointer(QT_OPACITY_ATTR, (GLfloat*)opacityArray.data());
|
---|
639 | }
|
---|
640 |
|
---|
641 | // This needs to change when we implement high-quality anti-aliasing...
|
---|
642 | if (newMode != TextDrawingMode)
|
---|
643 | shaderManager->setMaskType(QGLEngineShaderManager::NoMask);
|
---|
644 |
|
---|
645 | mode = newMode;
|
---|
646 | }
|
---|
647 |
|
---|
648 | struct QGL2PEVectorPathCache
|
---|
649 | {
|
---|
650 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
651 | GLuint vbo;
|
---|
652 | GLuint ibo;
|
---|
653 | #else
|
---|
654 | float *vertices;
|
---|
655 | void *indices;
|
---|
656 | #endif
|
---|
657 | int vertexCount;
|
---|
658 | int indexCount;
|
---|
659 | GLenum primitiveType;
|
---|
660 | qreal iscale;
|
---|
661 | };
|
---|
662 |
|
---|
663 | void QGL2PaintEngineExPrivate::cleanupVectorPath(QPaintEngineEx *engine, void *data)
|
---|
664 | {
|
---|
665 | QGL2PEVectorPathCache *c = (QGL2PEVectorPathCache *) data;
|
---|
666 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
667 | Q_ASSERT(engine->type() == QPaintEngine::OpenGL2);
|
---|
668 | static_cast<QGL2PaintEngineEx *>(engine)->d_func()->unusedVBOSToClean << c->vbo;
|
---|
669 | if (c->ibo)
|
---|
670 | d->unusedIBOSToClean << c->ibo;
|
---|
671 | #else
|
---|
672 | Q_UNUSED(engine);
|
---|
673 | qFree(c->vertices);
|
---|
674 | qFree(c->indices);
|
---|
675 | #endif
|
---|
676 | delete c;
|
---|
677 | }
|
---|
678 |
|
---|
679 | // Assumes everything is configured for the brush you want to use
|
---|
680 | void QGL2PaintEngineExPrivate::fill(const QVectorPath& path)
|
---|
681 | {
|
---|
682 | transferMode(BrushDrawingMode);
|
---|
683 |
|
---|
684 | const QOpenGL2PaintEngineState *s = q->state();
|
---|
685 | const bool newAddOffset = !(s->renderHints & QPainter::Antialiasing) &&
|
---|
686 | (qbrush_style(currentBrush) == Qt::SolidPattern) &&
|
---|
687 | !multisamplingAlwaysEnabled;
|
---|
688 |
|
---|
689 | if (addOffset != newAddOffset) {
|
---|
690 | addOffset = newAddOffset;
|
---|
691 | matrixDirty = true;
|
---|
692 | }
|
---|
693 |
|
---|
694 | if (snapToPixelGrid) {
|
---|
695 | snapToPixelGrid = false;
|
---|
696 | matrixDirty = true;
|
---|
697 | }
|
---|
698 |
|
---|
699 | // Might need to call updateMatrix to re-calculate inverseScale
|
---|
700 | if (matrixDirty)
|
---|
701 | updateMatrix();
|
---|
702 |
|
---|
703 | const QPointF* const points = reinterpret_cast<const QPointF*>(path.points());
|
---|
704 |
|
---|
705 | // Check to see if there's any hints
|
---|
706 | if (path.shape() == QVectorPath::RectangleHint) {
|
---|
707 | QGLRect rect(points[0].x(), points[0].y(), points[2].x(), points[2].y());
|
---|
708 | prepareForDraw(currentBrush.isOpaque());
|
---|
709 | composite(rect);
|
---|
710 | } else if (path.isConvex()) {
|
---|
711 |
|
---|
712 | if (path.isCacheable()) {
|
---|
713 | QVectorPath::CacheEntry *data = path.lookupCacheData(q);
|
---|
714 | QGL2PEVectorPathCache *cache;
|
---|
715 |
|
---|
716 | bool updateCache = false;
|
---|
717 |
|
---|
718 | if (data) {
|
---|
719 | cache = (QGL2PEVectorPathCache *) data->data;
|
---|
720 | // Check if scale factor is exceeded for curved paths and generate curves if so...
|
---|
721 | if (path.isCurved()) {
|
---|
722 | qreal scaleFactor = cache->iscale / inverseScale;
|
---|
723 | if (scaleFactor < 0.5 || scaleFactor > 2.0) {
|
---|
724 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
725 | glDeleteBuffers(1, &cache->vbo);
|
---|
726 | cache->vbo = 0;
|
---|
727 | Q_ASSERT(cache->ibo == 0);
|
---|
728 | #else
|
---|
729 | qFree(cache->vertices);
|
---|
730 | Q_ASSERT(cache->indices == 0);
|
---|
731 | #endif
|
---|
732 | updateCache = true;
|
---|
733 | }
|
---|
734 | }
|
---|
735 | } else {
|
---|
736 | cache = new QGL2PEVectorPathCache;
|
---|
737 | data = const_cast<QVectorPath &>(path).addCacheData(q, cache, cleanupVectorPath);
|
---|
738 | updateCache = true;
|
---|
739 | }
|
---|
740 |
|
---|
741 | // Flatten the path at the current scale factor and fill it into the cache struct.
|
---|
742 | if (updateCache) {
|
---|
743 | vertexCoordinateArray.clear();
|
---|
744 | vertexCoordinateArray.addPath(path, inverseScale, false);
|
---|
745 | int vertexCount = vertexCoordinateArray.vertexCount();
|
---|
746 | int floatSizeInBytes = vertexCount * 2 * sizeof(float);
|
---|
747 | cache->vertexCount = vertexCount;
|
---|
748 | cache->indexCount = 0;
|
---|
749 | cache->primitiveType = GL_TRIANGLE_FAN;
|
---|
750 | cache->iscale = inverseScale;
|
---|
751 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
752 | glGenBuffers(1, &cache->vbo);
|
---|
753 | glBindBuffer(GL_ARRAY_BUFFER, cache->vbo);
|
---|
754 | glBufferData(GL_ARRAY_BUFFER, floatSizeInBytes, vertexCoordinateArray.data(), GL_STATIC_DRAW);
|
---|
755 | cache->ibo = 0;
|
---|
756 | #else
|
---|
757 | cache->vertices = (float *) qMalloc(floatSizeInBytes);
|
---|
758 | memcpy(cache->vertices, vertexCoordinateArray.data(), floatSizeInBytes);
|
---|
759 | cache->indices = 0;
|
---|
760 | #endif
|
---|
761 | }
|
---|
762 |
|
---|
763 | prepareForDraw(currentBrush.isOpaque());
|
---|
764 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
765 | glBindBuffer(GL_ARRAY_BUFFER, cache->vbo);
|
---|
766 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, 0);
|
---|
767 | #else
|
---|
768 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, cache->vertices);
|
---|
769 | #endif
|
---|
770 | glDrawArrays(cache->primitiveType, 0, cache->vertexCount);
|
---|
771 |
|
---|
772 | } else {
|
---|
773 | // printf(" - Marking path as cachable...\n");
|
---|
774 | // Tag it for later so that if the same path is drawn twice, it is assumed to be static and thus cachable
|
---|
775 | path.makeCacheable();
|
---|
776 | vertexCoordinateArray.clear();
|
---|
777 | vertexCoordinateArray.addPath(path, inverseScale, false);
|
---|
778 | prepareForDraw(currentBrush.isOpaque());
|
---|
779 | drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN);
|
---|
780 | }
|
---|
781 |
|
---|
782 | } else {
|
---|
783 | bool useCache = path.isCacheable();
|
---|
784 | if (useCache) {
|
---|
785 | QRectF bbox = path.controlPointRect();
|
---|
786 | // If the path doesn't fit within these limits, it is possible that the triangulation will fail.
|
---|
787 | useCache &= (bbox.left() > -0x8000 * inverseScale)
|
---|
788 | && (bbox.right() < 0x8000 * inverseScale)
|
---|
789 | && (bbox.top() > -0x8000 * inverseScale)
|
---|
790 | && (bbox.bottom() < 0x8000 * inverseScale);
|
---|
791 | }
|
---|
792 |
|
---|
793 | if (useCache) {
|
---|
794 | QVectorPath::CacheEntry *data = path.lookupCacheData(q);
|
---|
795 | QGL2PEVectorPathCache *cache;
|
---|
796 |
|
---|
797 | bool updateCache = false;
|
---|
798 |
|
---|
799 | if (data) {
|
---|
800 | cache = (QGL2PEVectorPathCache *) data->data;
|
---|
801 | // Check if scale factor is exceeded for curved paths and generate curves if so...
|
---|
802 | if (path.isCurved()) {
|
---|
803 | qreal scaleFactor = cache->iscale / inverseScale;
|
---|
804 | if (scaleFactor < 0.5 || scaleFactor > 2.0) {
|
---|
805 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
806 | glDeleteBuffers(1, &cache->vbo);
|
---|
807 | glDeleteBuffers(1, &cache->ibo);
|
---|
808 | #else
|
---|
809 | qFree(cache->vertices);
|
---|
810 | qFree(cache->indices);
|
---|
811 | #endif
|
---|
812 | updateCache = true;
|
---|
813 | }
|
---|
814 | }
|
---|
815 | } else {
|
---|
816 | cache = new QGL2PEVectorPathCache;
|
---|
817 | data = const_cast<QVectorPath &>(path).addCacheData(q, cache, cleanupVectorPath);
|
---|
818 | updateCache = true;
|
---|
819 | }
|
---|
820 |
|
---|
821 | // Flatten the path at the current scale factor and fill it into the cache struct.
|
---|
822 | if (updateCache) {
|
---|
823 | QTriangleSet polys = qTriangulate(path, QTransform().scale(1 / inverseScale, 1 / inverseScale));
|
---|
824 | cache->vertexCount = polys.vertices.size() / 2;
|
---|
825 | cache->indexCount = polys.indices.size();
|
---|
826 | cache->primitiveType = GL_TRIANGLES;
|
---|
827 | cache->iscale = inverseScale;
|
---|
828 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
829 | glGenBuffers(1, &cache->vbo);
|
---|
830 | glGenBuffers(1, &cache->ibo);
|
---|
831 | glBindBuffer(GL_ARRAY_BUFFER, cache->vbo);
|
---|
832 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cache->ibo);
|
---|
833 |
|
---|
834 | if (QGLExtensions::glExtensions() & QGLExtensions::ElementIndexUint)
|
---|
835 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quint32) * polys.indices.size(), polys.indices.data(), GL_STATIC_DRAW);
|
---|
836 | else
|
---|
837 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quint16) * polys.indices.size(), polys.indices.data(), GL_STATIC_DRAW);
|
---|
838 |
|
---|
839 | QVarLengthArray<float> vertices(polys.vertices.size());
|
---|
840 | for (int i = 0; i < polys.vertices.size(); ++i)
|
---|
841 | vertices[i] = float(inverseScale * polys.vertices.at(i));
|
---|
842 | glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
|
---|
843 | #else
|
---|
844 | cache->vertices = (float *) qMalloc(sizeof(float) * polys.vertices.size());
|
---|
845 | if (QGLExtensions::glExtensions() & QGLExtensions::ElementIndexUint) {
|
---|
846 | cache->indices = (quint32 *) qMalloc(sizeof(quint32) * polys.indices.size());
|
---|
847 | memcpy(cache->indices, polys.indices.data(), sizeof(quint32) * polys.indices.size());
|
---|
848 | } else {
|
---|
849 | cache->indices = (quint16 *) qMalloc(sizeof(quint16) * polys.indices.size());
|
---|
850 | memcpy(cache->indices, polys.indices.data(), sizeof(quint16) * polys.indices.size());
|
---|
851 | }
|
---|
852 | for (int i = 0; i < polys.vertices.size(); ++i)
|
---|
853 | cache->vertices[i] = float(inverseScale * polys.vertices.at(i));
|
---|
854 | #endif
|
---|
855 | }
|
---|
856 |
|
---|
857 | prepareForDraw(currentBrush.isOpaque());
|
---|
858 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
859 | glBindBuffer(GL_ARRAY_BUFFER, cache->vbo);
|
---|
860 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cache->ibo);
|
---|
861 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, 0);
|
---|
862 | if (QGLExtensions::glExtensions() & QGLExtensions::ElementIndexUint)
|
---|
863 | glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_INT, 0);
|
---|
864 | else
|
---|
865 | glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_SHORT, 0);
|
---|
866 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
---|
867 | glBindBuffer(GL_ARRAY_BUFFER, 0);
|
---|
868 | #else
|
---|
869 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, cache->vertices);
|
---|
870 | if (QGLExtensions::glExtensions() & QGLExtensions::ElementIndexUint)
|
---|
871 | glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_INT, (qint32 *)cache->indices);
|
---|
872 | else
|
---|
873 | glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_SHORT, (qint16 *)cache->indices);
|
---|
874 | #endif
|
---|
875 |
|
---|
876 | } else {
|
---|
877 | // printf(" - Marking path as cachable...\n");
|
---|
878 | // Tag it for later so that if the same path is drawn twice, it is assumed to be static and thus cachable
|
---|
879 | path.makeCacheable();
|
---|
880 |
|
---|
881 | // The path is too complicated & needs the stencil technique
|
---|
882 | vertexCoordinateArray.clear();
|
---|
883 | vertexCoordinateArray.addPath(path, inverseScale, false);
|
---|
884 |
|
---|
885 | fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill());
|
---|
886 |
|
---|
887 | glStencilMask(0xff);
|
---|
888 | glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
|
---|
889 |
|
---|
890 | if (q->state()->clipTestEnabled) {
|
---|
891 | // Pass when high bit is set, replace stencil value with current clip
|
---|
892 | glStencilFunc(GL_NOTEQUAL, q->state()->currentClip, GL_STENCIL_HIGH_BIT);
|
---|
893 | } else if (path.hasWindingFill()) {
|
---|
894 | // Pass when any bit is set, replace stencil value with 0
|
---|
895 | glStencilFunc(GL_NOTEQUAL, 0, 0xff);
|
---|
896 | } else {
|
---|
897 | // Pass when high bit is set, replace stencil value with 0
|
---|
898 | glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT);
|
---|
899 | }
|
---|
900 | prepareForDraw(currentBrush.isOpaque());
|
---|
901 |
|
---|
902 | // Stencil the brush onto the dest buffer
|
---|
903 | composite(vertexCoordinateArray.boundingRect());
|
---|
904 | glStencilMask(0);
|
---|
905 | updateClipScissorTest();
|
---|
906 | }
|
---|
907 | }
|
---|
908 | }
|
---|
909 |
|
---|
910 |
|
---|
911 | void QGL2PaintEngineExPrivate::fillStencilWithVertexArray(const float *data,
|
---|
912 | int count,
|
---|
913 | int *stops,
|
---|
914 | int stopCount,
|
---|
915 | const QGLRect &bounds,
|
---|
916 | StencilFillMode mode)
|
---|
917 | {
|
---|
918 | Q_ASSERT(count || stops);
|
---|
919 |
|
---|
920 | // qDebug("QGL2PaintEngineExPrivate::fillStencilWithVertexArray()");
|
---|
921 | glStencilMask(0xff); // Enable stencil writes
|
---|
922 |
|
---|
923 | if (dirtyStencilRegion.intersects(currentScissorBounds)) {
|
---|
924 | QVector<QRect> clearRegion = dirtyStencilRegion.intersected(currentScissorBounds).rects();
|
---|
925 | glClearStencil(0); // Clear to zero
|
---|
926 | for (int i = 0; i < clearRegion.size(); ++i) {
|
---|
927 | #ifndef QT_GL_NO_SCISSOR_TEST
|
---|
928 | setScissor(clearRegion.at(i));
|
---|
929 | #endif
|
---|
930 | glClear(GL_STENCIL_BUFFER_BIT);
|
---|
931 | }
|
---|
932 |
|
---|
933 | dirtyStencilRegion -= currentScissorBounds;
|
---|
934 |
|
---|
935 | #ifndef QT_GL_NO_SCISSOR_TEST
|
---|
936 | updateClipScissorTest();
|
---|
937 | #endif
|
---|
938 | }
|
---|
939 |
|
---|
940 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // Disable color writes
|
---|
941 | useSimpleShader();
|
---|
942 | glEnable(GL_STENCIL_TEST); // For some reason, this has to happen _after_ the simple shader is use()'d
|
---|
943 |
|
---|
944 | if (mode == WindingFillMode) {
|
---|
945 | Q_ASSERT(stops && !count);
|
---|
946 | if (q->state()->clipTestEnabled) {
|
---|
947 | // Flatten clip values higher than current clip, and set high bit to match current clip
|
---|
948 | glStencilFunc(GL_LEQUAL, GL_STENCIL_HIGH_BIT | q->state()->currentClip, ~GL_STENCIL_HIGH_BIT);
|
---|
949 | glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
|
---|
950 | composite(bounds);
|
---|
951 |
|
---|
952 | glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT, GL_STENCIL_HIGH_BIT);
|
---|
953 | } else if (!stencilClean) {
|
---|
954 | // Clear stencil buffer within bounding rect
|
---|
955 | glStencilFunc(GL_ALWAYS, 0, 0xff);
|
---|
956 | glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
|
---|
957 | composite(bounds);
|
---|
958 | }
|
---|
959 |
|
---|
960 | // Inc. for front-facing triangle
|
---|
961 | glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_INCR_WRAP);
|
---|
962 | // Dec. for back-facing "holes"
|
---|
963 | glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_DECR_WRAP);
|
---|
964 | glStencilMask(~GL_STENCIL_HIGH_BIT);
|
---|
965 | drawVertexArrays(data, stops, stopCount, GL_TRIANGLE_FAN);
|
---|
966 |
|
---|
967 | if (q->state()->clipTestEnabled) {
|
---|
968 | // Clear high bit of stencil outside of path
|
---|
969 | glStencilFunc(GL_EQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT);
|
---|
970 | glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
|
---|
971 | glStencilMask(GL_STENCIL_HIGH_BIT);
|
---|
972 | composite(bounds);
|
---|
973 | }
|
---|
974 | } else if (mode == OddEvenFillMode) {
|
---|
975 | glStencilMask(GL_STENCIL_HIGH_BIT);
|
---|
976 | glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit
|
---|
977 | drawVertexArrays(data, stops, stopCount, GL_TRIANGLE_FAN);
|
---|
978 |
|
---|
979 | } else { // TriStripStrokeFillMode
|
---|
980 | Q_ASSERT(count && !stops); // tristrips generated directly, so no vertexArray or stops
|
---|
981 | glStencilMask(GL_STENCIL_HIGH_BIT);
|
---|
982 | #if 0
|
---|
983 | glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit
|
---|
984 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, data);
|
---|
985 | glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
|
---|
986 | #else
|
---|
987 |
|
---|
988 | glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
|
---|
989 | if (q->state()->clipTestEnabled) {
|
---|
990 | glStencilFunc(GL_LEQUAL, q->state()->currentClip | GL_STENCIL_HIGH_BIT,
|
---|
991 | ~GL_STENCIL_HIGH_BIT);
|
---|
992 | } else {
|
---|
993 | glStencilFunc(GL_ALWAYS, GL_STENCIL_HIGH_BIT, 0xff);
|
---|
994 | }
|
---|
995 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, data);
|
---|
996 | glDrawArrays(GL_TRIANGLE_STRIP, 0, count);
|
---|
997 | #endif
|
---|
998 | }
|
---|
999 |
|
---|
1000 | // Enable color writes & disable stencil writes
|
---|
1001 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | If the maximum value in the stencil buffer is GL_STENCIL_HIGH_BIT - 1,
|
---|
1006 | restore the stencil buffer to a pristine state. The current clip region
|
---|
1007 | is set to 1, and the rest to 0.
|
---|
1008 | */
|
---|
1009 | void QGL2PaintEngineExPrivate::resetClipIfNeeded()
|
---|
1010 | {
|
---|
1011 | if (maxClip != (GL_STENCIL_HIGH_BIT - 1))
|
---|
1012 | return;
|
---|
1013 |
|
---|
1014 | Q_Q(QGL2PaintEngineEx);
|
---|
1015 |
|
---|
1016 | useSimpleShader();
|
---|
1017 | glEnable(GL_STENCIL_TEST);
|
---|
1018 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
1019 |
|
---|
1020 | QRectF bounds = q->state()->matrix.inverted().mapRect(QRectF(0, 0, width, height));
|
---|
1021 | QGLRect rect(bounds.left(), bounds.top(), bounds.right(), bounds.bottom());
|
---|
1022 |
|
---|
1023 | // Set high bit on clip region
|
---|
1024 | glStencilFunc(GL_LEQUAL, q->state()->currentClip, 0xff);
|
---|
1025 | glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT);
|
---|
1026 | glStencilMask(GL_STENCIL_HIGH_BIT);
|
---|
1027 | composite(rect);
|
---|
1028 |
|
---|
1029 | // Reset clipping to 1 and everything else to zero
|
---|
1030 | glStencilFunc(GL_NOTEQUAL, 0x01, GL_STENCIL_HIGH_BIT);
|
---|
1031 | glStencilOp(GL_ZERO, GL_REPLACE, GL_REPLACE);
|
---|
1032 | glStencilMask(0xff);
|
---|
1033 | composite(rect);
|
---|
1034 |
|
---|
1035 | q->state()->currentClip = 1;
|
---|
1036 | q->state()->canRestoreClip = false;
|
---|
1037 |
|
---|
1038 | maxClip = 1;
|
---|
1039 |
|
---|
1040 | glStencilMask(0x0);
|
---|
1041 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | bool QGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque)
|
---|
1045 | {
|
---|
1046 | if (brushTextureDirty && mode != ImageDrawingMode && mode != ImageArrayDrawingMode)
|
---|
1047 | updateBrushTexture();
|
---|
1048 |
|
---|
1049 | if (compositionModeDirty)
|
---|
1050 | updateCompositionMode();
|
---|
1051 |
|
---|
1052 | if (matrixDirty)
|
---|
1053 | updateMatrix();
|
---|
1054 |
|
---|
1055 | const bool stateHasOpacity = q->state()->opacity < 0.99f;
|
---|
1056 | if (q->state()->composition_mode == QPainter::CompositionMode_Source
|
---|
1057 | || (q->state()->composition_mode == QPainter::CompositionMode_SourceOver
|
---|
1058 | && srcPixelsAreOpaque && !stateHasOpacity))
|
---|
1059 | {
|
---|
1060 | glDisable(GL_BLEND);
|
---|
1061 | } else {
|
---|
1062 | glEnable(GL_BLEND);
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | QGLEngineShaderManager::OpacityMode opacityMode;
|
---|
1066 | if (mode == ImageArrayDrawingMode) {
|
---|
1067 | opacityMode = QGLEngineShaderManager::AttributeOpacity;
|
---|
1068 | } else {
|
---|
1069 | opacityMode = stateHasOpacity ? QGLEngineShaderManager::UniformOpacity
|
---|
1070 | : QGLEngineShaderManager::NoOpacity;
|
---|
1071 | if (stateHasOpacity && (mode != ImageDrawingMode)) {
|
---|
1072 | // Using a brush
|
---|
1073 | bool brushIsPattern = (currentBrush.style() >= Qt::Dense1Pattern) &&
|
---|
1074 | (currentBrush.style() <= Qt::DiagCrossPattern);
|
---|
1075 |
|
---|
1076 | if ((currentBrush.style() == Qt::SolidPattern) || brushIsPattern)
|
---|
1077 | opacityMode = QGLEngineShaderManager::NoOpacity; // Global opacity handled by srcPixel shader
|
---|
1078 | }
|
---|
1079 | }
|
---|
1080 | shaderManager->setOpacityMode(opacityMode);
|
---|
1081 |
|
---|
1082 | bool changed = shaderManager->useCorrectShaderProg();
|
---|
1083 | // If the shader program needs changing, we change it and mark all uniforms as dirty
|
---|
1084 | if (changed) {
|
---|
1085 | // The shader program has changed so mark all uniforms as dirty:
|
---|
1086 | brushUniformsDirty = true;
|
---|
1087 | opacityUniformDirty = true;
|
---|
1088 | matrixUniformDirty = true;
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | if (brushUniformsDirty && mode != ImageDrawingMode && mode != ImageArrayDrawingMode)
|
---|
1092 | updateBrushUniforms();
|
---|
1093 |
|
---|
1094 | if (opacityMode == QGLEngineShaderManager::UniformOpacity && opacityUniformDirty) {
|
---|
1095 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::GlobalOpacity), (GLfloat)q->state()->opacity);
|
---|
1096 | opacityUniformDirty = false;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | if (matrixUniformDirty && shaderManager->hasComplexGeometry()) {
|
---|
1100 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::Matrix),
|
---|
1101 | pmvMatrix);
|
---|
1102 | matrixUniformDirty = false;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | return changed;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | void QGL2PaintEngineExPrivate::composite(const QGLRect& boundingRect)
|
---|
1109 | {
|
---|
1110 | setCoords(staticVertexCoordinateArray, boundingRect);
|
---|
1111 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray);
|
---|
1112 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | // Draws the vertex array as a set of <vertexArrayStops.size()> triangle fans.
|
---|
1116 | void QGL2PaintEngineExPrivate::drawVertexArrays(const float *data, int *stops, int stopCount,
|
---|
1117 | GLenum primitive)
|
---|
1118 | {
|
---|
1119 | // Now setup the pointer to the vertex array:
|
---|
1120 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)data);
|
---|
1121 |
|
---|
1122 | int previousStop = 0;
|
---|
1123 | for (int i=0; i<stopCount; ++i) {
|
---|
1124 | int stop = stops[i];
|
---|
1125 | /*
|
---|
1126 | qDebug("Drawing triangle fan for vertecies %d -> %d:", previousStop, stop-1);
|
---|
1127 | for (int i=previousStop; i<stop; ++i)
|
---|
1128 | qDebug(" %02d: [%.2f, %.2f]", i, vertexArray.data()[i].x, vertexArray.data()[i].y);
|
---|
1129 | */
|
---|
1130 | glDrawArrays(primitive, previousStop, stop - previousStop);
|
---|
1131 | previousStop = stop;
|
---|
1132 | }
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | /////////////////////////////////// Public Methods //////////////////////////////////////////
|
---|
1136 |
|
---|
1137 | QGL2PaintEngineEx::QGL2PaintEngineEx()
|
---|
1138 | : QPaintEngineEx(*(new QGL2PaintEngineExPrivate(this)))
|
---|
1139 | {
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | QGL2PaintEngineEx::~QGL2PaintEngineEx()
|
---|
1143 | {
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | void QGL2PaintEngineEx::fill(const QVectorPath &path, const QBrush &brush)
|
---|
1147 | {
|
---|
1148 | Q_D(QGL2PaintEngineEx);
|
---|
1149 |
|
---|
1150 | if (qbrush_style(brush) == Qt::NoBrush)
|
---|
1151 | return;
|
---|
1152 | ensureActive();
|
---|
1153 | d->setBrush(brush);
|
---|
1154 | d->fill(path);
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | extern Q_GUI_EXPORT bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
|
---|
1158 |
|
---|
1159 |
|
---|
1160 | void QGL2PaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
|
---|
1161 | {
|
---|
1162 | Q_D(QGL2PaintEngineEx);
|
---|
1163 |
|
---|
1164 | const QBrush &penBrush = qpen_brush(pen);
|
---|
1165 | if (qpen_style(pen) == Qt::NoPen || qbrush_style(penBrush) == Qt::NoBrush)
|
---|
1166 | return;
|
---|
1167 |
|
---|
1168 | QOpenGL2PaintEngineState *s = state();
|
---|
1169 | if (pen.isCosmetic() && !qt_scaleForTransform(s->transform(), 0)) {
|
---|
1170 | // QTriangulatingStroker class is not meant to support cosmetically sheared strokes.
|
---|
1171 | QPaintEngineEx::stroke(path, pen);
|
---|
1172 | return;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | ensureActive();
|
---|
1176 | d->setBrush(penBrush);
|
---|
1177 | d->stroke(path, pen);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | void QGL2PaintEngineExPrivate::stroke(const QVectorPath &path, const QPen &pen)
|
---|
1181 | {
|
---|
1182 | const QOpenGL2PaintEngineState *s = q->state();
|
---|
1183 | const bool newAddOffset = !(s->renderHints & QPainter::Antialiasing) && !multisamplingAlwaysEnabled;
|
---|
1184 | if (addOffset != newAddOffset) {
|
---|
1185 | addOffset = newAddOffset;
|
---|
1186 | matrixDirty = true;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | if (snapToPixelGrid) {
|
---|
1190 | snapToPixelGrid = false;
|
---|
1191 | matrixDirty = true;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | const Qt::PenStyle penStyle = qpen_style(pen);
|
---|
1195 | const QBrush &penBrush = qpen_brush(pen);
|
---|
1196 | const bool opaque = penBrush.isOpaque() && s->opacity > 0.99;
|
---|
1197 |
|
---|
1198 | transferMode(BrushDrawingMode);
|
---|
1199 |
|
---|
1200 | // updateMatrix() is responsible for setting the inverse scale on
|
---|
1201 | // the strokers, so we need to call it here and not wait for
|
---|
1202 | // prepareForDraw() down below.
|
---|
1203 | updateMatrix();
|
---|
1204 |
|
---|
1205 | QRectF clip = q->state()->matrix.inverted().mapRect(q->state()->clipEnabled
|
---|
1206 | ? q->state()->rectangleClip
|
---|
1207 | : QRectF(0, 0, width, height));
|
---|
1208 |
|
---|
1209 | if (penStyle == Qt::SolidLine) {
|
---|
1210 | stroker.process(path, pen, clip);
|
---|
1211 |
|
---|
1212 | } else { // Some sort of dash
|
---|
1213 | dasher.process(path, pen, clip);
|
---|
1214 |
|
---|
1215 | QVectorPath dashStroke(dasher.points(),
|
---|
1216 | dasher.elementCount(),
|
---|
1217 | dasher.elementTypes());
|
---|
1218 | stroker.process(dashStroke, pen, clip);
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | if (!stroker.vertexCount())
|
---|
1222 | return;
|
---|
1223 |
|
---|
1224 | if (opaque) {
|
---|
1225 | prepareForDraw(opaque);
|
---|
1226 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, stroker.vertices());
|
---|
1227 | glDrawArrays(GL_TRIANGLE_STRIP, 0, stroker.vertexCount() / 2);
|
---|
1228 |
|
---|
1229 | // QBrush b(Qt::green);
|
---|
1230 | // d->setBrush(&b);
|
---|
1231 | // d->prepareForDraw(true);
|
---|
1232 | // glDrawArrays(GL_LINE_STRIP, 0, d->stroker.vertexCount() / 2);
|
---|
1233 |
|
---|
1234 | } else {
|
---|
1235 | qreal width = qpen_widthf(pen) / 2;
|
---|
1236 | if (width == 0)
|
---|
1237 | width = 0.5;
|
---|
1238 | qreal extra = pen.joinStyle() == Qt::MiterJoin
|
---|
1239 | ? qMax(pen.miterLimit() * width, width)
|
---|
1240 | : width;
|
---|
1241 |
|
---|
1242 | if (pen.isCosmetic())
|
---|
1243 | extra = extra * inverseScale;
|
---|
1244 |
|
---|
1245 | QRectF bounds = path.controlPointRect().adjusted(-extra, -extra, extra, extra);
|
---|
1246 |
|
---|
1247 | fillStencilWithVertexArray(stroker.vertices(), stroker.vertexCount() / 2,
|
---|
1248 | 0, 0, bounds, QGL2PaintEngineExPrivate::TriStripStrokeFillMode);
|
---|
1249 |
|
---|
1250 | glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
|
---|
1251 |
|
---|
1252 | // Pass when any bit is set, replace stencil value with 0
|
---|
1253 | glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT);
|
---|
1254 | prepareForDraw(false);
|
---|
1255 |
|
---|
1256 | // Stencil the brush onto the dest buffer
|
---|
1257 | composite(bounds);
|
---|
1258 |
|
---|
1259 | glStencilMask(0);
|
---|
1260 |
|
---|
1261 | updateClipScissorTest();
|
---|
1262 | }
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | void QGL2PaintEngineEx::penChanged() { }
|
---|
1266 | void QGL2PaintEngineEx::brushChanged() { }
|
---|
1267 | void QGL2PaintEngineEx::brushOriginChanged() { }
|
---|
1268 |
|
---|
1269 | void QGL2PaintEngineEx::opacityChanged()
|
---|
1270 | {
|
---|
1271 | // qDebug("QGL2PaintEngineEx::opacityChanged()");
|
---|
1272 | Q_D(QGL2PaintEngineEx);
|
---|
1273 | state()->opacityChanged = true;
|
---|
1274 |
|
---|
1275 | Q_ASSERT(d->shaderManager);
|
---|
1276 | d->brushUniformsDirty = true;
|
---|
1277 | d->opacityUniformDirty = true;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | void QGL2PaintEngineEx::compositionModeChanged()
|
---|
1281 | {
|
---|
1282 | // qDebug("QGL2PaintEngineEx::compositionModeChanged()");
|
---|
1283 | Q_D(QGL2PaintEngineEx);
|
---|
1284 | state()->compositionModeChanged = true;
|
---|
1285 | d->compositionModeDirty = true;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | void QGL2PaintEngineEx::renderHintsChanged()
|
---|
1289 | {
|
---|
1290 | state()->renderHintsChanged = true;
|
---|
1291 |
|
---|
1292 | #if !defined(QT_OPENGL_ES_2)
|
---|
1293 | if ((state()->renderHints & QPainter::Antialiasing)
|
---|
1294 | || (state()->renderHints & QPainter::HighQualityAntialiasing))
|
---|
1295 | glEnable(GL_MULTISAMPLE);
|
---|
1296 | else
|
---|
1297 | glDisable(GL_MULTISAMPLE);
|
---|
1298 | #endif
|
---|
1299 |
|
---|
1300 | Q_D(QGL2PaintEngineEx);
|
---|
1301 | d->lastTextureUsed = GLuint(-1);
|
---|
1302 | d->brushTextureDirty = true;
|
---|
1303 | // qDebug("QGL2PaintEngineEx::renderHintsChanged() not implemented!");
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | void QGL2PaintEngineEx::transformChanged()
|
---|
1307 | {
|
---|
1308 | Q_D(QGL2PaintEngineEx);
|
---|
1309 | d->matrixDirty = true;
|
---|
1310 | state()->matrixChanged = true;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 |
|
---|
1314 | static const QRectF scaleRect(const QRectF &r, qreal sx, qreal sy)
|
---|
1315 | {
|
---|
1316 | return QRectF(r.x() * sx, r.y() * sy, r.width() * sx, r.height() * sy);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | void QGL2PaintEngineEx::drawPixmap(const QRectF& dest, const QPixmap & pixmap, const QRectF & src)
|
---|
1320 | {
|
---|
1321 | Q_D(QGL2PaintEngineEx);
|
---|
1322 | QGLContext *ctx = d->ctx;
|
---|
1323 |
|
---|
1324 | int max_texture_size = ctx->d_func()->maxTextureSize();
|
---|
1325 | if (pixmap.width() > max_texture_size || pixmap.height() > max_texture_size) {
|
---|
1326 | QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio);
|
---|
1327 |
|
---|
1328 | const qreal sx = scaled.width() / qreal(pixmap.width());
|
---|
1329 | const qreal sy = scaled.height() / qreal(pixmap.height());
|
---|
1330 |
|
---|
1331 | drawPixmap(dest, scaled, scaleRect(src, sx, sy));
|
---|
1332 | return;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | ensureActive();
|
---|
1336 | d->transferMode(ImageDrawingMode);
|
---|
1337 |
|
---|
1338 | glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
|
---|
1339 | QGLTexture *texture =
|
---|
1340 | ctx->d_func()->bindTexture(pixmap, GL_TEXTURE_2D, GL_RGBA,
|
---|
1341 | QGLContext::InternalBindOption
|
---|
1342 | | QGLContext::CanFlipNativePixmapBindOption);
|
---|
1343 |
|
---|
1344 | GLfloat top = texture->options & QGLContext::InvertedYBindOption ? (pixmap.height() - src.top()) : src.top();
|
---|
1345 | GLfloat bottom = texture->options & QGLContext::InvertedYBindOption ? (pixmap.height() - src.bottom()) : src.bottom();
|
---|
1346 | QGLRect srcRect(src.left(), top, src.right(), bottom);
|
---|
1347 |
|
---|
1348 | bool isBitmap = pixmap.isQBitmap();
|
---|
1349 | bool isOpaque = !isBitmap && !pixmap.hasAlpha();
|
---|
1350 |
|
---|
1351 | d->updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
|
---|
1352 | state()->renderHints & QPainter::SmoothPixmapTransform, texture->id);
|
---|
1353 | d->drawTexture(dest, srcRect, pixmap.size(), isOpaque, isBitmap);
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | void QGL2PaintEngineEx::drawImage(const QRectF& dest, const QImage& image, const QRectF& src,
|
---|
1357 | Qt::ImageConversionFlags)
|
---|
1358 | {
|
---|
1359 | Q_D(QGL2PaintEngineEx);
|
---|
1360 | QGLContext *ctx = d->ctx;
|
---|
1361 |
|
---|
1362 | int max_texture_size = ctx->d_func()->maxTextureSize();
|
---|
1363 | if (image.width() > max_texture_size || image.height() > max_texture_size) {
|
---|
1364 | QImage scaled = image.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio);
|
---|
1365 |
|
---|
1366 | const qreal sx = scaled.width() / qreal(image.width());
|
---|
1367 | const qreal sy = scaled.height() / qreal(image.height());
|
---|
1368 |
|
---|
1369 | drawImage(dest, scaled, scaleRect(src, sx, sy));
|
---|
1370 | return;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | ensureActive();
|
---|
1374 | d->transferMode(ImageDrawingMode);
|
---|
1375 |
|
---|
1376 | glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
|
---|
1377 |
|
---|
1378 | QGLTexture *texture = ctx->d_func()->bindTexture(image, GL_TEXTURE_2D, GL_RGBA, QGLContext::InternalBindOption);
|
---|
1379 | GLuint id = texture->id;
|
---|
1380 |
|
---|
1381 | d->updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
|
---|
1382 | state()->renderHints & QPainter::SmoothPixmapTransform, id);
|
---|
1383 | d->drawTexture(dest, src, image.size(), !image.hasAlphaChannel());
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | void QGL2PaintEngineEx::drawStaticTextItem(QStaticTextItem *textItem)
|
---|
1387 | {
|
---|
1388 | Q_D(QGL2PaintEngineEx);
|
---|
1389 |
|
---|
1390 | ensureActive();
|
---|
1391 |
|
---|
1392 | QFontEngineGlyphCache::Type glyphType = textItem->fontEngine()->glyphFormat >= 0
|
---|
1393 | ? QFontEngineGlyphCache::Type(textItem->fontEngine()->glyphFormat)
|
---|
1394 | : d->glyphCacheType;
|
---|
1395 | if (glyphType == QFontEngineGlyphCache::Raster_RGBMask) {
|
---|
1396 | if (d->device->alphaRequested() || state()->matrix.type() > QTransform::TxTranslate
|
---|
1397 | || (state()->composition_mode != QPainter::CompositionMode_Source
|
---|
1398 | && state()->composition_mode != QPainter::CompositionMode_SourceOver))
|
---|
1399 | {
|
---|
1400 | glyphType = QFontEngineGlyphCache::Raster_A8;
|
---|
1401 | }
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | d->drawCachedGlyphs(glyphType, textItem);
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | bool QGL2PaintEngineEx::drawTexture(const QRectF &dest, GLuint textureId, const QSize &size, const QRectF &src)
|
---|
1408 | {
|
---|
1409 | Q_D(QGL2PaintEngineEx);
|
---|
1410 | if (!d->shaderManager)
|
---|
1411 | return false;
|
---|
1412 |
|
---|
1413 | ensureActive();
|
---|
1414 | d->transferMode(ImageDrawingMode);
|
---|
1415 |
|
---|
1416 | #ifndef QT_OPENGL_ES_2
|
---|
1417 | QGLContext *ctx = d->ctx;
|
---|
1418 | #endif
|
---|
1419 | glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
|
---|
1420 | glBindTexture(GL_TEXTURE_2D, textureId);
|
---|
1421 |
|
---|
1422 | QGLRect srcRect(src.left(), src.bottom(), src.right(), src.top());
|
---|
1423 |
|
---|
1424 | d->updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
|
---|
1425 | state()->renderHints & QPainter::SmoothPixmapTransform, textureId);
|
---|
1426 | d->drawTexture(dest, srcRect, size, false);
|
---|
1427 | return true;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | void QGL2PaintEngineEx::drawTextItem(const QPointF &p, const QTextItem &textItem)
|
---|
1431 | {
|
---|
1432 | Q_D(QGL2PaintEngineEx);
|
---|
1433 |
|
---|
1434 | ensureActive();
|
---|
1435 | QOpenGL2PaintEngineState *s = state();
|
---|
1436 |
|
---|
1437 | const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
|
---|
1438 |
|
---|
1439 | QTransform::TransformationType txtype = s->matrix.type();
|
---|
1440 |
|
---|
1441 | float det = s->matrix.determinant();
|
---|
1442 | bool drawCached = txtype < QTransform::TxProject;
|
---|
1443 |
|
---|
1444 | // don't try to cache huge fonts or vastly transformed fonts
|
---|
1445 | const qreal pixelSize = ti.fontEngine->fontDef.pixelSize;
|
---|
1446 | if (pixelSize * pixelSize * qAbs(det) >= 64 * 64 || det < 0.25f || det > 4.f)
|
---|
1447 | drawCached = false;
|
---|
1448 |
|
---|
1449 | QFontEngineGlyphCache::Type glyphType = ti.fontEngine->glyphFormat >= 0
|
---|
1450 | ? QFontEngineGlyphCache::Type(ti.fontEngine->glyphFormat)
|
---|
1451 | : d->glyphCacheType;
|
---|
1452 |
|
---|
1453 |
|
---|
1454 | if (glyphType == QFontEngineGlyphCache::Raster_RGBMask) {
|
---|
1455 | if (d->device->alphaRequested() || txtype > QTransform::TxTranslate
|
---|
1456 | || (state()->composition_mode != QPainter::CompositionMode_Source
|
---|
1457 | && state()->composition_mode != QPainter::CompositionMode_SourceOver))
|
---|
1458 | {
|
---|
1459 | glyphType = QFontEngineGlyphCache::Raster_A8;
|
---|
1460 | }
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | if (drawCached) {
|
---|
1464 | QVarLengthArray<QFixedPoint> positions;
|
---|
1465 | QVarLengthArray<glyph_t> glyphs;
|
---|
1466 | QTransform matrix = QTransform::fromTranslate(p.x(), p.y());
|
---|
1467 | ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
|
---|
1468 |
|
---|
1469 | {
|
---|
1470 | QStaticTextItem staticTextItem;
|
---|
1471 | staticTextItem.chars = const_cast<QChar *>(ti.chars);
|
---|
1472 | staticTextItem.setFontEngine(ti.fontEngine);
|
---|
1473 | staticTextItem.glyphs = glyphs.data();
|
---|
1474 | staticTextItem.numChars = ti.num_chars;
|
---|
1475 | staticTextItem.numGlyphs = glyphs.size();
|
---|
1476 | staticTextItem.glyphPositions = positions.data();
|
---|
1477 |
|
---|
1478 | d->drawCachedGlyphs(glyphType, &staticTextItem);
|
---|
1479 | }
|
---|
1480 | return;
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | QPaintEngineEx::drawTextItem(p, ti);
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | namespace {
|
---|
1487 |
|
---|
1488 | class QOpenGLStaticTextUserData: public QStaticTextUserData
|
---|
1489 | {
|
---|
1490 | public:
|
---|
1491 | QOpenGLStaticTextUserData()
|
---|
1492 | : QStaticTextUserData(OpenGLUserData), cacheSize(0, 0)
|
---|
1493 | {
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | ~QOpenGLStaticTextUserData()
|
---|
1497 | {
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | QSize cacheSize;
|
---|
1501 | QGL2PEXVertexArray vertexCoordinateArray;
|
---|
1502 | QGL2PEXVertexArray textureCoordinateArray;
|
---|
1503 | QFontEngineGlyphCache::Type glyphType;
|
---|
1504 | };
|
---|
1505 |
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | // #define QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO
|
---|
1509 |
|
---|
1510 | void QGL2PaintEngineExPrivate::drawCachedGlyphs(QFontEngineGlyphCache::Type glyphType,
|
---|
1511 | QStaticTextItem *staticTextItem)
|
---|
1512 | {
|
---|
1513 | Q_Q(QGL2PaintEngineEx);
|
---|
1514 |
|
---|
1515 | QOpenGL2PaintEngineState *s = q->state();
|
---|
1516 |
|
---|
1517 | bool recreateVertexArrays = false;
|
---|
1518 |
|
---|
1519 | QGLTextureGlyphCache *cache =
|
---|
1520 | (QGLTextureGlyphCache *) staticTextItem->fontEngine()->glyphCache(ctx, glyphType, QTransform());
|
---|
1521 | if (!cache || cache->cacheType() != glyphType) {
|
---|
1522 | cache = new QGLTextureGlyphCache(ctx, glyphType, QTransform());
|
---|
1523 | staticTextItem->fontEngine()->setGlyphCache(ctx, cache);
|
---|
1524 | recreateVertexArrays = true;
|
---|
1525 | } else if (cache->context() == 0) { // Old context has been destroyed, new context has same ptr value
|
---|
1526 | cache->setContext(ctx);
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | if (staticTextItem->userDataNeedsUpdate) {
|
---|
1530 | recreateVertexArrays = true;
|
---|
1531 | } else if (staticTextItem->userData() == 0) {
|
---|
1532 | recreateVertexArrays = true;
|
---|
1533 | } else if (staticTextItem->userData()->type != QStaticTextUserData::OpenGLUserData) {
|
---|
1534 | recreateVertexArrays = true;
|
---|
1535 | } else {
|
---|
1536 | QOpenGLStaticTextUserData *userData = static_cast<QOpenGLStaticTextUserData *>(staticTextItem->userData());
|
---|
1537 | if (userData->glyphType != glyphType)
|
---|
1538 | recreateVertexArrays = true;
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | // We only need to update the cache with new glyphs if we are actually going to recreate the vertex arrays.
|
---|
1542 | // If the cache size has changed, we do need to regenerate the vertices, but we don't need to repopulate the
|
---|
1543 | // cache so this text is performed before we test if the cache size has changed.
|
---|
1544 | if (recreateVertexArrays) {
|
---|
1545 | cache->setPaintEnginePrivate(this);
|
---|
1546 | if (!cache->populate(staticTextItem->fontEngine(), staticTextItem->numGlyphs,
|
---|
1547 | staticTextItem->glyphs, staticTextItem->glyphPositions)) {
|
---|
1548 | // No space in cache. We need to clear the cache and try again
|
---|
1549 | cache->clear();
|
---|
1550 | cache->populate(staticTextItem->fontEngine(), staticTextItem->numGlyphs,
|
---|
1551 | staticTextItem->glyphs, staticTextItem->glyphPositions);
|
---|
1552 | }
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | if (cache->width() == 0 || cache->height() == 0)
|
---|
1556 | return;
|
---|
1557 |
|
---|
1558 | transferMode(TextDrawingMode);
|
---|
1559 |
|
---|
1560 | int margin = cache->glyphMargin();
|
---|
1561 |
|
---|
1562 | GLfloat dx = 1.0 / cache->width();
|
---|
1563 | GLfloat dy = 1.0 / cache->height();
|
---|
1564 |
|
---|
1565 | // Use global arrays by default
|
---|
1566 | QGL2PEXVertexArray *vertexCoordinates = &vertexCoordinateArray;
|
---|
1567 | QGL2PEXVertexArray *textureCoordinates = &textureCoordinateArray;
|
---|
1568 |
|
---|
1569 | if (staticTextItem->useBackendOptimizations) {
|
---|
1570 | QOpenGLStaticTextUserData *userData = 0;
|
---|
1571 |
|
---|
1572 | if (staticTextItem->userData() == 0
|
---|
1573 | || staticTextItem->userData()->type != QStaticTextUserData::OpenGLUserData) {
|
---|
1574 |
|
---|
1575 | userData = new QOpenGLStaticTextUserData();
|
---|
1576 | staticTextItem->setUserData(userData);
|
---|
1577 |
|
---|
1578 | } else {
|
---|
1579 | userData = static_cast<QOpenGLStaticTextUserData*>(staticTextItem->userData());
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | userData->glyphType = glyphType;
|
---|
1583 |
|
---|
1584 | // Use cache if backend optimizations is turned on
|
---|
1585 | vertexCoordinates = &userData->vertexCoordinateArray;
|
---|
1586 | textureCoordinates = &userData->textureCoordinateArray;
|
---|
1587 |
|
---|
1588 | QSize size(cache->width(), cache->height());
|
---|
1589 | if (userData->cacheSize != size) {
|
---|
1590 | recreateVertexArrays = true;
|
---|
1591 | userData->cacheSize = size;
|
---|
1592 | }
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 |
|
---|
1596 | if (recreateVertexArrays) {
|
---|
1597 | vertexCoordinates->clear();
|
---|
1598 | textureCoordinates->clear();
|
---|
1599 |
|
---|
1600 | for (int i=0; i<staticTextItem->numGlyphs; ++i) {
|
---|
1601 | const QTextureGlyphCache::Coord &c = cache->coords.value(staticTextItem->glyphs[i]);
|
---|
1602 | int x = staticTextItem->glyphPositions[i].x.toInt() + c.baseLineX - margin;
|
---|
1603 | int y = staticTextItem->glyphPositions[i].y.toInt() - c.baseLineY - margin;
|
---|
1604 |
|
---|
1605 | vertexCoordinates->addQuad(QRectF(x, y, c.w, c.h));
|
---|
1606 | textureCoordinates->addQuad(QRectF(c.x*dx, c.y*dy, c.w * dx, c.h * dy));
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | staticTextItem->userDataNeedsUpdate = false;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | if (elementIndices.size() < staticTextItem->numGlyphs*6) {
|
---|
1613 | Q_ASSERT(elementIndices.size() % 6 == 0);
|
---|
1614 | int j = elementIndices.size() / 6 * 4;
|
---|
1615 | while (j < staticTextItem->numGlyphs*4) {
|
---|
1616 | elementIndices.append(j + 0);
|
---|
1617 | elementIndices.append(j + 0);
|
---|
1618 | elementIndices.append(j + 1);
|
---|
1619 | elementIndices.append(j + 2);
|
---|
1620 | elementIndices.append(j + 3);
|
---|
1621 | elementIndices.append(j + 3);
|
---|
1622 |
|
---|
1623 | j += 4;
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | #if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO)
|
---|
1627 | if (elementIndicesVBOId == 0)
|
---|
1628 | glGenBuffers(1, &elementIndicesVBOId);
|
---|
1629 |
|
---|
1630 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementIndicesVBOId);
|
---|
1631 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementIndices.size() * sizeof(GLushort),
|
---|
1632 | elementIndices.constData(), GL_STATIC_DRAW);
|
---|
1633 | #endif
|
---|
1634 | } else {
|
---|
1635 | #if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO)
|
---|
1636 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementIndicesVBOId);
|
---|
1637 | #endif
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinates->data());
|
---|
1641 | setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinates->data());
|
---|
1642 |
|
---|
1643 | if (addOffset) {
|
---|
1644 | addOffset = false;
|
---|
1645 | matrixDirty = true;
|
---|
1646 | }
|
---|
1647 | if (!snapToPixelGrid) {
|
---|
1648 | snapToPixelGrid = true;
|
---|
1649 | matrixDirty = true;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | QBrush pensBrush = q->state()->pen.brush();
|
---|
1653 | setBrush(pensBrush);
|
---|
1654 |
|
---|
1655 | if (glyphType == QFontEngineGlyphCache::Raster_RGBMask) {
|
---|
1656 |
|
---|
1657 | // Subpixel antialiasing without gamma correction
|
---|
1658 |
|
---|
1659 | QPainter::CompositionMode compMode = q->state()->composition_mode;
|
---|
1660 | Q_ASSERT(compMode == QPainter::CompositionMode_Source
|
---|
1661 | || compMode == QPainter::CompositionMode_SourceOver);
|
---|
1662 |
|
---|
1663 | shaderManager->setMaskType(QGLEngineShaderManager::SubPixelMaskPass1);
|
---|
1664 |
|
---|
1665 | if (pensBrush.style() == Qt::SolidPattern) {
|
---|
1666 | // Solid patterns can get away with only one pass.
|
---|
1667 | QColor c = pensBrush.color();
|
---|
1668 | qreal oldOpacity = q->state()->opacity;
|
---|
1669 | if (compMode == QPainter::CompositionMode_Source) {
|
---|
1670 | c = qt_premultiplyColor(c, q->state()->opacity);
|
---|
1671 | q->state()->opacity = 1;
|
---|
1672 | opacityUniformDirty = true;
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | compositionModeDirty = false; // I can handle this myself, thank you very much
|
---|
1676 | prepareForDraw(false); // Text always causes src pixels to be transparent
|
---|
1677 |
|
---|
1678 | // prepareForDraw() have set the opacity on the current shader, so the opacity state can now be reset.
|
---|
1679 | if (compMode == QPainter::CompositionMode_Source) {
|
---|
1680 | q->state()->opacity = oldOpacity;
|
---|
1681 | opacityUniformDirty = true;
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | glEnable(GL_BLEND);
|
---|
1685 | glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR);
|
---|
1686 | glBlendColor(c.redF(), c.greenF(), c.blueF(), c.alphaF());
|
---|
1687 | } else {
|
---|
1688 | // Other brush styles need two passes.
|
---|
1689 |
|
---|
1690 | qreal oldOpacity = q->state()->opacity;
|
---|
1691 | if (compMode == QPainter::CompositionMode_Source) {
|
---|
1692 | q->state()->opacity = 1;
|
---|
1693 | opacityUniformDirty = true;
|
---|
1694 | pensBrush = Qt::white;
|
---|
1695 | setBrush(pensBrush);
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | compositionModeDirty = false; // I can handle this myself, thank you very much
|
---|
1699 | prepareForDraw(false); // Text always causes src pixels to be transparent
|
---|
1700 | glEnable(GL_BLEND);
|
---|
1701 | glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
|
---|
1702 |
|
---|
1703 | glActiveTexture(GL_TEXTURE0 + QT_MASK_TEXTURE_UNIT);
|
---|
1704 | glBindTexture(GL_TEXTURE_2D, cache->texture());
|
---|
1705 | updateTextureFilter(GL_TEXTURE_2D, GL_REPEAT, false);
|
---|
1706 |
|
---|
1707 | #if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO)
|
---|
1708 | glDrawElements(GL_TRIANGLE_STRIP, 6 * staticTextItem->numGlyphs, GL_UNSIGNED_SHORT, 0);
|
---|
1709 | #else
|
---|
1710 | glDrawElements(GL_TRIANGLE_STRIP, 6 * staticTextItem->numGlyphs, GL_UNSIGNED_SHORT, elementIndices.data());
|
---|
1711 | #endif
|
---|
1712 |
|
---|
1713 | shaderManager->setMaskType(QGLEngineShaderManager::SubPixelMaskPass2);
|
---|
1714 |
|
---|
1715 | if (compMode == QPainter::CompositionMode_Source) {
|
---|
1716 | q->state()->opacity = oldOpacity;
|
---|
1717 | opacityUniformDirty = true;
|
---|
1718 | pensBrush = q->state()->pen.brush();
|
---|
1719 | setBrush(pensBrush);
|
---|
1720 | }
|
---|
1721 |
|
---|
1722 | compositionModeDirty = false;
|
---|
1723 | prepareForDraw(false); // Text always causes src pixels to be transparent
|
---|
1724 | glEnable(GL_BLEND);
|
---|
1725 | glBlendFunc(GL_ONE, GL_ONE);
|
---|
1726 | }
|
---|
1727 | compositionModeDirty = true;
|
---|
1728 | } else {
|
---|
1729 | // Greyscale/mono glyphs
|
---|
1730 |
|
---|
1731 | shaderManager->setMaskType(QGLEngineShaderManager::PixelMask);
|
---|
1732 | prepareForDraw(false); // Text always causes src pixels to be transparent
|
---|
1733 | }
|
---|
1734 | //### TODO: Gamma correction
|
---|
1735 |
|
---|
1736 | QGLTextureGlyphCache::FilterMode filterMode = (s->matrix.type() > QTransform::TxTranslate)?QGLTextureGlyphCache::Linear:QGLTextureGlyphCache::Nearest;
|
---|
1737 | if (lastMaskTextureUsed != cache->texture() || cache->filterMode() != filterMode) {
|
---|
1738 |
|
---|
1739 | glActiveTexture(GL_TEXTURE0 + QT_MASK_TEXTURE_UNIT);
|
---|
1740 | if (lastMaskTextureUsed != cache->texture()) {
|
---|
1741 | glBindTexture(GL_TEXTURE_2D, cache->texture());
|
---|
1742 | lastMaskTextureUsed = cache->texture();
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | if (cache->filterMode() != filterMode) {
|
---|
1746 | if (filterMode == QGLTextureGlyphCache::Linear) {
|
---|
1747 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
1748 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
1749 | } else {
|
---|
1750 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
1751 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
1752 | }
|
---|
1753 | cache->setFilterMode(filterMode);
|
---|
1754 | }
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | #if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO)
|
---|
1758 | glDrawElements(GL_TRIANGLE_STRIP, 6 * staticTextItem->numGlyphs, GL_UNSIGNED_SHORT, 0);
|
---|
1759 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
---|
1760 | #else
|
---|
1761 | glDrawElements(GL_TRIANGLE_STRIP, 6 * staticTextItem->numGlyphs, GL_UNSIGNED_SHORT, elementIndices.data());
|
---|
1762 | #endif
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | void QGL2PaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap,
|
---|
1766 | QPainter::PixmapFragmentHints hints)
|
---|
1767 | {
|
---|
1768 | Q_D(QGL2PaintEngineEx);
|
---|
1769 | // Use fallback for extended composition modes.
|
---|
1770 | if (state()->composition_mode > QPainter::CompositionMode_Plus) {
|
---|
1771 | QPaintEngineEx::drawPixmapFragments(fragments, fragmentCount, pixmap, hints);
|
---|
1772 | return;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | ensureActive();
|
---|
1776 | int max_texture_size = d->ctx->d_func()->maxTextureSize();
|
---|
1777 | if (pixmap.width() > max_texture_size || pixmap.height() > max_texture_size) {
|
---|
1778 | QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio);
|
---|
1779 | d->drawPixmapFragments(fragments, fragmentCount, scaled, hints);
|
---|
1780 | } else {
|
---|
1781 | d->drawPixmapFragments(fragments, fragmentCount, pixmap, hints);
|
---|
1782 | }
|
---|
1783 | }
|
---|
1784 |
|
---|
1785 |
|
---|
1786 | void QGL2PaintEngineExPrivate::drawPixmapFragments(const QPainter::PixmapFragment *fragments,
|
---|
1787 | int fragmentCount, const QPixmap &pixmap,
|
---|
1788 | QPainter::PixmapFragmentHints hints)
|
---|
1789 | {
|
---|
1790 | GLfloat dx = 1.0f / pixmap.size().width();
|
---|
1791 | GLfloat dy = 1.0f / pixmap.size().height();
|
---|
1792 |
|
---|
1793 | vertexCoordinateArray.clear();
|
---|
1794 | textureCoordinateArray.clear();
|
---|
1795 | opacityArray.reset();
|
---|
1796 |
|
---|
1797 | if (addOffset) {
|
---|
1798 | addOffset = false;
|
---|
1799 | matrixDirty = true;
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | if (snapToPixelGrid) {
|
---|
1803 | snapToPixelGrid = false;
|
---|
1804 | matrixDirty = true;
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | bool allOpaque = true;
|
---|
1808 |
|
---|
1809 | for (int i = 0; i < fragmentCount; ++i) {
|
---|
1810 | qreal s = 0;
|
---|
1811 | qreal c = 1;
|
---|
1812 | if (fragments[i].rotation != 0) {
|
---|
1813 | s = qFastSin(fragments[i].rotation * Q_PI / 180);
|
---|
1814 | c = qFastCos(fragments[i].rotation * Q_PI / 180);
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | qreal right = 0.5 * fragments[i].scaleX * fragments[i].width;
|
---|
1818 | qreal bottom = 0.5 * fragments[i].scaleY * fragments[i].height;
|
---|
1819 | QGLPoint bottomRight(right * c - bottom * s, right * s + bottom * c);
|
---|
1820 | QGLPoint bottomLeft(-right * c - bottom * s, -right * s + bottom * c);
|
---|
1821 |
|
---|
1822 | vertexCoordinateArray.addVertex(bottomRight.x + fragments[i].x, bottomRight.y + fragments[i].y);
|
---|
1823 | vertexCoordinateArray.addVertex(-bottomLeft.x + fragments[i].x, -bottomLeft.y + fragments[i].y);
|
---|
1824 | vertexCoordinateArray.addVertex(-bottomRight.x + fragments[i].x, -bottomRight.y + fragments[i].y);
|
---|
1825 | vertexCoordinateArray.addVertex(-bottomRight.x + fragments[i].x, -bottomRight.y + fragments[i].y);
|
---|
1826 | vertexCoordinateArray.addVertex(bottomLeft.x + fragments[i].x, bottomLeft.y + fragments[i].y);
|
---|
1827 | vertexCoordinateArray.addVertex(bottomRight.x + fragments[i].x, bottomRight.y + fragments[i].y);
|
---|
1828 |
|
---|
1829 | QGLRect src(fragments[i].sourceLeft * dx, fragments[i].sourceTop * dy,
|
---|
1830 | (fragments[i].sourceLeft + fragments[i].width) * dx,
|
---|
1831 | (fragments[i].sourceTop + fragments[i].height) * dy);
|
---|
1832 |
|
---|
1833 | textureCoordinateArray.addVertex(src.right, src.bottom);
|
---|
1834 | textureCoordinateArray.addVertex(src.right, src.top);
|
---|
1835 | textureCoordinateArray.addVertex(src.left, src.top);
|
---|
1836 | textureCoordinateArray.addVertex(src.left, src.top);
|
---|
1837 | textureCoordinateArray.addVertex(src.left, src.bottom);
|
---|
1838 | textureCoordinateArray.addVertex(src.right, src.bottom);
|
---|
1839 |
|
---|
1840 | qreal opacity = fragments[i].opacity * q->state()->opacity;
|
---|
1841 | opacityArray << opacity << opacity << opacity << opacity << opacity << opacity;
|
---|
1842 | allOpaque &= (opacity >= 0.99f);
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT);
|
---|
1846 | QGLTexture *texture = ctx->d_func()->bindTexture(pixmap, GL_TEXTURE_2D, GL_RGBA,
|
---|
1847 | QGLContext::InternalBindOption
|
---|
1848 | | QGLContext::CanFlipNativePixmapBindOption);
|
---|
1849 |
|
---|
1850 | if (texture->options & QGLContext::InvertedYBindOption) {
|
---|
1851 | // Flip texture y-coordinate.
|
---|
1852 | QGLPoint *data = textureCoordinateArray.data();
|
---|
1853 | for (int i = 0; i < 6 * fragmentCount; ++i)
|
---|
1854 | data[i].y = 1 - data[i].y;
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 | transferMode(ImageArrayDrawingMode);
|
---|
1858 |
|
---|
1859 | bool isBitmap = pixmap.isQBitmap();
|
---|
1860 | bool isOpaque = !isBitmap && (!pixmap.hasAlpha() || (hints & QPainter::OpaqueHint)) && allOpaque;
|
---|
1861 |
|
---|
1862 | updateTextureFilter(GL_TEXTURE_2D, GL_CLAMP_TO_EDGE,
|
---|
1863 | q->state()->renderHints & QPainter::SmoothPixmapTransform, texture->id);
|
---|
1864 |
|
---|
1865 | // Setup for texture drawing
|
---|
1866 | currentBrush = noBrush;
|
---|
1867 | shaderManager->setSrcPixelType(isBitmap ? QGLEngineShaderManager::PatternSrc
|
---|
1868 | : QGLEngineShaderManager::ImageSrc);
|
---|
1869 | if (prepareForDraw(isOpaque))
|
---|
1870 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT);
|
---|
1871 |
|
---|
1872 | if (isBitmap) {
|
---|
1873 | QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity);
|
---|
1874 | shaderManager->currentProgram()->setUniformValue(location(QGLEngineShaderManager::PatternColor), col);
|
---|
1875 | }
|
---|
1876 |
|
---|
1877 | glDrawArrays(GL_TRIANGLES, 0, 6 * fragmentCount);
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 | bool QGL2PaintEngineEx::begin(QPaintDevice *pdev)
|
---|
1881 | {
|
---|
1882 | Q_D(QGL2PaintEngineEx);
|
---|
1883 |
|
---|
1884 | // qDebug("QGL2PaintEngineEx::begin()");
|
---|
1885 | if (pdev->devType() == QInternal::OpenGL)
|
---|
1886 | d->device = static_cast<QGLPaintDevice*>(pdev);
|
---|
1887 | else
|
---|
1888 | d->device = QGLPaintDevice::getDevice(pdev);
|
---|
1889 |
|
---|
1890 | if (!d->device)
|
---|
1891 | return false;
|
---|
1892 |
|
---|
1893 | d->ctx = d->device->context();
|
---|
1894 | d->ctx->d_ptr->active_engine = this;
|
---|
1895 |
|
---|
1896 | const QSize sz = d->device->size();
|
---|
1897 | d->width = sz.width();
|
---|
1898 | d->height = sz.height();
|
---|
1899 | d->mode = BrushDrawingMode;
|
---|
1900 | d->brushTextureDirty = true;
|
---|
1901 | d->brushUniformsDirty = true;
|
---|
1902 | d->matrixUniformDirty = true;
|
---|
1903 | d->matrixDirty = true;
|
---|
1904 | d->compositionModeDirty = true;
|
---|
1905 | d->opacityUniformDirty = true;
|
---|
1906 | d->needsSync = true;
|
---|
1907 | d->useSystemClip = !systemClip().isEmpty();
|
---|
1908 | d->currentBrush = QBrush();
|
---|
1909 |
|
---|
1910 | d->dirtyStencilRegion = QRect(0, 0, d->width, d->height);
|
---|
1911 | d->stencilClean = true;
|
---|
1912 |
|
---|
1913 | // Calling begin paint should make the correct context current. So, any
|
---|
1914 | // code which calls into GL or otherwise needs a current context *must*
|
---|
1915 | // go after beginPaint:
|
---|
1916 | d->device->beginPaint();
|
---|
1917 |
|
---|
1918 | #if !defined(QT_OPENGL_ES_2)
|
---|
1919 | bool success = qt_resolve_version_2_0_functions(d->ctx)
|
---|
1920 | && qt_resolve_buffer_extensions(d->ctx);
|
---|
1921 | Q_ASSERT(success);
|
---|
1922 | Q_UNUSED(success);
|
---|
1923 | #endif
|
---|
1924 |
|
---|
1925 | d->shaderManager = new QGLEngineShaderManager(d->ctx);
|
---|
1926 |
|
---|
1927 | glDisable(GL_STENCIL_TEST);
|
---|
1928 | glDisable(GL_DEPTH_TEST);
|
---|
1929 | glDisable(GL_SCISSOR_TEST);
|
---|
1930 |
|
---|
1931 | #if !defined(QT_OPENGL_ES_2)
|
---|
1932 | glDisable(GL_MULTISAMPLE);
|
---|
1933 | #endif
|
---|
1934 |
|
---|
1935 | d->glyphCacheType = QFontEngineGlyphCache::Raster_A8;
|
---|
1936 |
|
---|
1937 | #if !defined(QT_OPENGL_ES_2)
|
---|
1938 | #if defined(Q_WS_WIN)
|
---|
1939 | if (qt_cleartype_enabled)
|
---|
1940 | #endif
|
---|
1941 | #if defined(Q_WS_MAC)
|
---|
1942 | if (qt_applefontsmoothing_enabled)
|
---|
1943 | #endif
|
---|
1944 | d->glyphCacheType = QFontEngineGlyphCache::Raster_RGBMask;
|
---|
1945 | #endif
|
---|
1946 |
|
---|
1947 | #if defined(QT_OPENGL_ES_2)
|
---|
1948 | // OpenGL ES can't switch MSAA off, so if the gl paint device is
|
---|
1949 | // multisampled, it's always multisampled.
|
---|
1950 | d->multisamplingAlwaysEnabled = d->device->format().sampleBuffers();
|
---|
1951 | #else
|
---|
1952 | d->multisamplingAlwaysEnabled = false;
|
---|
1953 | #endif
|
---|
1954 |
|
---|
1955 | return true;
|
---|
1956 | }
|
---|
1957 |
|
---|
1958 | bool QGL2PaintEngineEx::end()
|
---|
1959 | {
|
---|
1960 | Q_D(QGL2PaintEngineEx);
|
---|
1961 | QGLContext *ctx = d->ctx;
|
---|
1962 |
|
---|
1963 | glUseProgram(0);
|
---|
1964 | d->transferMode(BrushDrawingMode);
|
---|
1965 | d->device->endPaint();
|
---|
1966 |
|
---|
1967 | #if defined(Q_WS_X11)
|
---|
1968 | // On some (probably all) drivers, deleting an X pixmap which has been bound to a texture
|
---|
1969 | // before calling glFinish/swapBuffers renders garbage. Presumably this is because X deletes
|
---|
1970 | // the pixmap behind the driver's back before it's had a chance to use it. To fix this, we
|
---|
1971 | // reference all QPixmaps which have been bound to stop them being deleted and only deref
|
---|
1972 | // them here, after swapBuffers, where they can be safely deleted.
|
---|
1973 | ctx->d_func()->boundPixmaps.clear();
|
---|
1974 | #endif
|
---|
1975 | d->ctx->d_ptr->active_engine = 0;
|
---|
1976 |
|
---|
1977 | d->resetGLState();
|
---|
1978 |
|
---|
1979 | delete d->shaderManager;
|
---|
1980 | d->shaderManager = 0;
|
---|
1981 | d->currentBrush = QBrush();
|
---|
1982 |
|
---|
1983 | #ifdef QT_OPENGL_CACHE_AS_VBOS
|
---|
1984 | if (!d->unusedVBOSToClean.isEmpty()) {
|
---|
1985 | glDeleteBuffers(d->unusedVBOSToClean.size(), d->unusedVBOSToClean.constData());
|
---|
1986 | d->unusedVBOSToClean.clear();
|
---|
1987 | }
|
---|
1988 | if (!d->unusedIBOSToClean.isEmpty()) {
|
---|
1989 | glDeleteBuffers(d->unusedIBOSToClean.size(), d->unusedIBOSToClean.constData());
|
---|
1990 | d->unusedIBOSToClean.clear();
|
---|
1991 | }
|
---|
1992 | #endif
|
---|
1993 |
|
---|
1994 | return false;
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | void QGL2PaintEngineEx::ensureActive()
|
---|
1998 | {
|
---|
1999 | Q_D(QGL2PaintEngineEx);
|
---|
2000 | QGLContext *ctx = d->ctx;
|
---|
2001 |
|
---|
2002 | if (isActive() && ctx->d_ptr->active_engine != this) {
|
---|
2003 | ctx->d_ptr->active_engine = this;
|
---|
2004 | d->needsSync = true;
|
---|
2005 | }
|
---|
2006 |
|
---|
2007 | d->device->ensureActiveTarget();
|
---|
2008 |
|
---|
2009 | if (d->needsSync) {
|
---|
2010 | d->transferMode(BrushDrawingMode);
|
---|
2011 | glViewport(0, 0, d->width, d->height);
|
---|
2012 | d->needsSync = false;
|
---|
2013 | d->lastMaskTextureUsed = 0;
|
---|
2014 | d->shaderManager->setDirty();
|
---|
2015 | d->ctx->d_func()->syncGlState();
|
---|
2016 | for (int i = 0; i < 3; ++i)
|
---|
2017 | d->vertexAttribPointers[i] = (GLfloat*)-1; // Assume the pointers are clobbered
|
---|
2018 | setState(state());
|
---|
2019 | }
|
---|
2020 | }
|
---|
2021 |
|
---|
2022 | void QGL2PaintEngineExPrivate::updateClipScissorTest()
|
---|
2023 | {
|
---|
2024 | Q_Q(QGL2PaintEngineEx);
|
---|
2025 | if (q->state()->clipTestEnabled) {
|
---|
2026 | glEnable(GL_STENCIL_TEST);
|
---|
2027 | glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT);
|
---|
2028 | } else {
|
---|
2029 | glDisable(GL_STENCIL_TEST);
|
---|
2030 | glStencilFunc(GL_ALWAYS, 0, 0xff);
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 | #ifdef QT_GL_NO_SCISSOR_TEST
|
---|
2034 | currentScissorBounds = QRect(0, 0, width, height);
|
---|
2035 | #else
|
---|
2036 | QRect bounds = q->state()->rectangleClip;
|
---|
2037 | if (!q->state()->clipEnabled) {
|
---|
2038 | if (useSystemClip)
|
---|
2039 | bounds = systemClip.boundingRect();
|
---|
2040 | else
|
---|
2041 | bounds = QRect(0, 0, width, height);
|
---|
2042 | } else {
|
---|
2043 | if (useSystemClip)
|
---|
2044 | bounds = bounds.intersected(systemClip.boundingRect());
|
---|
2045 | else
|
---|
2046 | bounds = bounds.intersected(QRect(0, 0, width, height));
|
---|
2047 | }
|
---|
2048 |
|
---|
2049 | currentScissorBounds = bounds;
|
---|
2050 |
|
---|
2051 | if (bounds == QRect(0, 0, width, height)) {
|
---|
2052 | glDisable(GL_SCISSOR_TEST);
|
---|
2053 | } else {
|
---|
2054 | glEnable(GL_SCISSOR_TEST);
|
---|
2055 | setScissor(bounds);
|
---|
2056 | }
|
---|
2057 | #endif
|
---|
2058 | }
|
---|
2059 |
|
---|
2060 | void QGL2PaintEngineExPrivate::setScissor(const QRect &rect)
|
---|
2061 | {
|
---|
2062 | const int left = rect.left();
|
---|
2063 | const int width = rect.width();
|
---|
2064 | const int bottom = height - (rect.top() + rect.height());
|
---|
2065 | const int height = rect.height();
|
---|
2066 |
|
---|
2067 | glScissor(left, bottom, width, height);
|
---|
2068 | }
|
---|
2069 |
|
---|
2070 | void QGL2PaintEngineEx::clipEnabledChanged()
|
---|
2071 | {
|
---|
2072 | Q_D(QGL2PaintEngineEx);
|
---|
2073 |
|
---|
2074 | state()->clipChanged = true;
|
---|
2075 |
|
---|
2076 | if (painter()->hasClipping())
|
---|
2077 | d->regenerateClip();
|
---|
2078 | else
|
---|
2079 | d->systemStateChanged();
|
---|
2080 | }
|
---|
2081 |
|
---|
2082 | void QGL2PaintEngineExPrivate::clearClip(uint value)
|
---|
2083 | {
|
---|
2084 | dirtyStencilRegion -= currentScissorBounds;
|
---|
2085 |
|
---|
2086 | glStencilMask(0xff);
|
---|
2087 | glClearStencil(value);
|
---|
2088 | glClear(GL_STENCIL_BUFFER_BIT);
|
---|
2089 | glStencilMask(0x0);
|
---|
2090 |
|
---|
2091 | q->state()->needsClipBufferClear = false;
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | void QGL2PaintEngineExPrivate::writeClip(const QVectorPath &path, uint value)
|
---|
2095 | {
|
---|
2096 | transferMode(BrushDrawingMode);
|
---|
2097 |
|
---|
2098 | if (addOffset) {
|
---|
2099 | addOffset = false;
|
---|
2100 | matrixDirty = true;
|
---|
2101 | }
|
---|
2102 | if (snapToPixelGrid) {
|
---|
2103 | snapToPixelGrid = false;
|
---|
2104 | matrixDirty = true;
|
---|
2105 | }
|
---|
2106 |
|
---|
2107 | if (matrixDirty)
|
---|
2108 | updateMatrix();
|
---|
2109 |
|
---|
2110 | stencilClean = false;
|
---|
2111 |
|
---|
2112 | const bool singlePass = !path.hasWindingFill()
|
---|
2113 | && (((q->state()->currentClip == maxClip - 1) && q->state()->clipTestEnabled)
|
---|
2114 | || q->state()->needsClipBufferClear);
|
---|
2115 | const uint referenceClipValue = q->state()->needsClipBufferClear ? 1 : q->state()->currentClip;
|
---|
2116 |
|
---|
2117 | if (q->state()->needsClipBufferClear)
|
---|
2118 | clearClip(1);
|
---|
2119 |
|
---|
2120 | if (path.isEmpty()) {
|
---|
2121 | glEnable(GL_STENCIL_TEST);
|
---|
2122 | glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT);
|
---|
2123 | return;
|
---|
2124 | }
|
---|
2125 |
|
---|
2126 | if (q->state()->clipTestEnabled)
|
---|
2127 | glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT);
|
---|
2128 | else
|
---|
2129 | glStencilFunc(GL_ALWAYS, 0, 0xff);
|
---|
2130 |
|
---|
2131 | vertexCoordinateArray.clear();
|
---|
2132 | vertexCoordinateArray.addPath(path, inverseScale, false);
|
---|
2133 |
|
---|
2134 | if (!singlePass)
|
---|
2135 | fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill());
|
---|
2136 |
|
---|
2137 | glColorMask(false, false, false, false);
|
---|
2138 | glEnable(GL_STENCIL_TEST);
|
---|
2139 | useSimpleShader();
|
---|
2140 |
|
---|
2141 | if (singlePass) {
|
---|
2142 | // Under these conditions we can set the new stencil value in a single
|
---|
2143 | // pass, by using the current value and the "new value" as the toggles
|
---|
2144 |
|
---|
2145 | glStencilFunc(GL_LEQUAL, referenceClipValue, ~GL_STENCIL_HIGH_BIT);
|
---|
2146 | glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT);
|
---|
2147 | glStencilMask(value ^ referenceClipValue);
|
---|
2148 |
|
---|
2149 | drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN);
|
---|
2150 | } else {
|
---|
2151 | glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
|
---|
2152 | glStencilMask(0xff);
|
---|
2153 |
|
---|
2154 | if (!q->state()->clipTestEnabled && path.hasWindingFill()) {
|
---|
2155 | // Pass when any clip bit is set, set high bit
|
---|
2156 | glStencilFunc(GL_NOTEQUAL, GL_STENCIL_HIGH_BIT, ~GL_STENCIL_HIGH_BIT);
|
---|
2157 | composite(vertexCoordinateArray.boundingRect());
|
---|
2158 | }
|
---|
2159 |
|
---|
2160 | // Pass when high bit is set, replace stencil value with new clip value
|
---|
2161 | glStencilFunc(GL_NOTEQUAL, value, GL_STENCIL_HIGH_BIT);
|
---|
2162 |
|
---|
2163 | composite(vertexCoordinateArray.boundingRect());
|
---|
2164 | }
|
---|
2165 |
|
---|
2166 | glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT);
|
---|
2167 | glStencilMask(0);
|
---|
2168 |
|
---|
2169 | glColorMask(true, true, true, true);
|
---|
2170 | }
|
---|
2171 |
|
---|
2172 | void QGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op)
|
---|
2173 | {
|
---|
2174 | // qDebug("QGL2PaintEngineEx::clip()");
|
---|
2175 | Q_D(QGL2PaintEngineEx);
|
---|
2176 |
|
---|
2177 | state()->clipChanged = true;
|
---|
2178 |
|
---|
2179 | ensureActive();
|
---|
2180 |
|
---|
2181 | if (op == Qt::ReplaceClip) {
|
---|
2182 | op = Qt::IntersectClip;
|
---|
2183 | if (d->hasClipOperations()) {
|
---|
2184 | d->systemStateChanged();
|
---|
2185 | state()->canRestoreClip = false;
|
---|
2186 | }
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 | #ifndef QT_GL_NO_SCISSOR_TEST
|
---|
2190 | if (!path.isEmpty() && op == Qt::IntersectClip && (path.shape() == QVectorPath::RectangleHint)) {
|
---|
2191 | const QPointF* const points = reinterpret_cast<const QPointF*>(path.points());
|
---|
2192 | QRectF rect(points[0], points[2]);
|
---|
2193 |
|
---|
2194 | if (state()->matrix.type() <= QTransform::TxScale
|
---|
2195 | || (state()->matrix.type() == QTransform::TxRotate
|
---|
2196 | && qFuzzyIsNull(state()->matrix.m11())
|
---|
2197 | && qFuzzyIsNull(state()->matrix.m22())))
|
---|
2198 | {
|
---|
2199 | state()->rectangleClip = state()->rectangleClip.intersected(state()->matrix.mapRect(rect).toRect());
|
---|
2200 | d->updateClipScissorTest();
|
---|
2201 | return;
|
---|
2202 | }
|
---|
2203 | }
|
---|
2204 | #endif
|
---|
2205 |
|
---|
2206 | const QRect pathRect = state()->matrix.mapRect(path.controlPointRect()).toAlignedRect();
|
---|
2207 |
|
---|
2208 | switch (op) {
|
---|
2209 | case Qt::NoClip:
|
---|
2210 | if (d->useSystemClip) {
|
---|
2211 | state()->clipTestEnabled = true;
|
---|
2212 | state()->currentClip = 1;
|
---|
2213 | } else {
|
---|
2214 | state()->clipTestEnabled = false;
|
---|
2215 | }
|
---|
2216 | state()->rectangleClip = QRect(0, 0, d->width, d->height);
|
---|
2217 | state()->canRestoreClip = false;
|
---|
2218 | d->updateClipScissorTest();
|
---|
2219 | break;
|
---|
2220 | case Qt::IntersectClip:
|
---|
2221 | state()->rectangleClip = state()->rectangleClip.intersected(pathRect);
|
---|
2222 | d->updateClipScissorTest();
|
---|
2223 | d->resetClipIfNeeded();
|
---|
2224 | ++d->maxClip;
|
---|
2225 | d->writeClip(path, d->maxClip);
|
---|
2226 | state()->currentClip = d->maxClip;
|
---|
2227 | state()->clipTestEnabled = true;
|
---|
2228 | break;
|
---|
2229 | case Qt::UniteClip: {
|
---|
2230 | d->resetClipIfNeeded();
|
---|
2231 | ++d->maxClip;
|
---|
2232 | if (state()->rectangleClip.isValid()) {
|
---|
2233 | QPainterPath path;
|
---|
2234 | path.addRect(state()->rectangleClip);
|
---|
2235 |
|
---|
2236 | // flush the existing clip rectangle to the depth buffer
|
---|
2237 | d->writeClip(qtVectorPathForPath(state()->matrix.inverted().map(path)), d->maxClip);
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 | state()->clipTestEnabled = false;
|
---|
2241 | #ifndef QT_GL_NO_SCISSOR_TEST
|
---|
2242 | QRect oldRectangleClip = state()->rectangleClip;
|
---|
2243 |
|
---|
2244 | state()->rectangleClip = state()->rectangleClip.united(pathRect);
|
---|
2245 | d->updateClipScissorTest();
|
---|
2246 |
|
---|
2247 | QRegion extendRegion = QRegion(state()->rectangleClip) - oldRectangleClip;
|
---|
2248 |
|
---|
2249 | if (!extendRegion.isEmpty()) {
|
---|
2250 | QPainterPath extendPath;
|
---|
2251 | extendPath.addRegion(extendRegion);
|
---|
2252 |
|
---|
2253 | // first clear the depth buffer in the extended region
|
---|
2254 | d->writeClip(qtVectorPathForPath(state()->matrix.inverted().map(extendPath)), 0);
|
---|
2255 | }
|
---|
2256 | #endif
|
---|
2257 | // now write the clip path
|
---|
2258 | d->writeClip(path, d->maxClip);
|
---|
2259 | state()->canRestoreClip = false;
|
---|
2260 | state()->currentClip = d->maxClip;
|
---|
2261 | state()->clipTestEnabled = true;
|
---|
2262 | break;
|
---|
2263 | }
|
---|
2264 | default:
|
---|
2265 | break;
|
---|
2266 | }
|
---|
2267 | }
|
---|
2268 |
|
---|
2269 | void QGL2PaintEngineExPrivate::regenerateClip()
|
---|
2270 | {
|
---|
2271 | systemStateChanged();
|
---|
2272 | replayClipOperations();
|
---|
2273 | }
|
---|
2274 |
|
---|
2275 | void QGL2PaintEngineExPrivate::systemStateChanged()
|
---|
2276 | {
|
---|
2277 | Q_Q(QGL2PaintEngineEx);
|
---|
2278 |
|
---|
2279 | q->state()->clipChanged = true;
|
---|
2280 |
|
---|
2281 | if (systemClip.isEmpty()) {
|
---|
2282 | useSystemClip = false;
|
---|
2283 | } else {
|
---|
2284 | if (q->paintDevice()->devType() == QInternal::Widget && currentClipWidget) {
|
---|
2285 | QWidgetPrivate *widgetPrivate = qt_widget_private(currentClipWidget->window());
|
---|
2286 | useSystemClip = widgetPrivate->extra && widgetPrivate->extra->inRenderWithPainter;
|
---|
2287 | } else {
|
---|
2288 | useSystemClip = true;
|
---|
2289 | }
|
---|
2290 | }
|
---|
2291 |
|
---|
2292 | q->state()->clipTestEnabled = false;
|
---|
2293 | q->state()->needsClipBufferClear = true;
|
---|
2294 |
|
---|
2295 | q->state()->currentClip = 1;
|
---|
2296 | maxClip = 1;
|
---|
2297 |
|
---|
2298 | q->state()->rectangleClip = useSystemClip ? systemClip.boundingRect() : QRect(0, 0, width, height);
|
---|
2299 | updateClipScissorTest();
|
---|
2300 |
|
---|
2301 | if (systemClip.rectCount() == 1) {
|
---|
2302 | if (systemClip.boundingRect() == QRect(0, 0, width, height))
|
---|
2303 | useSystemClip = false;
|
---|
2304 | #ifndef QT_GL_NO_SCISSOR_TEST
|
---|
2305 | // scissoring takes care of the system clip
|
---|
2306 | return;
|
---|
2307 | #endif
|
---|
2308 | }
|
---|
2309 |
|
---|
2310 | if (useSystemClip) {
|
---|
2311 | clearClip(0);
|
---|
2312 |
|
---|
2313 | QPainterPath path;
|
---|
2314 | path.addRegion(systemClip);
|
---|
2315 |
|
---|
2316 | q->state()->currentClip = 0;
|
---|
2317 | writeClip(qtVectorPathForPath(q->state()->matrix.inverted().map(path)), 1);
|
---|
2318 | q->state()->currentClip = 1;
|
---|
2319 | q->state()->clipTestEnabled = true;
|
---|
2320 | }
|
---|
2321 | }
|
---|
2322 |
|
---|
2323 | void QGL2PaintEngineEx::setState(QPainterState *new_state)
|
---|
2324 | {
|
---|
2325 | // qDebug("QGL2PaintEngineEx::setState()");
|
---|
2326 |
|
---|
2327 | Q_D(QGL2PaintEngineEx);
|
---|
2328 |
|
---|
2329 | QOpenGL2PaintEngineState *s = static_cast<QOpenGL2PaintEngineState *>(new_state);
|
---|
2330 | QOpenGL2PaintEngineState *old_state = state();
|
---|
2331 |
|
---|
2332 | QPaintEngineEx::setState(s);
|
---|
2333 |
|
---|
2334 | if (s->isNew) {
|
---|
2335 | // Newly created state object. The call to setState()
|
---|
2336 | // will either be followed by a call to begin(), or we are
|
---|
2337 | // setting the state as part of a save().
|
---|
2338 | s->isNew = false;
|
---|
2339 | return;
|
---|
2340 | }
|
---|
2341 |
|
---|
2342 | // Setting the state as part of a restore().
|
---|
2343 |
|
---|
2344 | if (old_state == s || old_state->renderHintsChanged)
|
---|
2345 | renderHintsChanged();
|
---|
2346 |
|
---|
2347 | if (old_state == s || old_state->matrixChanged)
|
---|
2348 | d->matrixDirty = true;
|
---|
2349 |
|
---|
2350 | if (old_state == s || old_state->compositionModeChanged)
|
---|
2351 | d->compositionModeDirty = true;
|
---|
2352 |
|
---|
2353 | if (old_state == s || old_state->opacityChanged)
|
---|
2354 | d->opacityUniformDirty = true;
|
---|
2355 |
|
---|
2356 | if (old_state == s || old_state->clipChanged) {
|
---|
2357 | if (old_state && old_state != s && old_state->canRestoreClip) {
|
---|
2358 | d->updateClipScissorTest();
|
---|
2359 | glDepthFunc(GL_LEQUAL);
|
---|
2360 | } else {
|
---|
2361 | d->regenerateClip();
|
---|
2362 | }
|
---|
2363 | }
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | QPainterState *QGL2PaintEngineEx::createState(QPainterState *orig) const
|
---|
2367 | {
|
---|
2368 | if (orig)
|
---|
2369 | const_cast<QGL2PaintEngineEx *>(this)->ensureActive();
|
---|
2370 |
|
---|
2371 | QOpenGL2PaintEngineState *s;
|
---|
2372 | if (!orig)
|
---|
2373 | s = new QOpenGL2PaintEngineState();
|
---|
2374 | else
|
---|
2375 | s = new QOpenGL2PaintEngineState(*static_cast<QOpenGL2PaintEngineState *>(orig));
|
---|
2376 |
|
---|
2377 | s->matrixChanged = false;
|
---|
2378 | s->compositionModeChanged = false;
|
---|
2379 | s->opacityChanged = false;
|
---|
2380 | s->renderHintsChanged = false;
|
---|
2381 | s->clipChanged = false;
|
---|
2382 |
|
---|
2383 | return s;
|
---|
2384 | }
|
---|
2385 |
|
---|
2386 | QOpenGL2PaintEngineState::QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other)
|
---|
2387 | : QPainterState(other)
|
---|
2388 | {
|
---|
2389 | isNew = true;
|
---|
2390 | needsClipBufferClear = other.needsClipBufferClear;
|
---|
2391 | clipTestEnabled = other.clipTestEnabled;
|
---|
2392 | currentClip = other.currentClip;
|
---|
2393 | canRestoreClip = other.canRestoreClip;
|
---|
2394 | rectangleClip = other.rectangleClip;
|
---|
2395 | }
|
---|
2396 |
|
---|
2397 | QOpenGL2PaintEngineState::QOpenGL2PaintEngineState()
|
---|
2398 | {
|
---|
2399 | isNew = true;
|
---|
2400 | needsClipBufferClear = true;
|
---|
2401 | clipTestEnabled = false;
|
---|
2402 | canRestoreClip = true;
|
---|
2403 | }
|
---|
2404 |
|
---|
2405 | QOpenGL2PaintEngineState::~QOpenGL2PaintEngineState()
|
---|
2406 | {
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | QT_END_NAMESPACE
|
---|