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); |
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 | }; |
39 | |
40 | QSGTextMaskRhiShader::QSGTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat) |
41 | : m_glyphFormat(glyphFormat) |
42 | { |
43 | setShaderFileName(stage: VertexStage, |
44 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.vert.qsb" )); |
45 | setShaderFileName(stage: FragmentStage, |
46 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/textmask.frag.qsb" )); |
47 | } |
48 | |
49 | enum UbufOffset { |
50 | ModelViewMatrixOffset = 0, |
51 | ProjectionMatrixOffset = ModelViewMatrixOffset + 64, |
52 | ColorOffset = ProjectionMatrixOffset + 64, |
53 | TextureScaleOffset = ColorOffset + 16, |
54 | DprOffset = TextureScaleOffset + 8, |
55 | |
56 | // + 1 float padding (vec4 must be aligned to 16) |
57 | StyleColorOffset = DprOffset + 4 + 4, |
58 | ShiftOffset = StyleColorOffset + 16 |
59 | }; |
60 | |
61 | bool QSGTextMaskRhiShader::updateUniformData(RenderState &state, |
62 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
63 | { |
64 | Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type()); |
65 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
66 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
67 | |
68 | // updateUniformData() is called before updateSampledImage() by the |
69 | // renderer. Hence updating the glyph cache stuff here. |
70 | const bool updated = mat->ensureUpToDate(); |
71 | Q_ASSERT(mat->texture()); |
72 | Q_ASSERT(oldMat == nullptr || oldMat->texture()); |
73 | |
74 | bool changed = false; |
75 | QByteArray *buf = state.uniformData(); |
76 | Q_ASSERT(buf->size() >= DprOffset + 4); |
77 | |
78 | if (state.isMatrixDirty()) { |
79 | const QMatrix4x4 mv = state.modelViewMatrix(); |
80 | memcpy(dest: buf->data() + ModelViewMatrixOffset, src: mv.constData(), n: 64); |
81 | const QMatrix4x4 p = state.projectionMatrix(); |
82 | memcpy(dest: buf->data() + ProjectionMatrixOffset, src: p.constData(), n: 64); |
83 | |
84 | changed = true; |
85 | } |
86 | |
87 | QRhiTexture *oldRtex = oldMat ? oldMat->texture()->rhiTexture() : nullptr; |
88 | QRhiTexture *newRtex = mat->texture()->rhiTexture(); |
89 | if (updated || !oldMat || oldRtex != newRtex) { |
90 | const QVector2D textureScale = QVector2D(1.0f / mat->rhiGlyphCache()->width(), |
91 | 1.0f / mat->rhiGlyphCache()->height()); |
92 | memcpy(dest: buf->data() + TextureScaleOffset, src: &textureScale, n: 8); |
93 | changed = true; |
94 | } |
95 | |
96 | if (!oldMat) { |
97 | float dpr = state.devicePixelRatio(); |
98 | memcpy(dest: buf->data() + DprOffset, src: &dpr, n: 4); |
99 | } |
100 | |
101 | // move texture uploads/copies onto the renderer's soon-to-be-committed list |
102 | mat->rhiGlyphCache()->commitResourceUpdates(mergeInto: state.resourceUpdateBatch()); |
103 | |
104 | return changed; |
105 | } |
106 | |
107 | void QSGTextMaskRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture, |
108 | QSGMaterial *newMaterial, QSGMaterial *) |
109 | { |
110 | Q_UNUSED(state); |
111 | if (binding != 1) |
112 | return; |
113 | |
114 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
115 | QSGTexture *t = mat->texture(); |
116 | t->setFiltering(QSGTexture::Nearest); |
117 | *texture = t; |
118 | } |
119 | |
120 | class QSG8BitTextMaskRhiShader : public QSGTextMaskRhiShader |
121 | { |
122 | public: |
123 | QSG8BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) |
124 | : QSGTextMaskRhiShader(glyphFormat) |
125 | { |
126 | if (alphaTexture) |
127 | setShaderFileName(stage: FragmentStage, |
128 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask_a.frag.qsb" )); |
129 | else |
130 | setShaderFileName(stage: FragmentStage, |
131 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/8bittextmask.frag.qsb" )); |
132 | } |
133 | |
134 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
135 | }; |
136 | |
137 | bool QSG8BitTextMaskRhiShader::updateUniformData(RenderState &state, |
138 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
139 | { |
140 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
141 | |
142 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
143 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
144 | |
145 | QByteArray *buf = state.uniformData(); |
146 | Q_ASSERT(buf->size() >= ColorOffset + 16); |
147 | |
148 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
149 | const QVector4D color = qsg_premultiply(c: mat->color(), globalOpacity: state.opacity()); |
150 | memcpy(dest: buf->data() + ColorOffset, src: &color, n: 16); |
151 | changed = true; |
152 | } |
153 | |
154 | return changed; |
155 | } |
156 | |
157 | class QSG24BitTextMaskRhiShader : public QSGTextMaskRhiShader |
158 | { |
159 | public: |
160 | QSG24BitTextMaskRhiShader(QFontEngine::GlyphFormat glyphFormat) |
161 | : QSGTextMaskRhiShader(glyphFormat) |
162 | { |
163 | setFlag(flags: UpdatesGraphicsPipelineState, on: true); |
164 | setShaderFileName(stage: FragmentStage, |
165 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/24bittextmask.frag.qsb" )); |
166 | } |
167 | |
168 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
169 | bool updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, |
170 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
171 | }; |
172 | |
173 | bool QSG24BitTextMaskRhiShader::updateUniformData(RenderState &state, |
174 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
175 | { |
176 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
177 | |
178 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
179 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
180 | |
181 | QByteArray *buf = state.uniformData(); |
182 | Q_ASSERT(buf->size() >= ColorOffset + 16); |
183 | |
184 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
185 | // shader takes vec4 but uses alpha only; coloring happens via the blend constant |
186 | const QVector4D color = qsg_premultiply(c: mat->color(), globalOpacity: state.opacity()); |
187 | memcpy(dest: buf->data() + ColorOffset, src: &color, n: 16); |
188 | changed = true; |
189 | } |
190 | |
191 | return changed; |
192 | } |
193 | |
194 | bool QSG24BitTextMaskRhiShader::updateGraphicsPipelineState(RenderState &state, GraphicsPipelineState *ps, |
195 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
196 | { |
197 | Q_UNUSED(state); |
198 | Q_UNUSED(oldMaterial); |
199 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
200 | |
201 | ps->blendEnable = true; |
202 | ps->srcColor = GraphicsPipelineState::ConstantColor; |
203 | ps->dstColor = GraphicsPipelineState::OneMinusSrcColor; |
204 | |
205 | QVector4D color = mat->color(); |
206 | |
207 | // this is dynamic state but it's - magic! - taken care of by the renderer |
208 | ps->blendConstant = QColor::fromRgbF(r: color.x(), g: color.y(), b: color.z(), a: color.w()); |
209 | |
210 | return true; |
211 | } |
212 | |
213 | class QSG32BitColorTextRhiShader : public QSGTextMaskRhiShader |
214 | { |
215 | public: |
216 | QSG32BitColorTextRhiShader(QFontEngine::GlyphFormat glyphFormat) |
217 | : QSGTextMaskRhiShader(glyphFormat) |
218 | { |
219 | setShaderFileName(stage: FragmentStage, |
220 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/32bitcolortext.frag.qsb" )); |
221 | } |
222 | |
223 | bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
224 | }; |
225 | |
226 | bool QSG32BitColorTextRhiShader::updateUniformData(RenderState &state, |
227 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
228 | { |
229 | bool changed = QSGTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
230 | |
231 | QSGTextMaskMaterial *mat = static_cast<QSGTextMaskMaterial *>(newMaterial); |
232 | QSGTextMaskMaterial *oldMat = static_cast<QSGTextMaskMaterial *>(oldMaterial); |
233 | |
234 | QByteArray *buf = state.uniformData(); |
235 | Q_ASSERT(buf->size() >= ColorOffset + 16); |
236 | |
237 | if (oldMat == nullptr || mat->color() != oldMat->color() || state.isOpacityDirty()) { |
238 | // shader takes vec4 but uses alpha only |
239 | const QVector4D color(0, 0, 0, mat->color().w() * state.opacity()); |
240 | memcpy(dest: buf->data() + ColorOffset, src: &color, n: 16); |
241 | changed = true; |
242 | } |
243 | |
244 | return changed; |
245 | } |
246 | |
247 | class QSGStyledTextRhiShader : public QSG8BitTextMaskRhiShader |
248 | { |
249 | public: |
250 | QSGStyledTextRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) |
251 | : QSG8BitTextMaskRhiShader(glyphFormat, alphaTexture) |
252 | { |
253 | setShaderFileName(stage: VertexStage, |
254 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.vert.qsb" )); |
255 | if (alphaTexture) |
256 | setShaderFileName(stage: FragmentStage, |
257 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext_a.frag.qsb" )); |
258 | else |
259 | setShaderFileName(stage: FragmentStage, |
260 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/styledtext.frag.qsb" )); |
261 | } |
262 | |
263 | bool updateUniformData(RenderState &state, |
264 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override; |
265 | }; |
266 | |
267 | bool QSGStyledTextRhiShader::updateUniformData(RenderState &state, |
268 | QSGMaterial *newMaterial, QSGMaterial *oldMaterial) |
269 | { |
270 | bool changed = QSG8BitTextMaskRhiShader::updateUniformData(state, newMaterial, oldMaterial); |
271 | |
272 | QSGStyledTextMaterial *mat = static_cast<QSGStyledTextMaterial *>(newMaterial); |
273 | QSGStyledTextMaterial *oldMat = static_cast<QSGStyledTextMaterial *>(oldMaterial); |
274 | |
275 | QByteArray *buf = state.uniformData(); |
276 | Q_ASSERT(buf->size() >= ShiftOffset + 8); |
277 | |
278 | if (oldMat == nullptr || mat->styleColor() != oldMat->styleColor() || state.isOpacityDirty()) { |
279 | const QVector4D styleColor = qsg_premultiply(c: mat->styleColor(), globalOpacity: state.opacity()); |
280 | memcpy(dest: buf->data() + StyleColorOffset, src: &styleColor, n: 16); |
281 | changed = true; |
282 | } |
283 | |
284 | if (oldMat == nullptr || oldMat->styleShift() != mat->styleShift()) { |
285 | const QVector2D v = mat->styleShift(); |
286 | memcpy(dest: buf->data() + ShiftOffset, src: &v, n: 8); |
287 | changed = true; |
288 | } |
289 | |
290 | return changed; |
291 | } |
292 | |
293 | class QSGOutlinedTextRhiShader : public QSGStyledTextRhiShader |
294 | { |
295 | public: |
296 | QSGOutlinedTextRhiShader(QFontEngine::GlyphFormat glyphFormat, bool alphaTexture) |
297 | : QSGStyledTextRhiShader(glyphFormat, alphaTexture) |
298 | { |
299 | setShaderFileName(stage: VertexStage, |
300 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.vert.qsb" )); |
301 | if (alphaTexture) |
302 | setShaderFileName(stage: FragmentStage, |
303 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext_a.frag.qsb" )); |
304 | else |
305 | setShaderFileName(stage: FragmentStage, |
306 | QStringLiteral(":/qt-project.org/scenegraph/shaders_ng/outlinedtext.frag.qsb" )); |
307 | } |
308 | }; |
309 | |
310 | |
311 | // ***** common material stuff |
312 | |
313 | QSGTextMaskMaterial::QSGTextMaskMaterial(QSGRenderContext *rc, const QVector4D &color, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat) |
314 | : m_rc(qobject_cast<QSGDefaultRenderContext *>(object: rc)) |
315 | , m_texture(nullptr) |
316 | , m_glyphCache(nullptr) |
317 | , m_font(font) |
318 | , m_color(color) |
319 | { |
320 | init(glyphFormat); |
321 | } |
322 | |
323 | QSGTextMaskMaterial::~QSGTextMaskMaterial() |
324 | { |
325 | if (m_retainedFontEngine != nullptr) |
326 | m_rc->unregisterFontengineForCleanup(engine: m_retainedFontEngine); |
327 | delete m_texture; |
328 | } |
329 | |
330 | void 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(glyphFormat: QFontEngine::Format_ARGB); |
341 | } |
342 | |
343 | void QSGTextMaskMaterial::init(QFontEngine::GlyphFormat glyphFormat) |
344 | { |
345 | Q_ASSERT(m_font.isValid()); |
346 | |
347 | setFlag(flags: Blending, on: true); |
348 | |
349 | Q_ASSERT(m_rc); |
350 | m_rhi = m_rc->rhi(); |
351 | |
352 | updateCache(glyphFormat); |
353 | } |
354 | |
355 | void 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(font: 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(dx: devicePixelRatio, dy: devicePixelRatio); |
382 | if (!fontEngine->supportsTransformation(transform: glyphCacheTransform)) |
383 | glyphCacheTransform = QTransform(); |
384 | |
385 | 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(); |
386 | m_glyphCache = fontEngine->glyphCache(key: cacheKey, format: glyphFormat, transform: glyphCacheTransform, color); |
387 | if (!m_glyphCache || int(m_glyphCache->glyphFormat()) != glyphFormat) { |
388 | m_glyphCache = new QSGRhiTextureGlyphCache(m_rc, glyphFormat, glyphCacheTransform, color); |
389 | fontEngine->setGlyphCache(key: cacheKey, data: m_glyphCache.data()); |
390 | if (m_retainedFontEngine != nullptr) |
391 | m_rc->unregisterFontengineForCleanup(engine: 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(engine: fontEngine); |
397 | } |
398 | } |
399 | } |
400 | |
401 | void QSGTextMaskMaterial::populate(const QPointF &p, |
402 | const QVector<quint32> &glyphIndexes, |
403 | const QVector<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 | QVector<QFixedPoint> fixedPointPositions; |
412 | const int glyphPositionsSize = glyphPositions.size(); |
413 | fixedPointPositions.reserve(asize: glyphPositionsSize); |
414 | for (int i=0; i < glyphPositionsSize; ++i) |
415 | fixedPointPositions.append(t: QFixedPoint::fromPointF(p: position + glyphPositions.at(i))); |
416 | |
417 | QTextureGlyphCache *cache = glyphCache(); |
418 | |
419 | QRawFontPrivate *fontD = QRawFontPrivate::get(font: m_font); |
420 | cache->populate(fontEngine: fontD->fontEngine, |
421 | numGlyphs: glyphIndexes.size(), |
422 | glyphs: glyphIndexes.constData(), |
423 | positions: fixedPointPositions.data(), |
424 | renderHints: QPainter::RenderHints(), |
425 | includeGlyphCacheScale: true); |
426 | cache->fillInPendingGlyphs(); |
427 | |
428 | int margin = fontD->fontEngine->glyphMargin(format: 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(vertexCount: glyphIndexes.size() * 4, indexCount: 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(x: QFixed::fromReal(r: fixedPointPosition.x.toReal() * glyphCacheScaleX)); |
450 | |
451 | QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i), |
452 | QFixedPoint(subPixelPosition, 0)); |
453 | const QTextureGlyphCache::Coord &c = cache->coords.value(key: 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(v: glyphPosition.x() * glyphCacheScaleX) * glyphCacheInverseScaleX) + |
462 | (c.baseLineX * glyphCacheInverseScaleX) - scaledMargin; |
463 | qreal y = (qRound(d: 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 | |
499 | QSGMaterialType *QSGTextMaskMaterial::type() const |
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 | |
513 | QTextureGlyphCache *QSGTextMaskMaterial::glyphCache() const |
514 | { |
515 | return static_cast<QTextureGlyphCache *>(m_glyphCache.data()); |
516 | } |
517 | |
518 | QSGRhiTextureGlyphCache *QSGTextMaskMaterial::rhiGlyphCache() const |
519 | { |
520 | return static_cast<QSGRhiTextureGlyphCache *>(glyphCache()); |
521 | } |
522 | |
523 | QSGMaterialShader *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); |
531 | case QFontEngine::Format_A32: |
532 | return new QSG24BitTextMaskRhiShader(glyphFormat); |
533 | case QFontEngine::Format_A8: |
534 | default: |
535 | return new QSG8BitTextMaskRhiShader(glyphFormat, gc->eightBitFormatIsAlphaSwizzled()); |
536 | } |
537 | } |
538 | |
539 | static 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 | |
552 | int 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(a: m_color, b: other->m_color); |
559 | } |
560 | |
561 | bool QSGTextMaskMaterial::ensureUpToDate() |
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 | |
579 | QSGStyledTextMaterial::QSGStyledTextMaterial(QSGRenderContext *rc, const QRawFont &font) |
580 | : QSGTextMaskMaterial(rc, QVector4D(), font, QFontEngine::Format_A8) |
581 | { |
582 | } |
583 | |
584 | QSGMaterialType *QSGStyledTextMaterial::type() const |
585 | { |
586 | static QSGMaterialType type; |
587 | return &type; |
588 | } |
589 | |
590 | QSGMaterialShader *QSGStyledTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const |
591 | { |
592 | Q_UNUSED(renderMode); |
593 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
594 | return new QSGStyledTextRhiShader(gc->glyphFormat(), gc->eightBitFormatIsAlphaSwizzled()); |
595 | } |
596 | |
597 | int 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(a: m_styleColor, b: other->m_styleColor); |
605 | if (diff == 0) |
606 | return QSGTextMaskMaterial::compare(o); |
607 | return diff; |
608 | } |
609 | |
610 | |
611 | QSGOutlinedTextMaterial::QSGOutlinedTextMaterial(QSGRenderContext *rc, const QRawFont &font) |
612 | : QSGStyledTextMaterial(rc, font) |
613 | { |
614 | } |
615 | |
616 | QSGMaterialType *QSGOutlinedTextMaterial::type() const |
617 | { |
618 | static QSGMaterialType type; |
619 | return &type; |
620 | } |
621 | |
622 | QSGMaterialShader *QSGOutlinedTextMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const |
623 | { |
624 | Q_UNUSED(renderMode); |
625 | QSGRhiTextureGlyphCache *gc = rhiGlyphCache(); |
626 | return new QSGOutlinedTextRhiShader(gc->glyphFormat(), gc->eightBitFormatIsAlphaSwizzled()); |
627 | } |
628 | |
629 | QT_END_NAMESPACE |
630 | |