Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgcanvas_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 ** 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 #ifndef QSGCANVAS_P_H
43 #define QSGCANVAS_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 "qsgitem.h"
57 #include "qsgcanvas.h"
58 #include <private/qdeclarativeguard_p.h>
59
60 #include <private/qsgcontext_p.h>
61
62 #include <QtCore/qthread.h>
63 #include <QtCore/qmutex.h>
64 #include <QtCore/qwaitcondition.h>
65 #include <private/qwidget_p.h>
66 #include <private/qgl_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 //Make it easy to identify and customize the root item if needed
71 class QSGRootItem : public QSGItem
72 {
73     Q_OBJECT
74 public:
75     QSGRootItem();
76 };
77
78 class QSGCanvasPrivate;
79
80 class QSGThreadedRendererAnimationDriver : public QAnimationDriver
81 {
82 public:
83     QSGThreadedRendererAnimationDriver(QSGCanvasPrivate *r, QObject *parent);
84
85 protected:
86     virtual void started();
87     virtual void stopped();
88
89     QSGCanvasPrivate *renderer;
90 };
91
92 class QTouchEvent;
93 class QSGCanvasPrivate : public QGLWidgetPrivate
94 {
95 public:
96     Q_DECLARE_PUBLIC(QSGCanvas)
97
98     static inline QSGCanvasPrivate *get(QSGCanvas *c) { return c->d_func(); }
99
100     QSGCanvasPrivate();
101     virtual ~QSGCanvasPrivate();
102
103     void init(QSGCanvas *);
104
105     QSGRootItem *rootItem;
106
107     QSGItem *activeFocusItem;
108     QSGItem *mouseGrabberItem;
109
110     // Mouse positions are saved in widget coordinates
111     QPoint lastMousePosition;
112     QPoint buttonDownPositions[5]; // Left, Right, Middle, XButton1, XButton2
113     void sceneMouseEventFromMouseEvent(QGraphicsSceneMouseEvent &, QMouseEvent *);
114     void translateTouchEvent(QTouchEvent *touchEvent);
115     static QEvent::Type sceneMouseEventTypeFromMouseEvent(QMouseEvent *);
116     static void sceneMouseEventForTransform(QGraphicsSceneMouseEvent &, const QTransform &);
117     static void transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints, const QTransform &transform);
118     bool deliverInitialMousePressEvent(QSGItem *, QGraphicsSceneMouseEvent *);
119     bool deliverMouseEvent(QGraphicsSceneMouseEvent *);
120     bool sendFilteredMouseEvent(QSGItem *, QSGItem *, QGraphicsSceneMouseEvent *);
121     bool deliverWheelEvent(QSGItem *, QGraphicsSceneWheelEvent *);
122     bool deliverTouchPoints(QSGItem *, QTouchEvent *, const QList<QTouchEvent::TouchPoint> &, QSet<int> *,
123             QHash<QSGItem *, QList<QTouchEvent::TouchPoint> > *);
124     bool deliverTouchEvent(QTouchEvent *);
125     void sceneHoverEventFromMouseEvent(QGraphicsSceneHoverEvent &, QMouseEvent *);
126     bool deliverHoverEvent(QSGItem *, QGraphicsSceneHoverEvent *);
127     void sendHoverEvent(QEvent::Type, QSGItem *, QGraphicsSceneHoverEvent *);
128     void clearHover();
129
130     QDeclarativeGuard<QSGItem> hoverItem;
131     enum FocusOption {
132         DontChangeFocusProperty = 0x01,
133     };
134     Q_DECLARE_FLAGS(FocusOptions, FocusOption)
135
136     void setFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
137     void clearFocusInScope(QSGItem *scope, QSGItem *item, FocusOptions = 0);
138     void notifyFocusChangesRecur(QSGItem **item, int remaining);
139
140     void updateInputMethodData();
141
142     void dirtyItem(QSGItem *);
143     void cleanup(QSGNode *);
144
145     void initializeSceneGraph();
146     void polishItems();
147     void syncSceneGraph();
148     void renderSceneGraph();
149     void runThread();
150
151     QSGItem::UpdatePaintNodeData updatePaintNodeData;
152
153     QSGItem *dirtyItemList;
154     QList<QSGNode *> cleanupNodeList;
155
156     QSet<QSGItem *> itemsToPolish;
157
158     void updateDirtyNodes();
159     void cleanupNodes();
160     bool updateEffectiveOpacity(QSGItem *);
161     void updateEffectiveOpacityRoot(QSGItem *, qreal);
162     void updateDirtyNode(QSGItem *);
163
164     QSGContext *context;
165
166     uint contextInThread : 1;
167     uint threadedRendering : 1;
168     uint exitThread : 1;
169     uint animationRunning: 1;
170     uint idle : 1;              // Set to true when render thread sees no change and enters a wait()
171     uint needsRepaint : 1;      // Set by callback from render if scene needs repainting.
172     uint renderThreadAwakened : 1;
173
174     struct MyThread : public QThread {
175         MyThread(QSGCanvasPrivate *r) : renderer(r) {}
176         virtual void run() { renderer->runThread(); }
177         static void doWait() { QThread::msleep(16); }
178         QSGCanvasPrivate *renderer;
179     };
180     MyThread *thread;
181     QMutex mutex;
182     QWaitCondition wait;
183     QSize widgetSize;
184     QSize viewportSize;
185
186     QAnimationDriver *animationDriver;
187
188     QHash<int, QSGItem *> itemForTouchPointId;
189 };
190
191 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGCanvasPrivate::FocusOptions)
192
193 QT_END_NAMESPACE
194
195 #endif // QSGCANVAS_P_H