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
qsgopenvglayer.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
4#include "qsgopenvglayer.h"
7
9
12 , m_item(nullptr)
13 , m_renderer(nullptr)
14 , m_device_pixel_ratio(1)
15 , m_mirrorHorizontal(false)
16 , m_mirrorVertical(false)
17 , m_live(true)
18 , m_grab(true)
19 , m_recursive(false)
20 , m_dirtyTexture(true)
21 , m_offscreenSurface(nullptr)
22 , m_secondaryOffscreenSurface(nullptr)
23{
24 m_context = static_cast<QSGOpenVGRenderContext*>(renderContext);
25}
26
31
33{
34 if (m_offscreenSurface)
35 return static_cast<int>(m_offscreenSurface->image());
36 else
37 return 0;
38}
39
41{
42 if (m_offscreenSurface) {
43 return m_offscreenSurface->size();
44 }
45
46 return QSize();
47}
48
50{
51 return true;
52}
53
55{
56 return false;
57}
58
60{
61 bool doGrab = (m_live || m_grab) && m_dirtyTexture;
62 if (doGrab)
63 grab();
64 if (m_grab)
66 m_grab = false;
67 return doGrab;
68}
69
71{
72 if (item == m_item)
73 return;
74 m_item = item;
75
76 if (m_live && !m_item) {
77 delete m_offscreenSurface;
78 delete m_secondaryOffscreenSurface;
79 m_offscreenSurface = nullptr;
80 m_secondaryOffscreenSurface = nullptr;
81 }
82
84}
85
87{
88 if (rect == m_rect)
89 return;
90 m_rect = rect;
92}
93
95{
96 if (size == m_size)
97 return;
98 m_size = size;
99
100 if (m_live && m_size.isNull()) {
101 delete m_offscreenSurface;
102 delete m_secondaryOffscreenSurface;
103 m_offscreenSurface = nullptr;
104 m_secondaryOffscreenSurface = nullptr;
105 }
106
108}
109
111{
112 if (m_grab)
113 return;
114 m_grab = true;
115 if (m_dirtyTexture) {
117 }
118}
119
121{
122 return m_offscreenSurface->readbackQImage();
123}
124
126{
127 if (live == m_live)
128 return;
129 m_live = live;
130
131 if (m_live && (!m_item || m_size.isNull())) {
132 delete m_offscreenSurface;
133 delete m_secondaryOffscreenSurface;
134 m_offscreenSurface = nullptr;
135 m_secondaryOffscreenSurface = nullptr;
136 }
137
139}
140
142{
143 m_recursive = recursive;
144}
145
150
152{
153 Q_UNUSED(mipmap);
154}
155
157{
158 m_device_pixel_ratio = ratio;
159}
160
162{
163 if (m_mirrorHorizontal == mirror)
164 return;
165 m_mirrorHorizontal = mirror;
167}
168
170{
171 if (m_mirrorVertical == mirror)
172 return;
173 m_mirrorVertical = mirror;
175}
176
178{
179 m_dirtyTexture = true;
180 if (m_live || m_grab) {
182 }
183}
184
186{
187 delete m_offscreenSurface;
188 delete m_secondaryOffscreenSurface;
189 delete m_renderer;
190 m_renderer = nullptr;
191 m_offscreenSurface = nullptr;
192 m_secondaryOffscreenSurface = nullptr;
193}
194
195void QSGOpenVGLayer::grab()
196{
197 if (!m_item || m_size.isNull()) {
198 delete m_offscreenSurface;
199 delete m_secondaryOffscreenSurface;
200 m_offscreenSurface = nullptr;
201 m_secondaryOffscreenSurface = nullptr;
202 m_dirtyTexture = false;
203 return;
204 }
205 QSGNode *root = m_item;
206 while (root->firstChild() && root->type() != QSGNode::RootNodeType)
207 root = root->firstChild();
208 if (root->type() != QSGNode::RootNodeType)
209 return;
210
211 if (!m_renderer) {
212 m_renderer = new QSGOpenVGRenderer(m_context);
213 connect(m_renderer, SIGNAL(sceneGraphChanged()), this, SLOT(markDirtyTexture()));
214 }
215 m_renderer->setDevicePixelRatio(m_device_pixel_ratio);
216 m_renderer->setRootNode(static_cast<QSGRootNode *>(root));
217
218 bool deleteOffscreenSurfaceLater = false;
219 if (m_offscreenSurface == nullptr || m_offscreenSurface->size() != m_size ) {
220 if (m_recursive) {
221 deleteOffscreenSurfaceLater = true;
222 delete m_secondaryOffscreenSurface;
223 m_secondaryOffscreenSurface = new QOpenVGOffscreenSurface(m_size);
224 } else {
225 delete m_offscreenSurface;
226 delete m_secondaryOffscreenSurface;
227 m_offscreenSurface = new QOpenVGOffscreenSurface(m_size);
228 m_secondaryOffscreenSurface = nullptr;
229 }
230 }
231
232 if (m_recursive && !m_secondaryOffscreenSurface)
233 m_secondaryOffscreenSurface = new QOpenVGOffscreenSurface(m_size);
234
235 // Render texture.
236 root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update.
237 m_renderer->nodeChanged(root, QSGNode::DirtyForceUpdate); // Force render list update.
238
239 m_dirtyTexture = false;
240
241 m_renderer->setDeviceRect(m_size);
242 m_renderer->setViewportRect(m_size);
243 QRect mirrored(m_mirrorHorizontal ? m_rect.right() * m_device_pixel_ratio : m_rect.left() * m_device_pixel_ratio,
244 m_mirrorVertical ? m_rect.top() * m_device_pixel_ratio : m_rect.bottom() * m_device_pixel_ratio,
245 m_mirrorHorizontal ? -m_rect.width() * m_device_pixel_ratio : m_rect.width() * m_device_pixel_ratio,
246 m_mirrorVertical ? m_rect.height() * m_device_pixel_ratio : -m_rect.height() * m_device_pixel_ratio);
247 m_renderer->setProjectionMatrixToRect(mirrored);
248 m_renderer->setClearColor(Qt::transparent);
249
250
251 if (m_recursive)
252 m_secondaryOffscreenSurface->makeCurrent();
253 else
254 m_offscreenSurface->makeCurrent();
255
256 m_renderer->renderScene();
257
258 // Make the previous surface and context active again
259 if (m_recursive) {
260 if (deleteOffscreenSurfaceLater) {
261 delete m_offscreenSurface;
262 m_offscreenSurface = new QOpenVGOffscreenSurface(m_size);
263 }
264 m_secondaryOffscreenSurface->doneCurrent();
265 qSwap(m_offscreenSurface, m_secondaryOffscreenSurface);
266 } else {
267 m_offscreenSurface->doneCurrent();
268 }
269
270 root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update.
271
272 if (m_recursive)
273 markDirtyTexture(); // Continuously update if 'live' and 'recursive'.
274}
275
\inmodule QtGui
Definition qimage.h:37
\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 setProjectionMatrixToRect(const QRectF &rect)
Convenience method that calls setProjectionMatrix() with an orthographic matrix generated from rect.
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 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) override
void setLive(bool live) override
bool hasMipmaps() const override
Returns true if the texture data contains mipmap levels.
void markDirtyTexture() override
QImage toImage() const override
void setMirrorVertical(bool mirror) override
bool updateTexture() override
Call this function to explicitly update the dynamic texture.
void invalidated() override
QSGOpenVGLayer(QSGRenderContext *renderContext)
void setRecursive(bool recursive) override
void setFormat(uint format) override
void setRect(const QRectF &rect) override
QSize textureSize() const override
Returns the size of the texture in pixels.
void scheduleUpdate() override
void setMirrorHorizontal(bool mirror) override
void setItem(QSGNode *item) override
bool hasAlphaChannel() const override
Returns true if the texture data contains an alpha channel.
void setHasMipmaps(bool mipmap) override
qint64 comparisonKey() const override
Returns a key suitable for comparing textures.
void setSize(const QSize &size) override
void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override
void renderScene() override
Renders the scene.
void setDevicePixelRatio(qreal ratio)
The QSGRootNode is the toplevel root of any scene graph.
Definition qsgnode.h:259
\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
GLint GLsizei GLsizei GLenum format
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
#define emit
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:34
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