Refactor context2d thread logic
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcontext2dtexture_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKCONTEXT2DTEXTURE_P_H
43 #define QQUICKCONTEXT2DTEXTURE_P_H
44
45 #include <QtQuick/qsgtexture.h>
46 #include "qquickcanvasitem_p.h"
47 #include "qquickcontext2d_p.h"
48
49 #include <QOpenGLContext>
50 #include <QOpenGLFramebufferObject>
51
52 #include <QtCore/QMutex>
53 #include <QtCore/QWaitCondition>
54 #include <QtCore/QThread>
55
56 QT_BEGIN_HEADER
57
58 QT_BEGIN_NAMESPACE
59
60 class QQuickContext2DTile;
61 class QQuickContext2DCommandBuffer;
62
63 class QQuickContext2DTexture : public QSGDynamicTexture
64 {
65     Q_OBJECT
66 public:
67     QQuickContext2DTexture();
68     ~QQuickContext2DTexture();
69
70     virtual bool hasAlphaChannel() const {return true;}
71     virtual bool hasMipmaps() const {return false;}
72     virtual QSize textureSize() const;
73     virtual QQuickCanvasItem::RenderTarget renderTarget() const = 0;
74     static QRect tiledRect(const QRectF& window, const QSize& tileSize);
75
76     bool setCanvasSize(const QSize &size);
77     bool setTileSize(const QSize &size);
78     bool setCanvasWindow(const QRect& canvasWindow);
79     void setSmooth(bool smooth);
80     bool setDirtyRect(const QRect &dirtyRect);
81     bool canvasDestroyed();
82
83 Q_SIGNALS:
84     void textureChanged();
85
86 public Q_SLOTS:
87     void markDirtyTexture();
88     void setItem(QQuickCanvasItem* item);
89     void canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth);
90     void paint(QQuickContext2DCommandBuffer *ccb);
91     virtual void grabImage(const QRectF& region = QRectF()) = 0;
92
93 protected:
94     void paintWithoutTiles(QQuickContext2DCommandBuffer *ccb);
95     virtual QPaintDevice* beginPainting() {m_painting = true; return 0; }
96     virtual void endPainting() {m_painting = false;}
97     virtual QQuickContext2DTile* createTile() const = 0;
98     virtual void compositeTile(QQuickContext2DTile* tile) = 0;
99
100     void clearTiles();
101     virtual QSize adjustedTileSize(const QSize &ts);
102     QRect createTiles(const QRect& window);
103
104     QList<QQuickContext2DTile*> m_tiles;
105     QQuickContext2D* m_context;
106
107     QQuickContext2D::State m_state;
108
109     QQuickCanvasItem* m_item;
110     QSize m_canvasSize;
111     QSize m_tileSize;
112     QRect m_canvasWindow;
113
114     uint m_dirtyCanvas : 1;
115     uint m_canvasWindowChanged : 1;
116     uint m_dirtyTexture : 1;
117     uint m_smooth : 1;
118     uint m_tiledCanvas : 1;
119     uint m_painting : 1;
120 };
121
122 class QQuickContext2DFBOTexture : public QQuickContext2DTexture
123 {
124     Q_OBJECT
125
126 public:
127     QQuickContext2DFBOTexture();
128     ~QQuickContext2DFBOTexture();
129     virtual int textureId() const;
130     virtual bool updateTexture();
131     virtual QQuickContext2DTile* createTile() const;
132     virtual QPaintDevice* beginPainting();
133     virtual void endPainting();
134     QRectF normalizedTextureSubRect() const;
135     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
136     virtual void compositeTile(QQuickContext2DTile* tile);
137     virtual void bind();
138     QSize adjustedTileSize(const QSize &ts);
139
140 public Q_SLOTS:
141     virtual void grabImage(const QRectF& region = QRectF());
142
143 private:
144     bool doMultisampling() const;
145     QOpenGLFramebufferObject *m_fbo;
146     QOpenGLFramebufferObject *m_multisampledFbo;
147     QMutex m_mutex;
148     QWaitCondition m_condition;
149     QSize m_fboSize;
150     QPaintDevice *m_paint_device;
151 };
152
153 class QSGPlainTexture;
154 class QQuickContext2DImageTexture : public QQuickContext2DTexture
155 {
156     Q_OBJECT
157
158 public:
159     QQuickContext2DImageTexture();
160     ~QQuickContext2DImageTexture();
161     virtual int textureId() const;
162     virtual void bind();
163
164     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
165
166     virtual bool updateTexture();
167     virtual QQuickContext2DTile* createTile() const;
168     virtual QPaintDevice* beginPainting();
169     virtual void compositeTile(QQuickContext2DTile* tile);
170
171 public Q_SLOTS:
172     virtual void grabImage(const QRectF& region = QRectF());
173
174 private:
175     QSGPlainTexture *imageTexture() const;
176     QImage m_image;
177     QPainter m_painter;
178     QSGPlainTexture*  m_texture;
179 };
180
181 QT_END_HEADER
182
183 QT_END_NAMESPACE
184
185 #endif // QQUICKCONTEXT2DTEXTURE_P_H