| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtQuick 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 The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qquickcontext2dtile_p.h" | 
| 41 | #if QT_CONFIG(opengl) | 
| 42 | # include <QOpenGLFramebufferObject> | 
| 43 | # include <QOpenGLFramebufferObjectFormat> | 
| 44 | # include <QOpenGLPaintDevice> | 
| 45 | #endif | 
| 46 |  | 
| 47 | QT_BEGIN_NAMESPACE | 
| 48 |  | 
| 49 | QQuickContext2DTile::QQuickContext2DTile() | 
| 50 |     : m_dirty(true) | 
| 51 |     , m_rect(QRect(0, 0, 1, 1)) | 
| 52 |     , m_device(nullptr) | 
| 53 | { | 
| 54 | } | 
| 55 |  | 
| 56 | QQuickContext2DTile::~QQuickContext2DTile() | 
| 57 | { | 
| 58 |     if (m_painter.isActive()) | 
| 59 |         m_painter.end(); | 
| 60 | } | 
| 61 |  | 
| 62 | QPainter* QQuickContext2DTile::createPainter(bool smooth, bool antialiasing) | 
| 63 | { | 
| 64 |     if (m_painter.isActive()) | 
| 65 |         m_painter.end(); | 
| 66 |  | 
| 67 |     aboutToDraw(); | 
| 68 |     if (m_device) { | 
| 69 |         m_painter.begin(m_device); | 
| 70 |         m_painter.resetTransform(); | 
| 71 |         m_painter.setCompositionMode(QPainter::CompositionMode_Source); | 
| 72 |  | 
| 73 | #ifdef QQUICKCONTEXT2D_DEBUG | 
| 74 |         int v = 100; | 
| 75 |         int gray = (m_rect.x() / m_rect.width() + m_rect.y() / m_rect.height()) % 2; | 
| 76 |         if (gray) | 
| 77 |             v = 150; | 
| 78 |         m_painter.fillRect(QRect(0, 0, m_rect.width(), m_rect.height()), QColor(v, v, v, 255)); | 
| 79 | #endif | 
| 80 |  | 
| 81 |         if (antialiasing) | 
| 82 |             m_painter.setRenderHints(hints: QPainter::Antialiasing | QPainter::TextAntialiasing, on: true); | 
| 83 |         else | 
| 84 |             m_painter.setRenderHints(hints: QPainter::Antialiasing | QPainter::TextAntialiasing, on: false); | 
| 85 |  | 
| 86 |         if (smooth) | 
| 87 |             m_painter.setRenderHint(hint: QPainter::SmoothPixmapTransform, on: true); | 
| 88 |         else | 
| 89 |             m_painter.setRenderHint(hint: QPainter::SmoothPixmapTransform, on: false); | 
| 90 |  | 
| 91 |         m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver); | 
| 92 |         m_painter.translate(dx: -m_rect.left(), dy: -m_rect.top()); | 
| 93 |         m_painter.setClipRect(m_rect); | 
| 94 |         m_painter.setClipping(false); | 
| 95 |         return &m_painter; | 
| 96 |     } | 
| 97 |  | 
| 98 |     return nullptr; | 
| 99 | } | 
| 100 | #if QT_CONFIG(opengl) | 
| 101 | QQuickContext2DFBOTile::QQuickContext2DFBOTile() | 
| 102 |     : QQuickContext2DTile() | 
| 103 |     , m_fbo(nullptr) | 
| 104 | { | 
| 105 | } | 
| 106 |  | 
| 107 |  | 
| 108 | QQuickContext2DFBOTile::~QQuickContext2DFBOTile() | 
| 109 | { | 
| 110 |     if (m_fbo) | 
| 111 |         m_fbo->release(); | 
| 112 |     delete m_fbo; | 
| 113 | } | 
| 114 |  | 
| 115 | void QQuickContext2DFBOTile::aboutToDraw() | 
| 116 | { | 
| 117 |     m_fbo->bind(); | 
| 118 |     if (!m_device) { | 
| 119 |         QOpenGLPaintDevice *gl_device = new QOpenGLPaintDevice(rect().size()); | 
| 120 |         m_device = gl_device; | 
| 121 |         QPainter p(m_device); | 
| 122 |         p.fillRect(QRectF(0, 0, m_fbo->width(), m_fbo->height()), color: QColor(qRgba(r: 0, g: 0, b: 0, a: 0))); | 
| 123 |         p.end(); | 
| 124 |     } | 
| 125 | } | 
| 126 |  | 
| 127 | void QQuickContext2DFBOTile::drawFinished() | 
| 128 | { | 
| 129 | } | 
| 130 |  | 
| 131 | void QQuickContext2DFBOTile::setRect(const QRect& r) | 
| 132 | { | 
| 133 |     if (m_rect == r) | 
| 134 |         return; | 
| 135 |     m_rect = r; | 
| 136 |     m_dirty = true; | 
| 137 |     if (!m_fbo || m_fbo->size() != r.size()) { | 
| 138 |         QOpenGLFramebufferObjectFormat format; | 
| 139 |         format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); | 
| 140 |         format.setInternalTextureFormat(GL_RGBA); | 
| 141 |         format.setMipmap(false); | 
| 142 |  | 
| 143 |         if (m_painter.isActive()) | 
| 144 |             m_painter.end(); | 
| 145 |  | 
| 146 |         delete m_fbo; | 
| 147 |         m_fbo = new QOpenGLFramebufferObject(r.size(), format); | 
| 148 |     } | 
| 149 | } | 
| 150 | #endif | 
| 151 |  | 
| 152 | QQuickContext2DImageTile::QQuickContext2DImageTile() | 
| 153 |     : QQuickContext2DTile() | 
| 154 | { | 
| 155 | } | 
| 156 |  | 
| 157 | QQuickContext2DImageTile::~QQuickContext2DImageTile() | 
| 158 | { | 
| 159 | } | 
| 160 |  | 
| 161 | void QQuickContext2DImageTile::setRect(const QRect& r) | 
| 162 | { | 
| 163 |     if (m_rect == r) | 
| 164 |         return; | 
| 165 |     m_rect = r; | 
| 166 |     m_dirty = true; | 
| 167 |     if (m_image.size() != r.size()) { | 
| 168 |         m_image = QImage(r.size(), QImage::Format_ARGB32_Premultiplied); | 
| 169 |     } | 
| 170 |     m_device = &m_image; | 
| 171 | } | 
| 172 |  | 
| 173 | QT_END_NAMESPACE | 
| 174 |  | 
| 175 |  |