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