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
qsgdefaultrendercontext.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
6
7#include <QtGui/QGuiApplication>
8
9#include <QtQuick/private/qsgbatchrenderer_p.h>
10#include <QtQuick/private/qsgrenderer_p.h>
11#include <QtQuick/private/qsgrhiatlastexture_p.h>
12#include <QtQuick/private/qsgrhidistancefieldglyphcache_p.h>
13#include <QtQuick/private/qsgmaterialshader_p.h>
14
15#include <QtQuick/private/qsgcompressedtexture_p.h>
16
17#include <QtQuick/qsgrendererinterface.h>
18#include <QtQuick/qquickgraphicsconfiguration.h>
19
21
24 , m_rhi(nullptr)
25 , m_maxTextureSize(0)
26 , m_rhiAtlasManager(nullptr)
27 , m_currentFrameCommandBuffer(nullptr)
28 , m_currentFrameRenderPass(nullptr)
29 , m_useDepthBufferFor2D(true)
30 , m_glyphCacheResourceUpdates(nullptr)
31{
32}
33
39{
40 if (!m_sg)
41 return;
42
43 const InitParams *initParams = static_cast<const InitParams *>(params);
44 if (initParams->sType != INIT_PARAMS_MAGIC)
45 qFatal("QSGDefaultRenderContext: Invalid parameters passed to initialize()");
46
47 m_initParams = *initParams;
48
53
55
57
59}
60
62{
63 {
64 auto it = m_glyphCaches.begin();
65 while (it != m_glyphCaches.end()) {
66 if (!(*it)->isActive()) {
67 delete *it;
68 it = m_glyphCaches.erase(it);
69 } else {
70 ++it;
71 }
72 }
73 }
74
77
78 {
80 while (it != m_fontEnginesToClean.end()) {
81 if (it.value() == 0) {
82 it.key()->clearGlyphCache(this);
83 if (!it.key()->ref.deref())
84 delete it.key();
86 } else {
87 ++it;
88 }
89 }
90 }
91}
92
94{
95 if (!m_rhi)
96 return;
97
100
103
104 /* The cleanup of the atlas textures is a bit intriguing.
105 As part of the cleanup in the threaded render loop, we
106 do:
107 1. call this function
108 2. call QCoreApp::sendPostedEvents() to immediately process
109 any pending deferred deletes.
110 3. delete the GL context.
111
112 As textures need the atlas manager while cleaning up, the
113 manager needs to be cleaned up after the textures, so
114 we post a deleteLater here at the very bottom so it gets
115 deferred deleted last.
116
117 Another alternative would be to use a QPointer in
118 QSGOpenGLAtlasTexture::Texture, but this seemed simpler.
119 */
120 if (m_rhiAtlasManager) {
123 m_rhiAtlasManager = nullptr;
124 }
125
126 // The following piece of code will read/write to the font engine's caches,
127 // potentially from different threads. However, this is safe because this
128 // code is only called from QQuickWindow's shutdown which is called
129 // only when the GUI is blocked, and multiple threads will call it in
130 // sequence. (see qsgdefaultglyphnode_p.cpp's init())
132 it.key()->clearGlyphCache(this);
133 if (!it.key()->ref.deref())
134 delete it.key();
135 }
137
140
142 m_glyphCaches.clear();
143
145
146 m_rhi = nullptr;
147
148 if (m_sg)
150
152}
153
157{
158 m_currentDevicePixelRatio = devicePixelRatio;
159 m_useDepthBufferFor2D = config.isDepthBufferEnabledFor2D();
160
161 // we store the command buffer already here, in case there is something in
162 // an updatePaintNode() implementation that leads to needing it (for
163 // example, an updateTexture() call on a QSGRhiLayer)
165}
166
168 RenderPassCallback mainPassRecordingStart,
169 RenderPassCallback mainPassRecordingEnd,
170 void *callbackUserData)
171{
172 renderer->setRenderTarget(renderTarget);
173 renderer->setRenderPassRecordingCallbacks(mainPassRecordingStart, mainPassRecordingEnd, callbackUserData);
174
175 m_currentFrameCommandBuffer = renderTarget.cb; // usually the same as what was passed to prepareSync() but cannot count on that having been called
176 m_currentFrameRenderPass = renderTarget.rpDesc;
177}
178
183
190
192{
193 bool atlas = flags & CreateTexture_Atlas;
194 bool mipmap = flags & CreateTexture_Mipmap;
196
197 // The atlas implementation is only supported from the render thread and
198 // does not support mipmaps.
199 if (m_rhi) {
200 if (!mipmap && atlas && QThread::currentThread() == m_rhi->thread()) {
202 if (t)
203 return t;
204 }
205 }
206
209 if (texture->hasAlphaChannel() && !alpha)
210 texture->setHasAlphaChannel(false);
211
212 return texture;
213}
214
219
221{
222 // This is only used for atlasing compressed textures. Returning null implies no atlas.
223
226
227 return nullptr;
228}
229
234
236{
237 for (auto it = m_glyphCaches.begin(); it != m_glyphCaches.end(); ++it) {
238 it.value()->processPendingGlyphs();
239 it.value()->update();
240 }
241}
242
244{
245 FontKey key = FontKey(font, 0);
247 if (atlas == nullptr) {
248 atlas = new QSGCurveGlyphAtlas(font);
250 }
251
252 return atlas;
253}
254
256{
257 FontKey key(font, renderTypeQuality);
259 if (!cache && font.isValid()) {
260 cache = new QSGRhiDistanceFieldGlyphCache(this, font, renderTypeQuality);
261 m_glyphCaches.insert(key, cache);
262 }
263
264 return cache;
265}
266
271
279
285
287{
291 }
292
293 for (QRhiTexture *t : std::as_const(m_pendingGlyphCacheTextures))
294 t->deleteLater(); // the QRhiTexture object stays valid for the current frame
295
297}
298
300
301#include "moc_qsgdefaultrendercontext_p.cpp"
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1212
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1219
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1215
iterator erase(const_iterator it)
Definition qhash.h:1233
T value(const Key &key) const noexcept
Definition qhash.h:1054
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1216
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:951
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1303
\inmodule QtGui
Definition qimage.h:37
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
QQuickGraphicsConfiguration controls lower level graphics settings for the QQuickWindow.
The QRawFont class provides access to a single physical instance of a font.
Definition qrawfont.h:24
\inmodule QtGui
Definition qrhi.h:1651
\inmodule QtGui
Definition qrhi.h:1731
\inmodule QtGui
Definition qrhi.h:895
int resourceLimit(ResourceLimit limit) const
Definition qrhi.cpp:10121
QThread * thread() const
Definition qrhi.cpp:8792
@ TextureSizeMax
Definition qrhi.h:1888
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition qrhi.cpp:9252
The QSGContext holds the scene graph entry points for one QML engine.
virtual void renderContextInitialized(QSGRenderContext *renderContext)
virtual void renderContextInvalidated(QSGRenderContext *renderContext)
void deferredReleaseGlyphCacheTexture(QRhiTexture *texture)
QRhiResourceUpdateBatch * maybeGlyphCacheResourceUpdates()
void prepareSync(qreal devicePixelRatio, QRhiCommandBuffer *cb, const QQuickGraphicsConfiguration &config) override
void beginNextFrame(QSGRenderer *renderer, const QSGRenderTarget &renderTarget, RenderPassCallback mainPassRecordingStart, RenderPassCallback mainPassRecordingEnd, void *callbackUserData) override
virtual void initializeRhiShader(QSGMaterialShader *shader, QShader::Variant shaderVariant)
void initialize(const QSGRenderContext::InitParams *params) override
Initializes the scene graph render context with the GL context context.
QSGRenderer * createRenderer(QSGRendererInterface::RenderMode renderMode=QSGRendererInterface::RenderMode2D) override
QSGTexture * compressedTextureForFactory(const QSGCompressedTextureFactory *factory) const override
Return the texture corresponding to a texture factory.
QSGTexture * createTexture(const QImage &image, uint flags) const override
QSGDefaultRenderContext(QSGContext *context)
QRhiResourceUpdateBatch * m_glyphCacheResourceUpdates
QSGDistanceFieldGlyphCache * distanceFieldGlyphCache(const QRawFont &font, int renderTypeQuality) override
Factory function for scene graph backends of the distance-field glyph cache.
QSet< QRhiTexture * > m_pendingGlyphCacheTextures
QRhiRenderPassDescriptor * m_currentFrameRenderPass
void preprocess() override
Do necessary preprocessing before the frame.
QSGCurveGlyphAtlas * curveGlyphAtlas(const QRawFont &font) override
Factory function for curve atlases that can be used to provide geometry for the curve renderer for a ...
QSGRhiAtlasTexture::Manager * m_rhiAtlasManager
void endNextFrame(QSGRenderer *renderer) override
void renderNextFrame(QSGRenderer *renderer) override
QHash< FontKey, QSGCurveGlyphAtlas * > m_curveGlyphAtlases
QRhiResourceUpdateBatch * glyphCacheResourceUpdates()
QRhiCommandBuffer * m_currentFrameCommandBuffer
static QSGMaterialShaderPrivate * get(QSGMaterialShader *s)
The QSGMaterialShader class represents a graphics API independent shader program.
void setImage(const QImage &image)
QHash< QObject *, QSGTexture * > m_textures
QHash< QFontEngine *, int > m_fontEnginesToClean
QPointer< QSGContext > m_sg
void(*)(void *) RenderPassCallback
QHash< FontKey, QSGDistanceFieldGlyphCache * > m_glyphCaches
QSet< QSGTexture * > m_texturesToDelete
QRhiCommandBuffer * cb
QRhiRenderPassDescriptor * rpDesc
RenderMode
\value RenderMode2D Normal 2D rendering \value RenderMode2DNoDepthBuffer Normal 2D rendering with dep...
The renderer class is the abstract baseclass used for rendering the QML scene graph.
QSGTexture * create(const QImage &image, bool hasAlphaChannel)
\inmodule QtQuick
Definition qsgtexture.h:20
void clear()
Definition qset.h:61
iterator insert(const T &value)
Definition qset.h:155
Variant
Describes what kind of shader code an entry contains.
Definition qshader.h:103
static QThread * currentThread()
Definition qthread.cpp:1039
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
EGLConfig config
#define qFatal
Definition qlogging.h:168
GLuint64 key
GLbitfield flags
GLenum GLuint texture
void ** params
GLuint shader
Definition qopenglext.h:665
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
#define emit
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
QObject::connect nullptr
QItemEditorFactory * factory
QSvgRenderer * renderer
[0]