Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsgdefaultcontext.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/qsgdefaultinternalrectanglenode_p.h>
7#include <QtQuick/private/qsgdefaultinternalimagenode_p.h>
8#include <QtQuick/private/qsgdefaultpainternode_p.h>
9#include <QtQuick/private/qsgdefaultglyphnode_p.h>
10#include <QtQuick/private/qsgdistancefieldglyphnode_p.h>
11#include <QtQuick/private/qsgdistancefieldglyphnode_p_p.h>
12#include <QtQuick/private/qsgrhisupport_p.h>
13#include <QtQuick/private/qsgrhilayer_p.h>
14#include <QtQuick/private/qsgdefaultrendercontext_p.h>
15#include <QtQuick/private/qsgdefaultrectanglenode_p.h>
16#include <QtQuick/private/qsgdefaultimagenode_p.h>
17#include <QtQuick/private/qsgdefaultninepatchnode_p.h>
18#if QT_CONFIG(quick_sprite)
19#include <QtQuick/private/qsgdefaultspritenode_p.h>
20#endif
21#include <QtQuick/private/qsgrhishadereffectnode_p.h>
22
23#include <QOpenGLContext>
24
25#include <QtQuick/private/qquickwindow_p.h>
26#include <QtQuick/private/qquickitem_p.h>
27
28#include <private/qqmlglobal_p.h>
29
30#include <algorithm>
31
32#include <rhi/qrhi.h>
33
35
38 public:
40 void setAntialiasing(bool) override { }
41 };
42
44 public:
45 void setAntialiasing(bool) override { }
46 };
47}
48
49DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
50
53 , m_antialiasingMethod(QSGContext::UndecidedAntialiasing)
54 , m_distanceFieldDisabled(qmlDisableDistanceField())
55 , m_distanceFieldAntialiasing(QSGGlyphNode::HighQualitySubPixelAntialiasing)
56 , m_distanceFieldAntialiasingDecided(false)
57{
58 if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty("QSG_DISTANCEFIELD_ANTIALIASING"))) {
59 const QByteArray mode = qgetenv("QSG_DISTANCEFIELD_ANTIALIASING");
60 m_distanceFieldAntialiasingDecided = true;
61 if (mode == "subpixel")
62 m_distanceFieldAntialiasing = QSGGlyphNode::HighQualitySubPixelAntialiasing;
63 else if (mode == "subpixel-lowq")
64 m_distanceFieldAntialiasing = QSGGlyphNode::LowQualitySubPixelAntialiasing;
65 else if (mode == "gray")
66 m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
67 }
68
69 // Adds compatibility with Qt 5.3 and earlier's QSG_RENDER_TIMING
70 if (qEnvironmentVariableIsSet("QSG_RENDER_TIMING")) {
71 const_cast<QLoggingCategory &>(QSG_LOG_TIME_GLYPH()).setEnabled(QtDebugMsg, true);
72 const_cast<QLoggingCategory &>(QSG_LOG_TIME_TEXTURE()).setEnabled(QtDebugMsg, true);
73 const_cast<QLoggingCategory &>(QSG_LOG_TIME_RENDERER()).setEnabled(QtDebugMsg, true);
74 const_cast<QLoggingCategory &>(QSG_LOG_TIME_RENDERLOOP()).setEnabled(QtDebugMsg, true);
75 const_cast<QLoggingCategory &>(QSG_LOG_TIME_COMPILATION()).setEnabled(QtDebugMsg, true);
76 }
77}
78
80{
81
82}
83
85{
86 m_mutex.lock();
87
88 auto rc = static_cast<const QSGDefaultRenderContext *>(renderContext);
89 if (m_antialiasingMethod == UndecidedAntialiasing) {
90 if (Q_UNLIKELY(qEnvironmentVariableIsSet("QSG_ANTIALIASING_METHOD"))) {
91 const QByteArray aaType = qgetenv("QSG_ANTIALIASING_METHOD");
92 if (aaType == "msaa")
93 m_antialiasingMethod = MsaaAntialiasing;
94 else if (aaType == "vertex")
95 m_antialiasingMethod = VertexAntialiasing;
96 }
97 if (m_antialiasingMethod == UndecidedAntialiasing)
98 m_antialiasingMethod = rc->msaaSampleCount() > 1 ? MsaaAntialiasing : VertexAntialiasing;
99 }
100
101#if QT_CONFIG(opengl)
102 // With OpenGL ES, use GrayAntialiasing, unless
103 // some value had been requested explicitly. This could not be decided
104 // before without a context. Now the context is ready.
105 if (!m_distanceFieldAntialiasingDecided) {
106 m_distanceFieldAntialiasingDecided = true;
107 Q_ASSERT(rc->rhi());
108 if (rc->rhi()->backend() == QRhi::OpenGLES2
109 && static_cast<const QRhiGles2NativeHandles *>(rc->rhi()->nativeHandles())->context->isOpenGLES())
110 {
111 m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
112 }
113 }
114#endif
115
116 m_mutex.unlock();
117}
118
120{
121}
122
124{
125 return new QSGDefaultRenderContext(this);
126}
127
129{
130 return m_antialiasingMethod == MsaaAntialiasing
133}
134
136{
137 return m_antialiasingMethod == MsaaAntialiasing
138 ? new QSGMultisampleAntialiasing::ImageNode(static_cast<QSGDefaultRenderContext *>(renderContext))
139 : new QSGDefaultInternalImageNode(static_cast<QSGDefaultRenderContext *>(renderContext));
140}
141
143{
144 return new QSGDefaultPainterNode(item);
145}
146
148 bool preferNativeGlyphNode,
149 int renderTypeQuality)
150{
151 if (m_distanceFieldDisabled || preferNativeGlyphNode) {
152 return new QSGDefaultGlyphNode(rc);
153 } else {
155 node->setPreferredAntialiasingMode(m_distanceFieldAntialiasing);
156 node->setRenderTypeQuality(renderTypeQuality);
157 return node;
158 }
159}
160
162{
163 return new QSGRhiLayer(renderContext);
164}
165
167{
169 // These depend solely on the env.vars., not QQuickGraphicsConfiguration
170 // since that does not have a flag that maps 100% to QSG_NO_xx_BUFFER.
171 static bool useDepth = qEnvironmentVariableIsEmpty("QSG_NO_DEPTH_BUFFER");
172 static bool useStencil = qEnvironmentVariableIsEmpty("QSG_NO_STENCIL_BUFFER");
173 static bool enableDebug = qEnvironmentVariableIsSet("QSG_OPENGL_DEBUG");
174 static bool disableVSync = qEnvironmentVariableIsSet("QSG_NO_VSYNC");
175 if (useDepth && format.depthBufferSize() == -1)
176 format.setDepthBufferSize(24);
177 else if (!useDepth)
178 format.setDepthBufferSize(0);
179 if (useStencil && format.stencilBufferSize() == -1)
180 format.setStencilBufferSize(8);
181 else if (!useStencil)
182 format.setStencilBufferSize(0);
183 if (enableDebug)
185 if (QQuickWindow::hasDefaultAlphaBuffer())
186 format.setAlphaBufferSize(8);
187 format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
188 if (disableVSync) // swapInterval defaults to 1, it has no -1 special value
189 format.setSwapInterval(0);
190 return format;
191}
192
194{
195 m_distanceFieldDisabled = !enabled;
196}
197
199{
200 return !m_distanceFieldDisabled;
201}
202
204{
205 Q_UNUSED(renderContext);
206 return this;
207}
208
210{
211 return new QSGDefaultRectangleNode;
212}
213
215{
216 return new QSGDefaultImageNode;
217}
218
220{
221 return new QSGDefaultNinePatchNode;
222}
223
224#if QT_CONFIG(quick_sprite)
225QSGSpriteNode *QSGDefaultContext::createSpriteNode()
226{
227 return new QSGDefaultSpriteNode;
228}
229#endif
230
232{
234}
235
237{
238 return new QSGRhiShaderEffectNode(static_cast<QSGDefaultRenderContext *>(renderContext));
239}
240
242{
244}
245
247{
248 if (!window)
249 return nullptr;
250
251 // Unlike the graphicsApi and shaderType and similar queries, getting a
252 // native resource is only possible when there is an initialized
253 // rendercontext, or rather, only within rendering a frame, as per
254 // QSGRendererInterface docs. This is good since getting some things is
255 // only possible within a beginFrame - endFrame with the RHI.
256
257 const QSGDefaultRenderContext *rc = static_cast<const QSGDefaultRenderContext *>(
260
261#if QT_CONFIG(vulkan)
262 if (resource == VulkanInstanceResource)
263 return window->vulkanInstance();
264#endif
265 return const_cast<void *>(rhiSupport->rifResource(resource, rc, window));
266}
267
269{
270 return RhiShader;
271}
272
273QSGRendererInterface::ShaderCompilationTypes QSGDefaultContext::shaderCompilationType() const
274{
275 return OfflineCompilation;
276}
277
278QSGRendererInterface::ShaderSourceTypes QSGDefaultContext::shaderSourceType() const
279{
280 return ShaderSourceFile;
281}
282
284
285static void initResources()
286{
287 Q_INIT_RESOURCE(scenegraph_shaders);
288}
289
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
void unlock() noexcept
Unlocks the mutex.
Definition qmutex.h:293
void lock() noexcept
Locks the mutex.
Definition qmutex.h:290
\inmodule QtCore
Definition qobject.h:90
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
static QQuickWindowPrivate * get(QQuickWindow *c)
QSGRenderContext * context
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\variable QRhiGles2InitParams::format
@ OpenGLES2
Definition qrhi.h:1772
The QSGContext holds the scene graph entry points for one QML engine.
@ UndecidedAntialiasing
QSGGlyphNode * createGlyphNode(QSGRenderContext *rc, bool preferNativeGlyphNode, int renderTypeQuality) override
QSGRectangleNode * createRectangleNode() override
QSGInternalRectangleNode * createInternalRectangleNode() override
ShaderCompilationTypes shaderCompilationType() const override
bool isDistanceFieldEnabled() const
QSGImageNode * createImageNode() override
QSGRendererInterface * rendererInterface(QSGRenderContext *renderContext) override
Returns a pointer to the (presumably) global renderer interface.
QSGRenderContext * createRenderContext() override
ShaderSourceTypes shaderSourceType() const override
QSGShaderEffectNode * createShaderEffectNode(QSGRenderContext *renderContext) override
Creates a new shader effect node.
void renderContextInitialized(QSGRenderContext *renderContext) override
QSGGuiThreadShaderEffectManager * createGuiThreadShaderEffectManager() override
Creates a new shader effect helper instance.
QSGNinePatchNode * createNinePatchNode() override
ShaderType shaderType() const override
QSGPainterNode * createPainterNode(QQuickPaintedItem *item) override
QSurfaceFormat defaultSurfaceFormat() const override
void setDistanceFieldEnabled(bool enabled)
QSGInternalImageNode * createInternalImageNode(QSGRenderContext *renderContext) override
QSGLayer * createLayer(QSGRenderContext *renderContext) override
void renderContextInvalidated(QSGRenderContext *) override
GraphicsApi graphicsApi() const override
Returns the graphics API that is in use by the Qt Quick scenegraph.
void * getResource(QQuickWindow *window, Resource resource) const override
Queries a graphics resource in window.
void setRenderTypeQuality(int renderTypeQuality) override
void setPreferredAntialiasingMode(AntialiasingMode mode) override
The QSGImageNode class is provided for convenience to easily draw textured content using the QML scen...
ImageNode(QSGDefaultRenderContext *rc)
\inmodule QtQuick
The QSGRectangleNode class is a convenience class for drawing solid filled rectangles using scenegrap...
An interface providing access to some of the graphics API specific internals of the scenegraph.
Resource
\value DeviceResource The resource is a pointer to the graphics device, when applicable.
GraphicsApi
\value Unknown An unknown graphics API is in use \value Software The Qt Quick 2D Renderer is in use \...
ShaderType
\value UnknownShadingLanguage Not yet known due to no window and scenegraph associated \value GLSL GL...
const void * rifResource(QSGRendererInterface::Resource res, const QSGDefaultRenderContext *rc, const QQuickWindow *w)
QSGRendererInterface::GraphicsApi graphicsApi() const
static QSGRhiSupport * instance()
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
static QSurfaceFormat defaultFormat()
Returns the global default surface format.
Combined button and popup list for selecting options.
#define Q_UNLIKELY(x)
Q_CONSTRUCTOR_FUNCTION(initializeStandardUserDefaults)
@ QtDebugMsg
Definition qlogging.h:30
GLenum mode
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLint GLsizei GLsizei GLenum format
#define DEFINE_BOOL_CONFIG_OPTION(name, var)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QT_END_NAMESPACE void initResources()
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) noexcept
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_UNUSED(x)
#define Q_INIT_RESOURCE(name)
Definition qtresource.h:14
#define enabled
QGraphicsItem * item
aWidget window() -> setWindowTitle("New Window Title")
[2]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent