Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcontext2dtile.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickcontext2dtile_p.h"
43
44 #include <QOpenGLFramebufferObject>
45 #include <QOpenGLFramebufferObjectFormat>
46 #include <QOpenGLPaintDevice>
47
48 QT_BEGIN_NAMESPACE
49
50 QQuickContext2DTile::QQuickContext2DTile()
51     : m_dirty(true)
52     , m_rect(QRect(0, 0, 1, 1))
53     , m_device(0)
54 {
55 }
56
57 QQuickContext2DTile::~QQuickContext2DTile()
58 {
59     if (m_painter.isActive())
60         m_painter.end();
61 }
62
63 QPainter* QQuickContext2DTile::createPainter(bool smooth, bool antialiasing)
64 {
65     if (m_painter.isActive())
66         m_painter.end();
67
68     aboutToDraw();
69     if (m_device) {
70         m_painter.begin(m_device);
71         m_painter.resetTransform();
72         m_painter.setCompositionMode(QPainter::CompositionMode_Source);
73
74 #ifdef QQUICKCONTEXT2D_DEBUG
75         int v = 100;
76         int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2;
77         if (gray)
78             v = 150;
79         m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255));
80 #endif
81
82         if (antialiasing)
83             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing, true);
84         else
85             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::TextAntialiasing, false);
86
87         if (smooth)
88             m_painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
89         else
90             m_painter.setRenderHint(QPainter::SmoothPixmapTransform, false);
91
92         m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
93         m_painter.translate(-m_rect.left(), -m_rect.top());
94         m_painter.setClipRect(m_rect);
95         m_painter.setClipping(false);
96         return &m_painter;
97     }
98
99     return 0;
100 }
101
102 QQuickContext2DFBOTile::QQuickContext2DFBOTile()
103     : QQuickContext2DTile()
104     , m_fbo(0)
105 {
106 }
107
108
109 QQuickContext2DFBOTile::~QQuickContext2DFBOTile()
110 {
111     if (m_fbo)
112         m_fbo->release();
113     delete m_fbo;
114 }
115
116 void QQuickContext2DFBOTile::aboutToDraw()
117 {
118     m_fbo->bind();
119     if (!m_device) {
120         QOpenGLPaintDevice *gl_device = new QOpenGLPaintDevice(rect().size());
121         m_device = gl_device;
122         QPainter p(m_device);
123         p.fillRect(QRectF(0, 0, m_fbo->width(), m_fbo->height()), QColor(qRgba(0, 0, 0, 0)));
124         p.end();
125     }
126 }
127
128 void QQuickContext2DFBOTile::drawFinished()
129 {
130 }
131
132 void QQuickContext2DFBOTile::setRect(const QRect& r)
133 {
134     if (m_rect == r)
135         return;
136     m_rect = r;
137     m_dirty = true;
138     if (!m_fbo || m_fbo->size() != r.size()) {
139         QOpenGLFramebufferObjectFormat format;
140         format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
141         format.setInternalTextureFormat(GL_RGBA);
142         format.setMipmap(false);
143
144         if (m_painter.isActive())
145             m_painter.end();
146
147         delete m_fbo;
148         m_fbo = new QOpenGLFramebufferObject(r.size(), format);
149     }
150 }
151
152
153 QQuickContext2DImageTile::QQuickContext2DImageTile()
154     : QQuickContext2DTile()
155 {
156 }
157
158 QQuickContext2DImageTile::~QQuickContext2DImageTile()
159 {
160 }
161
162 void QQuickContext2DImageTile::setRect(const QRect& r)
163 {
164     if (m_rect == r)
165         return;
166     m_rect = r;
167     m_dirty = true;
168     if (m_image.size() != r.size()) {
169         m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied);
170     }
171     m_device = &m_image;
172 }
173
174 QT_END_NAMESPACE
175