Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgsoftwarepublicnodes.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
9#include <private/qsgplaintexture_p.h>
10
11QT_BEGIN_NAMESPACE
12
13QSGSoftwareRectangleNode::QSGSoftwareRectangleNode()
14 : m_color(QColor(255, 255, 255))
15{
16 setMaterial((QSGMaterial*)1);
17 setGeometry((QSGGeometry*)1);
18}
19
20void QSGSoftwareRectangleNode::paint(QPainter *painter)
21{
22 painter->fillRect(m_rect, m_color);
23}
24
25QSGSoftwareImageNode::QSGSoftwareImageNode()
26 : m_texture(nullptr),
27 m_owns(false),
28 m_filtering(QSGTexture::None),
29 m_transformMode(NoTransform),
30 m_cachedMirroredPixmapIsDirty(false)
31{
32 setMaterial((QSGMaterial*)1);
33 setGeometry((QSGGeometry*)1);
34}
35
36QSGSoftwareImageNode::~QSGSoftwareImageNode()
37{
38 if (m_owns)
39 delete m_texture;
40}
41
42void QSGSoftwareImageNode::setTexture(QSGTexture *texture)
43{
44 if (m_owns)
45 delete m_texture;
46
47 m_texture = texture; markDirty(DirtyMaterial);
48 m_cachedMirroredPixmapIsDirty = true;
49}
50
51void QSGSoftwareImageNode::setTextureCoordinatesTransform(QSGImageNode::TextureCoordinatesTransformMode transformNode)
52{
53 if (m_transformMode == transformNode)
54 return;
55
56 m_transformMode = transformNode;
57 m_cachedMirroredPixmapIsDirty = true;
58
59 markDirty(DirtyGeometry);
60}
61
62void QSGSoftwareImageNode::paint(QPainter *painter)
63{
64 if (m_cachedMirroredPixmapIsDirty)
65 updateCachedMirroredPixmap();
66
67 painter->setRenderHint(QPainter::SmoothPixmapTransform, (m_filtering == QSGTexture::Linear));
68 // Disable antialiased clipping. It causes transformed tiles to have gaps.
69 painter->setRenderHint(QPainter::Antialiasing, false);
70
71 if (!m_cachedPixmap.isNull()) {
72 painter->drawPixmap(m_rect, m_cachedPixmap, m_sourceRect);
73 } else if (QSGSoftwarePixmapTexture *pt = qobject_cast<QSGSoftwarePixmapTexture *>(m_texture)) {
74 const QPixmap &pm = pt->pixmap();
75 painter->drawPixmap(m_rect, pm, m_sourceRect);
76 } else if (QSGSoftwareLayer *pt = qobject_cast<QSGSoftwareLayer *>(m_texture)) {
77 const QPixmap &pm = pt->pixmap();
78 painter->drawPixmap(m_rect, pm, m_sourceRect);
79 } else if (QSGPlainTexture *pt = qobject_cast<QSGPlainTexture *>(m_texture)) {
80 const QImage &im = pt->image();
81 painter->drawImage(m_rect, im, m_sourceRect);
82 }
83}
84
85void QSGSoftwareImageNode::updateCachedMirroredPixmap()
86{
87 if (m_transformMode == NoTransform) {
88 m_cachedPixmap = QPixmap();
89 } else {
90 if (QSGSoftwarePixmapTexture *pt = qobject_cast<QSGSoftwarePixmapTexture *>(m_texture)) {
91 QTransform mirrorTransform;
92 if (m_transformMode.testFlag(MirrorVertically))
93 mirrorTransform = mirrorTransform.scale(1, -1);
94 if (m_transformMode.testFlag(MirrorHorizontally))
95 mirrorTransform = mirrorTransform.scale(-1, 1);
96 m_cachedPixmap = pt->pixmap().transformed(mirrorTransform);
97 } else if (QSGSoftwareLayer *pt = qobject_cast<QSGSoftwareLayer *>(m_texture)) {
98 QTransform mirrorTransform;
99 if (m_transformMode.testFlag(MirrorVertically))
100 mirrorTransform = mirrorTransform.scale(1, -1);
101 if (m_transformMode.testFlag(MirrorHorizontally))
102 mirrorTransform = mirrorTransform.scale(-1, 1);
103 m_cachedPixmap = pt->pixmap().transformed(mirrorTransform);
104 } else if (QSGPlainTexture *pt = qobject_cast<QSGPlainTexture *>(m_texture)) {
105 static constexpr Qt::Orientation none = Qt::Orientation(0);
106 const auto orientation = (m_transformMode.testFlag(MirrorHorizontally) ? Qt::Horizontal : none)
107 | (m_transformMode.testFlag(MirrorVertically) ? Qt::Vertical : none);
108 m_cachedPixmap = QPixmap::fromImage(pt->image().flipped(orientation));
109 } else {
110 m_cachedPixmap = QPixmap();
111 }
112 }
113
114 m_cachedMirroredPixmapIsDirty = false;
115}
116
117QSGSoftwareNinePatchNode::QSGSoftwareNinePatchNode()
118{
119 setMaterial((QSGMaterial*)1);
120 setGeometry((QSGGeometry*)1);
121}
122
123void QSGSoftwareNinePatchNode::setTexture(QSGTexture *texture)
124{
125 QSGSoftwarePixmapTexture *pt = qobject_cast<QSGSoftwarePixmapTexture*>(texture);
126 if (!pt) {
127 qWarning() << "Image used with invalid texture format.";
128 } else {
129 m_pixmap = pt->pixmap();
130 markDirty(DirtyMaterial);
131 }
132 delete texture;
133}
134
135void QSGSoftwareNinePatchNode::setBounds(const QRectF &bounds)
136{
137 if (m_bounds == bounds)
138 return;
139
140 m_bounds = bounds;
141 markDirty(DirtyGeometry);
142}
143
144void QSGSoftwareNinePatchNode::setDevicePixelRatio(qreal ratio)
145{
146 if (m_pixelRatio == ratio)
147 return;
148
149 m_pixelRatio = ratio;
150 markDirty(DirtyGeometry);
151}
152
153void QSGSoftwareNinePatchNode::setPadding(qreal left, qreal top, qreal right, qreal bottom)
154{
155 QMargins margins(qRound(left), qRound(top), qRound(right), qRound(bottom));
156 if (m_margins == margins)
157 return;
158
159 m_margins = QMargins(qRound(left), qRound(top), qRound(right), qRound(bottom));
160 markDirty(DirtyGeometry);
161}
162
163void QSGSoftwareNinePatchNode::update()
164{
165}
166
167void QSGSoftwareNinePatchNode::paint(QPainter *painter)
168{
169 // Disable antialiased clipping. It causes transformed tiles to have gaps.
170 painter->setRenderHint(QPainter::Antialiasing, false);
171
172 if (m_margins.isNull())
173 painter->drawPixmap(m_bounds, m_pixmap, QRectF(0, 0, m_pixmap.width(), m_pixmap.height()));
174 else
175 QSGSoftwareHelpers::qDrawBorderPixmap(painter, m_bounds.toRect(), m_margins, m_pixmap, QRect(0, 0, m_pixmap.width(), m_pixmap.height()),
176 m_margins, Qt::StretchTile, QSGSoftwareHelpers::QDrawBorderPixmap::DrawingHints{});
177}
178
179QRectF QSGSoftwareNinePatchNode::bounds() const
180{
181 return m_bounds;
182}
183
184QT_END_NAMESPACE