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