695c30f211971dac12516be7e727afc6cf8351ee
[profile/ivi/qtdeclarative.git] / src / declarative / items / context2d / qsgcontext2dtile.cpp
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 #include "qsgcontext2dtile_p.h"
43
44 #include <QtOpenGL/QGLFramebufferObject>
45 #include <QtOpenGL/QGLFramebufferObjectFormat>
46
47 QSGContext2DTile::QSGContext2DTile()
48     : m_dirty(true)
49     , m_rect(QRect(0, 0, 1, 1))
50     , m_device(0)
51 {
52 }
53
54 QSGContext2DTile::~QSGContext2DTile()
55 {
56     if (m_painter.isActive())
57         m_painter.end();
58 }
59
60 QPainter* QSGContext2DTile::createPainter(bool smooth)
61 {
62     if (m_painter.isActive())
63         m_painter.end();
64
65     if (m_device) {
66         m_painter.begin(m_device);
67         m_painter.resetTransform();
68         m_painter.setCompositionMode(QPainter::CompositionMode_Source);
69
70 #ifdef QSGCONTEXT2D_DEBUG
71         int v = 100;
72         int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2;
73         if (gray)
74             v = 150;
75         m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255));
76 #endif
77         if (smooth)
78             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
79                                    | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
80         else
81             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
82                                      | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, false);
83
84         m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
85         m_painter.translate(-m_rect.left(), -m_rect.top());
86         m_painter.setClipRect(m_rect);
87         m_painter.setClipping(false);
88         return &m_painter;
89     }
90
91     return 0;
92 }
93
94 QSGContext2DFBOTile::QSGContext2DFBOTile()
95     : QSGContext2DTile()
96     , m_fbo(0)
97 {
98 }
99
100 QSGContext2DFBOTile::~QSGContext2DFBOTile()
101 {
102     delete m_fbo;
103 }
104
105 void QSGContext2DFBOTile::setRect(const QRect& r)
106 {
107     if (m_rect == r)
108         return;
109     m_rect = r;
110     m_dirty = true;
111     if (!m_fbo || m_fbo->size() != r.size()) {
112         QGLFramebufferObjectFormat format;
113         format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
114         format.setInternalTextureFormat(GL_RGBA);
115         format.setMipmap(false);
116
117         if (m_painter.isActive())
118             m_painter.end();
119
120         delete m_fbo;
121         m_fbo = new QGLFramebufferObject(r.size(), format);
122     }
123     m_device = m_fbo;
124 }
125
126
127 QSGContext2DImageTile::QSGContext2DImageTile()
128     : QSGContext2DTile()
129 {
130 }
131
132 QSGContext2DImageTile::~QSGContext2DImageTile()
133 {
134 }
135
136 void QSGContext2DImageTile::setRect(const QRect& r)
137 {
138     if (m_rect == r)
139         return;
140     m_rect = r;
141     m_dirty = true;
142     if (m_image.size() != r.size()) {
143         m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied);
144     }
145     m_device = &m_image;
146 }