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