Coverage Report

Created: 2025-09-27 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/painting/qrgba64_p.h
Line
Count
Source
1
/****************************************************************************
2
**
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the QtGui 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
#ifndef QRGBA64_P_H
41
#define QRGBA64_P_H
42
43
//
44
//  W A R N I N G
45
//  -------------
46
//
47
// This file is not part of the Qt API.  It exists purely as an
48
// implementation detail.  This header file may change from version to
49
// version without notice, or even be removed.
50
//
51
// We mean it.
52
//
53
54
#include "qrgba64.h"
55
#include "qdrawhelper_p.h"
56
57
#include <QtCore/private/qsimd_p.h>
58
#include <QtGui/private/qtguiglobal_p.h>
59
60
QT_BEGIN_NAMESPACE
61
62
inline QRgba64 combineAlpha256(QRgba64 rgba64, uint alpha256)
63
3.14k
{
64
3.14k
    return QRgba64::fromRgba64(rgba64.red(), rgba64.green(), rgba64.blue(), (rgba64.alpha() * alpha256) >> 8);
65
3.14k
}
66
67
inline QRgba64 multiplyAlpha65535(QRgba64 rgba64, uint alpha65535)
68
0
{
69
0
    return QRgba64::fromRgba64(qt_div_65535(rgba64.red()   * alpha65535),
70
0
                               qt_div_65535(rgba64.green() * alpha65535),
71
0
                               qt_div_65535(rgba64.blue()  * alpha65535),
72
0
                               qt_div_65535(rgba64.alpha() * alpha65535));
73
0
}
74
75
#ifdef __SSE2__
76
Q_ALWAYS_INLINE __m128i multiplyAlpha65535(__m128i rgba64, __m128i va)
77
0
{
78
0
    __m128i vs = rgba64;
79
0
    vs = _mm_unpacklo_epi16(_mm_mullo_epi16(vs, va), _mm_mulhi_epu16(vs, va));
80
0
    vs = _mm_add_epi32(vs, _mm_srli_epi32(vs, 16));
81
0
    vs = _mm_add_epi32(vs, _mm_set1_epi32(0x8000));
82
0
    vs = _mm_srai_epi32(vs, 16);
83
0
    vs = _mm_packs_epi32(vs, _mm_setzero_si128());
84
0
    return vs;
85
0
}
86
Q_ALWAYS_INLINE __m128i multiplyAlpha65535(__m128i rgba64, uint alpha65535)
87
0
{
88
0
    const __m128i va = _mm_shufflelo_epi16(_mm_cvtsi32_si128(alpha65535), _MM_SHUFFLE(0, 0, 0, 0));
89
0
    return multiplyAlpha65535(rgba64, va);
90
0
}
91
#endif
92
93
#if defined(__ARM_NEON__)
94
Q_ALWAYS_INLINE uint16x4_t multiplyAlpha65535(uint16x4_t rgba64, uint16x4_t alpha65535)
95
{
96
    uint32x4_t vs32 = vmull_u16(rgba64, alpha65535); // vs = vs * alpha
97
    vs32 = vsraq_n_u32(vs32, vs32, 16); // vs = vs + (vs >> 16)
98
    return vrshrn_n_u32(vs32, 16); // vs = (vs + 0x8000) >> 16
99
}
100
Q_ALWAYS_INLINE uint16x4_t multiplyAlpha65535(uint16x4_t rgba64, uint alpha65535)
101
{
102
    uint32x4_t vs32 = vmull_n_u16(rgba64, alpha65535); // vs = vs * alpha
103
    vs32 = vsraq_n_u32(vs32, vs32, 16); // vs = vs + (vs >> 16)
104
    return vrshrn_n_u32(vs32, 16); // vs = (vs + 0x8000) >> 16
105
}
106
#endif
107
108
template<typename T>
109
inline T multiplyAlpha255(T rgba64, uint alpha255)
110
0
{
111
0
#if defined(__SSE2__) || defined(__ARM_NEON__)
112
0
    return multiplyAlpha65535(rgba64, alpha255 * 257);
113
#else
114
    return QRgba64::fromRgba64(qt_div_255(rgba64.red()   * alpha255),
115
                               qt_div_255(rgba64.green() * alpha255),
116
                               qt_div_255(rgba64.blue()  * alpha255),
117
                               qt_div_255(rgba64.alpha() * alpha255));
118
#endif
119
0
}
Unexecuted instantiation: QRgba64 multiplyAlpha255<QRgba64>(QRgba64, unsigned int)
Unexecuted instantiation: long long __vector(2) multiplyAlpha255<long long __vector(2)>(long long __vector(2), unsigned int)
120
121
inline QRgba64 interpolate255(QRgba64 x, uint alpha1, QRgba64 y, uint alpha2)
122
0
{
123
0
    return QRgba64::fromRgba64(multiplyAlpha255(x, alpha1) + multiplyAlpha255(y, alpha2));
124
0
}
125
126
#if defined __SSE2__
127
Q_ALWAYS_INLINE __m128i interpolate255(__m128i x, uint alpha1, __m128i y, uint alpha2)
128
0
{
129
0
    return _mm_add_epi32(multiplyAlpha255(x, alpha1), multiplyAlpha255(y, alpha2));
130
0
}
131
#endif
132
133
#if defined __ARM_NEON__
134
Q_ALWAYS_INLINE uint16x4_t interpolate255(uint16x4_t x, uint alpha1, uint16x4_t y, uint alpha2)
135
{
136
    return vadd_u16(multiplyAlpha255(x, alpha1), multiplyAlpha255(y, alpha2));
137
}
138
#endif
139
140
inline QRgba64 interpolate65535(QRgba64 x, uint alpha1, QRgba64 y, uint alpha2)
141
0
{
142
0
    return QRgba64::fromRgba64(multiplyAlpha65535(x, alpha1) + multiplyAlpha65535(y, alpha2));
143
0
}
144
145
#if defined __SSE2__
146
Q_ALWAYS_INLINE __m128i interpolate65535(__m128i x, uint alpha1, __m128i y, uint alpha2)
147
0
{
148
0
    return _mm_add_epi32(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
149
0
}
150
// alpha2 below is const-ref because otherwise MSVC2015 complains that it can't 16-byte align the argument.
151
Q_ALWAYS_INLINE __m128i interpolate65535(__m128i x, __m128i alpha1, __m128i y, const __m128i &alpha2)
152
0
{
153
0
    return _mm_add_epi32(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
154
0
}
155
#endif
156
157
#if defined __ARM_NEON__
158
Q_ALWAYS_INLINE uint16x4_t interpolate65535(uint16x4_t x, uint alpha1, uint16x4_t y, uint alpha2)
159
{
160
    return vadd_u16(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
161
}
162
Q_ALWAYS_INLINE uint16x4_t interpolate65535(uint16x4_t x, uint16x4_t alpha1, uint16x4_t y, uint16x4_t alpha2)
163
{
164
    return vadd_u16(multiplyAlpha65535(x, alpha1), multiplyAlpha65535(y, alpha2));
165
}
166
#endif
167
168
inline QRgba64 addWithSaturation(QRgba64 a, QRgba64 b)
169
0
{
170
0
    return QRgba64::fromRgba64(qMin(a.red() + b.red(), 65535),
171
0
                               qMin(a.green() + b.green(), 65535),
172
0
                               qMin(a.blue() + b.blue(), 65535),
173
0
                               qMin(a.alpha() + b.alpha(), 65535));
174
0
}
175
176
#if QT_COMPILER_SUPPORTS_HERE(SSE2)
177
QT_FUNCTION_TARGET(SSE2)
178
Q_ALWAYS_INLINE uint toArgb32(__m128i v)
179
0
{
180
0
    v = _mm_unpacklo_epi16(v, _mm_setzero_si128());
181
0
    v = _mm_add_epi32(v, _mm_set1_epi32(128));
182
0
    v = _mm_sub_epi32(v, _mm_srli_epi32(v, 8));
183
0
    v = _mm_srli_epi32(v, 8);
184
0
    v = _mm_packs_epi32(v, v);
185
0
    v = _mm_packus_epi16(v, v);
186
0
    return _mm_cvtsi128_si32(v);
187
0
}
188
#elif defined __ARM_NEON__
189
Q_ALWAYS_INLINE uint toArgb32(uint16x4_t v)
190
{
191
    v = vsub_u16(v, vrshr_n_u16(v, 8));
192
    v = vrshr_n_u16(v, 8);
193
    uint8x8_t v8 = vmovn_u16(vcombine_u16(v, v));
194
    return vget_lane_u32(vreinterpret_u32_u8(v8), 0);
195
}
196
#endif
197
198
Q_ALWAYS_INLINE uint toArgb32(QRgba64 rgba64)
199
0
{
200
0
#if defined __SSE2__
201
0
    __m128i v = _mm_loadl_epi64((const __m128i *)&rgba64);
202
0
    v = _mm_shufflelo_epi16(v, _MM_SHUFFLE(3, 0, 1, 2));
203
0
    return toArgb32(v);
204
#elif defined __ARM_NEON__
205
    uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
206
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
207
    const uint8x8_t shuffleMask = { 4, 5, 2, 3, 0, 1, 6, 7 };
208
    v = vreinterpret_u16_u8(vtbl1_u8(vreinterpret_u8_u16(v), shuffleMask));
209
#else
210
    v = vext_u16(v, v, 3);
211
#endif
212
    return toArgb32(v);
213
#else
214
    return rgba64.toArgb32();
215
#endif
216
0
}
217
218
Q_ALWAYS_INLINE uint toRgba8888(QRgba64 rgba64)
219
0
{
220
0
#if defined __SSE2__
221
0
    __m128i v = _mm_loadl_epi64((const __m128i *)&rgba64);
222
0
    return toArgb32(v);
223
#elif defined __ARM_NEON__
224
    uint16x4_t v = vreinterpret_u16_u64(vld1_u64(reinterpret_cast<const uint64_t *>(&rgba64)));
225
    return toArgb32(v);
226
#else
227
    return ARGB2RGBA(toArgb32(rgba64));
228
#endif
229
0
}
230
231
inline QRgba64 rgbBlend(QRgba64 d, QRgba64 s, uint rgbAlpha)
232
0
{
233
0
    QRgba64 blend;
234
0
#if defined(__SSE2__)
235
0
    __m128i vd = _mm_loadl_epi64((const __m128i *)&d);
236
0
    __m128i vs = _mm_loadl_epi64((const __m128i *)&s);
237
0
    __m128i va =  _mm_cvtsi32_si128(rgbAlpha);
238
0
    va = _mm_unpacklo_epi8(va, va);
239
0
    va = _mm_shufflelo_epi16(va, _MM_SHUFFLE(3, 0, 1, 2));
240
0
    __m128i vb = _mm_xor_si128(_mm_set1_epi16(-1), va);
241
242
0
    vs = _mm_unpacklo_epi16(_mm_mullo_epi16(vs, va), _mm_mulhi_epu16(vs, va));
243
0
    vd = _mm_unpacklo_epi16(_mm_mullo_epi16(vd, vb), _mm_mulhi_epu16(vd, vb));
244
0
    vd = _mm_add_epi32(vd, vs);
245
0
    vd = _mm_add_epi32(vd, _mm_srli_epi32(vd, 16));
246
0
    vd = _mm_add_epi32(vd, _mm_set1_epi32(0x8000));
247
0
    vd = _mm_srai_epi32(vd, 16);
248
0
    vd = _mm_packs_epi32(vd, _mm_setzero_si128());
249
250
0
    _mm_storel_epi64((__m128i *)&blend, vd);
251
#elif defined(__ARM_NEON__)
252
    uint16x4_t vd = vreinterpret_u16_u64(vmov_n_u64(d));
253
    uint16x4_t vs = vreinterpret_u16_u64(vmov_n_u64(s));
254
    uint8x8_t va8 = vreinterpret_u8_u32(vmov_n_u32(ARGB2RGBA(rgbAlpha)));
255
    uint16x4_t va = vreinterpret_u16_u8(vzip_u8(va8, va8).val[0]);
256
    uint16x4_t vb = vdup_n_u16(0xffff);
257
    vb = vsub_u16(vb, va);
258
259
    uint32x4_t vs32 = vmull_u16(vs, va);
260
    uint32x4_t vd32 = vmull_u16(vd, vb);
261
    vd32 = vaddq_u32(vd32, vs32);
262
    vd32 = vsraq_n_u32(vd32, vd32, 16);
263
    vd = vrshrn_n_u32(vd32, 16);
264
    vst1_u64(reinterpret_cast<uint64_t *>(&blend), vreinterpret_u64_u16(vd));
265
#else
266
    const int mr = qRed(rgbAlpha);
267
    const int mg = qGreen(rgbAlpha);
268
    const int mb = qBlue(rgbAlpha);
269
    blend = qRgba64(qt_div_255(s.red()   * mr + d.red()   * (255 - mr)),
270
                    qt_div_255(s.green() * mg + d.green() * (255 - mg)),
271
                    qt_div_255(s.blue()  * mb + d.blue()  * (255 - mb)),
272
                    s.alpha());
273
#endif
274
0
    return blend;
275
0
}
276
277
static Q_ALWAYS_INLINE void blend_pixel(QRgba64 &dst, QRgba64 src)
278
0
{
279
0
    if (src.isOpaque())
280
0
        dst = src;
281
0
    else if (!src.isTransparent())
282
0
        dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
283
0
}
Unexecuted instantiation: qdrawhelper.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qimagescale.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qpaintengine_raster.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qdrawhelper_sse2.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qdrawhelper_ssse3.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qdrawhelper_sse4.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qdrawhelper_avx2.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qcompositionfunctions.cpp:blend_pixel(QRgba64&, QRgba64)
Unexecuted instantiation: qcosmeticstroker.cpp:blend_pixel(QRgba64&, QRgba64)
284
285
static Q_ALWAYS_INLINE void blend_pixel(QRgba64 &dst, QRgba64 src, const int const_alpha)
286
0
{
287
0
    if (const_alpha == 255)
288
0
        return blend_pixel(dst, src);
289
0
    if (!src.isTransparent()) {
290
0
        src = multiplyAlpha255(src, const_alpha);
291
0
        dst = src + multiplyAlpha65535(dst, 65535 - src.alpha());
292
0
    }
293
0
}
Unexecuted instantiation: qdrawhelper.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qimagescale.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qpaintengine_raster.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qdrawhelper_sse2.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qdrawhelper_ssse3.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qdrawhelper_sse4.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qdrawhelper_avx2.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qcompositionfunctions.cpp:blend_pixel(QRgba64&, QRgba64, int)
Unexecuted instantiation: qcosmeticstroker.cpp:blend_pixel(QRgba64&, QRgba64, int)
294
295
QT_END_NAMESPACE
296
297
#endif // QRGBA64_P_H