8#include <QtGui/private/qdistancefield_p.h>
9#include <QtCore/qelapsedtimer.h>
10#include <QtQml/private/qqmlglobal_p.h>
16DEFINE_BOOL_CONFIG_OPTION(qmlUseGlyphCacheWorkaround, QML_USE_GLYPHCACHE_WORKAROUND)
17DEFINE_BOOL_CONFIG_OPTION(qsgPreferFullSizeGlyphCacheTextures, QSG_PREFER_FULLSIZE_GLYPHCACHE_TEXTURES)
19#if !defined(QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING)
20# define QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING 2
23QSGRhiDistanceFieldGlyphCache::QSGRhiDistanceFieldGlyphCache(QSGDefaultRenderContext *rc,
25 int renderTypeQuality)
26 : QSGDistanceFieldGlyphCache(font, renderTypeQuality)
31 loadPregeneratedCache(font);
34QSGRhiDistanceFieldGlyphCache::~QSGRhiDistanceFieldGlyphCache()
36 for (
const TextureInfo &t : std::as_const(m_textures))
37 m_rc->deferredReleaseGlyphCacheTexture(t.texture);
39 delete m_areaAllocator;
42void QSGRhiDistanceFieldGlyphCache::requestGlyphs(
const QSet<glyph_t> &glyphs)
44 QList<GlyphPosition> glyphPositions;
45 QList<glyph_t> glyphsToRender;
47 if (m_areaAllocator ==
nullptr)
48 m_areaAllocator =
new QSGAreaAllocator(QSize(maxTextureSize(), m_maxTextureCount * maxTextureSize()));
50 for (QSet<glyph_t>::const_iterator it = glyphs.constBegin(); it != glyphs.constEnd() ; ++it) {
51 glyph_t glyphIndex = *it;
54 QRectF boundingRect = glyphData(glyphIndex).boundingRect;
55 int glyphWidth = qCeil(boundingRect.width() + distanceFieldRadius() * 2);
56 int glyphHeight = qCeil(boundingRect.height() + distanceFieldRadius() * 2);
57 QSize glyphSize(glyphWidth + padding * 2, glyphHeight + padding * 2);
58 QRect alloc = m_areaAllocator->allocate(glyphSize);
62 while (alloc.isNull() && !m_unusedGlyphs.isEmpty()) {
63 glyph_t unusedGlyph = *m_unusedGlyphs.constBegin();
65 TexCoord unusedCoord = glyphTexCoord(unusedGlyph);
66 if (!unusedCoord.isNull()) {
67 QRectF unusedGlyphBoundingRect = glyphData(unusedGlyph).boundingRect;
68 int unusedGlyphWidth = qCeil(unusedGlyphBoundingRect.width() + distanceFieldRadius() * 2);
69 int unusedGlyphHeight = qCeil(unusedGlyphBoundingRect.height() + distanceFieldRadius() * 2);
70 m_areaAllocator->deallocate(QRect(unusedCoord.x - padding,
71 unusedCoord.y - padding,
72 padding * 2 + unusedGlyphWidth,
73 padding * 2 + unusedGlyphHeight));
76 m_unusedGlyphs.remove(unusedGlyph);
77 m_glyphsTexture.remove(unusedGlyph);
78 removeGlyph(unusedGlyph);
80 alloc = m_areaAllocator->allocate(glyphSize);
88 TextureInfo *tex = textureInfo(alloc.y() / maxTextureSize());
89 alloc = QRect(alloc.x(), alloc.y() % maxTextureSize(), alloc.width(), alloc.height());
91 tex->allocatedArea |= alloc;
92 Q_ASSERT(tex->padding == padding || tex->padding < 0);
93 tex->padding = padding;
97 p.position = alloc.topLeft() + QPoint(padding, padding);
99 glyphPositions.append(p);
100 glyphsToRender.append(glyphIndex);
101 m_glyphsTexture.insert(glyphIndex, tex);
104 setGlyphsPosition(glyphPositions);
105 markGlyphsToRender(glyphsToRender);
108bool QSGRhiDistanceFieldGlyphCache::isActive()
const
110 return !m_referencedGlyphs.empty();
113void QSGRhiDistanceFieldGlyphCache::storeGlyphs(
const QList<QDistanceField> &glyphs)
115 typedef QHash<TextureInfo *, QList<glyph_t> > GlyphTextureHash;
116 typedef GlyphTextureHash::const_iterator GlyphTextureHashConstIt;
118 GlyphTextureHash glyphTextures;
120 QVarLengthArray<QRhiTextureUploadEntry, 32> uploads;
121 for (
int i = 0; i < glyphs.size(); ++i) {
122 QDistanceField glyph = glyphs.at(i);
123 glyph_t glyphIndex = glyph.glyph();
124 TexCoord c = glyphTexCoord(glyphIndex);
125 TextureInfo *texInfo = m_glyphsTexture.value(glyphIndex);
127 resizeTexture(texInfo, texInfo->allocatedArea.width(), texInfo->allocatedArea.height());
129 glyphTextures[texInfo].append(glyphIndex);
131 int padding = texInfo->padding;
132 int expectedWidth = qCeil(c.width + c.xMargin * 2);
133 glyph = glyph.copy(-padding, -padding,
134 expectedWidth + padding * 2, glyph.height() + padding * 2);
136 if (useTextureResizeWorkaround()) {
137 uchar *inBits = glyph.scanLine(0);
138 uchar *outBits = texInfo->image.scanLine(
int(c.y) - padding) +
int(c.x) - padding;
139 for (
int y = 0; y < glyph.height(); ++y) {
140 memcpy(outBits, inBits, glyph.width());
141 inBits += glyph.width();
142 outBits += texInfo->image.width();
146 QRhiTextureSubresourceUploadDescription subresDesc(glyph.constBits(), glyph.width() * glyph.height());
147 subresDesc.setSourceSize(QSize(glyph.width(), glyph.height()));
148 subresDesc.setDestinationTopLeft(QPoint(c.x - padding, c.y - padding));
149 texInfo->uploads.append(QRhiTextureUploadEntry(0, 0, subresDesc));
152 QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates();
153 for (
int i = 0; i < glyphs.size(); ++i) {
154 TextureInfo *texInfo = m_glyphsTexture.value(glyphs.at(i).glyph());
155 if (!texInfo->uploads.isEmpty()) {
156 QRhiTextureUploadDescription desc;
157 desc.setEntries(texInfo->uploads.cbegin(), texInfo->uploads.cend());
158 resourceUpdates->uploadTexture(texInfo->texture, desc);
159 texInfo->uploads.clear();
163 for (GlyphTextureHashConstIt i = glyphTextures.constBegin(), cend = glyphTextures.constEnd(); i != cend; ++i) {
165 t.texture = i.key()->texture;
166 t.size = i.key()->size;
167 setGlyphsTexture(i.value(), t);
171void QSGRhiDistanceFieldGlyphCache::referenceGlyphs(
const QSet<glyph_t> &glyphs)
173 m_referencedGlyphs += glyphs;
174 m_unusedGlyphs -= glyphs;
177void QSGRhiDistanceFieldGlyphCache::releaseGlyphs(
const QSet<glyph_t> &glyphs)
179 m_referencedGlyphs -= glyphs;
180 m_unusedGlyphs += glyphs;
183void QSGRhiDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo,
187 QByteArray zeroBuf(width * height, 0);
188 createTexture(texInfo, width, height, zeroBuf.constData());
191void QSGRhiDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo,
196 if (useTextureResizeWorkaround() && texInfo->image.isNull()) {
197 texInfo->image = QDistanceField(width, height);
198 memcpy(texInfo->image.bits(), pixels, width * height);
201 texInfo->texture = m_rhi->newTexture(QRhiTexture::RED_OR_ALPHA8, QSize(width, height), 1, QRhiTexture::UsedAsTransferSource);
202 if (texInfo->texture->create()) {
203 QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates();
204 QRhiTextureSubresourceUploadDescription subresDesc(pixels, width * height);
205 subresDesc.setSourceSize(QSize(width, height));
206 resourceUpdates->uploadTexture(texInfo->texture, QRhiTextureUploadEntry(0, 0, subresDesc));
208 qWarning(
"Failed to create distance field glyph cache");
211 texInfo->size = QSize(width, height);
214void QSGRhiDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo,
int width,
int height)
216 int oldWidth = texInfo->size.width();
217 int oldHeight = texInfo->size.height();
218 if (width == oldWidth && height == oldHeight)
221 QRhiTexture *oldTexture = texInfo->texture;
222 createTexture(texInfo, width, height);
227 updateRhiTexture(oldTexture, texInfo->texture, texInfo->size);
229 QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates();
230 if (useTextureResizeWorkaround()) {
231 QRhiTextureSubresourceUploadDescription subresDesc(texInfo->image.constBits(),
232 oldWidth * oldHeight);
233 subresDesc.setSourceSize(QSize(oldWidth, oldHeight));
234 resourceUpdates->uploadTexture(texInfo->texture, QRhiTextureUploadEntry(0, 0, subresDesc));
235 texInfo->image = texInfo->image.copy(0, 0, width, height);
237 resourceUpdates->copyTexture(texInfo->texture, oldTexture);
240 m_rc->deferredReleaseGlyphCacheTexture(oldTexture);
243bool QSGRhiDistanceFieldGlyphCache::useTextureResizeWorkaround()
const
245 static bool set =
false;
246 static bool useWorkaround =
false;
248 useWorkaround = m_rhi->backend() == QRhi::OpenGLES2 || qmlUseGlyphCacheWorkaround();
251 return useWorkaround;
254bool QSGRhiDistanceFieldGlyphCache::createFullSizeTextures()
const
256 return qsgPreferFullSizeGlyphCacheTextures() && glyphCount() > QT_DISTANCEFIELD_HIGHGLYPHCOUNT();
259int QSGRhiDistanceFieldGlyphCache::maxTextureSize()
const
261 if (!m_maxTextureSize)
262 m_maxTextureSize = m_rhi->resourceLimit(QRhi::TextureSizeMax);
263 return m_maxTextureSize;
273 GlyphRecordSize = 46,
274 TextureRecordSize = 17
297 boundingRectWidth = 36,
298 boundingRectHeight = 40,
305 allocatedHeight = 12,
310 template <
typename T>
311 static inline T fetch(
const char *data, Offset offset)
313 return qFromBigEndian<T>(data +
int(offset));
318bool QSGRhiDistanceFieldGlyphCache::loadPregeneratedCache(
const QRawFont &font)
322 if (m_areaAllocator !=
nullptr) {
323 qWarning(
"Font cache must be loaded before cache is used");
327 static QElapsedTimer timer;
329 bool profile = QSG_LOG_TIME_GLYPH().isDebugEnabled();
333 QByteArray qtdfTable = font.fontTable(
"qtdf");
334 if (qtdfTable.isEmpty())
337 typedef QHash<TextureInfo *, QList<glyph_t> > GlyphTextureHash;
339 GlyphTextureHash glyphTextures;
341 if (uint(qtdfTable.size()) < Qtdf::HeaderSize) {
342 qWarning(
"Invalid qtdf table in font '%s'",
343 qPrintable(font.familyName()));
347 const char *qtdfTableStart = qtdfTable.constData();
348 const char *qtdfTableEnd = qtdfTableStart + qtdfTable.size();
351 int textureCount = 0;
353 quint8 majorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::majorVersion);
354 quint8 minorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::minorVersion);
355 if (majorVersion != 5 || minorVersion != 12) {
356 qWarning(
"Invalid version of qtdf table %d.%d in font '%s'",
359 qPrintable(font.familyName()));
363 qreal pixelSize = qreal(Qtdf::fetch<quint16>(qtdfTableStart, Qtdf::pixelSize));
364 m_maxTextureSize = Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::textureSize);
365 m_doubleGlyphResolution = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::flags) == 1;
366 padding = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::headerPadding);
368 if (pixelSize <= 0.0) {
369 qWarning(
"Invalid pixel size in '%s'", qPrintable(font.familyName()));
373 if (m_maxTextureSize <= 0) {
374 qWarning(
"Invalid texture size in '%s'", qPrintable(font.familyName()));
378 int systemMaxTextureSize = m_rhi->resourceLimit(QRhi::TextureSizeMax);
380 if (m_maxTextureSize > systemMaxTextureSize) {
381 qWarning(
"System maximum texture size is %d. This is lower than the value in '%s', which is %d",
382 systemMaxTextureSize,
383 qPrintable(font.familyName()),
388 qWarning(
"Padding mismatch in '%s'. Font requires %d, but Qt is compiled with %d.",
389 qPrintable(font.familyName()),
394 m_referenceFont.setPixelSize(pixelSize);
396 quint32 glyphCount = Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::numGlyphs);
397 m_unusedGlyphs.reserve(glyphCount);
399 const char *allocatorData = qtdfTableStart + Qtdf::HeaderSize;
401 m_areaAllocator =
new QSGAreaAllocator(QSize(0, 0));
402 allocatorData = m_areaAllocator->deserialize(allocatorData, qtdfTableEnd - allocatorData);
403 if (allocatorData ==
nullptr)
407 if (m_areaAllocator->size().height() % m_maxTextureSize != 0) {
408 qWarning(
"Area allocator size mismatch in '%s'", qPrintable(font.familyName()));
412 textureCount = m_areaAllocator->size().height() / m_maxTextureSize;
413 m_maxTextureCount = qMax(m_maxTextureCount, textureCount);
415 const char *textureRecord = allocatorData;
416 for (
int i = 0; i < textureCount; ++i, textureRecord += Qtdf::TextureRecordSize) {
417 if (qtdfTableEnd - textureRecord < Qtdf::TextureRecordSize) {
418 qWarning(
"qtdf table too small in font '%s'.",
419 qPrintable(font.familyName()));
423 TextureInfo *tex = textureInfo(i);
424 tex->allocatedArea.setX(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedX));
425 tex->allocatedArea.setY(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedY));
426 tex->allocatedArea.setWidth(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedWidth));
427 tex->allocatedArea.setHeight(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedHeight));
428 tex->padding = Qtdf::fetch<quint8>(textureRecord, Qtdf::texturePadding);
431 const char *glyphRecord = textureRecord;
432 for (quint32 i = 0; i < glyphCount; ++i, glyphRecord += Qtdf::GlyphRecordSize) {
433 if (qtdfTableEnd - glyphRecord < Qtdf:: GlyphRecordSize) {
434 qWarning(
"qtdf table too small in font '%s'.",
435 qPrintable(font.familyName()));
439 glyph_t glyph = Qtdf::fetch<quint32>(glyphRecord, Qtdf::glyphIndex);
440 m_unusedGlyphs.insert(glyph);
442 GlyphData &glyphData = emptyData(glyph);
444#define FROM_FIXED_POINT(value) \
445(((qreal)value)/(qreal)65536)
447 glyphData.texCoord.x =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetX));
448 glyphData.texCoord.y =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetY));
449 glyphData.texCoord.width =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureWidth));
450 glyphData.texCoord.height =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureHeight));
451 glyphData.texCoord.xMargin =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::xMargin));
452 glyphData.texCoord.yMargin =
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::yMargin));
453 glyphData.boundingRect.setX(
FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectX)));
454 glyphData.boundingRect.setY(
FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectY)));
455 glyphData.boundingRect.setWidth(
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectWidth)));
456 glyphData.boundingRect.setHeight(
FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectHeight)));
458#undef FROM_FIXED_POINT
460 int textureIndex = Qtdf::fetch<quint16>(glyphRecord, Qtdf::textureIndex);
461 if (textureIndex < 0 || textureIndex >= textureCount) {
462 qWarning(
"Invalid texture index %d (texture count == %d) in '%s'",
465 qPrintable(font.familyName()));
470 TextureInfo *texInfo = textureInfo(textureIndex);
471 m_glyphsTexture.insert(glyph, texInfo);
473 glyphTextures[texInfo].append(glyph);
476 const uchar *textureData =
reinterpret_cast<
const uchar *>(glyphRecord);
477 for (
int i = 0; i < textureCount; ++i) {
479 TextureInfo *texInfo = textureInfo(i);
481 int width = texInfo->allocatedArea.width();
482 int height = texInfo->allocatedArea.height();
483 qint64 size = qint64(width) * height;
484 if (qtdfTableEnd -
reinterpret_cast<
const char *>(textureData) < size) {
485 qWarning(
"qtdf table too small in font '%s'.",
486 qPrintable(font.familyName()));
490 createTexture(texInfo, width, height, textureData);
492 QList<glyph_t> glyphs = glyphTextures.value(texInfo);
495 t.texture = texInfo->texture;
496 t.size = texInfo->size;
498 setGlyphsTexture(glyphs, t);
505 quint64 now = timer.elapsed();
506 qCDebug(QSG_LOG_TIME_GLYPH,
507 "distancefield: %d pre-generated glyphs loaded in %dms",
508 int(m_unusedGlyphs.size()),
515void QSGRhiDistanceFieldGlyphCache::commitResourceUpdates(QRhiResourceUpdateBatch *mergeInto)
517 if (QRhiResourceUpdateBatch *resourceUpdates = m_rc->maybeGlyphCacheResourceUpdates()) {
518 mergeInto->merge(resourceUpdates);
519 m_rc->resetGlyphCacheResources();
523bool QSGRhiDistanceFieldGlyphCache::eightBitFormatIsAlphaSwizzled()
const
527 return !m_rhi->isFeatureSupported(QRhi::RedOrAlpha8IsRed);
530bool QSGRhiDistanceFieldGlyphCache::screenSpaceDerivativesSupported()
const
532 return m_rhi->isFeatureSupported(QRhi::ScreenSpaceDerivatives);
535#if defined(QSG_DISTANCEFIELD_CACHE_DEBUG)
536void QSGRhiDistanceFieldGlyphCache::saveTexture(QRhiTexture *texture,
const QString &nameBase)
const
538 quint64 textureId = texture->nativeTexture().object;
539 QString fileName = nameBase + QLatin1Char(
'_') + QString::number(textureId, 16);
540 fileName.replace(QLatin1Char(
'/'), QLatin1Char(
'_'));
541 fileName.replace(QLatin1Char(
' '), QLatin1Char(
'_'));
542 fileName.append(QLatin1String(
".png"));
544 QRhiReadbackResult *rbResult =
new QRhiReadbackResult;
545 rbResult->completed = [rbResult, fileName] {
546 const QSize size = rbResult->pixelSize;
547 const qint64 numPixels = qint64(size.width()) * size.height();
548 if (numPixels == rbResult->data.size()) {
550 QImage image(size, QImage::Format_Grayscale8);
551 const char *p = rbResult->data.constData();
552 for (
int i = 0; i < size.height(); i++)
553 memcpy(image.scanLine(i), p + (i * size.width()), size.width());
554 image.save(fileName);
555 }
else if (4 * numPixels == rbResult->data.size()) {
557 const uchar *p =
reinterpret_cast<
const uchar *>(rbResult->data.constData());
558 QImage image(p, size.width(), size.height(), QImage::Format_RGBA8888);
559 image.save(fileName);
561 qWarning(
"Unhandled data format in glyph texture");
566 QRhiReadbackDescription rb(texture);
567 QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates();
568 resourceUpdates->readBackTexture(rb, rbResult);
Combined button and popup list for selecting options.
#define QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING
#define FROM_FIXED_POINT(value)