| 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 | #include "qsgdefaultglyphnode_p_p.h" |
| 5 | #include <private/qsgmaterialshader_p.h> |
| 6 | |
| 7 | #include <QtGui/private/qguiapplication_p.h> |
| 8 | #include <qpa/qplatformintegration.h> |
| 9 | #include <private/qfontengine_p.h> |
| 10 | |
| 11 | #include <QtQuick/qquickwindow.h> |
| 12 | #include <QtQuick/private/qsgtexture_p.h> |
| 13 | #include <QtQuick/private/qsgdefaultrendercontext_p.h> |
| 14 | |
| 15 | #include <private/qrawfont_p.h> |
| 16 | #include <QtCore/qmath.h> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | static inline QVector4D qsg_premultiply(const QVector4D &c, float globalOpacity) |
| 21 | { |
| 22 | float o = c.w() * globalOpacity; |
| 23 | return QVector4D(c.x() * o, c.y() * o, c.z() * o, o); |
| 24 | } |
| 25 | |
| 26 | class QSGTextMaskRhiShader : public QSGMaterialShader |
| 27 | { |
| 28 | public: |
| 29 | QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount); |
| 30 | |
| 31 | bool updateUniformData(RenderState &state, |
| 32 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 33 | void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, |
| 34 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 35 | |
| 36 | protected: |
| 37 | QFontEngine::GlyphFormat m_glyphFormat; |
| 38 | quint32 m_currentUbufOffset; |
| 39 | }; |
| 40 | |
| 41 | QSGTextMaskRhiShader::QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) |
| 42 | : m_glyphFormat(glyphFormat) |
| 43 | { |
| 44 | setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb" ), viewCount); |
| 45 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb" ), viewCount); |
| 46 | } |
| 47 | |
| 48 | // uniform block layout: |
| 49 | // mat4 modelViewMatrix |
| 50 | // mat4 projectionMatrix or mat4 projectionMatrix[QSHADER_VIEW_COUNT] |
| 51 | // vec2 textureScale |
| 52 | // float dpr |
| 53 | // vec4 color |
| 54 | // [styled/outline only] |
| 55 | // vec4 styleColor |
| 56 | // vec2 shift |
| 57 | |
| 58 | bool QSGTextMaskRhiShader::updateUniformData(RenderState &state, |
| 59 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 60 | { |
| 61 | Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type()); |
| 62 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 63 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
| 64 | |
| 65 | // updateUniformData() is called before updateSampledImage() by the |
| 66 | // renderer. Hence updating the glyph cache stuff here. |
| 67 | const bool updated = mat->ensureUpToDate(); |
| 68 | Q_ASSERT(mat->texture()); |
| 69 | Q_ASSERT(oldMat == nullptr || oldMat->texture()); |
| 70 | |
| 71 | bool changed = false; |
| 72 | QByteArray *buf = state.uniformData(); |
| 73 | const int projectionMatrixCount = qMin(a: state.projectionMatrixCount(), b: newMaterial->viewCount()); |
| 74 | |
| 75 | quint32 offset = 0; // ModelViewMatrix |
| 76 | if (state.isMatrixDirty()) { |
| 77 | const QMatrix4x4 mv = state.modelViewMatrix(); |
| 78 | memcpy(dest: buf->data() + offset, src: mv.constData(), n: 64); |
| 79 | changed = true; |
| 80 | } |
| 81 | offset += 64; // now at ProjectionMatrix or ProjectionMatrix[0] |
| 82 | |
| 83 | for (int viewIndex = 0; viewIndex < projectionMatrixCount; ++viewIndex) { |
| 84 | if (state.isMatrixDirty()) { |
| 85 | const QMatrix4x4 p = state.projectionMatrix(index: viewIndex); |
| 86 | memcpy(dest: buf->data() + offset, src: p.constData(), n: 64); |
| 87 | changed = true; |
| 88 | } |
| 89 | offset += 64; |
| 90 | } |
| 91 | |
| 92 | // offset is now at TextureScale |
| 93 | QRhiTexture *oldRtex = oldMat ? oldMat->texture()->rhiTexture() : nullptr; |
| 94 | QRhiTexture *newRtex = mat->texture()->rhiTexture(); |
| 95 | if (updated || !oldMat || oldRtex != newRtex) { |
| 96 | const QVector2D textureScale = QVector2D(1.0f / mat->rhiGlyphCache()->width(), |
| 97 | 1.0f / mat->rhiGlyphCache()->height()); |
| 98 | memcpy(dest: buf->data() + offset, src: &textureScale, n: 8); |
| 99 | changed = true; |
| 100 | } |
| 101 | offset += 8; // now at Dpr |
| 102 | |
| 103 | if (!oldMat) { |
| 104 | float dpr = state.devicePixelRatio(); |
| 105 | memcpy(dest: buf->data() + offset, src: &dpr, n: 4); |
| 106 | } |
| 107 | offset += 4 + 4; // now at Color (with padding to ensure vec4 alignment) |
| 108 | |
| 109 | // move texture uploads/copies onto the renderer's soon-to-be-committed list |
| 110 | mat->rhiGlyphCache()->commitResourceUpdates(mergeInto: state.resourceUpdateBatch()); |
| 111 | |
| 112 | m_currentUbufOffset = offset; |
| 113 | return changed; |
| 114 | } |
| 115 | |
| 116 | void QSGTextMaskRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture, |
| 117 | QSGMaterial *newMaterial, QSGMaterial *) |
| 118 | { |
| 119 | Q_UNUSED(state); |
| 120 | if (binding != 1) |
| 121 | return; |
| 122 | |
| 123 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 124 | QSGTexture *t = mat->texture(); |
| 125 | t->setFiltering(QSGTexture::Nearest); |
| 126 | *texture = t; |
| 127 | } |
| 128 | |
| 129 | class QSG8BitTextMaskRhiShader : public QSGTextMaskRhiShader |
| 130 | { |
| 131 | public: |
| 132 | QSG8BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) |
| 133 | : QSGTextMaskRhiShader(glyphFormat, viewCount) |
| 134 | { |
| 135 | if (alphaTexture) |
| 136 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb" ), viewCount); |
| 137 | else |
| 138 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb" ), viewCount); |
| 139 | } |
| 140 | |
| 141 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 142 | }; |
| 143 | |
| 144 | bool QSG8BitTextMaskRhiShader::updateUniformData(RenderState &state, |
| 145 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 146 | { |
| 147 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
| 148 | |
| 149 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 150 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
| 151 | |
| 152 | QByteArray *buf = state.uniformData(); |
| 153 | |
| 154 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
| 155 | const QVector4D color = qsg_premultiply(c: mat->color(), globalOpacity: state.opacity()); |
| 156 | memcpy(dest: buf->data() + m_currentUbufOffset, src: &color, n: 16); |
| 157 | changed = true; |
| 158 | } |
| 159 | m_currentUbufOffset += 16; // now at StyleColor |
| 160 | |
| 161 | return changed; |
| 162 | } |
| 163 | |
| 164 | class QSG24BitTextMaskRhiShader : public QSGTextMaskRhiShader |
| 165 | { |
| 166 | public: |
| 167 | QSG24BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) |
| 168 | : QSGTextMaskRhiShader(glyphFormat, viewCount) |
| 169 | { |
| 170 | setFlag(flags: UpdatesGraphicsPipelineState, on: true); |
| 171 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb" ), viewCount); |
| 172 | } |
| 173 | |
| 174 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 175 | bool updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, |
| 176 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 177 | }; |
| 178 | |
| 179 | bool QSG24BitTextMaskRhiShader::updateUniformData(RenderState &state, |
| 180 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 181 | { |
| 182 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
| 183 | |
| 184 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 185 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
| 186 | |
| 187 | QByteArray *buf = state.uniformData(); |
| 188 | |
| 189 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
| 190 | // shader takes vec4 but uses alpha only; coloring happens via the blend constant |
| 191 | const QVector4D color = qsg_premultiply(c: mat->color(), globalOpacity: state.opacity()); |
| 192 | memcpy(dest: buf->data() + m_currentUbufOffset, src: &color, n: 16); |
| 193 | changed = true; |
| 194 | } |
| 195 | m_currentUbufOffset += 16; // now at StyleColor |
| 196 | |
| 197 | return changed; |
| 198 | } |
| 199 | |
| 200 | bool QSG24BitTextMaskRhiShader::updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, |
| 201 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 202 | { |
| 203 | Q_UNUSED(state); |
| 204 | Q_UNUSED(oldMaterial); |
| 205 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 206 | |
| 207 | ps->blendEnable = true; |
| 208 | ps->srcColor = GraphicsPipelineState::ConstantColor; |
| 209 | ps->dstColor = GraphicsPipelineState::OneMinusSrcColor; |
| 210 | |
| 211 | QVector4D color = mat->color(); |
| 212 | |
| 213 | // this is dynamic state but it's - magic! - taken care of by the renderer |
| 214 | ps->blendConstant = QColor::fromRgbF(r: color.x(), g: color.y(), b: color.z()); |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | class QSG32BitColorTextRhiShader : public QSGTextMaskRhiShader |
| 220 | { |
| 221 | public: |
| 222 | QSG32BitColorTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount) |
| 223 | : QSGTextMaskRhiShader(glyphFormat, viewCount) |
| 224 | { |
| 225 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb" ), viewCount); |
| 226 | } |
| 227 | |
| 228 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 229 | }; |
| 230 | |
| 231 | bool QSG32BitColorTextRhiShader::updateUniformData(RenderState &state, |
| 232 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 233 | { |
| 234 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
| 235 | |
| 236 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
| 237 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
| 238 | |
| 239 | QByteArray *buf = state.uniformData(); |
| 240 | |
| 241 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
| 242 | // shader takes vec4 but uses alpha only |
| 243 | const QVector4D color(0, 0, 0, mat->color().w() * state.opacity()); |
| 244 | memcpy(dest: buf->data() + m_currentUbufOffset, src: &color, n: 16); |
| 245 | changed = true; |
| 246 | } |
| 247 | m_currentUbufOffset += 16; // now at StyleColor |
| 248 | |
| 249 | return changed; |
| 250 | } |
| 251 | |
| 252 | class QSGStyledTextRhiShader : public QSG8BitTextMaskRhiShader |
| 253 | { |
| 254 | public: |
| 255 | QSGStyledTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) |
| 256 | : QSG8BitTextMaskRhiShader(glyphFormat, viewCount, alphaTexture) |
| 257 | { |
| 258 | setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb" ), viewCount); |
| 259 | if (alphaTexture) |
| 260 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb" ), viewCount); |
| 261 | else |
| 262 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb" ), viewCount); |
| 263 | } |
| 264 | |
| 265 | bool updateUniformData(RenderState &state, |
| 266 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
| 267 | }; |
| 268 | |
| 269 | bool QSGStyledTextRhiShader::updateUniformData(RenderState &state, |
| 270 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
| 271 | { |
| 272 | bool changed = QSG8BitTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
| 273 | |
| 274 | QSGStyledTextMaterial *mat = static_cast<QSGStyledTextMaterial *>(newMaterial); |
| 275 | QSGStyledTextMaterial *oldMat = static_cast<QSGStyledTextMaterial *>(oldMaterial); |
| 276 | |
| 277 | QByteArray *buf = state.uniformData(); |
| 278 | |
| 279 | if (oldMat == nullptr || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) { |
| 280 | const QVector4D styleColor = qsg_premultiply(c: mat->styleColor(), globalOpacity: state.opacity()); |
| 281 | memcpy(dest: buf->data() + m_currentUbufOffset, src: &styleColor, n: 16); |
| 282 | changed = true; |
| 283 | } |
| 284 | m_currentUbufOffset += 16; // now at StyleShift |
| 285 | |
| 286 | if (oldMat == nullptr || oldMat->styleShift() != mat->styleShift()) { |
| 287 | const QVector2D v = mat->styleShift(); |
| 288 | memcpy(dest: buf->data() + m_currentUbufOffset, src: &v, n: 8); |
| 289 | changed = true; |
| 290 | } |
| 291 | |
| 292 | return changed; |
| 293 | } |
| 294 | |
| 295 | class QSGOutlinedTextRhiShader : public QSGStyledTextRhiShader |
| 296 | { |
| 297 | public: |
| 298 | QSGOutlinedTextRhiShader(QFontEngine::GlyphFormat glyphFormat, int viewCount, bool alphaTexture) |
| 299 | : QSGStyledTextRhiShader(glyphFormat, viewCount, alphaTexture) |
| 300 | { |
| 301 | setShaderFileName(stage: VertexStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb" ), viewCount); |
| 302 | if (alphaTexture) |
| 303 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb" ), viewCount); |
| 304 | else |
| 305 | setShaderFileName(stage: FragmentStage, QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb" ), viewCount); |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | |
| 310 | // ***** common material stuff |
| 311 | |
| 312 | QSGTextMaskMaterial::QSGTextMaskMaterial(QSGRenderContext *rc, const QVector4D &color, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat) |
| 313 | : m_rc(qobject_cast<QSGDefaultRenderContext *>(object: rc)) |
| 314 | , m_texture(nullptr) |
| 315 | , m_glyphCache(nullptr) |
| 316 | , m_font(font) |
| 317 | , m_color(color) |
| 318 | { |
| 319 | init(glyphFormat); |
| 320 | } |
| 321 | |
| 322 | QSGTextMaskMaterial::~QSGTextMaskMaterial() |
| 323 | { |
| 324 | if (m_retainedFontEngine != nullptr) |
| 325 | m_rc->unregisterFontengineForCleanup(engine: m_retainedFontEngine); |
| 326 | delete m_texture; |
| 327 | } |
| 328 | |
| 329 | void QSGTextMaskMaterial::setColor(const QVector4D &color) |
| 330 | { |
| 331 | if (m_color == color) |
| 332 | return; |
| 333 | |
| 334 | m_color = color; |
| 335 | |
| 336 | // If it is an RGB cache, then the pen color is actually part of the cache key |
| 337 | // so it has to be updated |
| 338 | if (m_glyphCache != nullptr && m_glyphCache->glyphFormat() == QFontEngine::Format_ARGB) |
| 339 | updateCache(glyphFormat: QFontEngine::Format_ARGB); |
| 340 | } |
| 341 | |
| 342 | void QSGTextMaskMaterial::init(QFontEngine::GlyphFormat glyphFormat) |
| 343 | { |
| 344 | Q_ASSERT(m_font.isValid()); |
| 345 | |
| 346 | setFlag(flags: Blending, on: true); |
| 347 | |
| 348 | Q_ASSERT(m_rc); |
| 349 | m_rhi = m_rc->rhi(); |
| 350 | |
| 351 | updateCache(glyphFormat); |
| 352 | } |
| 353 | |
| 354 | void QSGTextMaskMaterial::updateCache(QFontEngine::GlyphFormat glyphFormat) |
| 355 | { |
| 356 | // The following piece of code will read/write to the font engine's caches, |
| 357 | // potentially from different threads. However, this is safe because this |
| 358 | // code is only called from QQuickItem::updatePaintNode() which is called |
| 359 | // only when the GUI is blocked, and multiple threads will call it in |
| 360 | // sequence. See also QSGRenderContext::invalidate |
| 361 | |
| 362 | QRawFontPrivate *fontD = QRawFontPrivate::get(font: m_font); |
| 363 | if (QFontEngine *fontEngine = fontD->fontEngine) { |
| 364 | if (glyphFormat == QFontEngine::Format_None) { |
| 365 | glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None |
| 366 | ? fontEngine->glyphFormat |
| 367 | : QFontEngine::Format_A32; |
| 368 | } |
| 369 | |
| 370 | qreal devicePixelRatio; |
| 371 | void *cacheKey; |
| 372 | Q_ASSERT(m_rhi); |
| 373 | Q_ASSERT(m_rc); |
| 374 | cacheKey = m_rc; |
| 375 | // Get the dpr the modern way. This value retrieved via the |
| 376 | // rendercontext matches what RenderState::devicePixelRatio() |
| 377 | // exposes to the material shaders later on. |
| 378 | devicePixelRatio = m_rc->currentDevicePixelRatio(); |
| 379 | |
| 380 | QTransform glyphCacheTransform = QTransform::fromScale(dx: devicePixelRatio, dy: devicePixelRatio); |
| 381 | if (!fontEngine->supportsTransformation(transform: glyphCacheTransform)) |
| 382 | glyphCacheTransform = QTransform(); |
| 383 | |
| 384 | QColor color = glyphFormat == QFontEngine::Format_ARGB ? QColor::fromRgbF(r: m_color.x(), g: m_color.y(), b: m_color.z(), a: m_color.w()) : QColor(); |
| 385 | m_glyphCache = fontEngine->glyphCache(key: cacheKey, format: glyphFormat, transform: glyphCacheTransform, color); |
| 386 | if (!m_glyphCache || int(m_glyphCache->glyphFormat()) != glyphFormat) { |
| 387 | m_glyphCache = new QSGRhiTextureGlyphCache(m_rc, glyphFormat, glyphCacheTransform, color); |
| 388 | fontEngine->setGlyphCache(key: cacheKey, data: m_glyphCache.data()); |
| 389 | if (m_retainedFontEngine != nullptr) |
| 390 | m_rc->unregisterFontengineForCleanup(engine: m_retainedFontEngine); |
| 391 | |
| 392 | // Note: This is reference counted by the render context, so it will stay alive until |
| 393 | // we release that reference |
| 394 | m_retainedFontEngine = fontEngine; |
| 395 | m_rc->registerFontengineForCleanup(engine: fontEngine); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void QSGTextMaskMaterial::populate(const QPointF &p, |
| 401 | const QVector<quint32> &glyphIndexes, |
| 402 | const QVector<QPointF> &glyphPositions, |
| 403 | QSGGeometry *geometry, |
| 404 | QRectF *boundingRect, |
| 405 | QPointF *baseLine, |
| 406 | const QMargins &margins) |
| 407 | { |
| 408 | Q_ASSERT(m_font.isValid()); |
| 409 | QPointF position(p.x(), p.y() - m_font.ascent()); |
| 410 | QVector<QFixedPoint> fixedPointPositions; |
| 411 | const int glyphPositionsSize = glyphPositions.size(); |
| 412 | fixedPointPositions.reserve(asize: glyphPositionsSize); |
| 413 | for (int i=0; i < glyphPositionsSize; ++i) |
| 414 | fixedPointPositions.append(t: QFixedPoint::fromPointF(p: position + glyphPositions.at(i))); |
| 415 | |
| 416 | QTextureGlyphCache *cache = glyphCache(); |
| 417 | |
| 418 | QRawFontPrivate *fontD = QRawFontPrivate::get(font: m_font); |
| 419 | cache->populate(fontEngine: fontD->fontEngine, |
| 420 | numGlyphs: glyphIndexes.size(), |
| 421 | glyphs: glyphIndexes.constData(), |
| 422 | positions: fixedPointPositions.data(), |
| 423 | renderHints: QPainter::RenderHints(), |
| 424 | includeGlyphCacheScale: true); |
| 425 | cache->fillInPendingGlyphs(); |
| 426 | |
| 427 | int margin = fontD->fontEngine->glyphMargin(format: cache->glyphFormat()); |
| 428 | |
| 429 | qreal glyphCacheScaleX = cache->transform().m11(); |
| 430 | qreal glyphCacheScaleY = cache->transform().m22(); |
| 431 | qreal glyphCacheInverseScaleX = 1.0 / glyphCacheScaleX; |
| 432 | qreal glyphCacheInverseScaleY = 1.0 / glyphCacheScaleY; |
| 433 | qreal scaledMargin = margin * glyphCacheInverseScaleX; |
| 434 | |
| 435 | Q_ASSERT(geometry->indexType() == QSGGeometry::UnsignedShortType); |
| 436 | geometry->allocate(vertexCount: glyphIndexes.size() * 4, indexCount: glyphIndexes.size() * 6); |
| 437 | QVector4D *vp = (QVector4D *)geometry->vertexDataAsTexturedPoint2D(); |
| 438 | Q_ASSERT(geometry->sizeOfVertex() == sizeof(QVector4D)); |
| 439 | ushort *ip = geometry->indexDataAsUShort(); |
| 440 | |
| 441 | bool supportsSubPixelPositions = fontD->fontEngine->supportsHorizontalSubPixelPositions(); |
| 442 | for (int i=0; i<glyphIndexes.size(); ++i) { |
| 443 | QPointF glyphPosition = glyphPositions.at(i) + position; |
| 444 | QFixedPoint fixedPointPosition = fixedPointPositions.at(i); |
| 445 | |
| 446 | QFixed subPixelPosition; |
| 447 | if (supportsSubPixelPositions) |
| 448 | subPixelPosition = fontD->fontEngine->subPixelPositionForX(x: QFixed::fromReal(r: fixedPointPosition.x.toReal() * glyphCacheScaleX)); |
| 449 | |
| 450 | QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i), |
| 451 | QFixedPoint(subPixelPosition, 0)); |
| 452 | const QTextureGlyphCache::Coord &c = cache->coords.value(key: glyph); |
| 453 | |
| 454 | // On a retina screen the glyph positions are not pre-scaled (as opposed to |
| 455 | // eg. the raster paint engine). To ensure that we get the same behavior as |
| 456 | // the raster engine (and CoreText itself) when it comes to rounding of the |
| 457 | // coordinates, we need to apply the scale factor before rounding, and then |
| 458 | // apply the inverse scale to get back to the coordinate system of the node. |
| 459 | |
| 460 | qreal x = (qFloor(v: glyphPosition.x() * glyphCacheScaleX) * glyphCacheInverseScaleX) + |
| 461 | (c.baseLineX * glyphCacheInverseScaleX) - scaledMargin; |
| 462 | qreal y = (qRound(d: glyphPosition.y() * glyphCacheScaleY) * glyphCacheInverseScaleY) - |
| 463 | (c.baseLineY * glyphCacheInverseScaleY) - scaledMargin; |
| 464 | |
| 465 | qreal w = c.w * glyphCacheInverseScaleX; |
| 466 | qreal h = c.h * glyphCacheInverseScaleY; |
| 467 | |
| 468 | *boundingRect |= QRectF(x + scaledMargin, y + scaledMargin, w, h); |
| 469 | |
| 470 | float cx1 = x - margins.left(); |
| 471 | float cx2 = x + w + margins.right(); |
| 472 | float cy1 = y - margins.top(); |
| 473 | float cy2 = y + h + margins.bottom(); |
| 474 | |
| 475 | float tx1 = c.x - margins.left(); |
| 476 | float tx2 = c.x + c.w + margins.right(); |
| 477 | float ty1 = c.y - margins.top(); |
| 478 | float ty2 = c.y + c.h + margins.bottom(); |
| 479 | |
| 480 | if (baseLine->isNull()) |
| 481 | *baseLine = glyphPosition; |
| 482 | |
| 483 | vp[4 * i + 0] = QVector4D(cx1, cy1, tx1, ty1); |
| 484 | vp[4 * i + 1] = QVector4D(cx2, cy1, tx2, ty1); |
| 485 | vp[4 * i + 2] = QVector4D(cx1, cy2, tx1, ty2); |
| 486 | vp[4 * i + 3] = QVector4D(cx2, cy2, tx2, ty2); |
| 487 | |
| 488 | int o = i * 4; |
| 489 | ip[6 * i + 0] = o + 0; |
| 490 | ip[6 * i + 1] = o + 2; |
| 491 | ip[6 * i + 2] = o + 3; |
| 492 | ip[6 * i + 3] = o + 3; |
| 493 | ip[6 * i + 4] = o + 1; |
| 494 | ip[6 * i + 5] = o + 0; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | QSGMaterialType *QSGTextMaskMaterial::type() const |
| 499 | { |
| 500 | static QSGMaterialType argb, rgb, gray; |
| 501 | switch (glyphCache()->glyphFormat()) { |
| 502 | case QFontEngine::Format_ARGB: |
| 503 | return &argb; |
| 504 | case QFontEngine::Format_A32: |
| 505 | return &rgb; |
| 506 | case QFontEngine::Format_A8: |
| 507 | default: |
| 508 | return &gray; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | QTextureGlyphCache *QSGTextMaskMaterial::glyphCache() const |
| 513 | { |
| 514 | return static_cast<QTextureGlyphCache *>(m_glyphCache.data()); |
| 515 | } |
| 516 | |
| 517 | QSGRhiTextureGlyphCache *QSGTextMaskMaterial::rhiGlyphCache() const |
| 518 | { |
| 519 | return static_cast<QSGRhiTextureGlyphCache *>(glyphCache()); |
| 520 | } |
| 521 | |
| 522 | QSGMaterialShader *QSGTextMaskMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const |
| 523 | { |
| 524 | Q_UNUSED(renderMode); |
| 525 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
| 526 | const QFontEngine::GlyphFormat glyphFormat = gc->glyphFormat(); |
| 527 | switch (glyphFormat) { |
| 528 | case QFontEngine::Format_ARGB: |
| 529 | return new QSG32BitColorTextRhiShader(glyphFormat, viewCount()); |
| 530 | case QFontEngine::Format_A32: |
| 531 | return new QSG24BitTextMaskRhiShader(glyphFormat, viewCount()); |
| 532 | case QFontEngine::Format_A8: |
| 533 | default: |
| 534 | return new QSG8BitTextMaskRhiShader(glyphFormat, viewCount(), gc->eightBitFormatIsAlphaSwizzled()); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static inline int qsg_colorDiff(const QVector4D &a, const QVector4D &b) |
| 539 | { |
| 540 | if (a.x() != b.x()) |
| 541 | return a.x() > b.x() ? 1 : -1; |
| 542 | if (a.y() != b.y()) |
| 543 | return a.y() > b.y() ? 1 : -1; |
| 544 | if (a.z() != b.z()) |
| 545 | return a.z() > b.z() ? 1 : -1; |
| 546 | if (a.w() != b.w()) |
| 547 | return a.w() > b.w() ? 1 : -1; |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | int QSGTextMaskMaterial::compare(const QSGMaterial *o) const |
| 552 | { |
| 553 | Q_ASSERT(o && type() == o->type()); |
| 554 | const QSGTextMaskMaterial *other = static_cast<const QSGTextMaskMaterial *>(o); |
| 555 | if (m_glyphCache != other->m_glyphCache) |
| 556 | return m_glyphCache.data() < other->m_glyphCache.data() ? -1 : 1; |
| 557 | return qsg_colorDiff(a: m_color, b: other->m_color); |
| 558 | } |
| 559 | |
| 560 | bool QSGTextMaskMaterial::ensureUpToDate() |
| 561 | { |
| 562 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
| 563 | QSize glyphCacheSize(gc->width(), gc->height()); |
| 564 | if (glyphCacheSize != m_size) { |
| 565 | if (m_texture) |
| 566 | delete m_texture; |
| 567 | m_texture = new QSGPlainTexture; |
| 568 | m_texture->setTexture(gc->texture()); |
| 569 | m_texture->setTextureSize(QSize(gc->width(), gc->height())); |
| 570 | m_texture->setOwnsTexture(false); |
| 571 | m_size = glyphCacheSize; |
| 572 | return true; |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | |
| 578 | QSGStyledTextMaterial::QSGStyledTextMaterial(QSGRenderContext *rc, const QRawFont &font) |
| 579 | : QSGTextMaskMaterial(rc, QVector4D(), font, QFontEngine::Format_A8) |
| 580 | { |
| 581 | } |
| 582 | |
| 583 | QSGMaterialType *QSGStyledTextMaterial::type() const |
| 584 | { |
| 585 | static QSGMaterialType type; |
| 586 | return &type; |
| 587 | } |
| 588 | |
| 589 | QSGMaterialShader *QSGStyledTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const |
| 590 | { |
| 591 | Q_UNUSED(renderMode); |
| 592 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
| 593 | return new QSGStyledTextRhiShader(gc->glyphFormat(), viewCount(), gc->eightBitFormatIsAlphaSwizzled()); |
| 594 | } |
| 595 | |
| 596 | int QSGStyledTextMaterial::compare(const QSGMaterial *o) const |
| 597 | { |
| 598 | const QSGStyledTextMaterial *other = static_cast<const QSGStyledTextMaterial *>(o); |
| 599 | |
| 600 | if (m_styleShift != other->m_styleShift) |
| 601 | return m_styleShift.y() - other->m_styleShift.y(); |
| 602 | |
| 603 | int diff = qsg_colorDiff(a: m_styleColor, b: other->m_styleColor); |
| 604 | if (diff == 0) |
| 605 | return QSGTextMaskMaterial::compare(o); |
| 606 | return diff; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | QSGOutlinedTextMaterial::QSGOutlinedTextMaterial(QSGRenderContext *rc, const QRawFont &font) |
| 611 | : QSGStyledTextMaterial(rc, font) |
| 612 | { |
| 613 | } |
| 614 | |
| 615 | QSGMaterialType *QSGOutlinedTextMaterial::type() const |
| 616 | { |
| 617 | static QSGMaterialType type; |
| 618 | return &type; |
| 619 | } |
| 620 | |
| 621 | QSGMaterialShader *QSGOutlinedTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const |
| 622 | { |
| 623 | Q_UNUSED(renderMode); |
| 624 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
| 625 | return new QSGOutlinedTextRhiShader(gc->glyphFormat(), viewCount(), gc->eightBitFormatIsAlphaSwizzled()); |
| 626 | } |
| 627 | |
| 628 | QT_END_NAMESPACE |
| 629 | |