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