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
qsgopenvgpainternode.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
6#include <private/qsgcontext_p.h>
7#include <qmath.h>
8
9#include <QtGui/QPainter>
10
12
14 : m_preferredRenderTarget(QQuickPaintedItem::Image)
15 , m_item(item)
16 , m_texture(nullptr)
17 , m_dirtyContents(false)
18 , m_opaquePainting(false)
19 , m_linear_filtering(false)
20 , m_smoothPainting(false)
21 , m_fillColor(Qt::transparent)
22 , m_contentsScale(1.0)
23 , m_dirtyGeometry(false)
24{
25 // Set Dummy material and geometry to avoid asserts
26 setMaterial((QSGMaterial*)1);
27 setGeometry((QSGGeometry*)1);
28}
29
31{
32 delete m_texture;
33}
34
38
39void QSGOpenVGPainterNode::setSize(const QSize &size)
40{
41 if (size == m_size)
42 return;
43
44 m_size = size;
45
46 m_dirtyGeometry = true;
47}
48
49void QSGOpenVGPainterNode::setDirty(const QRect &dirtyRect)
50{
51 m_dirtyContents = true;
52 m_dirtyRect = dirtyRect;
53 markDirty(DirtyMaterial);
54}
55
57{
58 if (opaque == m_opaquePainting)
59 return;
60
61 m_opaquePainting = opaque;
62}
63
64void QSGOpenVGPainterNode::setLinearFiltering(bool linearFiltering)
65{
66 if (linearFiltering == m_linear_filtering)
67 return;
68
69 m_linear_filtering = linearFiltering;
70}
71
73{
74
75}
76
78{
79 if (s == m_smoothPainting)
80 return;
81
82 m_smoothPainting = s;
83}
84
85void QSGOpenVGPainterNode::setFillColor(const QColor &c)
86{
87 if (c == m_fillColor)
88 return;
89
90 m_fillColor = c;
91 markDirty(DirtyMaterial);
92}
93
95{
96 if (s == m_contentsScale)
97 return;
98
99 m_contentsScale = s;
100 markDirty(DirtyMaterial);
101}
102
106
107void QSGOpenVGPainterNode::setTextureSize(const QSize &size)
108{
109 if (size == m_textureSize)
110 return;
111
112 m_textureSize = size;
113 m_dirtyGeometry = true;
114}
115
117{
118 return m_image;
119}
120
122{
123 if (m_dirtyGeometry) {
124 if (!m_opaquePainting)
125 m_image = QImage(m_size, QImage::Format_ARGB32_Premultiplied);
126 else
127 m_image = QImage(m_size, QImage::Format_RGB32);
128 }
129
130 if (m_dirtyContents)
131 paint();
132
133 m_dirtyGeometry = false;
134 m_dirtyContents = false;
135}
136
138{
139 return m_texture;
140}
141
143{
144 if (!m_texture)
145 return;
146
147 // Set Draw Mode
148 if (opacity() < 1.0) {
149 //Transparent
150 vgSetPaint(opacityPaint(), VG_FILL_PATH);
151 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_MULTIPLY);
152 } else {
153 vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
154 }
155
156 if (m_linear_filtering)
157 vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);
158 else
159 vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_NONANTIALIASED);
160
161 // Set Transform
162 vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
163 vgLoadMatrix(transform().constData());
164
165 vgDrawImage(static_cast<VGImage>(m_texture->comparisonKey()));
166}
167
169{
170 QRect dirtyRect = m_dirtyRect.isNull() ? QRect(0, 0, m_size.width(), m_size.height()) : m_dirtyRect;
171
172 QPainter painter;
173
174 painter.begin(&m_image);
175 if (m_smoothPainting) {
176 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
177 }
178
179 QRect clipRect;
180
181 if (m_contentsScale == 1) {
182 qreal scaleX = m_textureSize.width() / (qreal) m_size.width();
183 qreal scaleY = m_textureSize.height() / (qreal) m_size.height();
184 painter.scale(scaleX, scaleY);
185 clipRect = dirtyRect;
186 } else {
187 painter.scale(m_contentsScale, m_contentsScale);
188
189 QRect sclip(qFloor(dirtyRect.x()/m_contentsScale),
190 qFloor(dirtyRect.y()/m_contentsScale),
191 qCeil(dirtyRect.width()/m_contentsScale+dirtyRect.x()/m_contentsScale-qFloor(dirtyRect.x()/m_contentsScale)),
192 qCeil(dirtyRect.height()/m_contentsScale+dirtyRect.y()/m_contentsScale-qFloor(dirtyRect.y()/m_contentsScale)));
193
194 clipRect = sclip;
195 }
196
197 if (!m_dirtyRect.isNull())
198 painter.setClipRect(clipRect);
199
200 painter.setCompositionMode(QPainter::CompositionMode_Source);
201 painter.fillRect(clipRect, m_fillColor);
202 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
203
204 m_item->paint(&painter);
205 painter.end();
206
207 m_dirtyRect = QRect();
208
209 if (m_texture)
210 delete m_texture;
211
212 uint textureFlags = m_opaquePainting ? 0 : QSGRenderContext::CreateTexture_Alpha;
213 m_texture = new QSGOpenVGTexture(m_image, textureFlags);
214}
215
216QT_END_NAMESPACE
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
void setTextureSize(const QSize &size) override
QSGTexture * texture() const override
void setFillColor(const QColor &c) override
void setOpaquePainting(bool opaque) override
void setDirty(const QRect &dirtyRect) override
void setMipmapping(bool mipmapping) override
void setSmoothPainting(bool s) override
void setPreferredRenderTarget(QQuickPaintedItem::RenderTarget target) override
void setLinearFiltering(bool linearFiltering) override
void setContentsScale(qreal s) override
QImage toImage() const override
void setSize(const QSize &size) override
void setFastFBOResizing(bool dynamic) override
const QOpenVGMatrix & transform() const