diff options
Diffstat (limited to 'src/quick/scenegraph/qsgdefaultglyphnode_p.cpp')
-rw-r--r-- | src/quick/scenegraph/qsgdefaultglyphnode_p.cpp | 234 |
1 files changed, 93 insertions, 141 deletions
diff --git a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp index 578b5ab2f2..3d6cabe6fc 100644 --- a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp +++ b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qsgdefaultglyphnode_p_p.h" #include <private/qsgmaterialshader_p.h> @@ -53,45 +17,16 @@ QT_BEGIN_NAMESPACE -#ifndef GL_FRAMEBUFFER_SRGB -#define GL_FRAMEBUFFER_SRGB 0x8DB9 -#endif - -#ifndef GL_FRAMEBUFFER_SRGB_CAPABLE -#define GL_FRAMEBUFFER_SRGB_CAPABLE 0x8DBA -#endif - static inline QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity) { float o = c.w() * globalOpacity; return QVector4D(c.x() * o, c.y() * o, c.z() * o, o); } -#if 0 -static inline qreal qt_sRGB_to_linear_RGB(qreal f) -{ - return f > 0.04045 ? qPow((f + 0.055) / 1.055, 2.4) : f / 12.92; -} - -static inline QVector4D qt_sRGB_to_linear_RGB(const QVector4D &color) -{ - return QVector4D(qt_sRGB_to_linear_RGB(color.x()), - qt_sRGB_to_linear_RGB(color.y()), - qt_sRGB_to_linear_RGB(color.z()), - color.w()); -} - -static inline qreal fontSmoothingGamma() -{ - static qreal fontSmoothingGamma = QGuiApplicationPrivate::platformIntegration()->styleHint(QPlatformIntegration::FontSmoothingGamma).toReal(); - return fontSmoothingGamma; -} -#endif - class QSGTextMaskRhiShader : public QSGMaterialShader { public: - QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat); + QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount); bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; @@ -100,17 +35,26 @@ public: protected: QFontEngine::GlyphFormat m_glyphFormat; + quint32 m_currentUbufOffset; }; -QSGTextMaskRhiShader::QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat) +QSGTextMaskRhiShader::QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) : m_glyphFormat(glyphFormat) { - setShaderFileName(VertexStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb")); - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb")); + setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb"), viewCount); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb"), viewCount); } +// uniform block layout: +// mat4 modelViewMatrix +// mat4 projectionMatrix or mat4 projectionMatrix[QSHADER_VIEW_COUNT] +// vec2 textureScale +// float dpr +// vec4 color +// [styled/outline only] +// vec4 styleColor +// vec2 shift + bool QSGTextMaskRhiShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) { @@ -126,31 +70,46 @@ bool QSGTextMaskRhiShader::updateUniformData(RenderState &state, bool changed = false; QByteArray *buf = state.uniformData(); - Q_ASSERT(buf->size() >= 92); + const int projectionMatrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount()); + quint32 offset = 0; // ModelViewMatrix if (state.isMatrixDirty()) { - const QMatrix4x4 m = state.combinedMatrix(); - memcpy(buf->data(), m.constData(), 64); + const QMatrix4x4 mv = state.modelViewMatrix(); + memcpy(buf->data() + offset, mv.constData(), 64); changed = true; } + offset += 64; // now at ProjectionMatrix or ProjectionMatrix[0] + + for (int viewIndex = 0; viewIndex < projectionMatrixCount; ++viewIndex) { + if (state.isMatrixDirty()) { + const QMatrix4x4 p = state.projectionMatrix(viewIndex); + memcpy(buf->data() + offset, p.constData(), 64); + changed = true; + } + offset += 64; + } + // offset is now at TextureScale QRhiTexture *oldRtex = oldMat ? oldMat->texture()->rhiTexture() : nullptr; QRhiTexture *newRtex = mat->texture()->rhiTexture(); if (updated || !oldMat || oldRtex != newRtex) { const QVector2D textureScale = QVector2D(1.0f / mat->rhiGlyphCache()->width(), 1.0f / mat->rhiGlyphCache()->height()); - memcpy(buf->data() + 64 + 16, &textureScale, 8); + memcpy(buf->data() + offset, &textureScale, 8); changed = true; } + offset += 8; // now at Dpr if (!oldMat) { float dpr = state.devicePixelRatio(); - memcpy(buf->data() + 64 + 16 + 8, &dpr, 4); + memcpy(buf->data() + offset, &dpr, 4); } + offset += 4 + 4; // now at Color (with padding to ensure vec4 alignment) // move texture uploads/copies onto the renderer's soon-to-be-committed list mat->rhiGlyphCache()->commitResourceUpdates(state.resourceUpdateBatch()); + m_currentUbufOffset = offset; return changed; } @@ -170,15 +129,13 @@ void QSGTextMaskRhiShader::updateSampledImage(RenderState &state, int binding, Q class QSG8BitTextMaskRhiShader : public QSGTextMaskRhiShader { public: - QSG8BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) - : QSGTextMaskRhiShader(glyphFormat) + QSG8BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) + : QSGTextMaskRhiShader(glyphFormat, viewCount) { if (alphaTexture) - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb"), viewCount); else - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb"), viewCount); } bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; @@ -193,13 +150,13 @@ bool QSG8BitTextMaskRhiShader::updateUniformData(RenderState &state, QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); QByteArray *buf = state.uniformData(); - Q_ASSERT(buf->size() >= 80); if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { const QVector4D color = qsg_premultiply(mat->color(), state.opacity()); - memcpy(buf->data() + 64, &color, 16); + memcpy(buf->data() + m_currentUbufOffset, &color, 16); changed = true; } + m_currentUbufOffset += 16; // now at StyleColor return changed; } @@ -207,12 +164,11 @@ bool QSG8BitTextMaskRhiShader::updateUniformData(RenderState &state, class QSG24BitTextMaskRhiShader : public QSGTextMaskRhiShader { public: - QSG24BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat) - : QSGTextMaskRhiShader(glyphFormat) + QSG24BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) + : QSGTextMaskRhiShader(glyphFormat, viewCount) { setFlag(UpdatesGraphicsPipelineState, true); - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb"), viewCount); } bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; @@ -220,13 +176,6 @@ public: QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; }; -// ### gamma correction (sRGB) Unsurprisingly, the GL approach is not portable -// to anything else - it just does not work that way, there is no opt-in/out -// switch and magic winsys-provided maybe-sRGB buffers. When requesting an sRGB -// QRhiSwapChain (which we do not do), it is full sRGB, with the sRGB -// framebuffer update and blending always on... Could we do gamma correction in -// the shader for text? (but that's bad for blending?) - bool QSG24BitTextMaskRhiShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) { @@ -236,14 +185,14 @@ bool QSG24BitTextMaskRhiShader::updateUniformData(RenderState &state, QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); QByteArray *buf = state.uniformData(); - Q_ASSERT(buf->size() >= 92); if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { // shader takes vec4 but uses alpha only; coloring happens via the blend constant const QVector4D color = qsg_premultiply(mat->color(), state.opacity()); - memcpy(buf->data() + 64, &color, 16); + memcpy(buf->data() + m_currentUbufOffset, &color, 16); changed = true; } + m_currentUbufOffset += 16; // now at StyleColor return changed; } @@ -259,12 +208,10 @@ bool QSG24BitTextMaskRhiShader::updateGraphicsPipelineState(RenderState &state, ps->srcColor = GraphicsPipelineState::ConstantColor; ps->dstColor = GraphicsPipelineState::OneMinusSrcColor; - QVector4D color = qsg_premultiply(mat->color(), state.opacity()); - // if (useSRGB()) - // color = qt_sRGB_to_linear_RGB(color); + QVector4D color = mat->color(); // this is dynamic state but it's - magic! - taken care of by the renderer - ps->blendConstant = QColor::fromRgbF(color.x(), color.y(), color.z(), color.w()); + ps->blendConstant = QColor::fromRgbF(color.x(), color.y(), color.z()); return true; } @@ -272,11 +219,10 @@ bool QSG24BitTextMaskRhiShader::updateGraphicsPipelineState(RenderState &state, class QSG32BitColorTextRhiShader : public QSGTextMaskRhiShader { public: - QSG32BitColorTextRhiShader(QFontEngine::GlyphFormat glyphFormat) - : QSGTextMaskRhiShader(glyphFormat) + QSG32BitColorTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) + : QSGTextMaskRhiShader(glyphFormat, viewCount) { - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb"), viewCount); } bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; @@ -291,14 +237,14 @@ bool QSG32BitColorTextRhiShader::updateUniformData(RenderState &state, QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); QByteArray *buf = state.uniformData(); - Q_ASSERT(buf->size() >= 92); if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { // shader takes vec4 but uses alpha only const QVector4D color(0, 0, 0, mat->color().w() * state.opacity()); - memcpy(buf->data() + 64, &color, 16); + memcpy(buf->data() + m_currentUbufOffset, &color, 16); changed = true; } + m_currentUbufOffset += 16; // now at StyleColor return changed; } @@ -306,17 +252,14 @@ bool QSG32BitColorTextRhiShader::updateUniformData(RenderState &state, class QSGStyledTextRhiShader : public QSG8BitTextMaskRhiShader { public: - QSGStyledTextRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) - : QSG8BitTextMaskRhiShader(glyphFormat, alphaTexture) + QSGStyledTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) + : QSG8BitTextMaskRhiShader(glyphFormat, viewCount, alphaTexture) { - setShaderFileName(VertexStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb")); + setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb"), viewCount); if (alphaTexture) - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb"), viewCount); else - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb"), viewCount); } bool updateUniformData(RenderState &state, @@ -332,20 +275,17 @@ bool QSGStyledTextRhiShader::updateUniformData(RenderState &state, QSGStyledTextMaterial *oldMat = static_cast<QSGStyledTextMaterial *>(oldMaterial); QByteArray *buf = state.uniformData(); - Q_ASSERT(buf->size() >= 120); - - // matrix..dpr + 1 float padding (vec4 must be aligned to 16) - const int startOffset = 64 + 16 + 8 + 4 + 4; if (oldMat == nullptr || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) { const QVector4D styleColor = qsg_premultiply(mat->styleColor(), state.opacity()); - memcpy(buf->data() + startOffset, &styleColor, 16); + memcpy(buf->data() + m_currentUbufOffset, &styleColor, 16); changed = true; } + m_currentUbufOffset += 16; // now at StyleShift if (oldMat == nullptr || oldMat->styleShift() != mat->styleShift()) { const QVector2D v = mat->styleShift(); - memcpy(buf->data() + startOffset + 16, &v, 8); + memcpy(buf->data() + m_currentUbufOffset, &v, 8); changed = true; } @@ -355,17 +295,14 @@ bool QSGStyledTextRhiShader::updateUniformData(RenderState &state, class QSGOutlinedTextRhiShader : public QSGStyledTextRhiShader { public: - QSGOutlinedTextRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) - : QSGStyledTextRhiShader(glyphFormat, alphaTexture) + QSGOutlinedTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) + : QSGStyledTextRhiShader(glyphFormat, viewCount, alphaTexture) { - setShaderFileName(VertexStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb")); + setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb"), viewCount); if (alphaTexture) - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb"), viewCount); else - setShaderFileName(FragmentStage, - QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb")); + setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb"), viewCount); } }; @@ -384,6 +321,8 @@ QSGTextMaskMaterial::QSGTextMaskMaterial(QSGRenderContext *rc, const QVector4D & QSGTextMaskMaterial::~QSGTextMaskMaterial() { + if (m_retainedFontEngine != nullptr) + m_rc->unregisterFontengineForCleanup(m_retainedFontEngine); delete m_texture; } @@ -431,7 +370,8 @@ void QSGTextMaskMaterial::updateCache(QFontEngine::GlyphFormat glyphFormat) qreal devicePixelRatio; void *cacheKey; Q_ASSERT(m_rhi); - cacheKey = m_rhi; + Q_ASSERT(m_rc); + cacheKey = m_rc; // Get the dpr the modern way. This value retrieved via the // rendercontext matches what RenderState::devicePixelRatio() // exposes to the material shaders later on. @@ -444,8 +384,14 @@ void QSGTextMaskMaterial::updateCache(QFontEngine::GlyphFormat glyphFormat) QColor color = glyphFormat == QFontEngine::Format_ARGB ? QColor::fromRgbF(m_color.x(), m_color.y(), m_color.z(), m_color.w()) : QColor(); m_glyphCache = fontEngine->glyphCache(cacheKey, glyphFormat, glyphCacheTransform, color); if (!m_glyphCache || int(m_glyphCache->glyphFormat()) != glyphFormat) { - m_glyphCache = new QSGRhiTextureGlyphCache(m_rhi, glyphFormat, glyphCacheTransform, color); + m_glyphCache = new QSGRhiTextureGlyphCache(m_rc, glyphFormat, glyphCacheTransform, color); fontEngine->setGlyphCache(cacheKey, m_glyphCache.data()); + if (m_retainedFontEngine != nullptr) + m_rc->unregisterFontengineForCleanup(m_retainedFontEngine); + + // Note: This is reference counted by the render context, so it will stay alive until + // we release that reference + m_retainedFontEngine = fontEngine; m_rc->registerFontengineForCleanup(fontEngine); } } @@ -470,8 +416,12 @@ void QSGTextMaskMaterial::populate(const QPointF &p, QTextureGlyphCache *cache = glyphCache(); QRawFontPrivate *fontD = QRawFontPrivate::get(m_font); - cache->populate(fontD->fontEngine, glyphIndexes.size(), glyphIndexes.constData(), - fixedPointPositions.data()); + cache->populate(fontD->fontEngine, + glyphIndexes.size(), + glyphIndexes.constData(), + fixedPointPositions.data(), + QPainter::RenderHints(), + true); cache->fillInPendingGlyphs(); int margin = fontD->fontEngine->glyphMargin(cache->glyphFormat()); @@ -491,9 +441,11 @@ void QSGTextMaskMaterial::populate(const QPointF &p, bool supportsSubPixelPositions = fontD->fontEngine->supportsHorizontalSubPixelPositions(); for (int i=0; i<glyphIndexes.size(); ++i) { QPointF glyphPosition = glyphPositions.at(i) + position; + QFixedPoint fixedPointPosition = fixedPointPositions.at(i); + QFixed subPixelPosition; if (supportsSubPixelPositions) - subPixelPosition = fontD->fontEngine->subPixelPositionForX(QFixed::fromReal(glyphPosition.x())); + subPixelPosition = fontD->fontEngine->subPixelPositionForX(QFixed::fromReal(fixedPointPosition.x.toReal() * glyphCacheScaleX)); QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i), QFixedPoint(subPixelPosition, 0)); @@ -574,12 +526,12 @@ QSGMaterialShader *QSGTextMaskMaterial::createShader(QSGRendererInterface::Rende const QFontEngine::GlyphFormat glyphFormat = gc->glyphFormat(); switch (glyphFormat) { case QFontEngine::Format_ARGB: - return new QSG32BitColorTextRhiShader(glyphFormat); + return new QSG32BitColorTextRhiShader(glyphFormat, viewCount()); case QFontEngine::Format_A32: - return new QSG24BitTextMaskRhiShader(glyphFormat); + return new QSG24BitTextMaskRhiShader(glyphFormat, viewCount()); case QFontEngine::Format_A8: default: - return new QSG8BitTextMaskRhiShader(glyphFormat, gc->eightBitFormatIsAlphaSwizzled()); + return new QSG8BitTextMaskRhiShader(glyphFormat, viewCount(), gc->eightBitFormatIsAlphaSwizzled()); } } @@ -638,7 +590,7 @@ QSGMaterialShader *QSGStyledTextMaterial::createShader(QSGRendererInterface::Ren { Q_UNUSED(renderMode); QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); - return new QSGStyledTextRhiShader(gc->glyphFormat(), gc->eightBitFormatIsAlphaSwizzled()); + return new QSGStyledTextRhiShader(gc->glyphFormat(), viewCount(), gc->eightBitFormatIsAlphaSwizzled()); } int QSGStyledTextMaterial::compare(const QSGMaterial *o) const @@ -670,7 +622,7 @@ QSGMaterialShader *QSGOutlinedTextMaterial::createShader(QSGRendererInterface::R { Q_UNUSED(renderMode); QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); - return new QSGOutlinedTextRhiShader(gc->glyphFormat(), gc->eightBitFormatIsAlphaSwizzled()); + return new QSGOutlinedTextRhiShader(gc->glyphFormat(), viewCount(), gc->eightBitFormatIsAlphaSwizzled()); } QT_END_NAMESPACE |