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