1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
57 #include "qsgcanvas.h"
58 #include <private/qdeclarativeguard_p.h>
60 #include <private/qsgcontext_p.h>
61 #include <private/qsgdrag_p.h>
63 #include <QtCore/qthread.h>
64 #include <QtCore/qmutex.h>
65 #include <QtCore/qwaitcondition.h>
66 #include <private/qwindow_p.h>
67 #include <private/qopengl_p.h>
68 #include <qopenglcontext.h>
69 #include <QtGui/qopenglframebufferobject.h>
70 #include <QtGui/qevent.h>
71 #include <QtGui/qinputpanel.h>
75 //Make it easy to identify and customize the root item if needed
76 class QSGRootItem : public QSGItem
83 class QSGCanvasPrivate;
86 class QSGCanvasRenderLoop;
87 class QSGCanvasIncubationController;
89 class QSGCanvasPrivate : public QWindowPrivate
92 Q_DECLARE_PUBLIC(QSGCanvas)
94 static inline QSGCanvasPrivate *get(QSGCanvas *c) { return c->d_func(); }
97 virtual ~QSGCanvasPrivate();
99 void init(QSGCanvas *);
101 QSGRootItem *rootItem;
103 QSGItem *activeFocusItem;
104 QSGItem *mouseGrabberItem;
105 QSGDragGrabber dragGrabber;
107 // Mouse positions are saved in widget coordinates
108 QPointF lastMousePosition;
109 void translateTouchEvent(QTouchEvent *touchEvent);
110 static void transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform);
111 bool deliverInitialMousePressEvent(QSGItem *, QMouseEvent *);
112 bool deliverMouseEvent(QMouseEvent *);
113 bool sendFilteredMouseEvent(QSGItem *, QSGItem *, QMouseEvent *);
114 bool deliverWheelEvent(QSGItem *, QWheelEvent *);
115 bool deliverTouchPoints(QSGItem *, QTouchEvent *, const QList<QTouchEvent::TouchPoint> &, QSet<int> *,
116 QHash<QSGItem *, QList<QTouchEvent::TouchPoint> > *);
117 bool deliverTouchEvent(QTouchEvent *);
118 bool deliverHoverEvent(QSGItem *, const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, bool &accepted);
119 bool sendHoverEvent(QEvent::Type, QSGItem *, const QPointF &scenePos, const QPointF &lastScenePos,
120 Qt::KeyboardModifiers modifiers, bool accepted);
122 void deliverDragEvent(QSGDragGrabber *, QEvent *);
123 bool deliverDragEvent(QSGDragGrabber *, QSGItem *, QDragMoveEvent *);
125 QList<QSGItem*> hoverItems;
127 DontChangeFocusProperty = 0x01,
129 Q_DECLARE_FLAGS(FocusOptions, FocusOption)
131 void setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
132 void clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
133 void notifyFocusChangesRecur(QSGItem **item, int remaining);
135 void updateInputMethodData();
136 void updateFocusItemTransform();
138 void dirtyItem(QSGItem *);
139 void cleanup(QSGNode *);
141 void initializeSceneGraph();
143 void syncSceneGraph();
144 void renderSceneGraph(const QSize &size);
146 QSGItem::UpdatePaintNodeData updatePaintNodeData;
148 QSGItem *dirtyItemList;
149 QList<QSGNode *> cleanupNodeList;
151 QSet<QSGItem *> itemsToPolish;
153 void updateDirtyNodes();
155 bool updateEffectiveOpacity(QSGItem *);
156 void updateEffectiveOpacityRoot(QSGItem *, qreal);
157 void updateDirtyNode(QSGItem *);
161 uint vsyncAnimations : 1;
163 QSGCanvasRenderLoop *thread;
167 QAnimationDriver *animationDriver;
169 QOpenGLFramebufferObject *renderTarget;
171 QHash<int, QSGItem *> itemForTouchPointId;
173 mutable QSGCanvasIncubationController *incubationController;
176 class QSGCanvasRenderLoop
179 QSGCanvasRenderLoop()
185 virtual ~QSGCanvasRenderLoop()
190 friend class QSGCanvasPrivate;
192 virtual void paint() = 0;
193 virtual void resize(const QSize &size) = 0;
194 virtual void startRendering() = 0;
195 virtual void stopRendering() = 0;
196 virtual QImage grab() = 0;
197 virtual void setWindowSize(const QSize &size) = 0;
198 virtual void maybeUpdate() = 0;
199 virtual bool isRunning() const = 0;
200 virtual void animationStarted() = 0;
201 virtual void animationStopped() = 0;
202 virtual void moveContextToThread(QSGContext *) { }
203 virtual bool *allowMainThreadProcessing() { return 0; }
206 void initializeSceneGraph() { d->initializeSceneGraph(); }
207 void syncSceneGraph() { d->syncSceneGraph(); }
208 void renderSceneGraph(const QSize &size) { d->renderSceneGraph(size); }
209 void polishItems() { d->polishItems(); }
210 QAnimationDriver *animationDriver() const { return d->animationDriver; }
212 inline QOpenGLContext *glContext() const { return gl; }
213 void createGLContext();
214 void makeCurrent() { gl->makeCurrent(renderer); }
215 void doneCurrent() { gl->doneCurrent(); }
217 gl->swapBuffers(renderer);
218 emit renderer->frameSwapped();
228 class QSGCanvasRenderThread : public QThread, public QSGCanvasRenderLoop
232 QSGCanvasRenderThread()
233 : mutex(QMutex::NonRecursive)
234 , allowMainThreadProcessingFlag(true)
235 , animationRunning(false)
237 , isPaintCompleted(false)
238 , isGuiBlockPending(false)
239 , isRenderBlocked(false)
240 , isExternalUpdatePending(false)
241 , syncAlreadyHappened(false)
246 , renderThreadAwakened(false)
249 inline void lock() { mutex.lock(); }
250 inline void unlock() { mutex.unlock(); }
251 inline void wait() { condition.wait(&mutex); }
252 inline void wake() { condition.wakeOne(); }
258 void resize(const QSize &size);
259 void startRendering();
260 void stopRendering();
261 void exhaustSyncEvent();
262 void sync(bool guiAlreadyLocked);
263 bool isRunning() const { return QThread::isRunning(); }
264 void setWindowSize(const QSize &size) { windowSize = size; }
266 void moveContextToThread(QSGContext *c) { c->moveToThread(this); }
267 bool *allowMainThreadProcessing() { return &allowMainThreadProcessingFlag; }
269 bool event(QEvent *);
274 void animationStarted();
275 void animationStopped();
279 QWaitCondition condition;
281 bool allowMainThreadProcessingFlag;
286 uint animationRunning: 1;
288 uint isPaintCompleted : 1;
289 uint isGuiBlockPending : 1;
290 uint isRenderBlocked : 1;
291 uint isExternalUpdatePending : 1;
292 uint syncAlreadyHappened : 1;
297 uint renderThreadAwakened : 1;
304 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGCanvasPrivate::FocusOptions)
308 #endif // QSGCANVAS_P_H