aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scenegraph/d3d12/qsgd3d12glyphcache.cpp
blob: 1f8c525db7ff689e663e3ea243384ccdc51c43c8 (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
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qsgd3d12glyphcache_p.h"
#include "qsgd3d12engine_p.h"

QT_BEGIN_NAMESPACE

// Convert A8 glyphs to 32-bit in the engine. This is here to work around
// QTBUG-55330 for AMD cards.
// If removing, textmask.hlsl must be adjusted! (.a -> .r)
#define ALWAYS_32BIT

// NOTE: Avoid categorized logging. It is slow.

#define DECLARE_DEBUG_VAR(variable) \
    static bool debug_ ## variable() \
    { static bool value = qgetenv("QSG_RENDERER_DEBUG").contains(QT_STRINGIFY(variable)); return value; }

DECLARE_DEBUG_VAR(render)

QSGD3D12GlyphCache::QSGD3D12GlyphCache(QSGD3D12Engine *engine, QFontEngine::GlyphFormat format, const QTransform &matrix)
    : QTextureGlyphCache(format, matrix),
      m_engine(engine)
{
}

QSGD3D12GlyphCache::~QSGD3D12GlyphCache()
{
    if (m_id)
        m_engine->releaseTexture(m_id);
}

void QSGD3D12GlyphCache::createTextureData(int width, int height)
{
    width = qMax(128, width);
    height = qMax(32, height);

    m_id = m_engine->genTexture();
    Q_ASSERT(m_id);

    if (Q_UNLIKELY(debug_render()))
        qDebug("new glyph cache texture %u of size %dx%d, fontengine format %d", m_id, width, height, m_format);

    m_size = QSize(width, height);

    const QImage::Format imageFormat =
            m_format == QFontEngine::Format_A8 ? QImage::Format_Alpha8 : QImage::Format_ARGB32_Premultiplied;
    m_engine->createTexture(m_id, m_size, imageFormat, QSGD3D12Engine::TextureWithAlpha
#ifdef ALWAYS_32BIT
                            | QSGD3D12Engine::TextureAlways32Bit
#endif
                            );
}

void QSGD3D12GlyphCache::resizeTextureData(int width, int height)
{
    width = qMax(128, width);
    height = qMax(32, height);

    if (m_size.width() >= width && m_size.height() >= height)
        return;

    if (Q_UNLIKELY(debug_render()))
        qDebug("glyph cache texture %u resize to %dx%d", m_id, width, height);

    m_size = QSize(width, height);

    m_engine->queueTextureResize(m_id, m_size);
}

void QSGD3D12GlyphCache::beginFillTexture()
{
    Q_ASSERT(m_glyphImages.isEmpty() && m_glyphPos.isEmpty());
}

void QSGD3D12GlyphCache::fillTexture(const Coord &c, glyph_t glyph, QFixed subPixelPosition)
{
    QImage mask = textureMapForGlyph(glyph, subPixelPosition);
    const int maskWidth = mask.width();
    const int maskHeight = mask.height();

    if (mask.format() == QImage::Format_Mono) {
        mask = mask.convertToFormat(QImage::Format_Indexed8);
        for (int y = 0; y < maskHeight; ++y) {
            uchar *src = mask.scanLine(y);
            for (int x = 0; x < maskWidth; ++x)
                src[x] = -src[x]; // convert 0 and 1 into 0 and 255
        }
    } else if (mask.depth() == 32) {
        if (mask.format() == QImage::Format_RGB32) {
            // We need to make the alpha component equal to the average of the RGB values.
            // This is needed when drawing sub-pixel antialiased text on translucent targets.
            for (int y = 0; y < maskHeight; ++y) {
                QRgb *src = reinterpret_cast<QRgb *>(mask.scanLine(y));
                for (int x = 0; x < maskWidth; ++x) {
                    const int r = qRed(src[x]);
                    const int g = qGreen(src[x]);
                    const int b = qBlue(src[x]);
                    int avg;
                    if (mask.format() == QImage::Format_RGB32)
                        avg = (r + g + b + 1) / 3; // "+1" for rounding.
                    else // Format_ARGB32_Premultiplied
                        avg = qAlpha(src[x]);
                    src[x] = qRgba(r, g, b, avg);
                }
            }
        }
    }

    m_glyphImages.append(mask);
    m_glyphPos.append(QPoint(c.x, c.y));
}

void QSGD3D12GlyphCache::endFillTexture()
{
    if (m_glyphImages.isEmpty())
        return;

    Q_ASSERT(m_id);

    m_engine->queueTextureUpload(m_id, m_glyphImages, m_glyphPos
#ifdef ALWAYS_32BIT
                                 , QSGD3D12Engine::TextureUploadAlways32Bit
#endif
                                 );

    // Nothing else left to do, it is up to the text material to call
    // useTexture() which will then add the texture dependency to the frame.

    m_glyphImages.clear();
    m_glyphPos.clear();
}

int QSGD3D12GlyphCache::glyphPadding() const
{
    return 1;
}

int QSGD3D12GlyphCache::maxTextureWidth() const
{
    return 16384;
}

int QSGD3D12GlyphCache::maxTextureHeight() const
{
    return 16384;
}

void QSGD3D12GlyphCache::useTexture()
{
    if (m_id)
        m_engine->useTexture(m_id);
}

QSize QSGD3D12GlyphCache::currentSize() const
{
    return m_size;
}

QT_END_NAMESPACE