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 "qsgsoftwarepainternode_p.h" |
41 | #include "qsgsoftwarepixmaptexture_p.h" |
42 | #include <qmath.h> |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | QSGSoftwarePainterNode::QSGSoftwarePainterNode(QQuickPaintedItem *item) |
47 | : QSGPainterNode() |
48 | , m_preferredRenderTarget(QQuickPaintedItem::Image) |
49 | , m_item(item) |
50 | , m_texture(nullptr) |
51 | , m_dirtyContents(false) |
52 | , m_opaquePainting(false) |
53 | , m_linear_filtering(false) |
54 | , m_mipmapping(false) |
55 | , m_smoothPainting(false) |
56 | , m_fastFBOResizing(false) |
57 | , m_fillColor(Qt::transparent) |
58 | , m_contentsScale(1.0) |
59 | , m_dirtyGeometry(false) |
60 | { |
61 | setMaterial((QSGMaterial*)1); |
62 | setGeometry((QSGGeometry*)1); |
63 | } |
64 | |
65 | QSGSoftwarePainterNode::~QSGSoftwarePainterNode() |
66 | { |
67 | delete m_texture; |
68 | } |
69 | |
70 | void QSGSoftwarePainterNode::setPreferredRenderTarget(QQuickPaintedItem::RenderTarget target) |
71 | { |
72 | if (m_preferredRenderTarget == target) |
73 | return; |
74 | |
75 | m_preferredRenderTarget = target; |
76 | } |
77 | |
78 | void QSGSoftwarePainterNode::setSize(const QSize &size) |
79 | { |
80 | if (size == m_size) |
81 | return; |
82 | |
83 | m_size = size; |
84 | |
85 | m_dirtyGeometry = true; |
86 | } |
87 | |
88 | void QSGSoftwarePainterNode::setDirty(const QRect &dirtyRect) |
89 | { |
90 | m_dirtyContents = true; |
91 | m_dirtyRect = dirtyRect; |
92 | markDirty(bits: DirtyMaterial); |
93 | } |
94 | |
95 | void QSGSoftwarePainterNode::setOpaquePainting(bool opaque) |
96 | { |
97 | if (opaque == m_opaquePainting) |
98 | return; |
99 | |
100 | m_opaquePainting = opaque; |
101 | } |
102 | |
103 | void QSGSoftwarePainterNode::setLinearFiltering(bool linearFiltering) |
104 | { |
105 | if (linearFiltering == m_linear_filtering) |
106 | return; |
107 | |
108 | m_linear_filtering = linearFiltering; |
109 | } |
110 | |
111 | void QSGSoftwarePainterNode::setMipmapping(bool mipmapping) |
112 | { |
113 | if (mipmapping == m_mipmapping) |
114 | return; |
115 | |
116 | m_mipmapping = mipmapping; |
117 | } |
118 | |
119 | void QSGSoftwarePainterNode::setSmoothPainting(bool s) |
120 | { |
121 | if (s == m_smoothPainting) |
122 | return; |
123 | |
124 | m_smoothPainting = s; |
125 | } |
126 | |
127 | void QSGSoftwarePainterNode::setFillColor(const QColor &c) |
128 | { |
129 | if (c == m_fillColor) |
130 | return; |
131 | |
132 | m_fillColor = c; |
133 | markDirty(bits: DirtyMaterial); |
134 | } |
135 | |
136 | void QSGSoftwarePainterNode::setContentsScale(qreal s) |
137 | { |
138 | if (s == m_contentsScale) |
139 | return; |
140 | |
141 | m_contentsScale = s; |
142 | markDirty(bits: DirtyMaterial); |
143 | } |
144 | |
145 | void QSGSoftwarePainterNode::setFastFBOResizing(bool dynamic) |
146 | { |
147 | m_fastFBOResizing = dynamic; |
148 | } |
149 | |
150 | QImage QSGSoftwarePainterNode::toImage() const |
151 | { |
152 | return m_pixmap.toImage(); |
153 | } |
154 | |
155 | void QSGSoftwarePainterNode::update() |
156 | { |
157 | if (m_dirtyGeometry) { |
158 | m_pixmap = QPixmap(m_textureSize); |
159 | if (!m_opaquePainting) |
160 | m_pixmap.fill(fillColor: Qt::transparent); |
161 | |
162 | if (m_texture) |
163 | delete m_texture; |
164 | m_texture = new QSGSoftwarePixmapTexture(m_pixmap); |
165 | } |
166 | |
167 | if (m_dirtyContents) |
168 | paint(); |
169 | |
170 | m_dirtyGeometry = false; |
171 | m_dirtyContents = false; |
172 | } |
173 | |
174 | void QSGSoftwarePainterNode::paint(QPainter *painter) |
175 | { |
176 | painter->drawPixmap(x: 0, y: 0, w: m_size.width(), h: m_size.height(), pm: m_pixmap); |
177 | } |
178 | |
179 | void QSGSoftwarePainterNode::paint() |
180 | { |
181 | QRect dirtyRect = m_dirtyRect.isNull() ? QRect(0, 0, m_size.width(), m_size.height()) : m_dirtyRect; |
182 | |
183 | QPainter painter; |
184 | |
185 | painter.begin(&m_pixmap); |
186 | if (m_smoothPainting) { |
187 | painter.setRenderHints(hints: QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform); |
188 | } |
189 | |
190 | QRect clipRect; |
191 | |
192 | if (m_contentsScale == 1) { |
193 | qreal scaleX = m_textureSize.width() / (qreal) m_size.width(); |
194 | qreal scaleY = m_textureSize.height() / (qreal) m_size.height(); |
195 | painter.scale(sx: scaleX, sy: scaleY); |
196 | clipRect = dirtyRect; |
197 | } else { |
198 | painter.scale(sx: m_contentsScale, sy: m_contentsScale); |
199 | |
200 | QRect sclip(qFloor(v: dirtyRect.x()/m_contentsScale), |
201 | qFloor(v: dirtyRect.y()/m_contentsScale), |
202 | qCeil(v: dirtyRect.width()/m_contentsScale+dirtyRect.x()/m_contentsScale-qFloor(v: dirtyRect.x()/m_contentsScale)), |
203 | qCeil(v: dirtyRect.height()/m_contentsScale+dirtyRect.y()/m_contentsScale-qFloor(v: dirtyRect.y()/m_contentsScale))); |
204 | |
205 | clipRect = sclip; |
206 | } |
207 | |
208 | if (!m_dirtyRect.isNull()) |
209 | painter.setClipRect(clipRect); |
210 | |
211 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
212 | painter.fillRect(clipRect, color: m_fillColor); |
213 | painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
214 | |
215 | m_item->paint(painter: &painter); |
216 | painter.end(); |
217 | |
218 | m_dirtyRect = QRect(); |
219 | } |
220 | |
221 | |
222 | void QSGSoftwarePainterNode::setTextureSize(const QSize &size) |
223 | { |
224 | if (size == m_textureSize) |
225 | return; |
226 | |
227 | m_textureSize = size; |
228 | m_dirtyGeometry = true; |
229 | } |
230 | |
231 | QT_END_NAMESPACE |
232 | |