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_lasx.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_LASX
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_lasx(quint32 *dst, const uchar *src, int len)
17{
18 int i = 0;
19
20 // Prologue, align dst to 32 bytes.
21 ALIGNMENT_PROLOGUE_32BYTES(dst, i, len) {
22 dst[i] = qRgb(src[0], src[1], src[2]);
23 src += 3;
24 }
25
26 // Mask 8 colors of the RGB888 vector
27 const __m256i shuffleMask1 = (__m256i)(v32i8){2, 1, 0, 16, 5, 4, 3, 16, 8, 7, 6, 16, 11, 10, 9, 16,
28 30, 29, 28, 16, 1, 0, 31, 16, 4, 3, 2, 16, 7, 6, 5, 16};
29
30 // Mask 8 colors of a RGB888 vector with an offset of shuffleMask1
31 const __m256i shuffleMask2 = (__m256i)(v32i8){10, 9, 8, 0, 13, 12, 11, 0, 16, 15, 14, 0, 19, 18, 17, 0,
32 6, 5, 4, 0, 9, 8, 7, 0, 12, 11, 10, 0, 15, 14, 13, 0};
33 const __m256i alphaMask = __lasx_xvreplgr2vr_w(0xff000000);
34 const __m256i *inVectorPtr = (const __m256i *)src;
35 __m256i *dstVectorPtr = (__m256i *)(dst + i);
36
37 for (; i < (len - 31); i += 32) { // one iteration in the loop converts 32 pixels
38 /*
39 RGB888 has 10 pixels per vector, + 2 byte from the next pixel. The idea here is
40 to load vectors of RGB888 and use palignr to select a vector out of two vectors.
41
42 After 3 loads of RGB888 and 3 stores of RGB32, we have 8 pixels left in the last
43 vector of RGB888, we can mask it directly to get a last store or RGB32. After that,
44 the first next byte is a R, and we can loop for the next 32 pixels.
45
46 The conversion itself is done with a byte permutation (xvshuf_b and xvpermi_q).
47 */
48 __m256i firstSrcVector = __lasx_xvld(inVectorPtr, 0);
49 __m256i rFirstSrcVector = __lasx_xvpermi_q(firstSrcVector, firstSrcVector, 0b00000001);
50 __m256i outputVector = __lasx_xvshuf_b(rFirstSrcVector, firstSrcVector, shuffleMask1);
51 __lasx_xvst(__lasx_xvor_v(outputVector, alphaMask), dstVectorPtr, 0);
52 ++inVectorPtr;
53 ++dstVectorPtr;
54
55 // There are 8 unused bytes left in srcVector, we need to load the next 32 bytes
56 // and load the next input with palignr
57 __m256i secondSrcVector = __lasx_xvld(inVectorPtr, 0);
58 __m256i srcVector = __lasx_xvpermi_q(secondSrcVector, firstSrcVector, 0b00100001);
59 __m256i rSrcVector = __lasx_xvpermi_q(srcVector, srcVector, 0b00000001);
60 outputVector = __lasx_xvshuf_b(rSrcVector, srcVector, shuffleMask2);
61
62 __lasx_xvst(__lasx_xvor_v(outputVector, alphaMask), dstVectorPtr, 0);
63 ++inVectorPtr;
64 ++dstVectorPtr;
65
66 // We now have 16 unused bytes left in firstSrcVector
67 __m256i thirdSrcVector = __lasx_xvld(inVectorPtr, 0);
68 srcVector = __lasx_xvpermi_q(thirdSrcVector, secondSrcVector, 0b00100001);
69 rSrcVector = __lasx_xvpermi_q(srcVector, srcVector, 0b00000001);
70 outputVector = __lasx_xvshuf_b(rSrcVector, srcVector, shuffleMask1);
71 __lasx_xvst(__lasx_xvor_v(outputVector, alphaMask), dstVectorPtr, 0);
72 ++inVectorPtr;
73 ++dstVectorPtr;
74
75 // There are now 24 unused bytes in firstSrcVector.
76 // We can mask them directly, almost there.
77 srcVector = thirdSrcVector;
78 rSrcVector = __lasx_xvpermi_q(srcVector, srcVector, 0b00000001);
79 outputVector = __lasx_xvshuf_b(rSrcVector, srcVector, shuffleMask2);
80 __lasx_xvst(__lasx_xvor_v(outputVector, alphaMask), dstVectorPtr, 0);
81 ++dstVectorPtr;
82 }
83 src = (const uchar *)inVectorPtr;
84
85 SIMD_EPILOGUE(i, len, 31) {
86 dst[i] = qRgb(src[0], src[1], src[2]);
87 src += 3;
88 }
89}
90
91void convert_RGB888_to_RGB32_lasx(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
92{
93 Q_ASSERT(src->format == QImage::Format_RGB888 || src->format == QImage::Format_BGR888);
94 if (src->format == QImage::Format_BGR888)
95 Q_ASSERT(dest->format == QImage::Format_RGBX8888 || dest->format == QImage::Format_RGBA8888 || dest->format == QImage::Format_RGBA8888_Premultiplied);
96 else
97 Q_ASSERT(dest->format == QImage::Format_RGB32 || dest->format == QImage::Format_ARGB32 || dest->format == QImage::Format_ARGB32_Premultiplied);
98 Q_ASSERT(src->width == dest->width);
99 Q_ASSERT(src->height == dest->height);
100
101 const uchar *src_data = (uchar *) src->data;
102 quint32 *dest_data = (quint32 *) dest->data;
103
104 for (int i = 0; i < src->height; ++i) {
105 qt_convert_rgb888_to_rgb32_lasx(dest_data, src_data, src->width);
106 src_data += src->bytes_per_line;
107 dest_data = (quint32 *)((uchar*)dest_data + dest->bytes_per_line);
108 }
109}
110
111QT_END_NAMESPACE
112
113#endif // QT_COMPILER_SUPPORTS_LASX