1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qquickcontext2dtile_p.h"
44 #include <QOpenGLFramebufferObject>
45 #include <QOpenGLFramebufferObjectFormat>
46 #include <QOpenGLPaintDevice>
48 QQuickContext2DTile::QQuickContext2DTile()
50 , m_rect(QRect(0, 0, 1, 1))
55 QQuickContext2DTile::~QQuickContext2DTile()
57 if (m_painter.isActive())
61 QPainter* QQuickContext2DTile::createPainter(bool smooth)
63 if (m_painter.isActive())
68 m_painter.begin(m_device);
69 m_painter.resetTransform();
70 m_painter.setCompositionMode(QPainter::CompositionMode_Source);
72 #ifdef QQUICKCONTEXT2D_DEBUG
74 int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2;
77 m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255));
80 m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
81 | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
83 m_painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing
84 | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, false);
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);
96 QQuickContext2DFBOTile::QQuickContext2DFBOTile()
97 : QQuickContext2DTile()
103 QQuickContext2DFBOTile::~QQuickContext2DFBOTile()
108 void QQuickContext2DFBOTile::aboutToDraw()
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)));
120 void QQuickContext2DFBOTile::drawFinished()
125 void QQuickContext2DFBOTile::setRect(const QRect& r)
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);
137 if (m_painter.isActive())
141 m_fbo = new QOpenGLFramebufferObject(r.size(), format);
146 QQuickContext2DImageTile::QQuickContext2DImageTile()
147 : QQuickContext2DTile()
151 QQuickContext2DImageTile::~QQuickContext2DImageTile()
155 void QQuickContext2DImageTile::setRect(const QRect& r)
161 if (m_image.size() != r.size()) {
162 m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied);