Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsgdefaultpainternode.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
5
6#include <QtQuick/private/qquickpainteditem_p.h>
7
8#include <QtQuick/private/qsgdefaultrendercontext_p.h>
9#include <QtQuick/private/qsgcontext_p.h>
10#include <qmath.h>
11#include <qpainter.h>
12
14
15#define QT_MINIMUM_DYNAMIC_FBO_SIZE 64U
16
22
24{
25 if (!m_dirty_rect.isNull()) {
27 m_dirty_rect = QRect();
28 }
29 QSGPlainTexture::commitTextureOperations(rhi, resourceUpdates);
30}
31
34 , m_preferredRenderTarget(QQuickPaintedItem::Image)
35 , m_actualRenderTarget(QQuickPaintedItem::Image)
36 , m_item(item)
37 , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
38 , m_texture(nullptr)
39 , m_fillColor(Qt::transparent)
40 , m_contentsScale(1.0)
41 , m_dirtyContents(false)
42 , m_opaquePainting(false)
43 , m_linear_filtering(false)
44 , m_mipmapping(false)
45 , m_smoothPainting(false)
46 , m_multisamplingSupported(false)
47 , m_fastFBOResizing(false)
48 , m_dirtyGeometry(false)
49 , m_dirtyRenderTarget(false)
50 , m_dirtyTexture(false)
51{
52 Q_UNUSED(m_multisamplingSupported);
53 m_context = static_cast<QSGDefaultRenderContext *>(static_cast<QQuickPaintedItemPrivate *>(QObjectPrivate::get(item))->sceneGraphRenderContext());
54
55 setMaterial(&m_materialO);
56 setOpaqueMaterial(&m_material);
57 setGeometry(&m_geometry);
58
59#ifdef QSG_RUNTIME_DESCRIPTION
60 qsgnode_set_description(this, QString::fromLatin1("QQuickPaintedItem(%1):%2").arg(QString::fromLatin1(item->metaObject()->className())).arg(item->objectName()));
61#endif
62}
63
65{
66 delete m_texture;
67}
68
70{
71 QRect dirtyRect = m_dirtyRect.isNull() ? QRect(0, 0, m_size.width(), m_size.height()) : m_dirtyRect;
72
74 Q_ASSERT(m_actualRenderTarget == QQuickPaintedItem::Image);
75 if (m_image.isNull())
76 return;
77 painter.begin(&m_image);
78
79 if (m_smoothPainting) {
81 }
82
83 QRect clipRect;
84 QRect dirtyTextureRect;
85
86 if (m_contentsScale == 1) {
87 qreal scaleX = m_textureSize.width() / (qreal) m_size.width();
88 qreal scaleY = m_textureSize.height() / (qreal) m_size.height();
89 painter.scale(scaleX, scaleY);
90 clipRect = dirtyRect;
91 dirtyTextureRect = QRectF(dirtyRect.x() * scaleX,
92 dirtyRect.y() * scaleY,
93 dirtyRect.width() * scaleX,
94 dirtyRect.height() * scaleY).toAlignedRect();
95 } else {
96 painter.scale(m_contentsScale, m_contentsScale);
97 QRect sclip(qFloor(dirtyRect.x()/m_contentsScale),
98 qFloor(dirtyRect.y()/m_contentsScale),
99 qCeil(dirtyRect.width()/m_contentsScale+dirtyRect.x()/m_contentsScale-qFloor(dirtyRect.x()/m_contentsScale)),
100 qCeil(dirtyRect.height()/m_contentsScale+dirtyRect.y()/m_contentsScale-qFloor(dirtyRect.y()/m_contentsScale)));
101 clipRect = sclip;
102 dirtyTextureRect = dirtyRect;
103 }
104
105 // only clip if we were originally updating only a subrect
106 if (!m_dirtyRect.isNull()) {
107 painter.setClipRect(clipRect);
108 }
109
111 if (m_fillColor.isValid())
112 painter.fillRect(clipRect, m_fillColor);
114
115 m_item->paint(&painter);
116 painter.end();
117
118 m_texture->setImage(m_image);
119 m_texture->setDirtyRect(dirtyTextureRect);
120
121 m_dirtyRect = QRect();
122}
123
125{
126 if (m_dirtyRenderTarget)
127 updateRenderTarget();
128 if (m_dirtyGeometry)
129 updateGeometry();
130 if (m_dirtyTexture)
131 updateTexture();
132
133 if (m_dirtyContents)
134 paint();
135
136 m_dirtyGeometry = false;
137 m_dirtyRenderTarget = false;
138 m_dirtyTexture = false;
139 m_dirtyContents = false;
140}
141
142void QSGDefaultPainterNode::updateTexture()
143{
144 m_texture->setHasAlphaChannel(!m_opaquePainting);
145 m_material.setTexture(m_texture);
146 m_materialO.setTexture(m_texture);
147
149}
150
151void QSGDefaultPainterNode::updateGeometry()
152{
153 QRectF source(0, 0, 1, 1);
154 QRectF dest(0, 0, m_size.width(), m_size.height());
155 if (m_actualRenderTarget == QQuickPaintedItem::InvertedYFramebufferObject)
156 dest = QRectF(QPointF(0, m_size.height()), QPointF(m_size.width(), 0));
158 dest,
159 source);
161}
162
163void QSGDefaultPainterNode::updateRenderTarget()
164{
165 m_dirtyContents = true;
166
167 m_actualRenderTarget = QQuickPaintedItem::Image;
168 if (!m_image.isNull() && !m_dirtyGeometry)
169 return;
170
171 m_image = QImage(m_textureSize, QImage::Format_RGBA8888_Premultiplied);
172 m_image.fill(Qt::transparent);
173
174 if (!m_texture) {
175 m_texture = new QSGPainterTexture;
176 m_texture->setOwnsTexture(true);
177 }
178 m_texture->setTextureSize(m_textureSize);
179}
180
182{
183 if (m_preferredRenderTarget == target)
184 return;
185
186 m_preferredRenderTarget = target;
187
188 m_dirtyRenderTarget = true;
189 m_dirtyGeometry = true;
190 m_dirtyTexture = true;
191}
192
194{
195 if (size == m_size)
196 return;
197
198 m_size = size;
199 m_dirtyGeometry = true;
200}
201
203{
204 if (size == m_textureSize)
205 return;
206
207 m_textureSize = size;
208 m_dirtyRenderTarget = true;
209 m_dirtyGeometry = true;
210 m_dirtyTexture = true;
211}
212
214{
215 m_dirtyContents = true;
216 m_dirtyRect = dirtyRect;
217
218 if (m_mipmapping)
219 m_dirtyTexture = true;
220
222}
223
225{
226 if (opaque == m_opaquePainting)
227 return;
228
229 m_opaquePainting = opaque;
230 m_dirtyTexture = true;
231}
232
234{
235 if (linearFiltering == m_linear_filtering)
236 return;
237
238 m_linear_filtering = linearFiltering;
239
243}
244
246{
247 if (mipmapping == m_mipmapping)
248 return;
249
250 m_mipmapping = mipmapping;
253 m_dirtyTexture = true;
254}
255
257{
258 if (s == m_smoothPainting)
259 return;
260
261 m_smoothPainting = s;
262 m_dirtyRenderTarget = true;
263}
264
266{
267 if (c == m_fillColor)
268 return;
269
270 m_fillColor = c;
272}
273
275{
276 if (s == m_contentsScale)
277 return;
278
279 m_contentsScale = s;
281}
282
284{
285 if (m_fastFBOResizing == fastResizing)
286 return;
287
288 m_fastFBOResizing = fastResizing;
289}
290
292{
293 Q_ASSERT(m_actualRenderTarget == QQuickPaintedItem::Image);
294 return m_image;
295}
296
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
bool isValid() const noexcept
Returns true if the color is valid; otherwise returns false.
Definition qcolor.h:285
\inmodule QtGui
Definition qimage.h:37
bool isNull() const
Returns true if it is a null image, otherwise returns false.
Definition qimage.cpp:1222
@ Format_RGBA8888_Premultiplied
Definition qimage.h:60
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition qimage.cpp:1758
static QObjectPrivate * get(QObject *o)
Definition qobject_p.h:150
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void setClipRect(const QRectF &, Qt::ClipOperation op=Qt::ReplaceClip)
Enables clipping, and sets the clip region to the given rectangle using the given clip operation.
bool begin(QPaintDevice *)
Begins painting the paint device and returns true if successful; otherwise returns false.
void scale(qreal sx, qreal sy)
Scales the coordinate system by ({sx}, {sy}).
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
@ SmoothPixmapTransform
Definition qpainter.h:54
@ Antialiasing
Definition qpainter.h:52
@ TextAntialiasing
Definition qpainter.h:53
bool end()
Ends painting.
@ CompositionMode_SourceOver
Definition qpainter.h:98
@ CompositionMode_Source
Definition qpainter.h:101
void setRenderHints(RenderHints hints, bool on=true)
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
\inmodule QtCore\reentrant
Definition qpoint.h:217
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
virtual void paint(QPainter *painter)=0
This function, which is usually called by the QML Scene Graph, paints the contents of an item in loca...
RenderTarget
This enum describes QQuickPaintedItem's render targets.
\inmodule QtCore\reentrant
Definition qrect.h:484
QRect toAlignedRect() const noexcept
Definition qrect.cpp:2338
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:239
constexpr bool isNull() const noexcept
Returns true if the rectangle is a null rectangle, otherwise returns false.
Definition qrect.h:164
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:185
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:236
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:188
\inmodule QtGui
Definition qrhi.h:1731
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:1804
void setGeometry(QSGGeometry *geometry)
Sets the geometry of this node to geometry.
Definition qsgnode.cpp:764
void setSize(const QSize &size) override
QImage toImage() const override
void setSmoothPainting(bool s) override
void setFillColor(const QColor &c) override
void setPreferredRenderTarget(QQuickPaintedItem::RenderTarget target) override
void setContentsScale(qreal s) override
void setTextureSize(const QSize &textureSize) override
void setLinearFiltering(bool linearFiltering) override
void setOpaquePainting(bool opaque) override
void setDirty(const QRect &dirtyRect=QRect()) override
void setFastFBOResizing(bool fastResizing) override
void setMipmapping(bool mipmapping) override
QSGDefaultPainterNode(QQuickPaintedItem *item)
void setMaterial(QSGMaterial *material)
Sets the material of this geometry node to material.
Definition qsgnode.cpp:927
void setOpaqueMaterial(QSGMaterial *material)
Sets the opaque material of this geometry to material.
Definition qsgnode.cpp:958
The QSGGeometry class provides low-level storage for graphics primitives in the \l{Qt Quick Scene Gra...
Definition qsggeometry.h:15
static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect)
Updates the geometry g with the coordinates in rect and texture coordinates from textureRect.
@ DirtyMaterial
Definition qsgnode.h:75
@ DirtyGeometry
Definition qsgnode.h:74
void markDirty(DirtyState bits)
Notifies all connected renderers that the node has dirty bits.
Definition qsgnode.cpp:624
void setFiltering(QSGTexture::Filtering filteringType)
Sets the filtering to filtering.
void setMipmapFiltering(QSGTexture::Filtering filteringType)
Sets the mipmap mode to filtering.
void setTexture(QSGTexture *texture)
Sets the texture of this material to texture.
void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override
Call this function to enqueue image upload operations to resourceUpdates, in case there are any pendi...
void setDirtyRect(const QRect &rect)
void setTextureSize(const QSize &size)
void setImage(const QImage &image)
void commitTextureOperations(QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates) override
Call this function to enqueue image upload operations to resourceUpdates, in case there are any pendi...
void setOwnsTexture(bool owns)
void setHasAlphaChannel(bool alpha)
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
Definition qcompare.h:63
@ transparent
Definition qnamespace.h:47
int qFloor(T v)
Definition qmath.h:42
int qCeil(T v)
Definition qmath.h:36
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum target
GLsizei GLsizei GLchar * source
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
void qsgnode_set_description(QSGNode *node, const QString &description)
Definition qsgnode.cpp:641
SSL_CTX int void * arg
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:187
QObject::connect nullptr
QGraphicsItem * item
QPainter painter(this)
[7]