1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qsgrhidistancefieldglyphcache_p.h" |
41 | #include "qsgcontext_p.h" |
42 | #include <QtGui/private/qdistancefield_p.h> |
43 | #include <QtCore/qelapsedtimer.h> |
44 | #include <QtQml/private/qqmlglobal_p.h> |
45 | #include <qmath.h> |
46 | #include <qendian.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | DEFINE_BOOL_CONFIG_OPTION(qmlUseGlyphCacheWorkaround, QML_USE_GLYPHCACHE_WORKAROUND) |
51 | DEFINE_BOOL_CONFIG_OPTION(qsgPreferFullSizeGlyphCacheTextures, QSG_PREFER_FULLSIZE_GLYPHCACHE_TEXTURES) |
52 | |
53 | #if !defined(QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING) |
54 | # define QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING 2 |
55 | #endif |
56 | |
57 | QSGRhiDistanceFieldGlyphCache::QSGRhiDistanceFieldGlyphCache(QRhi *rhi, const QRawFont &font) |
58 | : QSGDistanceFieldGlyphCache(font) |
59 | , m_rhi(rhi) |
60 | { |
61 | // Load a pregenerated cache if the font contains one |
62 | loadPregeneratedCache(font); |
63 | } |
64 | |
65 | QSGRhiDistanceFieldGlyphCache::~QSGRhiDistanceFieldGlyphCache() |
66 | { |
67 | for (int i = 0; i < m_textures.count(); ++i) |
68 | delete m_textures[i].texture; |
69 | |
70 | delete m_areaAllocator; |
71 | |
72 | // should be empty, but just in case |
73 | qDeleteAll(c: m_pendingDispose); |
74 | } |
75 | |
76 | void QSGRhiDistanceFieldGlyphCache::requestGlyphs(const QSet<glyph_t> &glyphs) |
77 | { |
78 | QList<GlyphPosition> glyphPositions; |
79 | QVector<glyph_t> glyphsToRender; |
80 | |
81 | if (m_areaAllocator == nullptr) |
82 | m_areaAllocator = new QSGAreaAllocator(QSize(maxTextureSize(), m_maxTextureCount * maxTextureSize())); |
83 | |
84 | for (QSet<glyph_t>::const_iterator it = glyphs.constBegin(); it != glyphs.constEnd() ; ++it) { |
85 | glyph_t glyphIndex = *it; |
86 | |
87 | int padding = QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING; |
88 | QRectF boundingRect = glyphData(glyph: glyphIndex).boundingRect; |
89 | int glyphWidth = qCeil(v: boundingRect.width() + distanceFieldRadius() * 2); |
90 | int glyphHeight = qCeil(v: boundingRect.height() + distanceFieldRadius() * 2); |
91 | QSize glyphSize(glyphWidth + padding * 2, glyphHeight + padding * 2); |
92 | QRect alloc = m_areaAllocator->allocate(size: glyphSize); |
93 | |
94 | if (alloc.isNull()) { |
95 | // Unallocate unused glyphs until we can allocated the new glyph |
96 | while (alloc.isNull() && !m_unusedGlyphs.isEmpty()) { |
97 | glyph_t unusedGlyph = *m_unusedGlyphs.constBegin(); |
98 | |
99 | TexCoord unusedCoord = glyphTexCoord(glyph: unusedGlyph); |
100 | QRectF unusedGlyphBoundingRect = glyphData(glyph: unusedGlyph).boundingRect; |
101 | int unusedGlyphWidth = qCeil(v: unusedGlyphBoundingRect.width() + distanceFieldRadius() * 2); |
102 | int unusedGlyphHeight = qCeil(v: unusedGlyphBoundingRect.height() + distanceFieldRadius() * 2); |
103 | m_areaAllocator->deallocate(rect: QRect(unusedCoord.x - padding, |
104 | unusedCoord.y - padding, |
105 | padding * 2 + unusedGlyphWidth, |
106 | padding * 2 + unusedGlyphHeight)); |
107 | |
108 | m_unusedGlyphs.remove(value: unusedGlyph); |
109 | m_glyphsTexture.remove(akey: unusedGlyph); |
110 | removeGlyph(glyph: unusedGlyph); |
111 | |
112 | alloc = m_areaAllocator->allocate(size: glyphSize); |
113 | } |
114 | |
115 | // Not enough space left for this glyph... skip to the next one |
116 | if (alloc.isNull()) |
117 | continue; |
118 | } |
119 | |
120 | TextureInfo *tex = textureInfo(index: alloc.y() / maxTextureSize()); |
121 | alloc = QRect(alloc.x(), alloc.y() % maxTextureSize(), alloc.width(), alloc.height()); |
122 | |
123 | tex->allocatedArea |= alloc; |
124 | Q_ASSERT(tex->padding == padding || tex->padding < 0); |
125 | tex->padding = padding; |
126 | |
127 | GlyphPosition p; |
128 | p.glyph = glyphIndex; |
129 | p.position = alloc.topLeft() + QPoint(padding, padding); |
130 | |
131 | glyphPositions.append(t: p); |
132 | glyphsToRender.append(t: glyphIndex); |
133 | m_glyphsTexture.insert(akey: glyphIndex, avalue: tex); |
134 | } |
135 | |
136 | setGlyphsPosition(glyphPositions); |
137 | markGlyphsToRender(glyphs: glyphsToRender); |
138 | } |
139 | |
140 | void QSGRhiDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField> &glyphs) |
141 | { |
142 | typedef QHash<TextureInfo *, QVector<glyph_t> > GlyphTextureHash; |
143 | typedef GlyphTextureHash::const_iterator GlyphTextureHashConstIt; |
144 | |
145 | GlyphTextureHash glyphTextures; |
146 | |
147 | QVarLengthArray<QRhiTextureUploadEntry, 32> uploads; |
148 | for (int i = 0; i < glyphs.size(); ++i) { |
149 | QDistanceField glyph = glyphs.at(i); |
150 | glyph_t glyphIndex = glyph.glyph(); |
151 | TexCoord c = glyphTexCoord(glyph: glyphIndex); |
152 | TextureInfo *texInfo = m_glyphsTexture.value(akey: glyphIndex); |
153 | |
154 | resizeTexture(texInfo, width: texInfo->allocatedArea.width(), height: texInfo->allocatedArea.height()); |
155 | |
156 | glyphTextures[texInfo].append(t: glyphIndex); |
157 | |
158 | int padding = texInfo->padding; |
159 | int expectedWidth = qCeil(v: c.width + c.xMargin * 2); |
160 | glyph = glyph.copy(x: -padding, y: -padding, |
161 | w: expectedWidth + padding * 2, h: glyph.height() + padding * 2); |
162 | |
163 | if (useTextureResizeWorkaround()) { |
164 | uchar *inBits = glyph.scanLine(0); |
165 | uchar *outBits = texInfo->image.scanLine(int(c.y) - padding) + int(c.x) - padding; |
166 | for (int y = 0; y < glyph.height(); ++y) { |
167 | memcpy(dest: outBits, src: inBits, n: glyph.width()); |
168 | inBits += glyph.width(); |
169 | outBits += texInfo->image.width(); |
170 | } |
171 | } |
172 | |
173 | QRhiTextureSubresourceUploadDescription subresDesc(glyph.constBits(), glyph.width() * glyph.height()); |
174 | subresDesc.setSourceSize(QSize(glyph.width(), glyph.height())); |
175 | subresDesc.setDestinationTopLeft(QPoint(c.x - padding, c.y - padding)); |
176 | texInfo->uploads.append(t: QRhiTextureUploadEntry(0, 0, subresDesc)); |
177 | } |
178 | |
179 | if (!m_resourceUpdates) |
180 | m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
181 | |
182 | for (int i = 0; i < glyphs.size(); ++i) { |
183 | TextureInfo *texInfo = m_glyphsTexture.value(akey: glyphs.at(i).glyph()); |
184 | if (!texInfo->uploads.isEmpty()) { |
185 | QRhiTextureUploadDescription desc; |
186 | desc.setEntries(first: texInfo->uploads.cbegin(), last: texInfo->uploads.cend()); |
187 | m_resourceUpdates->uploadTexture(tex: texInfo->texture, desc); |
188 | texInfo->uploads.clear(); |
189 | } |
190 | } |
191 | |
192 | for (GlyphTextureHashConstIt i = glyphTextures.constBegin(), cend = glyphTextures.constEnd(); i != cend; ++i) { |
193 | Texture t; |
194 | t.texture = i.key()->texture; |
195 | t.size = i.key()->size; |
196 | t.rhiBased = true; |
197 | setGlyphsTexture(glyphs: i.value(), tex: t); |
198 | } |
199 | } |
200 | |
201 | void QSGRhiDistanceFieldGlyphCache::referenceGlyphs(const QSet<glyph_t> &glyphs) |
202 | { |
203 | m_unusedGlyphs -= glyphs; |
204 | } |
205 | |
206 | void QSGRhiDistanceFieldGlyphCache::releaseGlyphs(const QSet<glyph_t> &glyphs) |
207 | { |
208 | m_unusedGlyphs += glyphs; |
209 | } |
210 | |
211 | void QSGRhiDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, |
212 | int width, |
213 | int height) |
214 | { |
215 | QByteArray zeroBuf(width * height, 0); |
216 | createTexture(texInfo, width, height, pixels: zeroBuf.constData()); |
217 | } |
218 | |
219 | void QSGRhiDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, |
220 | int width, |
221 | int height, |
222 | const void *pixels) |
223 | { |
224 | if (useTextureResizeWorkaround() && texInfo->image.isNull()) { |
225 | texInfo->image = QDistanceField(width, height); |
226 | memcpy(dest: texInfo->image.bits(), src: pixels, n: width * height); |
227 | } |
228 | |
229 | texInfo->texture = m_rhi->newTexture(format: QRhiTexture::RED_OR_ALPHA8, pixelSize: QSize(width, height), sampleCount: 1, flags: QRhiTexture::UsedAsTransferSource); |
230 | if (texInfo->texture->build()) { |
231 | if (!m_resourceUpdates) |
232 | m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
233 | |
234 | QRhiTextureSubresourceUploadDescription subresDesc(pixels, width * height); |
235 | subresDesc.setSourceSize(QSize(width, height)); |
236 | m_resourceUpdates->uploadTexture(tex: texInfo->texture, desc: QRhiTextureUploadEntry(0, 0, subresDesc)); |
237 | } else { |
238 | qWarning(msg: "Failed to create distance field glyph cache" ); |
239 | } |
240 | |
241 | texInfo->size = QSize(width, height); |
242 | } |
243 | |
244 | void QSGRhiDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int width, int height) |
245 | { |
246 | int oldWidth = texInfo->size.width(); |
247 | int oldHeight = texInfo->size.height(); |
248 | if (width == oldWidth && height == oldHeight) |
249 | return; |
250 | |
251 | QRhiTexture *oldTexture = texInfo->texture; |
252 | createTexture(texInfo, width, height); |
253 | |
254 | if (!oldTexture) |
255 | return; |
256 | |
257 | updateRhiTexture(oldTex: oldTexture, newTex: texInfo->texture, newTexSize: texInfo->size); |
258 | |
259 | if (!m_resourceUpdates) |
260 | m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); |
261 | |
262 | if (useTextureResizeWorkaround()) { |
263 | QRhiTextureSubresourceUploadDescription subresDesc(texInfo->image.constBits(), |
264 | oldWidth * oldHeight); |
265 | subresDesc.setSourceSize(QSize(oldWidth, oldHeight)); |
266 | m_resourceUpdates->uploadTexture(tex: texInfo->texture, desc: QRhiTextureUploadEntry(0, 0, subresDesc)); |
267 | texInfo->image = texInfo->image.copy(x: 0, y: 0, w: width, h: height); |
268 | } else { |
269 | m_resourceUpdates->copyTexture(dst: texInfo->texture, src: oldTexture); |
270 | } |
271 | |
272 | m_pendingDispose.insert(value: oldTexture); |
273 | } |
274 | |
275 | bool QSGRhiDistanceFieldGlyphCache::useTextureResizeWorkaround() const |
276 | { |
277 | static bool set = false; |
278 | static bool useWorkaround = false; |
279 | if (!set) { |
280 | useWorkaround = m_rhi->backend() == QRhi::OpenGLES2 || qmlUseGlyphCacheWorkaround(); |
281 | set = true; |
282 | } |
283 | return useWorkaround; |
284 | } |
285 | |
286 | bool QSGRhiDistanceFieldGlyphCache::createFullSizeTextures() const |
287 | { |
288 | return qsgPreferFullSizeGlyphCacheTextures() && glyphCount() > QT_DISTANCEFIELD_HIGHGLYPHCOUNT(); |
289 | } |
290 | |
291 | int QSGRhiDistanceFieldGlyphCache::maxTextureSize() const |
292 | { |
293 | if (!m_maxTextureSize) |
294 | m_maxTextureSize = m_rhi->resourceLimit(limit: QRhi::TextureSizeMax); |
295 | return m_maxTextureSize; |
296 | } |
297 | |
298 | namespace { |
299 | struct Qtdf { |
300 | // We need these structs to be tightly packed, but some compilers we use do not |
301 | // support #pragma pack(1), so we need to hardcode the offsets/sizes in the |
302 | // file format |
303 | enum TableSize { |
304 | = 14, |
305 | GlyphRecordSize = 46, |
306 | TextureRecordSize = 17 |
307 | }; |
308 | |
309 | enum Offset { |
310 | // Header |
311 | majorVersion = 0, |
312 | minorVersion = 1, |
313 | pixelSize = 2, |
314 | textureSize = 4, |
315 | flags = 8, |
316 | = 9, |
317 | numGlyphs = 10, |
318 | |
319 | // Glyph record |
320 | glyphIndex = 0, |
321 | textureOffsetX = 4, |
322 | textureOffsetY = 8, |
323 | textureWidth = 12, |
324 | textureHeight = 16, |
325 | xMargin = 20, |
326 | yMargin = 24, |
327 | boundingRectX = 28, |
328 | boundingRectY = 32, |
329 | boundingRectWidth = 36, |
330 | boundingRectHeight = 40, |
331 | textureIndex = 44, |
332 | |
333 | // Texture record |
334 | allocatedX = 0, |
335 | allocatedY = 4, |
336 | allocatedWidth = 8, |
337 | allocatedHeight = 12, |
338 | texturePadding = 16 |
339 | |
340 | }; |
341 | |
342 | template <typename T> |
343 | static inline T fetch(const char *data, Offset offset) |
344 | { |
345 | return qFromBigEndian<T>(data + int(offset)); |
346 | } |
347 | }; |
348 | } |
349 | |
350 | bool QSGRhiDistanceFieldGlyphCache::loadPregeneratedCache(const QRawFont &font) |
351 | { |
352 | // The pregenerated data must be loaded first, otherwise the area allocator |
353 | // will be wrong |
354 | if (m_areaAllocator != nullptr) { |
355 | qWarning(msg: "Font cache must be loaded before cache is used" ); |
356 | return false; |
357 | } |
358 | |
359 | static QElapsedTimer timer; |
360 | |
361 | bool profile = QSG_LOG_TIME_GLYPH().isDebugEnabled(); |
362 | if (profile) |
363 | timer.start(); |
364 | |
365 | QByteArray qtdfTable = font.fontTable(tagName: "qtdf" ); |
366 | if (qtdfTable.isEmpty()) |
367 | return false; |
368 | |
369 | typedef QHash<TextureInfo *, QVector<glyph_t> > GlyphTextureHash; |
370 | |
371 | GlyphTextureHash glyphTextures; |
372 | |
373 | if (uint(qtdfTable.size()) < Qtdf::HeaderSize) { |
374 | qWarning(msg: "Invalid qtdf table in font '%s'" , |
375 | qPrintable(font.familyName())); |
376 | return false; |
377 | } |
378 | |
379 | const char *qtdfTableStart = qtdfTable.constData(); |
380 | const char *qtdfTableEnd = qtdfTableStart + qtdfTable.size(); |
381 | |
382 | int padding = 0; |
383 | int textureCount = 0; |
384 | { |
385 | quint8 majorVersion = Qtdf::fetch<quint8>(data: qtdfTableStart, offset: Qtdf::majorVersion); |
386 | quint8 minorVersion = Qtdf::fetch<quint8>(data: qtdfTableStart, offset: Qtdf::minorVersion); |
387 | if (majorVersion != 5 || minorVersion != 12) { |
388 | qWarning(msg: "Invalid version of qtdf table %d.%d in font '%s'" , |
389 | majorVersion, |
390 | minorVersion, |
391 | qPrintable(font.familyName())); |
392 | return false; |
393 | } |
394 | |
395 | qreal pixelSize = qreal(Qtdf::fetch<quint16>(data: qtdfTableStart, offset: Qtdf::pixelSize)); |
396 | m_maxTextureSize = Qtdf::fetch<quint32>(data: qtdfTableStart, offset: Qtdf::textureSize); |
397 | m_doubleGlyphResolution = Qtdf::fetch<quint8>(data: qtdfTableStart, offset: Qtdf::flags) == 1; |
398 | padding = Qtdf::fetch<quint8>(data: qtdfTableStart, offset: Qtdf::headerPadding); |
399 | |
400 | if (pixelSize <= 0.0) { |
401 | qWarning(msg: "Invalid pixel size in '%s'" , qPrintable(font.familyName())); |
402 | return false; |
403 | } |
404 | |
405 | if (m_maxTextureSize <= 0) { |
406 | qWarning(msg: "Invalid texture size in '%s'" , qPrintable(font.familyName())); |
407 | return false; |
408 | } |
409 | |
410 | int systemMaxTextureSize = m_rhi->resourceLimit(limit: QRhi::TextureSizeMax); |
411 | |
412 | if (m_maxTextureSize > systemMaxTextureSize) { |
413 | qWarning(msg: "System maximum texture size is %d. This is lower than the value in '%s', which is %d" , |
414 | systemMaxTextureSize, |
415 | qPrintable(font.familyName()), |
416 | m_maxTextureSize); |
417 | } |
418 | |
419 | if (padding != QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING) { |
420 | qWarning(msg: "Padding mismatch in '%s'. Font requires %d, but Qt is compiled with %d." , |
421 | qPrintable(font.familyName()), |
422 | padding, |
423 | QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING); |
424 | } |
425 | |
426 | m_referenceFont.setPixelSize(pixelSize); |
427 | |
428 | quint32 glyphCount = Qtdf::fetch<quint32>(data: qtdfTableStart, offset: Qtdf::numGlyphs); |
429 | m_unusedGlyphs.reserve(asize: glyphCount); |
430 | |
431 | const char *allocatorData = qtdfTableStart + Qtdf::HeaderSize; |
432 | { |
433 | m_areaAllocator = new QSGAreaAllocator(QSize(0, 0)); |
434 | allocatorData = m_areaAllocator->deserialize(data: allocatorData, size: qtdfTableEnd - allocatorData); |
435 | if (allocatorData == nullptr) |
436 | return false; |
437 | } |
438 | |
439 | if (m_areaAllocator->size().height() % m_maxTextureSize != 0) { |
440 | qWarning(msg: "Area allocator size mismatch in '%s'" , qPrintable(font.familyName())); |
441 | return false; |
442 | } |
443 | |
444 | textureCount = m_areaAllocator->size().height() / m_maxTextureSize; |
445 | m_maxTextureCount = qMax(a: m_maxTextureCount, b: textureCount); |
446 | |
447 | const char *textureRecord = allocatorData; |
448 | for (int i = 0; i < textureCount; ++i, textureRecord += Qtdf::TextureRecordSize) { |
449 | if (qtdfTableEnd - textureRecord < Qtdf::TextureRecordSize) { |
450 | qWarning(msg: "qtdf table too small in font '%s'." , |
451 | qPrintable(font.familyName())); |
452 | return false; |
453 | } |
454 | |
455 | TextureInfo *tex = textureInfo(index: i); |
456 | tex->allocatedArea.setX(Qtdf::fetch<quint32>(data: textureRecord, offset: Qtdf::allocatedX)); |
457 | tex->allocatedArea.setY(Qtdf::fetch<quint32>(data: textureRecord, offset: Qtdf::allocatedY)); |
458 | tex->allocatedArea.setWidth(Qtdf::fetch<quint32>(data: textureRecord, offset: Qtdf::allocatedWidth)); |
459 | tex->allocatedArea.setHeight(Qtdf::fetch<quint32>(data: textureRecord, offset: Qtdf::allocatedHeight)); |
460 | tex->padding = Qtdf::fetch<quint8>(data: textureRecord, offset: Qtdf::texturePadding); |
461 | } |
462 | |
463 | const char *glyphRecord = textureRecord; |
464 | for (quint32 i = 0; i < glyphCount; ++i, glyphRecord += Qtdf::GlyphRecordSize) { |
465 | if (qtdfTableEnd - glyphRecord < Qtdf:: GlyphRecordSize) { |
466 | qWarning(msg: "qtdf table too small in font '%s'." , |
467 | qPrintable(font.familyName())); |
468 | return false; |
469 | } |
470 | |
471 | glyph_t glyph = Qtdf::fetch<quint32>(data: glyphRecord, offset: Qtdf::glyphIndex); |
472 | m_unusedGlyphs.insert(value: glyph); |
473 | |
474 | GlyphData &glyphData = emptyData(glyph); |
475 | |
476 | #define FROM_FIXED_POINT(value) \ |
477 | (((qreal)value)/(qreal)65536) |
478 | |
479 | glyphData.texCoord.x = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetX)); |
480 | glyphData.texCoord.y = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetY)); |
481 | glyphData.texCoord.width = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureWidth)); |
482 | glyphData.texCoord.height = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureHeight)); |
483 | glyphData.texCoord.xMargin = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::xMargin)); |
484 | glyphData.texCoord.yMargin = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::yMargin)); |
485 | glyphData.boundingRect.setX(FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectX))); |
486 | glyphData.boundingRect.setY(FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectY))); |
487 | glyphData.boundingRect.setWidth(FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectWidth))); |
488 | glyphData.boundingRect.setHeight(FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectHeight))); |
489 | |
490 | #undef FROM_FIXED_POINT |
491 | |
492 | int textureIndex = Qtdf::fetch<quint16>(data: glyphRecord, offset: Qtdf::textureIndex); |
493 | if (textureIndex < 0 || textureIndex >= textureCount) { |
494 | qWarning(msg: "Invalid texture index %d (texture count == %d) in '%s'" , |
495 | textureIndex, |
496 | textureCount, |
497 | qPrintable(font.familyName())); |
498 | return false; |
499 | } |
500 | |
501 | |
502 | TextureInfo *texInfo = textureInfo(index: textureIndex); |
503 | m_glyphsTexture.insert(akey: glyph, avalue: texInfo); |
504 | |
505 | glyphTextures[texInfo].append(t: glyph); |
506 | } |
507 | |
508 | const uchar *textureData = reinterpret_cast<const uchar *>(glyphRecord); |
509 | for (int i = 0; i < textureCount; ++i) { |
510 | |
511 | TextureInfo *texInfo = textureInfo(index: i); |
512 | |
513 | int width = texInfo->allocatedArea.width(); |
514 | int height = texInfo->allocatedArea.height(); |
515 | qint64 size = qint64(width) * height; |
516 | if (qtdfTableEnd - reinterpret_cast<const char *>(textureData) < size) { |
517 | qWarning(msg: "qtdf table too small in font '%s'." , |
518 | qPrintable(font.familyName())); |
519 | return false; |
520 | } |
521 | |
522 | createTexture(texInfo, width, height, pixels: textureData); |
523 | |
524 | QVector<glyph_t> glyphs = glyphTextures.value(akey: texInfo); |
525 | |
526 | Texture t; |
527 | t.texture = texInfo->texture; |
528 | t.size = texInfo->size; |
529 | t.rhiBased = true; |
530 | |
531 | setGlyphsTexture(glyphs, tex: t); |
532 | |
533 | textureData += size; |
534 | } |
535 | } |
536 | |
537 | if (profile) { |
538 | quint64 now = timer.elapsed(); |
539 | qCDebug(QSG_LOG_TIME_GLYPH, |
540 | "distancefield: %d pre-generated glyphs loaded in %dms" , |
541 | m_unusedGlyphs.size(), |
542 | (int) now); |
543 | } |
544 | |
545 | return true; |
546 | } |
547 | |
548 | void QSGRhiDistanceFieldGlyphCache::commitResourceUpdates(QRhiResourceUpdateBatch *mergeInto) |
549 | { |
550 | if (m_resourceUpdates) { |
551 | mergeInto->merge(other: m_resourceUpdates); |
552 | m_resourceUpdates->release(); |
553 | m_resourceUpdates = nullptr; |
554 | } |
555 | |
556 | // now let's assume the resource updates will be committed in this frame |
557 | for (QRhiTexture *t : m_pendingDispose) |
558 | t->releaseAndDestroyLater(); // will be releaseAndDestroyed after the frame is submitted -> safe |
559 | |
560 | m_pendingDispose.clear(); |
561 | } |
562 | |
563 | bool QSGRhiDistanceFieldGlyphCache::eightBitFormatIsAlphaSwizzled() const |
564 | { |
565 | // return true when the shaders for 8-bit formats need .a instead of .r |
566 | // when sampling the texture |
567 | return !m_rhi->isFeatureSupported(feature: QRhi::RedOrAlpha8IsRed); |
568 | } |
569 | |
570 | QT_END_NAMESPACE |
571 | |