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
qsgsoftwarelayer.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
8
10
13 , m_item(nullptr)
14 , m_context(renderContext)
15 , m_renderer(nullptr)
16 , m_device_pixel_ratio(1)
17 , m_mirrorHorizontal(false)
18 , m_mirrorVertical(true)
19 , m_live(true)
20 , m_grab(true)
21 , m_recursive(false)
22 , m_dirtyTexture(true)
23{
24
25}
26
31
33{
34 return 0;
35}
36
38{
39 return m_pixmap.size();
40}
41
43{
44 return m_pixmap.hasAlphaChannel();
45}
46
48{
49 return false;
50}
51
53{
54 bool doGrab = (m_live || m_grab) && m_dirtyTexture;
55 if (doGrab)
56 grab();
57 if (m_grab)
59 m_grab = false;
60 return doGrab;
61}
62
64{
65 if (item == m_item)
66 return;
67 m_item = item;
68
69 if (m_live && !m_item)
70 m_pixmap = QPixmap();
71
73}
74
76{
77 if (rect == m_rect)
78 return;
79 m_rect = rect;
81}
82
84{
85 if (size == m_size)
86 return;
87 m_size = size;
88
89 if (m_live && m_size.isNull())
90 m_pixmap = QPixmap();
91
93}
94
96{
97 if (m_grab)
98 return;
99 m_grab = true;
100 if (m_dirtyTexture) {
102 }
103}
104
106{
107 return m_pixmap.toImage();
108}
109
111{
112 if (live == m_live)
113 return;
114 m_live = live;
115
116 if (m_live && (!m_item || m_size.isNull()))
117 m_pixmap = QPixmap();
118
120}
121
123{
124 m_recursive = recursive;
125}
126
130
134
136{
137 m_device_pixel_ratio = ratio;
138}
139
141{
142 if (m_mirrorHorizontal == mirror)
143 return;
144 m_mirrorHorizontal = mirror;
146}
147
149{
150 if (m_mirrorVertical == mirror)
151 return;
152 m_mirrorVertical = mirror;
154}
155
157{
158 m_dirtyTexture = true;
159 if (m_live || m_grab) {
161 }
162}
163
165{
166 delete m_renderer;
167 m_renderer = nullptr;
168}
169
170void QSGSoftwareLayer::grab()
171{
172 if (!m_item || m_size.isNull()) {
173 m_pixmap = QPixmap();
174 m_dirtyTexture = false;
175 return;
176 }
177 QSGNode *root = m_item;
178 while (root->firstChild() && root->type() != QSGNode::RootNodeType)
179 root = root->firstChild();
180 if (root->type() != QSGNode::RootNodeType)
181 return;
182
183 if (!m_renderer) {
184 m_renderer = new QSGSoftwarePixmapRenderer(m_context);
185 connect(m_renderer, SIGNAL(sceneGraphChanged()), this, SLOT(markDirtyTexture()));
186 }
187 m_renderer->setDevicePixelRatio(m_device_pixel_ratio);
188 m_renderer->setRootNode(static_cast<QSGRootNode *>(root));
189
190 if (m_pixmap.size() != m_size) {
191 m_pixmap = QPixmap(m_size);
192 m_pixmap.setDevicePixelRatio(m_device_pixel_ratio);
193 }
194
195 // Render texture.
196 root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update.
197 m_renderer->nodeChanged(root, QSGNode::DirtyForceUpdate); // Force render list update.
198
199 m_dirtyTexture = false;
200
201 m_renderer->setDeviceRect(m_size);
202 m_renderer->setViewportRect(m_size);
203 QRect mirrored(m_mirrorHorizontal ? m_rect.right() * m_device_pixel_ratio : m_rect.left() * m_device_pixel_ratio,
204 m_mirrorVertical ? m_rect.bottom() * m_device_pixel_ratio : m_rect.top() * m_device_pixel_ratio,
205 m_mirrorHorizontal ? -m_rect.width() * m_device_pixel_ratio : m_rect.width() * m_device_pixel_ratio,
206 m_mirrorVertical ? -m_rect.height() * m_device_pixel_ratio : m_rect.height() * m_device_pixel_ratio);
207 m_renderer->setProjectionRect(mirrored);
208 m_renderer->setClearColor(Qt::transparent);
209
210 m_renderer->renderScene();
211 m_renderer->render(&m_pixmap);
212
213 root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update.
214
215 if (m_recursive)
216 markDirtyTexture(); // Continuously update if 'live' and 'recursive'.
217}
218
220
221#include "moc_qsgsoftwarelayer_p.cpp"
\inmodule QtGui
Definition qimage.h:37
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
QImage toImage() const
Converts the pixmap to a QImage.
Definition qpixmap.cpp:408
QSize size() const
Returns the size of the pixmap.
Definition qpixmap.cpp:493
void setDevicePixelRatio(qreal scaleFactor)
Sets the device pixel ratio for the pixmap.
Definition qpixmap.cpp:604
bool hasAlphaChannel() const
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr qreal right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:499
\inmodule QtCore\reentrant
Definition qrect.h:30
void setViewportRect(const QRect &rect)
Sets rect as the geometry of the viewport to render on the surface.
void setDeviceRect(const QRect &rect)
Sets rect as the geometry of the surface being rendered to.
void setClearColor(const QColor &color)
Sets the color to clear the framebuffer.
void setRootNode(QSGRootNode *node)
Sets the node as the root of the QSGNode scene that you want to render.
void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override
void updateRequested()
void scheduledUpdateCompleted()
\group qtquick-scenegraph-nodes \title Qt Quick Scene Graph Node classes
Definition qsgnode.h:37
@ DirtyForceUpdate
Definition qsgnode.h:78
@ RootNodeType
Definition qsgnode.h:45
QSGNode * firstChild() const
Returns the first child of this node.
Definition qsgnode.h:105
void markDirty(DirtyState bits)
Notifies all connected renderers that the node has dirty bits.
Definition qsgnode.cpp:624
NodeType type() const
Returns the type of this node.
Definition qsgnode.h:110
void setDevicePixelRatio(qreal ratio)
The QSGRootNode is the toplevel root of any scene graph.
Definition qsgnode.h:259
void setItem(QSGNode *item) override
QSize textureSize() const override
Returns the size of the texture in pixels.
void setMirrorHorizontal(bool mirror) override
void setMirrorVertical(bool mirror) override
void setRecursive(bool recursive) override
qint64 comparisonKey() const override
Returns a key suitable for comparing textures.
QImage toImage() const override
void setDevicePixelRatio(qreal ratio) override
bool hasAlphaChannel() const override
Returns true if the texture data contains an alpha channel.
void scheduleUpdate() override
bool updateTexture() override
Call this function to explicitly update the dynamic texture.
void invalidated() override
bool hasMipmaps() const override
Returns true if the texture data contains mipmap levels.
QSGSoftwareLayer(QSGRenderContext *renderContext)
void setSize(const QSize &size) override
void setFormat(Format) override
void setRect(const QRectF &rect) override
void markDirtyTexture() override
void setLive(bool live) override
void setHasMipmaps(bool) override
void renderScene() final
Renders the scene.
void setProjectionRect(const QRect &projectionRect)
\inmodule QtCore
Definition qsize.h:25
constexpr bool isNull() const noexcept
Returns true if both the width and height is 0; otherwise returns false.
Definition qsize.h:121
#define this
Definition dialogs.cpp:9
rect
[4]
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:47
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLdouble GLdouble GLdouble GLdouble top
GLint GLsizei width
GLint left
GLint GLint bottom
#define emit
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QObject::connect nullptr
QGraphicsItem * item