Coverage Report

Created: 2025-09-27 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qt/qtbase/src/gui/painting/qdrawhelper_ssse3.cpp
Line
Count
Source
1
// Copyright (C) 2018 The Qt Company Ltd.
2
// Copyright (C) 2018 Intel Corporation.
3
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5
#include <private/qdrawhelper_x86_p.h>
6
7
#if defined(QT_COMPILER_SUPPORTS_SSSE3)
8
9
#include <private/qdrawingprimitive_sse2_p.h>
10
11
QT_BEGIN_NAMESPACE
12
13
/* The instruction palignr uses direct arguments, so we have to generate the code fo the different
14
   shift (4, 8, 12). Checking the alignment inside the loop is unfortunately way too slow.
15
 */
16
#define BLENDING_LOOP(palignrOffset, length)\
17
0
    for (; x-minusOffsetToAlignSrcOn16Bytes < length-7; x += 4) { \
18
0
        const __m128i srcVectorLastLoaded = _mm_load_si128((const __m128i *)&src[x - minusOffsetToAlignSrcOn16Bytes + 4]);\
19
0
        const __m128i srcVector = _mm_alignr_epi8(srcVectorLastLoaded, srcVectorPrevLoaded, palignrOffset); \
20
0
        const __m128i srcVectorAlpha = _mm_and_si128(srcVector, alphaMask); \
21
0
        if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, alphaMask)) == 0xffff) { \
22
0
            _mm_store_si128((__m128i *)&dst[x], srcVector); \
23
0
        } else if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, nullVector)) != 0xffff) { \
24
0
            __m128i alphaChannel = _mm_shuffle_epi8(srcVector, alphaShuffleMask); \
25
0
            alphaChannel = _mm_sub_epi16(one, alphaChannel); \
26
0
            const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]); \
27
0
            __m128i destMultipliedByOneMinusAlpha; \
28
0
            BYTE_MUL_SSE2(destMultipliedByOneMinusAlpha, dstVector, alphaChannel, colorMask, half); \
29
0
            const __m128i result = _mm_add_epi8(srcVector, destMultipliedByOneMinusAlpha); \
30
0
            _mm_store_si128((__m128i *)&dst[x], result); \
31
0
        } \
32
0
        srcVectorPrevLoaded = srcVectorLastLoaded;\
33
0
    }
34
35
36
// Basically blend src over dst with the const alpha defined as constAlphaVector.
37
// nullVector, half, one, colorMask are constant across the whole image/texture, and should be defined as:
38
//const __m128i nullVector = _mm_set1_epi32(0);
39
//const __m128i half = _mm_set1_epi16(0x80);
40
//const __m128i one = _mm_set1_epi16(0xff);
41
//const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
42
//const __m128i alphaMask = _mm_set1_epi32(0xff000000);
43
//
44
// The computation being done is:
45
// result = s + d * (1-alpha)
46
// with shortcuts if fully opaque or fully transparent.
47
static inline void Q_DECL_VECTORCALL
48
BLEND_SOURCE_OVER_ARGB32_SSSE3(quint32 *dst, const quint32 *src, int length,
49
                               __m128i nullVector, __m128i half, __m128i one, __m128i colorMask, __m128i alphaMask)
50
0
{
51
0
    int x = 0;
52
53
    /* First, get dst aligned. */
54
0
    ALIGNMENT_PROLOGUE_16BYTES(dst, x, length) {
55
0
        blend_pixel(dst[x], src[x]);
56
0
    }
57
58
0
    const int minusOffsetToAlignSrcOn16Bytes = (reinterpret_cast<quintptr>(&(src[x])) >> 2) & 0x3;
59
60
0
    if (!minusOffsetToAlignSrcOn16Bytes) {
61
        /* src is aligned, usual algorithm but with aligned operations.
62
           See the SSE2 version for more documentation on the algorithm itself. */
63
0
        const __m128i alphaShuffleMask = _mm_set_epi8(char(0xff),15,char(0xff),15,char(0xff),11,char(0xff),11,char(0xff),7,char(0xff),7,char(0xff),3,char(0xff),3);
64
0
        for (; x < length-3; x += 4) {
65
0
            const __m128i srcVector = _mm_load_si128((const __m128i *)&src[x]);
66
0
            const __m128i srcVectorAlpha = _mm_and_si128(srcVector, alphaMask);
67
0
            if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, alphaMask)) == 0xffff) {
68
0
                _mm_store_si128((__m128i *)&dst[x], srcVector);
69
0
            } else if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, nullVector)) != 0xffff) {
70
0
                __m128i alphaChannel = _mm_shuffle_epi8(srcVector, alphaShuffleMask);
71
0
                alphaChannel = _mm_sub_epi16(one, alphaChannel);
72
0
                const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
73
0
                __m128i destMultipliedByOneMinusAlpha;
74
0
                BYTE_MUL_SSE2(destMultipliedByOneMinusAlpha, dstVector, alphaChannel, colorMask, half);
75
0
                const __m128i result = _mm_add_epi8(srcVector, destMultipliedByOneMinusAlpha);
76
0
                _mm_store_si128((__m128i *)&dst[x], result);
77
0
            }
78
0
        } /* end for() */
79
0
    } else if ((length - x) >= 8) {
80
        /* We use two vectors to extract the src: prevLoaded for the first pixels, lastLoaded for the current pixels. */
81
0
        __m128i srcVectorPrevLoaded = _mm_load_si128((const __m128i *)&src[x - minusOffsetToAlignSrcOn16Bytes]);
82
0
        const int palignrOffset = minusOffsetToAlignSrcOn16Bytes << 2;
83
84
0
        const __m128i alphaShuffleMask = _mm_set_epi8(char(0xff),15,char(0xff),15,char(0xff),11,char(0xff),11,char(0xff),7,char(0xff),7,char(0xff),3,char(0xff),3);
85
0
        switch (palignrOffset) {
86
0
        case 4:
87
0
            BLENDING_LOOP(4, length)
88
0
            break;
89
0
        case 8:
90
0
            BLENDING_LOOP(8, length)
91
0
            break;
92
0
        case 12:
93
0
            BLENDING_LOOP(12, length)
94
0
            break;
95
0
        }
96
0
    }
97
0
    for (; x < length; ++x)
98
0
        blend_pixel(dst[x], src[x]);
99
0
}
100
101
void qt_blend_argb32_on_argb32_ssse3(uchar *destPixels, int dbpl,
102
                                     const uchar *srcPixels, int sbpl,
103
                                     int w, int h,
104
                                     int const_alpha)
105
0
{
106
0
    const quint32 *src = (const quint32 *) srcPixels;
107
0
    quint32 *dst = (quint32 *) destPixels;
108
0
    if (const_alpha == 256) {
109
0
        const __m128i alphaMask = _mm_set1_epi32(0xff000000);
110
0
        const __m128i nullVector = _mm_setzero_si128();
111
0
        const __m128i half = _mm_set1_epi16(0x80);
112
0
        const __m128i one = _mm_set1_epi16(0xff);
113
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
114
115
0
        for (int y = 0; y < h; ++y) {
116
0
            BLEND_SOURCE_OVER_ARGB32_SSSE3(dst, src, w, nullVector, half, one, colorMask, alphaMask);
117
0
            dst = (quint32 *)(((uchar *) dst) + dbpl);
118
0
            src = (const quint32 *)(((const uchar *) src) + sbpl);
119
0
        }
120
0
    } else if (const_alpha != 0) {
121
        // dest = (s + d * sia) * ca + d * cia
122
        //      = s * ca + d * (sia * ca + cia)
123
        //      = s * ca + d * (1 - sa*ca)
124
0
        const_alpha = (const_alpha * 255) >> 8;
125
0
        const __m128i nullVector = _mm_setzero_si128();
126
0
        const __m128i half = _mm_set1_epi16(0x80);
127
0
        const __m128i one = _mm_set1_epi16(0xff);
128
0
        const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
129
0
        const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
130
0
        for (int y = 0; y < h; ++y) {
131
0
            BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_SSE2(dst, src, w, nullVector, half, one, colorMask, constAlphaVector)
132
0
            dst = (quint32 *)(((uchar *) dst) + dbpl);
133
0
            src = (const quint32 *)(((const uchar *) src) + sbpl);
134
0
        }
135
0
    }
136
0
}
137
138
const uint *QT_FASTCALL fetchPixelsBPP24_ssse3(uint *buffer, const uchar *src, int index, int count)
139
0
{
140
0
    const quint24 *s = reinterpret_cast<const quint24 *>(src);
141
0
    for (int i = 0; i < count; ++i)
142
0
        buffer[i] = s[index + i];
143
0
    return buffer;
144
0
}
145
146
extern void QT_FASTCALL qt_convert_rgb888_to_rgb32_ssse3(quint32 *dst, const uchar *src, int len);
147
148
const uint * QT_FASTCALL qt_fetchUntransformed_888_ssse3(uint *buffer, const Operator *, const QSpanData *data,
149
                                                         int y, int x, int length)
150
0
{
151
0
    const uchar *line = data->texture.scanLine(y) + x * 3;
152
0
    qt_convert_rgb888_to_rgb32_ssse3(buffer, line, length);
153
0
    return buffer;
154
0
}
155
156
void qt_memfill24_ssse3(quint24 *dest, quint24 color, qsizetype count)
157
0
{
158
    // LCM of 12 and 16 bytes is 48 bytes (16 px)
159
0
    quint32 v = color;
160
0
    __m128i m = _mm_cvtsi32_si128(v);
161
0
    quint24 *end = dest + count;
162
163
0
    constexpr uchar x = 2, y = 1, z = 0;
164
0
    alignas(__m128i) static const uchar
165
0
    shuffleMask[16 + 1] = { x, y, z, x,  y, z, x, y,  z, x, y, z,  x, y, z, x,  y };
166
167
0
    __m128i mval1 = _mm_shuffle_epi8(m, _mm_load_si128(reinterpret_cast<const __m128i *>(shuffleMask)));
168
0
    __m128i mval2 = _mm_shuffle_epi8(m, _mm_loadu_si128(reinterpret_cast<const __m128i *>(shuffleMask + 1)));
169
0
    __m128i mval3 = _mm_alignr_epi8(mval2, mval1, 2);
170
171
0
    for ( ; dest + 16 <= end; dest += 16) {
172
#ifdef __AVX__
173
        // Store using 32-byte AVX instruction
174
        __m256 mval12 = _mm256_castps128_ps256(_mm_castsi128_ps(mval1));
175
        mval12 = _mm256_insertf128_ps(mval12, _mm_castsi128_ps(mval2), 1);
176
        _mm256_storeu_ps(reinterpret_cast<float *>(dest), mval12);
177
#else
178
0
        _mm_storeu_si128(reinterpret_cast<__m128i *>(dest) + 0, mval1);
179
0
        _mm_storeu_si128(reinterpret_cast<__m128i *>(dest) + 1, mval2);
180
0
#endif
181
0
        _mm_storeu_si128(reinterpret_cast<__m128i *>(dest) + 2, mval3);
182
0
    }
183
184
0
    if (count < 3) {
185
0
        if (count > 1)
186
0
            end[-2] = v;
187
0
        if (count)
188
0
            end[-1] = v;
189
0
        return;
190
0
    }
191
192
    // less than 16px/48B left
193
0
    uchar *ptr = reinterpret_cast<uchar *>(dest);
194
0
    uchar *ptr_end = reinterpret_cast<uchar *>(end);
195
0
    qptrdiff left = ptr_end - ptr;
196
0
    if (left >= 24) {
197
        // 8px/24B or more left
198
0
        _mm_storeu_si128(reinterpret_cast<__m128i *>(ptr) + 0, mval1);
199
0
        _mm_storel_epi64(reinterpret_cast<__m128i *>(ptr) + 1, mval2);
200
0
        ptr += 24;
201
0
        left -= 24;
202
0
    }
203
204
    // less than 8px/24B left
205
206
0
    if (left >= 16) {
207
        // but more than 5px/15B left
208
0
        _mm_storeu_si128(reinterpret_cast<__m128i *>(ptr) , mval1);
209
0
    } else if (left >= 8) {
210
        // but more than 2px/6B left
211
0
        _mm_storel_epi64(reinterpret_cast<__m128i *>(ptr), mval1);
212
0
    }
213
214
0
    if (left) {
215
        // 1 or 2px left
216
        // store 8 bytes ending with the right values (will overwrite a bit)
217
0
        _mm_storel_epi64(reinterpret_cast<__m128i *>(ptr_end - 8), mval2);
218
0
    }
219
0
}
220
221
void QT_FASTCALL rbSwap_888_ssse3(uchar *dst, const uchar *src, int count)
222
0
{
223
0
    int i = 0;
224
225
0
    const static __m128i shuffleMask1 = _mm_setr_epi8(2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9, 14, 13, 12, /*!!*/15);
226
0
    const static __m128i shuffleMask2 = _mm_setr_epi8(0, /*!!*/1, 4, 3, 2, 7, 6, 5, 10, 9, 8, 13, 12, 11, /*!!*/14, 15);
227
0
    const static __m128i shuffleMask3 = _mm_setr_epi8(/*!!*/0, 3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13);
228
229
0
    for (; i + 15 < count; i += 16) {
230
0
        __m128i s1 = _mm_loadu_si128((const __m128i *)src);
231
0
        __m128i s2 = _mm_loadu_si128((const __m128i *)(src + 16));
232
0
        __m128i s3 = _mm_loadu_si128((const __m128i *)(src + 32));
233
0
        s1 = _mm_shuffle_epi8(s1, shuffleMask1);
234
0
        s2 = _mm_shuffle_epi8(s2, shuffleMask2);
235
0
        s3 = _mm_shuffle_epi8(s3, shuffleMask3);
236
0
        _mm_storeu_si128((__m128i *)dst, s1);
237
0
        _mm_storeu_si128((__m128i *)(dst + 16), s2);
238
0
        _mm_storeu_si128((__m128i *)(dst + 32), s3);
239
240
        // Now fix the last four misplaced values
241
0
        std::swap(dst[15], dst[17]);
242
0
        std::swap(dst[30], dst[32]);
243
244
0
        src += 48;
245
0
        dst += 48;
246
0
    }
247
248
0
    if (src != dst) {
249
0
        SIMD_EPILOGUE(i, count, 15) {
250
0
            dst[0] = src[2];
251
0
            dst[1] = src[1];
252
0
            dst[2] = src[0];
253
0
            dst += 3;
254
0
            src += 3;
255
0
        }
256
0
    } else {
257
0
        SIMD_EPILOGUE(i, count, 15) {
258
0
            std::swap(dst[0], dst[2]);
259
0
            dst += 3;
260
0
        }
261
0
    }
262
0
}
263
264
QT_END_NAMESPACE
265
266
#endif // QT_COMPILER_SUPPORTS_SSSE3