Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcontext2dtile.cpp
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 QtDeclarative 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 #include "qquickcontext2dtile_p.h"
43
44 #include <QOpenGLFramebufferObject>
45 #include <QOpenGLFramebufferObjectFormat>
46 #include <QOpenGLPaintDevice>
47
48 QQuickContext2DTile::QQuickContext2DTile()
49     : m_dirty(true)
50     , m_rect(QRect(0, 0, 1, 1))
51     , m_device(0)
52 {
53 }
54
55 QQuickContext2DTile::~QQuickContext2DTile()
56 {
57     if (m_painter.isActive())
58         m_painter.end();
59 }
60
61 QPainter* QQuickContext2DTile::createPainter(bool smooth)
62 {
63     if (m_painter.isActive())
64         m_painter.end();
65
66     if (m_device) {
67         aboutToDraw();
68         m_painter.begin(m_device);
69         m_painter.resetTransform();
70         m_painter.setCompositionMode(QPainter::CompositionMode_Source);
71
72 #ifdef QQUICKCONTEXT2D_DEBUG
73         int v = 100;
74         int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2;
75         if (gray)
76             v = 150;
77         m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255));
78 #endif
79         if (smooth)
80             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
81                                    | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
82         else
83             m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
84                                      | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, false);
85
86         m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
87         m_painter.translate(-m_rect.left(), -m_rect.top());
88         m_painter.setClipRect(m_rect);
89         m_painter.setClipping(false);
90         return &m_painter;
91     }
92
93     return 0;
94 }
95
96 QQuickContext2DFBOTile::QQuickContext2DFBOTile()
97     : QQuickContext2DTile()
98     , m_fbo(0)
99 {
100 }
101
102
103 QQuickContext2DFBOTile::~QQuickContext2DFBOTile()
104 {
105     delete m_fbo;
106 }
107
108 void QQuickContext2DFBOTile::aboutToDraw()
109 {
110     m_fbo->bind();
111     if (!m_device) {
112         QOpenGLPaintDevice *gl_device = new QOpenGLPaintDevice(rect().size());
113         m_device = gl_device;
114         QPainter p(m_device);
115         p.fillRect(QRectF(0, 0, m_fbo->width(), m_fbo->height()), QColor(qRgba(0, 0, 0, 0)));
116         p.end();
117     }
118 }
119
120 void QQuickContext2DFBOTile::drawFinished()
121 {
122     m_fbo->release();
123 }
124
125 void QQuickContext2DFBOTile::setRect(const QRect& r)
126 {
127     if (m_rect == r)
128         return;
129     m_rect = r;
130     m_dirty = true;
131     if (!m_fbo || m_fbo->size() != r.size()) {
132         QOpenGLFramebufferObjectFormat format;
133         format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
134         format.setInternalTextureFormat(GL_RGBA);
135         format.setMipmap(false);
136
137         if (m_painter.isActive())
138             m_painter.end();
139
140         delete m_fbo;
141         m_fbo = new QOpenGLFramebufferObject(r.size(), format);
142     }
143 }
144
145
146 QQuickContext2DImageTile::QQuickContext2DImageTile()
147     : QQuickContext2DTile()
148 {
149 }
150
151 QQuickContext2DImageTile::~QQuickContext2DImageTile()
152 {
153 }
154
155 void QQuickContext2DImageTile::setRect(const QRect& r)
156 {
157     if (m_rect == r)
158         return;
159     m_rect = r;
160     m_dirty = true;
161     if (m_image.size() != r.size()) {
162         m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied);
163     }
164     m_device = &m_image;
165 }