fix declarative to work after broken refactor
[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 <QOpenGLFramebufferObject>
45 #include <QOpenGLFramebufferObjectFormat>
46 #include <QOpenGLPaintDevice>
47
48 QSGContext2DTile::QSGContext2DTile()
49     : m_dirty(true)
50     , m_rect(QRect(0, 0, 1, 1))
51     , m_device(0)
52 {
53 }
54
55 QSGContext2DTile::~QSGContext2DTile()
56 {
57     if (m_painter.isActive())
58         m_painter.end();
59 }
60
61 QPainter* QSGContext2DTile::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 QSGCONTEXT2D_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 QSGContext2DFBOTile::QSGContext2DFBOTile()
97     : QSGContext2DTile()
98     , m_fbo(0)
99 {
100 }
101
102
103 QSGContext2DFBOTile::~QSGContext2DFBOTile()
104 {
105     delete m_fbo;
106 }
107
108 void QSGContext2DFBOTile::aboutToDraw()
109 {
110     m_fbo->bind();
111     if (!m_device) {
112         m_device = new QOpenGLPaintDevice(rect().size());
113     }
114 }
115
116
117 void QSGContext2DFBOTile::setRect(const QRect& r)
118 {
119     if (m_rect == r)
120         return;
121     m_rect = r;
122     m_dirty = true;
123     if (!m_fbo || m_fbo->size() != r.size()) {
124         QOpenGLFramebufferObjectFormat format;
125         format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
126         format.setInternalTextureFormat(GL_RGBA);
127         format.setMipmap(false);
128
129         if (m_painter.isActive())
130             m_painter.end();
131
132         delete m_fbo;
133         m_fbo = new QOpenGLFramebufferObject(r.size(), format);
134     }
135 }
136
137
138 QSGContext2DImageTile::QSGContext2DImageTile()
139     : QSGContext2DTile()
140 {
141 }
142
143 QSGContext2DImageTile::~QSGContext2DImageTile()
144 {
145 }
146
147 void QSGContext2DImageTile::setRect(const QRect& r)
148 {
149     if (m_rect == r)
150         return;
151     m_rect = r;
152     m_dirty = true;
153     if (m_image.size() != r.size()) {
154         m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied);
155     }
156     m_device = &m_image;
157 }