1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qsimplecanvas.h"
#include "qsimplecanvas_p.h"
#include "qsimplecanvasitem_p.h"
#include "qsimplecanvasfilter_p.h"
#include <gltexture.h>
#include <glsave.h>
#include <math.h>
QT_BEGIN_NAMESPACE
void CanvasEGLWidget::updateGL()
{
_clip = QRect();
QGLWidget::updateGL();
}
void CanvasEGLWidget::updateGL(const QRect &r)
{
if (r.isEmpty())
return;
_clip = r;
QGLWidget::updateGL();
}
void CanvasEGLWidget::paintGL()
{
if (!_clip.isEmpty()) {
glEnable(GL_SCISSOR_TEST);
glScissor(_clip.x(), _clip.y(), _clip.width(), _clip.height());
} else {
glDisable(GL_SCISSOR_TEST);
}
glDepthMask(GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDepthMask(GL_FALSE);
_canvas->paintGL();
}
QRect CanvasEGLWidget::map(const QRectF &f)
{
return invDefaultTransform.mapRect(f).toAlignedRect();
}
void CanvasEGLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void CanvasEGLWidget::resizeEvent(QResizeEvent *e)
{
defaultTransform.identity();
defaultTransform.flipCoordinates();
defaultTransform.translate(-1, -1, 0);
defaultTransform.scale(2. / width(), 2. / height(), -2. / 65536.);
invDefaultTransform = defaultTransform.inverted();
_canvas->root->d_func()->transformActive = defaultTransform;
QGLWidget::resizeEvent(e);
}
void CanvasEGLWidget::initializeGL()
{
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_GREATER);
#ifdef QT_OPENGL_ES
glClearDepthf(0);
#else
glClearDepth(0);
#endif
}
void QSimpleCanvasPrivate::paintGL()
{
lrpTimer.start();
QSimpleCanvasItemPrivate::GLPaintParameters p;
p.sceneRect = QRect(0, 0, q->width(), q->height());
p.clipRect = p.sceneRect;
p.opacity = 1;
p.forceParamRefresh = false;
if (!isSetup) {
unsigned int zero = 0;
root->d_func()->setupPainting(0, QRect(), &zero);
}
root->d_func()->paint(p);
lrpTime = lrpTimer.elapsed();
}
QSimpleCanvas::Matrix QSimpleCanvasItemPrivate::localTransform() const
{
Q_Q(const QSimpleCanvasItem);
QSimpleCanvas::Matrix trans;
trans.translate(q->x(), q->y());
QPointF to = transformOrigin();
trans.translate(to.x(), to.y());
trans.scale(q->scale().value(), q->scale().value());
trans.translate(-to.x(), -to.y());
trans *= transformUser;
return trans;
}
void QSimpleCanvasItemPrivate::simplePaintChild(const GLPaintParameters ¶ms, QSimpleCanvasItem *child)
{
GLPaintParameters childParams = params;
if (child->d_func()->activeOpacity != 0) {
childParams.boundingRect = child->boundingRect();
child->d_func()->paint(childParams);
}
}
void QSimpleCanvasItemPrivate::paintChild(const GLPaintParameters ¶ms,
QSimpleCanvasItem *child)
{
if (params.forceParamRefresh) {
QSimpleCanvas::Matrix t = child->d_func()->transformActive;
qreal o = child->d_func()->activeOpacity;
setupChildState(child);
simplePaintChild(params, child);
child->d_func()->transformActive = t;
child->d_func()->activeOpacity = o;
} else {
simplePaintChild(params, child);
}
}
void QSimpleCanvasItemPrivate::setupChildState(QSimpleCanvasItem *child)
{
qreal visible = child->visible().value();
child->d_func()->activeOpacity = activeOpacity;
if (visible != 1)
child->d_func()->activeOpacity *= visible;
if (child->d_func()->activeOpacity != 0) {
// Calculate child's transform
qreal x = child->x();
qreal y = child->y();
qreal scale = child->scale().value();
QSimpleCanvasItem::Flip flip = child->flip();
QSimpleCanvas::Matrix &am = child->d_func()->transformActive;
am = transformActive;
if (x != 0 || y != 0)
am.translate(x, y);
if (scale != 1) {
QPointF to = child->d_func()->transformOrigin();
if (to.x() != 0. || to.y() != 0.)
am.translate(to.x(), to.y());
am.scale(scale, scale);
if (to.x() != 0. || to.y() != 0.)
am.translate(-to.x(), -to.y());
}
if (child->d_func()->transformUserSet)
am *= child->d_func()->transformUser;
if (flip) {
QRectF br = child->boundingRect();
am.translate(br.width() / 2., br.height() / 2);
am.rotate(180, (flip & QSimpleCanvasItem::VerticalFlip)?1:0, (flip & QSimpleCanvasItem::HorizontalFlip)?1:0, 0);
am.translate(-br.width() / 2., -br.height() / 2);
}
}
}
QRectF QSimpleCanvasItemPrivate::setupPainting(int version, const QRect &bounding, unsigned int *zero)
{
Q_Q(QSimpleCanvasItem);
QRectF rv = transformActive.mapRect(q->boundingRect());
unsigned int oldZero = *zero;
for (int ii = 0; ii < children.count(); ++ii) {
QSimpleCanvasItem *child = children.at(ii);
setupChildState(child);
if (child->d_func()->activeOpacity != 0)
rv |= child->d_func()->setupPainting(version, bounding, zero);
}
if (clip || oldZero != *zero)
(*zero)++;
transformActive.translate(0, 0, *zero);
lastPaintRect = rv;
return rv;
}
void QSimpleCanvasItemPrivate::paintNoClip(GLPaintParameters ¶ms, QSimpleCanvasFilter::Layer layer)
{
Q_Q(QSimpleCanvasItem);
zOrderChildren();
int upto = 0;
for (upto = 0; upto < children.count(); ++upto) {
QSimpleCanvasItem *c = children.at(upto);
if (c->zValue().value() < 0) {
if (layer & QSimpleCanvasFilter::ChildrenUnderItem)
paintChild(params, c);
} else {
break;
}
}
if (layer & QSimpleCanvasFilter::Item &&
q->options() & QSimpleCanvasItem::HasContents) {
QSimpleCanvasItem::GLPainter painter(q);
painter.activeTransform = transformActive;
painter.activeOpacity = activeOpacity;
painter.sceneClipRect = params.clipRect;
q->paintGLContents(painter);
}
if (layer & QSimpleCanvasFilter::ChildrenAboveItem) {
for (; upto < children.count(); ++upto) {
QSimpleCanvasItem *c = children.at(upto);
paintChild(params, c);
}
}
}
void QSimpleCanvasItemPrivate::paint(GLPaintParameters ¶ms, QSimpleCanvasFilter::Layer layer)
{
if (!layer)
return;
// XXX Handle separate cliping modes
if (clip) {
GLSaveScissor ss;
qreal width = params.boundingRect.width();
qreal height = params.boundingRect.height();
float margin = width + height;
GLfloat clipvertices[] =
{
-margin, -margin,
margin, -margin,
margin, 0,
-margin, -margin,
-margin, 0,
margin, 0,
-margin, 0,
-margin, margin,
0, 0,
0, 0,
0, margin,
-margin, margin,
0, height,
0, margin,
margin, margin,
margin, margin,
0, height,
margin, height,
width, 0,
margin, 0,
margin, height,
margin, height,
width, height,
width, 0
};
QRectF r = transformActive.mapRect(params.boundingRect);
r.translate(1, 1);
float xscale = 0.5 * float(params.sceneRect.width());
float yscale = 0.5 * float(params.sceneRect.height());
r.moveTo(r.x() * xscale, r.y() * yscale);
r.setSize(QSizeF(r.width() * xscale, r.height() * yscale));
glEnable(GL_SCISSOR_TEST);
int sr_x = ::floorf(r.x());
int sr_y = ::floorf(r.y());
int sr_width = ::ceilf(r.right()) - sr_x;
int sr_height = ::ceilf(r.bottom()) - sr_y;
QRect sr(sr_x, sr_y, sr_width, sr_height);
if (ss.wasEnabled())
sr &= ss.rect();
glScissor(sr.x(), sr.y(), sr.width(), sr.height());
{
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(transformActive.data());
glDepthMask(GL_TRUE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, clipvertices);
glDrawArrays(GL_TRIANGLES, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_FALSE);
}
GLPaintParameters newParams = params;
newParams.clipRect = sr;
newParams.clipRect.moveTo(sr.x(), params.sceneRect.height() - sr.y() - sr.height());
paintNoClip(newParams, layer);
} else {
paintNoClip(params, layer);
}
}
QGLShaderProgram *QSimpleCanvasItem::GLPainter::useTextureShader()
{
qFatal("Cannot call QSimpleCanvasItem::GLPainter::useTextureShader() when using OpenGL ES 1.1");
return 0;
}
QGLShaderProgram *QSimpleCanvasItem::GLPainter::useColorShader(const QColor &color)
{
Q_UNUSED(color);
qFatal("Cannot call QSimpleCanvasItem::GLPainter::useColorShader() when using OpenGL ES 1.1");
return 0;
}
GLBasicShaders *QSimpleCanvasItemPrivate::basicShaders() const
{
qFatal("Cannot call QSimpleCanvasItem::basicShaders() when using OpenGL ES 1.1");
return 0;
}
QGLFramebufferObject *QSimpleCanvasPrivate::acquire(int, int)
{
return 0;
}
void QSimpleCanvasPrivate::release(QGLFramebufferObject *)
{
}
void QSimpleCanvasItem::GLPainter::drawImage(const QPointF &point,
const GLTexture &texture)
{
drawImage(QRectF(point, QSizeF(texture.width(), texture.height())), texture);
}
void QSimpleCanvasItem::GLPainter::drawImage(const QRectF &rect,
const GLTexture &img)
{
qFatal("Cannot call QSimpleCanvasItem::GLPainter::drawImage() when using OpenGL ES 1.1");
}
QT_END_NAMESPACE
|