| 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 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | class QRhi; |
| 24 | class QRhiCommandBuffer; |
| 25 | class QRhiRenderPassDescriptor; |
| 26 | class QRhiResourceUpdateBatch; |
| 27 | class QRhiTexture; |
| 28 | class QSGMaterialShader; |
| 29 | class QSurface; |
| 30 | |
| 31 | namespace QSGRhiAtlasTexture { |
| 32 | class Manager; |
| 33 | } |
| 34 | |
| 35 | class Q_QUICK_EXPORT QSGDefaultRenderContext : public QSGRenderContext |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | public: |
| 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 | QSGCurveGlyphAtlas *curveGlyphAtlas(const QRawFont &font) override; |
| 74 | |
| 75 | QSGTexture *createTexture(const QImage &image, uint flags) const override; |
| 76 | QSGRenderer *createRenderer(QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D) override; |
| 77 | QSGTexture *compressedTextureForFactory(const QSGCompressedTextureFactory *factory) const override; |
| 78 | |
| 79 | virtual void initializeRhiShader(QSGMaterialShader *shader, QShader::Variant shaderVariant); |
| 80 | |
| 81 | int maxTextureSize() const override { return m_maxTextureSize; } |
| 82 | bool useDepthBufferFor2D() const { return m_useDepthBufferFor2D; } |
| 83 | int msaaSampleCount() const { return m_initParams.sampleCount; } |
| 84 | |
| 85 | QRhiCommandBuffer *currentFrameCommandBuffer() const { |
| 86 | // may be null if not in an active frame, but returning null is valid then |
| 87 | return m_currentFrameCommandBuffer; |
| 88 | } |
| 89 | QRhiRenderPassDescriptor *currentFrameRenderPass() const { |
| 90 | // may be null if not in an active frame, but returning null is valid then |
| 91 | return m_currentFrameRenderPass; |
| 92 | } |
| 93 | |
| 94 | qreal currentDevicePixelRatio() const |
| 95 | { |
| 96 | // Valid starting from QQuickWindow::syncSceneGraph(). This takes the |
| 97 | // redirections, e.g. QQuickWindow::setRenderTarget(), into account. |
| 98 | // This calculation logic matches what the renderer does, so this is |
| 99 | // the same value that gets exposed in RenderState::devicePixelRatio() |
| 100 | // to material shaders. This getter is useful to perform dpr-related |
| 101 | // operations in the sync phase (in updatePaintNode()). |
| 102 | return m_currentDevicePixelRatio; |
| 103 | } |
| 104 | |
| 105 | QRhiResourceUpdateBatch *maybeGlyphCacheResourceUpdates(); |
| 106 | QRhiResourceUpdateBatch *glyphCacheResourceUpdates(); |
| 107 | void deferredReleaseGlyphCacheTexture(QRhiTexture *texture); |
| 108 | void resetGlyphCacheResources(); |
| 109 | |
| 110 | protected: |
| 111 | InitParams m_initParams; |
| 112 | QRhi *m_rhi; |
| 113 | int m_maxTextureSize; |
| 114 | QSGRhiAtlasTexture::Manager *m_rhiAtlasManager; |
| 115 | QRhiCommandBuffer *m_currentFrameCommandBuffer; |
| 116 | QRhiRenderPassDescriptor *m_currentFrameRenderPass; |
| 117 | qreal m_currentDevicePixelRatio; |
| 118 | bool m_useDepthBufferFor2D; |
| 119 | QRhiResourceUpdateBatch *m_glyphCacheResourceUpdates; |
| 120 | QSet<QRhiTexture *> m_pendingGlyphCacheTextures; |
| 121 | QHash<FontKey, QSGCurveGlyphAtlas *> m_curveGlyphAtlases; |
| 122 | }; |
| 123 | |
| 124 | QT_END_NAMESPACE |
| 125 | |
| 126 | #endif // QSGDEFAULTRENDERCONTEXT_H |
| 127 | |