summaryrefslogtreecommitdiffstats
path: root/src/runtimerender/Qt3DSDistanceFieldGlyphCache.cpp
blob: b919ca526d0feea3036551a59080e226948652d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSDistanceFieldGlyphCache_p.h"

#include <QtQuick/private/qsgareaallocator_p.h>

#include <QtCore/qmath.h>
#include <QtCore/qendian.h>
#include <QtGui/qimage.h>

#include "render/Qt3DSRenderContext.h"
#include "render/Qt3DSRenderBaseTypes.h"
#include "Qt3DSRenderResourceManager.h"
#include "foundation/Qt3DSAllocator.h"

#if QT_VERSION >= QT_VERSION_CHECK(5,12,2)

QT_BEGIN_NAMESPACE

// Should work on most hardware. Used as stop gap until Qt 3D provides a
// way to retrieve the system value
#ifndef Q3DSDISTANCEFIELDGLYPHCACHE_MAXIMUM_TEXURE_SIZE
#  define Q3DSDISTANCEFIELDGLYPHCACHE_MAXIMUM_TEXURE_SIZE 2048
#endif

#if !defined(Q3DSDISTANCEFIELDGLYPHCACHE_PADDING)
#  define Q3DSDISTANCEFIELDGLYPHCACHE_PADDING 2
#endif

Q3DSDistanceFieldGlyphCache::Q3DSDistanceFieldGlyphCache(
        const QRawFont &font, qt3ds::render::IQt3DSRenderContext &context)
    : QSGDistanceFieldGlyphCache(font)
    , m_context(context)
{
    m_maxTextureSize = Q3DSDISTANCEFIELDGLYPHCACHE_MAXIMUM_TEXURE_SIZE;

    loadPregeneratedCache(font);
}

Q3DSDistanceFieldGlyphCache::~Q3DSDistanceFieldGlyphCache()
{
    for (auto &texture : m_textures)
        qt3ds::foundation::NVDelete(m_context.GetAllocator(), texture.texture);
    delete m_areaAllocator;
}

int Q3DSDistanceFieldGlyphCache::maxTextureSize() const
{
    return m_maxTextureSize;
}

Q3DSDistanceFieldGlyphCache::TextureInfo *Q3DSDistanceFieldGlyphCache::textureInfo(int index) const
{
    while (index >= m_textures.size())
        m_textures.append(TextureInfo());
    return &m_textures[index];
}

void Q3DSDistanceFieldGlyphCache::referenceGlyphs(const QSet<glyph_t> &glyphs)
{
    m_unusedGlyphs -= glyphs;
}

void Q3DSDistanceFieldGlyphCache::releaseGlyphs(const QSet<glyph_t> &glyphs)
{
    m_unusedGlyphs += glyphs;
}

void Q3DSDistanceFieldGlyphCache::setTextureData(qt3ds::render::NVRenderTexture2D *texture,
                                                 QImage &image)
{
    bool isGLES2 = m_context.GetRenderContext().GetRenderContextType()
            == qt3ds::render::NVRenderContextValues::GLES2;
    texture->SetTextureData(qt3ds::render::toU8DataRef(image.bits(), image.byteCount()),
                            0, image.width(), image.height(),
                            isGLES2 ? qt3ds::render::NVRenderTextureFormats::Alpha8
                                    : qt3ds::render::NVRenderTextureFormats::R8);
}

void Q3DSDistanceFieldGlyphCache::resizeTexture(TextureInfo *info, int width, int height)
{
    QImage &image = info->copy;
    if (info->texture == nullptr) {
        info->texture = m_context.GetRenderContext().CreateTexture2D();
        info->texture->SetMinFilter(qt3ds::render::NVRenderTextureMinifyingOp::Enum::Linear);
        info->texture->SetMagFilter(qt3ds::render::NVRenderTextureMagnifyingOp::Enum::Linear);
        setTextureData(info->texture, image);
    }

    qt3ds::render::STextureDetails textureDetails = info->texture->GetTextureDetails();
    if (int(textureDetails.m_Width) != width || int(textureDetails.m_Height) != height)
        setTextureData(info->texture, image);

    if (info->copy.width() != width || info->copy.height() != height) {
        QImage newImage(width, height, QImage::Format_Alpha8);

        for (int y = 0; y < info->copy.height(); ++y) {
            uchar *dest = newImage.scanLine(y);
            const uchar *src = info->copy.scanLine(y);
            ::memcpy(dest, src, size_t(info->copy.width()));
        }

        info->copy = newImage;

        setTextureData(info->texture, image);
    }
}

void Q3DSDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField> &glyphs)
{
    using GlyphTextureHash = QHash<TextureInfo *, QVector<glyph_t> >;
    using GlyphTextureHashConstIt = GlyphTextureHash::const_iterator;

    GlyphTextureHash glyphTextures;
    for (int i = 0; i < glyphs.size(); ++i) {
        QDistanceField glyph = glyphs.at(i);
        glyph_t glyphIndex = glyph.glyph();
        TexCoord c = glyphTexCoord(glyphIndex);
        TextureInfo *texInfo = m_glyphsTexture.value(glyphIndex);

        resizeTexture(texInfo, maxTextureSize(), texInfo->allocatedArea.height());

        Q_ASSERT(!glyphTextures[texInfo].contains(glyphIndex));
        glyphTextures[texInfo].append(glyphIndex);

        int padding = texInfo->padding;
        int expectedWidth = qCeil(c.width + c.xMargin * 2);
        glyph = glyph.copy(-padding, -padding,
                           expectedWidth + padding * 2, glyph.height() + padding * 2);

        for (int y = 0; y < glyph.height(); ++y) {
            const uchar *src = glyph.scanLine(y);
            uchar *dest = texInfo->copy.scanLine(int(c.y) + y - padding) + (int(c.x) - padding);
            ::memcpy(dest, src, size_t(glyph.width()));
        }
    }

    for (GlyphTextureHashConstIt i = glyphTextures.constBegin(),
         cend = glyphTextures.constEnd(); i != cend; ++i) {
        Texture t;
         // 0 == empty texture, (i - 1) == index into m_textures
        t.textureId = uint(i.key() - m_textures.constData()) + 1;
        qt3ds::render::STextureDetails textureDetails = i.key()->texture->GetTextureDetails();
        t.size = QSize(textureDetails.m_Width, textureDetails.m_Height);
        setGlyphsTexture(i.value(), t);

        QImage &image = i.key()->copy;

        setTextureData(i.key()->texture, image);
    }
}

void Q3DSDistanceFieldGlyphCache::requestGlyphs(const QSet<glyph_t> &glyphs)
{
    // Note: Most of this is copy-pasted from QSGDefaultDistanceFieldGlyphCache in Qt Quick.
    // All of this can probably be shared as a default implementation, since it does not
    // actually create any textures, but it might have to be either templated or based
    // on void*. For now we will just live with the duplication.

    QList<GlyphPosition> glyphPositions;
    QVector<glyph_t> glyphsToRender;

    if (m_areaAllocator == nullptr) {
        m_areaAllocator = new QSGAreaAllocator(QSize(maxTextureSize(),
                                                     m_maxTextureCount * maxTextureSize()));
    }

    for (QSet<glyph_t>::const_iterator it = glyphs.constBegin(); it != glyphs.constEnd() ; ++it) {
        glyph_t glyphIndex = *it;

        int padding = Q3DSDISTANCEFIELDGLYPHCACHE_PADDING;
        QRectF boundingRect = glyphData(glyphIndex).boundingRect;
        int glyphWidth = qCeil(boundingRect.width()) + distanceFieldRadius() * 2;
        int glyphHeight = qCeil(boundingRect.height()) + distanceFieldRadius() * 2;
        QSize glyphSize(glyphWidth + padding * 2, glyphHeight + padding * 2);
        QRect alloc = m_areaAllocator->allocate(glyphSize);

        if (alloc.isNull()) {
            // Unallocate unused glyphs until we can allocated the new glyph
            while (alloc.isNull() && !m_unusedGlyphs.isEmpty()) {
                glyph_t unusedGlyph = *m_unusedGlyphs.constBegin();

                TexCoord unusedCoord = glyphTexCoord(unusedGlyph);
                QRectF unusedGlyphBoundingRect = glyphData(unusedGlyph).boundingRect;
                int unusedGlyphWidth = qCeil(unusedGlyphBoundingRect.width())
                        + distanceFieldRadius() * 2;
                int unusedGlyphHeight = qCeil(unusedGlyphBoundingRect.height())
                        + distanceFieldRadius() * 2;
                m_areaAllocator->deallocate(QRect(int(unusedCoord.x) - padding,
                                                  int(unusedCoord.y) - padding,
                                                  padding * 2 + unusedGlyphWidth,
                                                  padding * 2 + unusedGlyphHeight));

                m_unusedGlyphs.remove(unusedGlyph);
                m_glyphsTexture.remove(unusedGlyph);
                removeGlyph(unusedGlyph);

                alloc = m_areaAllocator->allocate(glyphSize);
            }

            // Not enough space left for this glyph... skip to the next one
            if (alloc.isNull())
                continue;
        }

        TextureInfo *tex = textureInfo(alloc.y() / maxTextureSize());
        alloc = QRect(alloc.x(), alloc.y() % maxTextureSize(), alloc.width(), alloc.height());

        tex->allocatedArea |= alloc;
        Q_ASSERT(tex->padding == padding || tex->padding < 0);
        tex->padding = padding;

        GlyphPosition p;
        p.glyph = glyphIndex;
        p.position = alloc.topLeft() + QPoint(padding, padding);

        glyphPositions.append(p);
        glyphsToRender.append(glyphIndex);
        m_glyphsTexture.insert(glyphIndex, tex);
    }

    setGlyphsPosition(glyphPositions);
    markGlyphsToRender(glyphsToRender);
}

void Q3DSDistanceFieldGlyphCache::processPendingGlyphs()
{
    update();
}

Q3DSDistanceFieldGlyphCache::TextureInfo *Q3DSDistanceFieldGlyphCache::textureInfoById(
        uint id) const
{
    Q_ASSERT(id > 0);
    return textureInfo(id - 1);
}

// This is all copy-pasted from Qt Quick, as sharing it would require some refactoring, and we
// need to work with Qt 5.12.2 at the moment.
namespace {
    struct Qtdf {
        // We need these structs to be tightly packed, but some compilers we use do not
        // support #pragma pack(1), so we need to hardcode the offsets/sizes in the
        // file format
        enum TableSize {
            HeaderSize        = 14,
            GlyphRecordSize   = 46,
            TextureRecordSize = 17
        };

        enum Offset {
            // Header
            majorVersion       = 0,
            minorVersion       = 1,
            pixelSize          = 2,
            textureSize        = 4,
            flags              = 8,
            headerPadding      = 9,
            numGlyphs          = 10,

            // Glyph record
            glyphIndex         = 0,
            textureOffsetX     = 4,
            textureOffsetY     = 8,
            textureWidth       = 12,
            textureHeight      = 16,
            xMargin            = 20,
            yMargin            = 24,
            boundingRectX      = 28,
            boundingRectY      = 32,
            boundingRectWidth  = 36,
            boundingRectHeight = 40,
            textureIndex       = 44,

            // Texture record
            allocatedX         = 0,
            allocatedY         = 4,
            allocatedWidth     = 8,
            allocatedHeight    = 12,
            texturePadding     = 16

        };

        template <typename T>
        static inline T fetch(const char *data, Offset offset)
        {
            return qFromBigEndian<T>(data + int(offset));
        }
    };
}

qreal Q3DSDistanceFieldGlyphCache::fontSize() const
{
    return QT_DISTANCEFIELD_BASEFONTSIZE(m_doubleGlyphResolution);
}

bool Q3DSDistanceFieldGlyphCache::loadPregeneratedCache(const QRawFont &font)
{
    // The pregenerated data must be loaded first, otherwise the area allocator
    // will be wrong
    if (m_areaAllocator != nullptr) {
        qWarning("Font cache must be loaded before cache is used");
        return false;
    }

    QByteArray qtdfTable = font.fontTable("qtdf");
    if (qtdfTable.isEmpty())
        return false;

    using GlyphTextureHash = QHash<TextureInfo *, QVector<glyph_t> >;

    GlyphTextureHash glyphTextures;

    if (uint(qtdfTable.size()) < Qtdf::HeaderSize) {
        qWarning("Invalid qtdf table in font '%s'",
                 qPrintable(font.familyName()));
        return false;
    }

    const char *qtdfTableStart = qtdfTable.constData();
    const char *qtdfTableEnd = qtdfTableStart + qtdfTable.size();

    int padding = 0;
    int textureCount = 0;
    {
        quint8 majorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::majorVersion);
        quint8 minorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::minorVersion);
        if (majorVersion != 5 || minorVersion != 12) {
            qWarning("Invalid version of qtdf table %d.%d in font '%s'",
                     majorVersion,
                     minorVersion,
                     qPrintable(font.familyName()));
            return false;
        }

        qreal pixelSize = qreal(Qtdf::fetch<quint16>(qtdfTableStart, Qtdf::pixelSize));
        m_maxTextureSize = int(Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::textureSize));
        m_doubleGlyphResolution = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::flags) == 1;
        padding = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::headerPadding);

        if (pixelSize <= 0.0) {
            qWarning("Invalid pixel size in '%s'", qPrintable(font.familyName()));
            return false;
        }

        if (m_maxTextureSize <= 0) {
            qWarning("Invalid texture size in '%s'", qPrintable(font.familyName()));
            return false;
        }

        if (padding != Q3DSDISTANCEFIELDGLYPHCACHE_PADDING) {
            qWarning("Padding mismatch in '%s'. Font requires %d, but Qt is compiled with %d.",
                     qPrintable(font.familyName()),
                     padding,
                     Q3DSDISTANCEFIELDGLYPHCACHE_PADDING);
        }

        m_referenceFont.setPixelSize(pixelSize);

        quint32 glyphCount = Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::numGlyphs);
        m_unusedGlyphs.reserve(int(glyphCount));

        const char *allocatorData = qtdfTableStart + Qtdf::HeaderSize;
        {
            m_areaAllocator = new QSGAreaAllocator(QSize(0, 0));
            allocatorData = m_areaAllocator->deserialize(allocatorData,
                                                         qtdfTableEnd - allocatorData);
            if (allocatorData == nullptr)
                return false;
        }

        if (m_areaAllocator->size().height() % m_maxTextureSize != 0) {
            qWarning("Area allocator size mismatch in '%s'", qPrintable(font.familyName()));
            return false;
        }

        textureCount = m_areaAllocator->size().height() / m_maxTextureSize;
        m_maxTextureCount = qMax(m_maxTextureCount, textureCount);

        const char *textureRecord = allocatorData;
        for (int i = 0; i < textureCount; ++i, textureRecord += Qtdf::TextureRecordSize) {
            if (textureRecord + Qtdf::TextureRecordSize > qtdfTableEnd) {
                qWarning("qtdf table too small in font '%s'.",
                         qPrintable(font.familyName()));
                return false;
            }

            TextureInfo *tex = textureInfo(i);
            tex->allocatedArea.setX(int(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedX)));
            tex->allocatedArea.setY(int(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedY)));
            tex->allocatedArea.setWidth(int(Qtdf::fetch<quint32>(textureRecord,
                                                                 Qtdf::allocatedWidth)));
            tex->allocatedArea.setHeight(int(Qtdf::fetch<quint32>(textureRecord,
                                                                  Qtdf::allocatedHeight)));
            tex->padding = Qtdf::fetch<quint8>(textureRecord, Qtdf::texturePadding);
        }

        const char *glyphRecord = textureRecord;
        for (quint32 i = 0; i < glyphCount; ++i, glyphRecord += Qtdf::GlyphRecordSize) {
            if (glyphRecord + Qtdf::GlyphRecordSize > qtdfTableEnd) {
                qWarning("qtdf table too small in font '%s'.",
                         qPrintable(font.familyName()));
                return false;
            }

            glyph_t glyph = Qtdf::fetch<quint32>(glyphRecord, Qtdf::glyphIndex);
            m_unusedGlyphs.insert(glyph);

            GlyphData &glyphData = emptyData(glyph);

#define FROM_FIXED_POINT(value) (qreal(value)/qreal(65536))

            glyphData.texCoord.x
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetX));
            glyphData.texCoord.y
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetY));
            glyphData.texCoord.width
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureWidth));
            glyphData.texCoord.height
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureHeight));
            glyphData.texCoord.xMargin
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::xMargin));
            glyphData.texCoord.yMargin
                    = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::yMargin));
            glyphData.boundingRect.setX(
                        FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectX)));
            glyphData.boundingRect.setY(
                        FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectY)));
            glyphData.boundingRect.setWidth(
                        FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord,
                                                              Qtdf::boundingRectWidth)));
            glyphData.boundingRect.setHeight(
                        FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord,
                                                              Qtdf::boundingRectHeight)));

#undef FROM_FIXED_POINT

            int textureIndex = Qtdf::fetch<quint16>(glyphRecord, Qtdf::textureIndex);
            if (textureIndex < 0 || textureIndex >= textureCount) {
                qWarning("Invalid texture index %d (texture count == %d) in '%s'",
                         textureIndex,
                         textureCount,
                         qPrintable(font.familyName()));
                return false;
            }


            TextureInfo *texInfo = textureInfo(textureIndex);
            m_glyphsTexture.insert(glyph, texInfo);

            glyphTextures[texInfo].append(glyph);
        }

        const uchar *textureData = reinterpret_cast<const uchar *>(glyphRecord);
        for (int i = 0; i < textureCount; ++i) {

            TextureInfo *texInfo = textureInfo(i);

            int width = texInfo->allocatedArea.width();
            int height = texInfo->allocatedArea.height();
            qint64 size = width * height;
            if (reinterpret_cast<const char *>(textureData + size) > qtdfTableEnd) {
                qWarning("qtdf table too small in font '%s'.",
                         qPrintable(font.familyName()));
                return false;
            }

            resizeTexture(texInfo, width, height);

            memcpy(texInfo->copy.bits(), textureData, size);
            textureData += size;

            QImage &image = texInfo->copy;
            setTextureData(texInfo->texture, image);

            QVector<glyph_t> glyphs = glyphTextures.value(texInfo);

            Texture t;
            t.textureId = uint(i + 1);
            t.size = texInfo->copy.size();

            setGlyphsTexture(glyphs, t);
        }
    }

    return true;
}

#if QT_VERSION >= QT_VERSION_CHECK(5,14,0)
bool Q3DSDistanceFieldGlyphCache::eightBitFormatIsAlphaSwizzled() const
{
    return m_context.GetRenderContext().GetRenderContextType()
        == qt3ds::render::NVRenderContextValues::GLES2;
}
#endif // QT_VERSION >= QT_VERSION_CHECK(5,14,0)

QT_END_NAMESPACE

#endif // QT_VERSION >= QT_VERSION_CHECK(5,12,2)