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 ****************************************************************************/
42 #ifndef QQUICKCANVAS_P_H
43 #define QQUICKCANVAS_P_H
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.
56 #include "qquickitem.h"
57 #include "qquickcanvas.h"
58 #include <private/qdeclarativeguard_p.h>
60 #include <private/qsgcontext_p.h>
61 #include <private/qquickdrag_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 QQuickRootItem : public QQuickItem
83 class QQuickCanvasPrivate;
86 class QQuickCanvasRenderLoop;
87 class QQuickCanvasIncubationController;
89 class QQuickCanvasPrivate : public QWindowPrivate
92 Q_DECLARE_PUBLIC(QQuickCanvas)
94 static inline QQuickCanvasPrivate *get(QQuickCanvas *c) { return c->d_func(); }
96 QQuickCanvasPrivate();
97 virtual ~QQuickCanvasPrivate();
99 void init(QQuickCanvas *);
101 QQuickRootItem *rootItem;
103 QQuickItem *activeFocusItem;
104 QQuickItem *mouseGrabberItem;
105 QQuickDragGrabber 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(QQuickItem *, QMouseEvent *);
112 bool deliverMouseEvent(QMouseEvent *);
113 bool sendFilteredMouseEvent(QQuickItem *, QQuickItem *, QEvent *);
114 bool deliverWheelEvent(QQuickItem *, QWheelEvent *);
115 bool deliverTouchPoints(QQuickItem *, QTouchEvent *, const QList<QTouchEvent::TouchPoint> &, QSet<int> *,
116 QHash<QQuickItem *, QList<QTouchEvent::TouchPoint> > *);
117 bool deliverTouchEvent(QTouchEvent *);
118 bool deliverHoverEvent(QQuickItem *, const QPointF &scenePos, const QPointF &lastScenePos, Qt::KeyboardModifiers modifiers, bool &accepted);
119 bool sendHoverEvent(QEvent::Type, QQuickItem *, const QPointF &scenePos, const QPointF &lastScenePos,
120 Qt::KeyboardModifiers modifiers, bool accepted);
122 void deliverDragEvent(QQuickDragGrabber *, QEvent *);
123 bool deliverDragEvent(QQuickDragGrabber *, QQuickItem *, QDragMoveEvent *);
125 QList<QQuickItem*> hoverItems;
127 DontChangeFocusProperty = 0x01,
129 Q_DECLARE_FLAGS(FocusOptions, FocusOption)
131 void setFocusInScope(QQuickItem *scope, QQuickItem *item, FocusOptions = 0);
132 void clearFocusInScope(QQuickItem *scope, QQuickItem *item, FocusOptions = 0);
133 void notifyFocusChangesRecur(QQuickItem **item, int remaining);
135 void updateInputMethodData();
136 void updateFocusItemTransform();
138 void dirtyItem(QQuickItem *);
139 void cleanup(QSGNode *);
141 void initializeSceneGraph();
143 void syncSceneGraph();
144 void renderSceneGraph(const QSize &size);
146 QQuickItem::UpdatePaintNodeData updatePaintNodeData;
148 QQuickItem *dirtyItemList;
149 QList<QSGNode *> cleanupNodeList;
151 QSet<QQuickItem *> itemsToPolish;
153 void updateDirtyNodes();
155 void cleanupNodesOnShutdown();
156 bool updateEffectiveOpacity(QQuickItem *);
157 void updateEffectiveOpacityRoot(QQuickItem *, qreal);
158 void updateDirtyNode(QQuickItem *);
162 uint vsyncAnimations : 1;
164 QQuickCanvasRenderLoop *thread;
168 QAnimationDriver *animationDriver;
170 QOpenGLFramebufferObject *renderTarget;
172 QHash<int, QQuickItem *> itemForTouchPointId;
174 mutable QQuickCanvasIncubationController *incubationController;
176 static void cleanupNodesOnShutdown(QQuickItem *);
179 class QQuickCanvasRenderLoop
182 QQuickCanvasRenderLoop()
188 virtual ~QQuickCanvasRenderLoop()
193 friend class QQuickCanvasPrivate;
195 virtual void paint() = 0;
196 virtual void resize(const QSize &size) = 0;
197 virtual void startRendering() = 0;
198 virtual void stopRendering() = 0;
199 virtual QImage grab() = 0;
200 virtual void setWindowSize(const QSize &size) = 0;
201 virtual void maybeUpdate() = 0;
202 virtual bool isRunning() const = 0;
203 virtual void animationStarted() = 0;
204 virtual void animationStopped() = 0;
205 virtual void moveContextToThread(QSGContext *) { }
206 virtual bool *allowMainThreadProcessing() { return 0; }
209 void initializeSceneGraph() { d->initializeSceneGraph(); }
210 void syncSceneGraph() { d->syncSceneGraph(); }
211 void cleanupNodesOnShutdown() { d->cleanupNodesOnShutdown(); }
212 void renderSceneGraph(const QSize &size) { d->renderSceneGraph(size); }
213 void polishItems() { d->polishItems(); }
214 QAnimationDriver *animationDriver() const { return d->animationDriver; }
216 inline QOpenGLContext *glContext() const { return gl; }
217 void createGLContext();
218 void makeCurrent() { gl->makeCurrent(renderer); }
219 void doneCurrent() { gl->doneCurrent(); }
221 gl->swapBuffers(renderer);
222 emit renderer->frameSwapped();
226 QQuickCanvasPrivate *d;
227 QQuickCanvas *renderer;
232 class QQuickCanvasRenderThread : public QThread, public QQuickCanvasRenderLoop
236 QQuickCanvasRenderThread()
237 : mutex(QMutex::NonRecursive)
238 , allowMainThreadProcessingFlag(true)
239 , animationRunning(false)
241 , isPaintCompleted(false)
242 , isGuiBlockPending(false)
243 , isRenderBlocked(false)
244 , isExternalUpdatePending(false)
245 , syncAlreadyHappened(false)
250 , renderThreadAwakened(false)
253 inline void lock() { mutex.lock(); }
254 inline void unlock() { mutex.unlock(); }
255 inline void wait() { condition.wait(&mutex); }
256 inline void wake() { condition.wakeOne(); }
262 void resize(const QSize &size);
263 void startRendering();
264 void stopRendering();
265 void exhaustSyncEvent();
266 void sync(bool guiAlreadyLocked);
267 bool isRunning() const { return QThread::isRunning(); }
268 void setWindowSize(const QSize &size) { windowSize = size; }
270 void moveContextToThread(QSGContext *c) { c->moveToThread(this); }
271 bool *allowMainThreadProcessing() { return &allowMainThreadProcessingFlag; }
273 bool event(QEvent *);
278 void animationStarted();
279 void animationStopped();
283 QWaitCondition condition;
285 bool allowMainThreadProcessingFlag;
290 uint animationRunning: 1;
292 uint isPaintCompleted : 1;
293 uint isGuiBlockPending : 1;
294 uint isRenderBlocked : 1;
295 uint isExternalUpdatePending : 1;
296 uint syncAlreadyHappened : 1;
301 uint renderThreadAwakened : 1;
308 Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickCanvasPrivate::FocusOptions)
312 #endif // QQUICKCANVAS_P_H