0a81decd02cb01cbcf10598a4595b9d7612c4b12
[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 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 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     virtual bool setCanvasSize(const QSize &size);
85     virtual bool setTileSize(const QSize &size);
86     virtual 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     QRect createTiles(const QRect& window);
108
109     QList<QQuickContext2DTile*> m_tiles;
110     QQuickContext2D* m_context;
111
112     QQuickContext2D::State m_state;
113
114     QQuickCanvasItem* m_item;
115     QSize m_canvasSize;
116     QSize m_tileSize;
117     QRect m_canvasWindow;
118
119     uint m_dirtyCanvas : 1;
120     uint m_dirtyTexture : 1;
121     uint m_threadRendering : 1;
122     uint m_smooth : 1;
123     uint m_tiledCanvas : 1;
124     uint m_doGrabImage : 1;
125     uint m_painting : 1;
126 };
127
128 class QQuickContext2DFBOTexture : public QQuickContext2DTexture
129 {
130     Q_OBJECT
131
132 public:
133     QQuickContext2DFBOTexture();
134     ~QQuickContext2DFBOTexture();
135     virtual int textureId() const;
136     virtual bool updateTexture();
137     virtual QQuickContext2DTile* createTile() const;
138     virtual QImage toImage(const QRectF& region = QRectF());
139     virtual QPaintDevice* beginPainting();
140     virtual void endPainting();
141     QRectF normalizedTextureSubRect() const;
142     virtual bool supportThreadRendering() const {return false;}
143     virtual bool supportDirectRendering() const {return false;}
144     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
145     virtual void compositeTile(QQuickContext2DTile* tile);
146     virtual void bind();
147     virtual bool setCanvasSize(const QSize &size);
148     virtual bool setTileSize(const QSize &size);
149     virtual bool setCanvasWindow(const QRect& canvasWindow);
150 private Q_SLOTS:
151     void grabImage();
152
153 private:
154     bool doMultisampling() const;
155     QImage m_grabedImage;
156     QOpenGLFramebufferObject *m_fbo;
157     QOpenGLFramebufferObject *m_multisampledFbo;
158     QMutex m_mutex;
159     QWaitCondition m_condition;
160     QSize m_fboSize;
161     QPaintDevice *m_paint_device;
162 };
163
164 class QSGPlainTexture;
165 class QQuickContext2DImageTexture : public QQuickContext2DTexture
166 {
167     Q_OBJECT
168
169 public:
170     QQuickContext2DImageTexture(bool threadRendering = true);
171     ~QQuickContext2DImageTexture();
172     virtual int textureId() const;
173     virtual void bind();
174     virtual bool supportThreadRendering() const {return true;}
175     virtual bool supportDirectRendering() const;
176     virtual QQuickCanvasItem::RenderTarget renderTarget() const;
177     virtual void lock();
178     virtual void unlock();
179     virtual void wait();
180     virtual void wake();
181
182     virtual bool updateTexture();
183     virtual QQuickContext2DTile* createTile() const;
184     virtual QImage toImage(const QRectF& region = QRectF());
185     virtual QPaintDevice* beginPainting();
186     virtual void compositeTile(QQuickContext2DTile* tile);
187
188 private Q_SLOTS:
189     void grabImage(const QRect& r);
190 private:
191     QImage m_image;
192     QImage m_grabedImage;
193     QMutex m_mutex;
194     QWaitCondition m_waitCondition;
195     QPainter m_painter;
196     QSGPlainTexture*  m_texture;
197 };
198
199 QT_END_HEADER
200
201 QT_END_NAMESPACE
202
203 #endif // QQUICKCONTEXT2DTEXTURE_P_H