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
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/qsgcurveglyphnode_p.h>
11#include <QtQuick/private/qsgdistancefieldglyphnode_p.h>
12#include <QtQuick/private/qsgdistancefieldglyphnode_p_p.h>
13#include <QtQuick/private/qsgrhisupport_p.h>
14#include <QtQuick/private/qsgrhilayer_p.h>
15#include <QtQuick/private/qsgdefaultrendercontext_p.h>
16#include <QtQuick/private/qsgdefaultrectanglenode_p.h>
17#include <QtQuick/private/qsgdefaultimagenode_p.h>
18#include <QtQuick/private/qsgdefaultninepatchnode_p.h>
19#if QT_CONFIG(quick_sprite)
20#include <QtQuick/private/qsgdefaultspritenode_p.h>
21#endif
22#include <QtQuick/private/qsgrhishadereffectnode_p.h>
23#include <QtQuick/private/qsginternaltextnode_p.h>
24#include <QtQuick/private/qsgrhiinternaltextnode_p.h>
25
26#include <QOpenGLContext>
27
28#include <QtQuick/private/qquickwindow_p.h>
29#include <QtQuick/private/qquickitem_p.h>
30
31#include <private/qqmlglobal_p.h>
32
33#include <algorithm>
34
35#include <rhi/qrhi.h>
36
38
41 public:
43 void setAntialiasing(bool) override { }
44 };
45
47 public:
48 void setAntialiasing(bool) override { }
49 };
50}
51
52DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
53
55 : QSGContext (parent)
56 , m_antialiasingMethod(QSGContext::UndecidedAntialiasing)
57 , m_distanceFieldDisabled(qmlDisableDistanceField())
58 , m_distanceFieldAntialiasing(QSGGlyphNode::HighQualitySubPixelAntialiasing)
59 , m_distanceFieldAntialiasingDecided(false)
60{
61 if (Q_UNLIKELY(!qEnvironmentVariableIsEmpty("QSG_DISTANCEFIELD_ANTIALIASING"))) {
62 const QByteArray mode = qgetenv("QSG_DISTANCEFIELD_ANTIALIASING");
63 m_distanceFieldAntialiasingDecided = true;
64 if (mode == "subpixel")
65 m_distanceFieldAntialiasing = QSGGlyphNode::HighQualitySubPixelAntialiasing;
66 else if (mode == "subpixel-lowq")
67 m_distanceFieldAntialiasing = QSGGlyphNode::LowQualitySubPixelAntialiasing;
68 else if (mode == "gray")
69 m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
70 }
71
72 // Adds compatibility with Qt 5.3 and earlier's QSG_RENDER_TIMING
73 if (qEnvironmentVariableIsSet("QSG_RENDER_TIMING")) {
74 const_cast<QLoggingCategory &>(QSG_LOG_TIME_GLYPH()).setEnabled(QtDebugMsg, true);
75 const_cast<QLoggingCategory &>(QSG_LOG_TIME_TEXTURE()).setEnabled(QtDebugMsg, true);
76 const_cast<QLoggingCategory &>(QSG_LOG_TIME_RENDERER()).setEnabled(QtDebugMsg, true);
77 const_cast<QLoggingCategory &>(QSG_LOG_TIME_RENDERLOOP()).setEnabled(QtDebugMsg, true);
78 const_cast<QLoggingCategory &>(QSG_LOG_TIME_COMPILATION()).setEnabled(QtDebugMsg, true);
79 }
80}
81
86
88{
89 m_mutex.lock();
90
91 auto rc = static_cast<const QSGDefaultRenderContext *>(renderContext);
92 if (m_antialiasingMethod == UndecidedAntialiasing) {
93 if (Q_UNLIKELY(qEnvironmentVariableIsSet("QSG_ANTIALIASING_METHOD"))) {
94 const QByteArray aaType = qgetenv("QSG_ANTIALIASING_METHOD");
95 if (aaType == "msaa")
96 m_antialiasingMethod = MsaaAntialiasing;
97 else if (aaType == "vertex")
98 m_antialiasingMethod = VertexAntialiasing;
99 }
100 if (m_antialiasingMethod == UndecidedAntialiasing)
101 m_antialiasingMethod = rc->msaaSampleCount() > 1 ? MsaaAntialiasing : VertexAntialiasing;
102 }
103
104#if QT_CONFIG(opengl)
105 // With OpenGL ES, use GrayAntialiasing, unless
106 // some value had been requested explicitly. This could not be decided
107 // before without a context. Now the context is ready.
108 if (!m_distanceFieldAntialiasingDecided) {
109 m_distanceFieldAntialiasingDecided = true;
110 Q_ASSERT(rc->rhi());
111 if (rc->rhi()->backend() == QRhi::OpenGLES2
112 && static_cast<const QRhiGles2NativeHandles *>(rc->rhi()->nativeHandles())->context->isOpenGLES())
113 {
114 m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
115 }
116 }
117#endif
118
119 m_mutex.unlock();
120}
121
125
130
137
139{
140 return m_antialiasingMethod == MsaaAntialiasing
141 ? new QSGMultisampleAntialiasing::ImageNode(static_cast<QSGDefaultRenderContext *>(renderContext))
142 : new QSGDefaultInternalImageNode(static_cast<QSGDefaultRenderContext *>(renderContext));
143}
144
149
154
156 QSGTextNode::RenderType renderType,
157 int renderTypeQuality)
158{
159 if (renderType == QSGTextNode::CurveRendering) {
160 return new QSGCurveGlyphNode(rc);
161 } else if (m_distanceFieldDisabled || renderType == QSGTextNode::NativeRendering) {
162 return new QSGDefaultGlyphNode(rc);
163 } else {
165 node->setPreferredAntialiasingMode(m_distanceFieldAntialiasing);
166 node->setRenderTypeQuality(renderTypeQuality);
167 return node;
168 }
169}
170
172{
173 return new QSGRhiLayer(renderContext);
174}
175
177{
179 // These depend solely on the env.vars., not QQuickGraphicsConfiguration
180 // since that does not have a flag that maps 100% to QSG_NO_xx_BUFFER.
181 static bool useDepth = qEnvironmentVariableIsEmpty("QSG_NO_DEPTH_BUFFER");
182 static bool useStencil = qEnvironmentVariableIsEmpty("QSG_NO_STENCIL_BUFFER");
183 static bool enableDebug = qEnvironmentVariableIsSet("QSG_OPENGL_DEBUG");
184 static bool disableVSync = qEnvironmentVariableIsSet("QSG_NO_VSYNC");
185 if (useDepth && format.depthBufferSize() == -1)
186 format.setDepthBufferSize(24);
187 else if (!useDepth)
188 format.setDepthBufferSize(0);
189 if (useStencil && format.stencilBufferSize() == -1)
190 format.setStencilBufferSize(8);
191 else if (!useStencil)
192 format.setStencilBufferSize(0);
193 if (enableDebug)
195 if (QQuickWindow::hasDefaultAlphaBuffer())
196 format.setAlphaBufferSize(8);
197 format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
198 if (disableVSync) // swapInterval defaults to 1, it has no -1 special value
199 format.setSwapInterval(0);
200 return format;
201}
202
204{
205 m_distanceFieldDisabled = !enabled;
206}
207
209{
210 return !m_distanceFieldDisabled;
211}
212
214{
215 Q_UNUSED(renderContext);
216 return this;
217}
218
223
228
233
234#if QT_CONFIG(quick_sprite)
235QSGSpriteNode *QSGDefaultContext::createSpriteNode()
236{
237 return new QSGDefaultSpriteNode;
238}
239#endif
240
245
250
255
257{
258 if (!window)
259 return nullptr;
260
261 // Unlike the graphicsApi and shaderType and similar queries, getting a
262 // native resource is only possible when there is an initialized
263 // rendercontext, or rather, only within rendering a frame, as per
264 // QSGRendererInterface docs. This is good since getting some things is
265 // only possible within a beginFrame - endFrame with the RHI.
266
267 const QSGDefaultRenderContext *rc = static_cast<const QSGDefaultRenderContext *>(
270
271#if QT_CONFIG(vulkan)
272 if (resource == VulkanInstanceResource)
273 return window->vulkanInstance();
274#endif
275 return const_cast<void *>(rhiSupport->rifResource(resource, rc, window));
276}
277
282
283QSGRendererInterface::ShaderCompilationTypes QSGDefaultContext::shaderCompilationType() const
284{
285 return OfflineCompilation;
286}
287
288QSGRendererInterface::ShaderSourceTypes QSGDefaultContext::shaderSourceType() const
289{
290 return ShaderSourceFile;
291}
292
294
295static void initResources()
296{
297 Q_INIT_RESOURCE(scenegraph_shaders);
298}
299
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
void unlock() noexcept
Unlocks the mutex.
Definition qmutex.h:289
void lock() noexcept
Locks the mutex.
Definition qmutex.h:286
\inmodule QtCore
Definition qobject.h:103
The QQuickPaintedItem class provides a way to use the QPainter API in the QML Scene Graph.
static QQuickWindowPrivate * get(QQuickWindow *c)
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\variable QRhiGles2InitParams::format
@ OpenGLES2
Definition qrhi.h:1809
The QSGContext holds the scene graph entry points for one QML engine.
@ UndecidedAntialiasing
QSGRectangleNode * createRectangleNode() override
QSGGlyphNode * createGlyphNode(QSGRenderContext *rc, QSGTextNode::RenderType renderType, int renderTypeQuality) override
QSGInternalRectangleNode * createInternalRectangleNode() override
ShaderCompilationTypes shaderCompilationType() const override
bool isDistanceFieldEnabled() const
QSGImageNode * createImageNode() override
QSGInternalTextNode * createInternalTextNode(QSGRenderContext *renderContext) 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)
static QSGRhiSupport * instance()
RenderType
This enum type describes type of glyph node used for rendering the text.
Definition qsgtextnode.h:29
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(qt_apple_check_os_version)
@ 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]