Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qimage_lsx.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 Loongson Technology Corporation Limited.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <qimage.h>
6#include <private/qimage_p.h>
7#include <private/qsimd_p.h>
8
9#ifdef QT_COMPILER_SUPPORTS_LSX
10
11QT_BEGIN_NAMESPACE
12
13// Convert a scanline of RGB888 (src) to RGB32 (dst)
14// src must be at least len * 3 bytes
15// dst must be at least len * 4 bytes
16Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32_lsx(quint32 *dst, const uchar *src, int len)
17{
18 int i = 0;
19
20 // Prologue, align dst to 16 bytes.
21 ALIGNMENT_PROLOGUE_16BYTES(dst, i, len) {
22 dst[i] = qRgb(src[0], src[1], src[2]);
23 src += 3;
24 }
25
26 // Mask the 4 first colors of the RGB888 vector
27 const __m128i shuffleMask = (__m128i)(v16i8){2, 1, 0, 16, 5, 4, 3, 16,
28 8, 7, 6, 16, 11, 10, 9, 16};
29 // Mask the 4 last colors of a RGB888 vector with an offset of 1 (so the last 3 bytes are RGB)
30 const __m128i shuffleMaskEnd = (__m128i)(v16i8){6, 5, 4, 16, 9, 8, 7, 16,
31 12, 11, 10, 16, 15, 14, 13, 16};
32 // Mask to have alpha = 0xff
33 const __m128i alphaMask = __lsx_vreplgr2vr_b(0xff);
34
35 // Mask to concatenate 16-byte blocks in a and b into a 32-byte temporary result, shift the result right by 12 bytes
36 const __m128i indexMask1 = (__m128i)(v16i8){12, 13, 14, 15, 16, 17, 18, 19,
37 20, 21, 22, 23, 24, 25, 26, 27};
38
39 // Mask to concatenate 16-byte blocks in a and b into a 32-byte temporary result, shift the result right by 8 bytes
40 const __m128i indexMask2 = (__m128i)(v16i8){8, 9, 10, 11, 12, 13, 14, 15,
41 16, 17, 18, 19, 20, 21, 22, 23};
42
43 const __m128i *inVectorPtr = (const __m128i *)src;
44 __m128i *dstVectorPtr = (__m128i *)(dst + i);
45
46 for (; i < (len - 15); i += 16) { // one iteration in the loop converts 16 pixels
47 /*
48 RGB888 has 5 pixels per vector, + 1 byte from the next pixel. The idea here is
49 to load vectors of RGB888 and use palignr to select a vector out of two vectors.
50
51 After 3 loads of RGB888 and 3 stores of RGB32, we have 4 pixels left in the last
52 vector of RGB888, we can mask it directly to get a last store or RGB32. After that,
53 the first next byte is a R, and we can loop for the next 16 pixels.
54
55 The conversion itself is done with a byte permutation (vshuf_b).
56 */
57 __m128i firstSrcVector = __lsx_vld(inVectorPtr, 0);
58 __m128i outputVector = __lsx_vshuf_b(alphaMask, firstSrcVector, shuffleMask);
59 __lsx_vst(outputVector, dstVectorPtr, 0);
60 ++inVectorPtr;
61 ++dstVectorPtr;
62
63 // There are 4 unused bytes left in srcVector, we need to load the next 16 bytes
64 __m128i secondSrcVector = __lsx_vld(inVectorPtr, 0);
65 __m128i srcVector = __lsx_vshuf_b(secondSrcVector, firstSrcVector, indexMask1);
66 outputVector = __lsx_vshuf_b(alphaMask, srcVector, shuffleMask);
67 __lsx_vst(outputVector, dstVectorPtr, 0);
68 ++inVectorPtr;
69 ++dstVectorPtr;
70 firstSrcVector = secondSrcVector;
71
72 // We now have 8 unused bytes left in firstSrcVector
73 secondSrcVector = __lsx_vld(inVectorPtr, 0);
74 srcVector = __lsx_vshuf_b(secondSrcVector, firstSrcVector, indexMask2);
75 outputVector = __lsx_vshuf_b(alphaMask, srcVector, shuffleMask);
76 __lsx_vst(outputVector, dstVectorPtr, 0);
77 ++inVectorPtr;
78 ++dstVectorPtr;
79
80 // There are now 12 unused bytes in firstSrcVector.
81 // We can mask them directly, almost there.
82 outputVector = __lsx_vshuf_b(alphaMask, secondSrcVector, shuffleMaskEnd);
83 __lsx_vst(outputVector, dstVectorPtr, 0);
84 ++dstVectorPtr;
85 }
86 src = (const uchar *)inVectorPtr;
87
88 SIMD_EPILOGUE(i, len, 15) {
89 dst[i] = qRgb(src[0], src[1], src[2]);
90 src += 3;
91 }
92}
93
94void convert_RGB888_to_RGB32_lsx(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
95{
96 Q_ASSERT(src->format == QImage::Format_RGB888 || src->format == QImage::Format_BGR888);
97 if (src->format == QImage::Format_BGR888)
98 Q_ASSERT(dest->format == QImage::Format_RGBX8888 || dest->format == QImage::Format_RGBA8888 || dest->format == QImage::Format_RGBA8888_Premultiplied);
99 else
100 Q_ASSERT(dest->format == QImage::Format_RGB32 || dest->format == QImage::Format_ARGB32 || dest->format == QImage::Format_ARGB32_Premultiplied);
101 Q_ASSERT(src->width == dest->width);
102 Q_ASSERT(src->height == dest->height);
103
104 const uchar *src_data = (uchar *) src->data;
105 quint32 *dest_data = (quint32 *) dest->data;
106
107 for (int i = 0; i < src->height; ++i) {
108 qt_convert_rgb888_to_rgb32_lsx(dest_data, src_data, src->width);
109 src_data += src->bytes_per_line;
110 dest_data = (quint32 *)((uchar*)dest_data + dest->bytes_per_line);
111 }
112}
113
114QT_END_NAMESPACE
115
116#endif // QT_COMPILER_SUPPORTS_LSX