702234d9e83f0c34a75a5d5030c4ccabd888f1d6
[profile/ivi/qtdeclarative.git] / src / declarative / items / qquickcanvas_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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 QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKCANVAS_P_H
43 #define QQUICKCANVAS_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include "qquickitem.h"
57 #include "qquickcanvas.h"
58 #include <private/qdeclarativeguard_p.h>
59
60 #include <private/qsgcontext_p.h>
61 #include <private/qquickdrag_p.h>
62
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>
72
73 QT_BEGIN_NAMESPACE
74
75 //Make it easy to identify and customize the root item if needed
76 class QQuickRootItem : public QQuickItem
77 {
78     Q_OBJECT
79 public:
80     QQuickRootItem();
81 };
82
83 class QQuickCanvasPrivate;
84
85 class QTouchEvent;
86 class QQuickCanvasRenderLoop;
87 class QQuickCanvasIncubationController;
88
89 class QQuickCanvasPrivate : public QWindowPrivate
90 {
91 public:
92     Q_DECLARE_PUBLIC(QQuickCanvas)
93
94     static inline QQuickCanvasPrivate *get(QQuickCanvas *c) { return c->d_func(); }
95
96     QQuickCanvasPrivate();
97     virtual ~QQuickCanvasPrivate();
98
99     void init(QQuickCanvas *);
100
101     QQuickRootItem *rootItem;
102
103     QQuickItem *activeFocusItem;
104     QQuickItem *mouseGrabberItem;
105     QQuickDragGrabber dragGrabber;
106
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);
121     bool clearHover();
122     void deliverDragEvent(QQuickDragGrabber *, QEvent *);
123     bool deliverDragEvent(QQuickDragGrabber *, QQuickItem *, QDragMoveEvent *);
124
125     QList<QQuickItem*> hoverItems;
126     enum FocusOption {
127         DontChangeFocusProperty = 0x01,
128     };
129     Q_DECLARE_FLAGS(FocusOptions, FocusOption)
130
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);
134
135     void updateInputMethodData();
136     void updateFocusItemTransform();
137
138     void dirtyItem(QQuickItem *);
139     void cleanup(QSGNode *);
140
141     void initializeSceneGraph();
142     void polishItems();
143     void syncSceneGraph();
144     void renderSceneGraph(const QSize &size);
145
146     QQuickItem::UpdatePaintNodeData updatePaintNodeData;
147
148     QQuickItem *dirtyItemList;
149     QList<QSGNode *> cleanupNodeList;
150
151     QSet<QQuickItem *> itemsToPolish;
152
153     void updateDirtyNodes();
154     void cleanupNodes();
155     void cleanupNodesOnShutdown();
156     bool updateEffectiveOpacity(QQuickItem *);
157     void updateEffectiveOpacityRoot(QQuickItem *, qreal);
158     void updateDirtyNode(QQuickItem *);
159
160     QSGContext *context;
161
162     uint vsyncAnimations : 1;
163
164     QQuickCanvasRenderLoop *thread;
165     QSize widgetSize;
166     QSize viewportSize;
167
168     QAnimationDriver *animationDriver;
169
170     QOpenGLFramebufferObject *renderTarget;
171
172     QHash<int, QQuickItem *> itemForTouchPointId;
173
174     mutable QQuickCanvasIncubationController *incubationController;
175 private:
176     static void cleanupNodesOnShutdown(QQuickItem *);
177 };
178
179 class QQuickCanvasRenderLoop
180 {
181 public:
182     QQuickCanvasRenderLoop()
183         : d(0)
184         , renderer(0)
185         , gl(0)
186     {
187     }
188     virtual ~QQuickCanvasRenderLoop()
189     {
190         delete gl;
191     }
192
193     friend class QQuickCanvasPrivate;
194
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; }
207
208 protected:
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; }
215
216     inline QOpenGLContext *glContext() const { return gl; }
217     void createGLContext();
218     void makeCurrent() { gl->makeCurrent(renderer); }
219     void doneCurrent() { gl->doneCurrent(); }
220     void swapBuffers() {
221         gl->swapBuffers(renderer);
222         emit renderer->frameSwapped();
223     }
224
225 private:
226     QQuickCanvasPrivate *d;
227     QQuickCanvas *renderer;
228
229     QOpenGLContext *gl;
230 };
231
232 class QQuickCanvasRenderThread : public QThread, public QQuickCanvasRenderLoop
233 {
234     Q_OBJECT
235 public:
236     QQuickCanvasRenderThread()
237         : mutex(QMutex::NonRecursive)
238         , allowMainThreadProcessingFlag(true)
239         , animationRunning(false)
240         , isGuiBlocked(0)
241         , isPaintCompleted(false)
242         , isGuiBlockPending(false)
243         , isRenderBlocked(false)
244         , isExternalUpdatePending(false)
245         , syncAlreadyHappened(false)
246         , inSync(false)
247         , doGrab(false)
248         , shouldExit(false)
249         , hasExited(false)
250         , renderThreadAwakened(false)
251     {}
252
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(); }
257
258     void lockInGui();
259     void unlockInGui();
260
261     void paint();
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; }
269     void maybeUpdate();
270     void moveContextToThread(QSGContext *c) { c->moveToThread(this); }
271     bool *allowMainThreadProcessing() { return &allowMainThreadProcessingFlag; }
272
273     bool event(QEvent *);
274
275     QImage grab();
276
277 public slots:
278     void animationStarted();
279     void animationStopped();
280
281 public:
282     QMutex mutex;
283     QWaitCondition condition;
284
285     bool allowMainThreadProcessingFlag;
286
287     QSize windowSize;
288     QSize renderedSize;
289
290     uint animationRunning: 1;
291     int isGuiBlocked;
292     uint isPaintCompleted : 1;
293     uint isGuiBlockPending : 1;
294     uint isRenderBlocked : 1;
295     uint isExternalUpdatePending : 1;
296     uint syncAlreadyHappened : 1;
297     uint inSync : 1;
298     uint doGrab : 1;
299     uint shouldExit : 1;
300     uint hasExited : 1;
301     uint renderThreadAwakened : 1;
302
303     QImage grabContent;
304
305     void run();
306 };
307
308 Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickCanvasPrivate::FocusOptions)
309
310 QT_END_NAMESPACE
311
312 #endif // QQUICKCANVAS_P_H