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#ifndef QSGDEFAULTRENDERCONTEXT_H
5#define QSGDEFAULTRENDERCONTEXT_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuick/private/qsgcontext_p.h>
19#include <rhi/qshader.h>
20
21QT_BEGIN_NAMESPACE
22
23class QRhi;
24class QRhiCommandBuffer;
25class QRhiRenderPassDescriptor;
26class QRhiResourceUpdateBatch;
27class QRhiTexture;
28class QSGMaterialShader;
29class QSurface;
30
31namespace QSGRhiAtlasTexture {
32 class Manager;
33}
34
35class Q_QUICK_PRIVATE_EXPORT QSGDefaultRenderContext : public QSGRenderContext
36{
37 Q_OBJECT
38public:
39 QSGDefaultRenderContext(QSGContext *context);
40
41 QRhi *rhi() const override { return m_rhi; }
42 bool isValid() const override { return m_rhi != nullptr; }
43
44 static const int INIT_PARAMS_MAGIC = 0x50E;
45 struct InitParams : public QSGRenderContext::InitParams {
46 int sType = INIT_PARAMS_MAGIC; // help discovering broken code passing something else as 'context'
47 QRhi *rhi = nullptr;
48 int sampleCount = 1; // 1, 4, 8, ...
49 // only used as a hint f.ex. in the texture atlas init
50 QSize initialSurfacePixelSize;
51 // The first window that will be used with this rc, if available.
52 // Only a hint, to help picking better values for atlases.
53 QSurface *maybeSurface = nullptr;
54 };
55
56 void initialize(const QSGRenderContext::InitParams *params) override;
57 void invalidate() override;
58
59 void prepareSync(qreal devicePixelRatio,
60 QRhiCommandBuffer *cb,
61 const QQuickGraphicsConfiguration &config) override;
62
63 void beginNextFrame(QSGRenderer *renderer, const QSGRenderTarget &renderTarget,
64 RenderPassCallback mainPassRecordingStart,
65 RenderPassCallback mainPassRecordingEnd,
66 void *callbackUserData) override;
67 void renderNextFrame(QSGRenderer *renderer) override;
68 void endNextFrame(QSGRenderer *renderer) override;
69
70 void preprocess() override;
71 void invalidateGlyphCaches() override;
72 QSGDistanceFieldGlyphCache *distanceFieldGlyphCache(const QRawFont &font, int renderTypeQuality) override;
73
74 QSGTexture *createTexture(const QImage &image, uint flags) const override;
75 QSGRenderer *createRenderer(QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D) override;
76 QSGTexture *compressedTextureForFactory(const QSGCompressedTextureFactory *factory) const override;
77
78 virtual void initializeRhiShader(QSGMaterialShader *shader, QShader::Variant shaderVariant);
79
80 int maxTextureSize() const override { return m_maxTextureSize; }
81 bool useDepthBufferFor2D() const { return m_useDepthBufferFor2D; }
82 int msaaSampleCount() const { return m_initParams.sampleCount; }
83
84 QRhiCommandBuffer *currentFrameCommandBuffer() const {
85 // may be null if not in an active frame, but returning null is valid then
86 return m_currentFrameCommandBuffer;
87 }
88 QRhiRenderPassDescriptor *currentFrameRenderPass() const {
89 // may be null if not in an active frame, but returning null is valid then
90 return m_currentFrameRenderPass;
91 }
92
93 qreal currentDevicePixelRatio() const
94 {
95 // Valid starting from QQuickWindow::syncSceneGraph(). This takes the
96 // redirections, e.g. QQuickWindow::setRenderTarget(), into account.
97 // This calculation logic matches what the renderer does, so this is
98 // the same value that gets exposed in RenderState::devicePixelRatio()
99 // to material shaders. This getter is useful to perform dpr-related
100 // operations in the sync phase (in updatePaintNode()).
101 return m_currentDevicePixelRatio;
102 }
103
104 QRhiResourceUpdateBatch *maybeGlyphCacheResourceUpdates();
105 QRhiResourceUpdateBatch *glyphCacheResourceUpdates();
106 void deferredReleaseGlyphCacheTexture(QRhiTexture *texture);
107 void resetGlyphCacheResources();
108
109protected:
110 static QString fontKey(const QRawFont &font, int renderTypeQuality);
111
112 InitParams m_initParams;
113 QRhi *m_rhi;
114 int m_maxTextureSize;
115 QSGRhiAtlasTexture::Manager *m_rhiAtlasManager;
116 QRhiCommandBuffer *m_currentFrameCommandBuffer;
117 QRhiRenderPassDescriptor *m_currentFrameRenderPass;
118 qreal m_currentDevicePixelRatio;
119 bool m_useDepthBufferFor2D;
120 QRhiResourceUpdateBatch *m_glyphCacheResourceUpdates;
121 QSet<QRhiTexture *> m_pendingGlyphCacheTextures;
122};
123
124QT_END_NAMESPACE
125
126#endif // QSGDEFAULTRENDERCONTEXT_H
127

source code of qtdeclarative/src/quick/scenegraph/qsgdefaultrendercontext_p.h