81b239dc168b2790346b4e6d0356bfd5bca9e96c
[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 void lock() {}
74     virtual void unlock() {}
75     virtual void wait() {}
76     virtual void wake() {}
77     bool threadRendering() const {return m_threadRendering;}
78     virtual bool supportThreadRendering() const = 0;
79     virtual bool supportDirectRendering() const = 0;
80     virtual QQuickCanvasItem::RenderTarget renderTarget() const = 0;
81     virtual QImage toImage(const QRectF& region = QRectF()) = 0;
82     static QRect tiledRect(const QRectF& window, const QSize& tileSize);
83
84     bool setCanvasSize(const QSize &size);
85     bool setTileSize(const QSize &size);
86     bool setCanvasWindow(const QRect& canvasWindow);
87     void setSmooth(bool smooth);
88     bool setDirtyRect(const QRect &dirtyRect);
89     virtual void canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth);
90     bool canvasDestroyed();
91 Q_SIGNALS:
92     void textureChanged();
93
94 public Q_SLOTS:
95     void markDirtyTexture();
96     void setItem(QQuickCanvasItem* item);
97     void paint();
98
99 protected:
100     void paintWithoutTiles();
101     virtual QPaintDevice* beginPainting() {m_painting = true; return 0; }
102     virtual void endPainting() {m_painting = false;}
103     virtual QQuickContext2DTile* createTile() const = 0;
104     virtual void compositeTile(QQuickContext2DTile* tile) = 0;
105
106     void clearTiles();
107     virtual QSize adjustedTileSize(const QSize &ts);
108     QRect createTiles(const QRect& window);
109
110     QList<QQuickContext2DTile*> m_tiles;
111     QQuickContext2D* m_context;
112
113     QQuickContext2D::State m_state;
114
115     QQuickCanvasItem* m_item;
116     QSize m_canvasSize;
117     QSize m_tileSize;
118     QRect m_canvasWindow;
119
120     uint m_dirtyCanvas : 1;
121     uint m_canvasWindowChanged : 1;
122     uint m_dirtyTexture : 1;
123     uint m_threadRendering : 1;
124     uint m_smooth : 1;
125     uint m_tiledCanvas : 1;
126     uint m_doGrabImage : 1;
127     uint m_painting : 1;
128 };
129
130 class QQuickContext2DFBOTexture : public QQuickContext2DTexture
131 {
132     Q_OBJECT
133
134 public:
135     QQuickContext2DFBOTexture();
136     ~QQuickContext2DFBOTexture();
137     virtual int textureId() const;
138     virtual bool updateTexture();
139     virtual QQuickContext2DTile* createTile() const;
140     virtual QImage toImage(const QRectF& region = QRectF());
141     virtual QPaintDevice* beginPainting();
142     virtual void endPainting();
143     QRectF normalizedTextureSubRect() const;
144     virtual bool supportThreadRendering() const {return false;}
145     virtual bool supportDirectRendering() const {return false;}
146     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
147     virtual void compositeTile(QQuickContext2DTile* tile);
148     virtual void bind();
149     QSize adjustedTileSize(const QSize &ts);
150
151 private Q_SLOTS:
152     void grabImage();
153
154 private:
155     bool doMultisampling() const;
156     QImage m_grabedImage;
157     QOpenGLFramebufferObject *m_fbo;
158     QOpenGLFramebufferObject *m_multisampledFbo;
159     QMutex m_mutex;
160     QWaitCondition m_condition;
161     QSize m_fboSize;
162     QPaintDevice *m_paint_device;
163 };
164
165 class QSGPlainTexture;
166 class QQuickContext2DImageTexture : public QQuickContext2DTexture
167 {
168     Q_OBJECT
169
170 public:
171     QQuickContext2DImageTexture(bool threadRendering = true);
172     ~QQuickContext2DImageTexture();
173     virtual int textureId() const;
174     virtual void bind();
175     virtual bool supportThreadRendering() const {return true;}
176     virtual bool supportDirectRendering() const;
177     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
178     virtual void lock();
179     virtual void unlock();
180     virtual void wait();
181     virtual void wake();
182
183     virtual bool updateTexture();
184     virtual QQuickContext2DTile* createTile() const;
185     virtual QImage toImage(const QRectF& region = QRectF());
186     virtual QPaintDevice* beginPainting();
187     virtual void compositeTile(QQuickContext2DTile* tile);
188
189 private Q_SLOTS:
190     void grabImage(const QRect& r);
191 private:
192     QImage m_image;
193     QImage m_grabedImage;
194     QMutex m_mutex;
195     QWaitCondition m_waitCondition;
196     QPainter m_painter;
197     QSGPlainTexture*  m_texture;
198 };
199
200 QT_END_HEADER
201
202 QT_END_NAMESPACE
203
204 #endif // QQUICKCONTEXT2DTEXTURE_P_H