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
qdrawhelper_sse2.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 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// Qt-Security score:significant reason:default
5
6#include <private/qdrawhelper_x86_p.h>
7
8#ifdef QT_COMPILER_SUPPORTS_SSE2
9
10#include <private/qblendfunctions_p.h>
11#include <private/qdrawingprimitive_sse2_p.h>
12#include <private/qpaintengine_raster_p.h>
13
14QT_BEGIN_NAMESPACE
15
16#ifndef QDRAWHELPER_AVX
17// in AVX mode, we'll use the SSSE3 code
18void qt_blend_argb32_on_argb32_sse2(uchar *destPixels, int dbpl,
19 const uchar *srcPixels, int sbpl,
20 int w, int h,
21 int const_alpha)
22{
23 const quint32 *src = (const quint32 *) srcPixels;
24 quint32 *dst = (quint32 *) destPixels;
25 if (const_alpha == 256) {
26 const __m128i alphaMask = _mm_set1_epi32(0xff000000);
27 const __m128i nullVector = _mm_set1_epi32(0);
28 const __m128i half = _mm_set1_epi16(0x80);
29 const __m128i one = _mm_set1_epi16(0xff);
30 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
31 for (int y = 0; y < h; ++y) {
32 BLEND_SOURCE_OVER_ARGB32_SSE2(dst, src, w, nullVector, half, one, colorMask, alphaMask);
33 dst = (quint32 *)(((uchar *) dst) + dbpl);
34 src = (const quint32 *)(((const uchar *) src) + sbpl);
35 }
36 } else if (const_alpha != 0) {
37 // dest = (s + d * sia) * ca + d * cia
38 // = s * ca + d * (sia * ca + cia)
39 // = s * ca + d * (1 - sa*ca)
40 const_alpha = (const_alpha * 255) >> 8;
41 const __m128i nullVector = _mm_set1_epi32(0);
42 const __m128i half = _mm_set1_epi16(0x80);
43 const __m128i one = _mm_set1_epi16(0xff);
44 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
45 const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
46 for (int y = 0; y < h; ++y) {
47 BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_SSE2(dst, src, w, nullVector, half, one, colorMask, constAlphaVector)
48 dst = (quint32 *)(((uchar *) dst) + dbpl);
49 src = (const quint32 *)(((const uchar *) src) + sbpl);
50 }
51 }
52}
53#endif
54
55// qblendfunctions.cpp
56void qt_blend_rgb32_on_rgb32(uchar *destPixels, int dbpl,
57 const uchar *srcPixels, int sbpl,
58 int w, int h,
59 int const_alpha);
60
61void qt_blend_rgb32_on_rgb32_sse2(uchar *destPixels, int dbpl,
62 const uchar *srcPixels, int sbpl,
63 int w, int h,
64 int const_alpha)
65{
66 const quint32 *src = (const quint32 *) srcPixels;
67 quint32 *dst = (quint32 *) destPixels;
68 if (const_alpha != 256) {
69 if (const_alpha != 0) {
70 const __m128i half = _mm_set1_epi16(0x80);
71 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
72
73 const_alpha = (const_alpha * 255) >> 8;
74 int one_minus_const_alpha = 255 - const_alpha;
75 const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
76 const __m128i oneMinusConstAlpha = _mm_set1_epi16(one_minus_const_alpha);
77 for (int y = 0; y < h; ++y) {
78 int x = 0;
79
80 // First, align dest to 16 bytes:
81 ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
82 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], one_minus_const_alpha);
83 }
84
85 for (; x < w-3; x += 4) {
86 __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
87 const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
88 __m128i result;
89 INTERPOLATE_PIXEL_255_SSE2(result, srcVector, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half);
90 _mm_store_si128((__m128i *)&dst[x], result);
91 }
92 SIMD_EPILOGUE(x, w, 3)
93 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], one_minus_const_alpha);
94 dst = (quint32 *)(((uchar *) dst) + dbpl);
95 src = (const quint32 *)(((const uchar *) src) + sbpl);
96 }
97 }
98 } else {
99 qt_blend_rgb32_on_rgb32(destPixels, dbpl, srcPixels, sbpl, w, h, const_alpha);
100 }
101}
102
103void QT_FASTCALL comp_func_SourceOver_sse2(uint *destPixels, const uint *srcPixels, int length, uint const_alpha)
104{
105 Q_ASSERT(const_alpha < 256);
106
107 const quint32 *src = (const quint32 *) srcPixels;
108 quint32 *dst = (quint32 *) destPixels;
109
110 const __m128i nullVector = _mm_set1_epi32(0);
111 const __m128i half = _mm_set1_epi16(0x80);
112 const __m128i one = _mm_set1_epi16(0xff);
113 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
114 if (const_alpha == 255) {
115 const __m128i alphaMask = _mm_set1_epi32(0xff000000);
116 BLEND_SOURCE_OVER_ARGB32_SSE2(dst, src, length, nullVector, half, one, colorMask, alphaMask);
117 } else {
118 const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
119 BLEND_SOURCE_OVER_ARGB32_WITH_CONST_ALPHA_SSE2(dst, src, length, nullVector, half, one, colorMask, constAlphaVector);
120 }
121}
122
123void QT_FASTCALL comp_func_Plus_sse2(uint *dst, const uint *src, int length, uint const_alpha)
124{
125 int x = 0;
126
127 if (const_alpha == 255) {
128 // 1) Prologue: align destination on 16 bytes
129 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
130 dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
131
132 // 2) composition with SSE2
133 for (; x < length - 3; x += 4) {
134 const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
135 const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
136
137 const __m128i result = _mm_adds_epu8(srcVector, dstVector);
138 _mm_store_si128((__m128i *)&dst[x], result);
139 }
140
141 // 3) Epilogue:
142 SIMD_EPILOGUE(x, length, 3)
143 dst[x] = comp_func_Plus_one_pixel(dst[x], src[x]);
144 } else {
145 const int one_minus_const_alpha = 255 - const_alpha;
146 const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
147 const __m128i oneMinusConstAlpha = _mm_set1_epi16(one_minus_const_alpha);
148
149 // 1) Prologue: align destination on 16 bytes
150 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
151 dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x], const_alpha, one_minus_const_alpha);
152
153 const __m128i half = _mm_set1_epi16(0x80);
154 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
155 // 2) composition with SSE2
156 for (; x < length - 3; x += 4) {
157 const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
158 const __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
159
160 __m128i result = _mm_adds_epu8(srcVector, dstVector);
161 INTERPOLATE_PIXEL_255_SSE2(result, result, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half)
162 _mm_store_si128((__m128i *)&dst[x], result);
163 }
164
165 // 3) Epilogue:
166 SIMD_EPILOGUE(x, length, 3)
167 dst[x] = comp_func_Plus_one_pixel_const_alpha(dst[x], src[x], const_alpha, one_minus_const_alpha);
168 }
169}
170
171void QT_FASTCALL comp_func_Source_sse2(uint *dst, const uint *src, int length, uint const_alpha)
172{
173 if (const_alpha == 255) {
174 ::memcpy(dst, src, length * sizeof(uint));
175 } else {
176 const int ialpha = 255 - const_alpha;
177
178 int x = 0;
179
180 // 1) prologue, align on 16 bytes
181 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
182 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
183
184 // 2) interpolate pixels with SSE2
185 const __m128i half = _mm_set1_epi16(0x80);
186 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
187 const __m128i constAlphaVector = _mm_set1_epi16(const_alpha);
188 const __m128i oneMinusConstAlpha = _mm_set1_epi16(ialpha);
189 for (; x < length - 3; x += 4) {
190 const __m128i srcVector = _mm_loadu_si128((const __m128i *)&src[x]);
191 __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
192 INTERPOLATE_PIXEL_255_SSE2(dstVector, srcVector, dstVector, constAlphaVector, oneMinusConstAlpha, colorMask, half)
193 _mm_store_si128((__m128i *)&dst[x], dstVector);
194 }
195
196 // 3) Epilogue
197 SIMD_EPILOGUE(x, length, 3)
198 dst[x] = INTERPOLATE_PIXEL_255(src[x], const_alpha, dst[x], ialpha);
199 }
200}
201
202#ifndef __haswell__
203Q_NEVER_INLINE static
204void Q_DECL_VECTORCALL qt_memfillXX_aligned(void *dest, __m128i value128, quintptr bytecount)
205{
206 __m128i *dst128 = reinterpret_cast<__m128i *>(dest);
207 __m128i *end128 = reinterpret_cast<__m128i *>(static_cast<uchar *>(dest) + bytecount);
208
209 while (dst128 + 4 <= end128) {
210 _mm_store_si128(dst128 + 0, value128);
211 _mm_store_si128(dst128 + 1, value128);
212 _mm_store_si128(dst128 + 2, value128);
213 _mm_store_si128(dst128 + 3, value128);
214 dst128 += 4;
215 }
216
217 bytecount %= 4 * sizeof(__m128i);
218 switch (bytecount / sizeof(__m128i)) {
219 case 3: _mm_store_si128(dst128++, value128); Q_FALLTHROUGH();
220 case 2: _mm_store_si128(dst128++, value128); Q_FALLTHROUGH();
221 case 1: _mm_store_si128(dst128++, value128);
222 }
223}
224
225void qt_memfill64_sse2(quint64 *dest, quint64 value, qsizetype count)
226{
227 quintptr misaligned = quintptr(dest) % sizeof(__m128i);
228 if (misaligned && count) {
229#if defined(Q_PROCESSOR_X86_32)
230 // Before SSE came out, the alignment of the stack used to be only 4
231 // bytes and some OS/ABIs (notably, code generated by MSVC) still only
232 // align to that. In any case, we cannot count on the alignment of
233 // quint64 to be 8 -- see QtPrivate::AlignOf_WorkaroundForI386Abi in
234 // qglobal.h.
235 //
236 // If the pointer is not aligned to at least 8 bytes, then we'll never
237 // in turn hit a multiple of 16 for the qt_memfillXX_aligned call
238 // below.
239 if (Q_UNLIKELY(misaligned % sizeof(quint64)))
240 return qt_memfill_template(dest, value, count);
241#endif
242
243 *dest++ = value;
244 --count;
245 }
246
247 if (count % 2) {
248 dest[count - 1] = value;
249 --count;
250 }
251
252 qt_memfillXX_aligned(dest, _mm_set1_epi64x(value), count * sizeof(quint64));
253}
254
255void qt_memfill32_sse2(quint32 *dest, quint32 value, qsizetype count)
256{
257 if (count < 4) {
258 // this simplifies the code below: the first switch can fall through
259 // without checking the value of count
260 switch (count) {
261 case 3: *dest++ = value; Q_FALLTHROUGH();
262 case 2: *dest++ = value; Q_FALLTHROUGH();
263 case 1: *dest = value;
264 }
265 return;
266 }
267
268 const int align = (quintptr)(dest) & 0xf;
269 switch (align) {
270 case 4: *dest++ = value; --count; Q_FALLTHROUGH();
271 case 8: *dest++ = value; --count; Q_FALLTHROUGH();
272 case 12: *dest++ = value; --count;
273 }
274
275 const int rest = count & 0x3;
276 if (rest) {
277 switch (rest) {
278 case 3: dest[count - 3] = value; Q_FALLTHROUGH();
279 case 2: dest[count - 2] = value; Q_FALLTHROUGH();
280 case 1: dest[count - 1] = value;
281 }
282 }
283
284 qt_memfillXX_aligned(dest, _mm_set1_epi32(value), count * sizeof(quint32));
285}
286#endif // !__haswell__
287
288void QT_FASTCALL comp_func_solid_Source_sse2(uint *destPixels, int length, uint color, uint const_alpha)
289{
290 if (const_alpha == 255) {
291 qt_memfill32(destPixels, color, length);
292 } else {
293 const quint32 ialpha = 255 - const_alpha;
294 color = BYTE_MUL(color, const_alpha);
295 int x = 0;
296
297 quint32 *dst = (quint32 *) destPixels;
298 const __m128i colorVector = _mm_set1_epi32(color);
299 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
300 const __m128i half = _mm_set1_epi16(0x80);
301 const __m128i iAlphaVector = _mm_set1_epi16(ialpha);
302
303 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
304 destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
305
306 for (; x < length-3; x += 4) {
307 __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
308 BYTE_MUL_SSE2(dstVector, dstVector, iAlphaVector, colorMask, half);
309 dstVector = _mm_add_epi8(colorVector, dstVector);
310 _mm_store_si128((__m128i *)&dst[x], dstVector);
311 }
312 SIMD_EPILOGUE(x, length, 3)
313 destPixels[x] = color + BYTE_MUL(destPixels[x], ialpha);
314 }
315}
316
317void QT_FASTCALL comp_func_solid_SourceOver_sse2(uint *destPixels, int length, uint color, uint const_alpha)
318{
319 if ((const_alpha & qAlpha(color)) == 255) {
320 qt_memfill32(destPixels, color, length);
321 } else {
322 if (const_alpha != 255)
323 color = BYTE_MUL(color, const_alpha);
324
325 const quint32 minusAlphaOfColor = qAlpha(~color);
326 int x = 0;
327
328 quint32 *dst = (quint32 *) destPixels;
329 const __m128i colorVector = _mm_set1_epi32(color);
330 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
331 const __m128i half = _mm_set1_epi16(0x80);
332 const __m128i minusAlphaOfColorVector = _mm_set1_epi16(minusAlphaOfColor);
333
334 ALIGNMENT_PROLOGUE_16BYTES(dst, x, length)
335 destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
336
337 for (; x < length-3; x += 4) {
338 __m128i dstVector = _mm_load_si128((__m128i *)&dst[x]);
339 BYTE_MUL_SSE2(dstVector, dstVector, minusAlphaOfColorVector, colorMask, half);
340 dstVector = _mm_add_epi8(colorVector, dstVector);
341 _mm_store_si128((__m128i *)&dst[x], dstVector);
342 }
343 SIMD_EPILOGUE(x, length, 3)
344 destPixels[x] = color + BYTE_MUL(destPixels[x], minusAlphaOfColor);
345 }
346}
347
348void qt_bitmapblit32_sse2_base(QRasterBuffer *rasterBuffer, int x, int y,
349 quint32 color,
350 const uchar *src, int width, int height, int stride)
351{
352 quint32 *dest = reinterpret_cast<quint32*>(rasterBuffer->scanLine(y)) + x;
353 const int destStride = rasterBuffer->stride<quint32>();
354
355 const __m128i c128 = _mm_set1_epi32(color);
356 const __m128i maskmask1 = _mm_set_epi32(0x10101010, 0x20202020,
357 0x40404040, 0x80808080);
358 const __m128i maskadd1 = _mm_set_epi32(0x70707070, 0x60606060,
359 0x40404040, 0x00000000);
360
361 if (width > 4) {
362 const __m128i maskmask2 = _mm_set_epi32(0x01010101, 0x02020202,
363 0x04040404, 0x08080808);
364 const __m128i maskadd2 = _mm_set_epi32(0x7f7f7f7f, 0x7e7e7e7e,
365 0x7c7c7c7c, 0x78787878);
366 while (--height >= 0) {
367 for (int x = 0; x < width; x += 8) {
368 const quint8 s = src[x >> 3];
369 if (!s)
370 continue;
371 __m128i mask1 = _mm_set1_epi8(s);
372 __m128i mask2 = mask1;
373
374 mask1 = _mm_and_si128(mask1, maskmask1);
375 mask1 = _mm_add_epi8(mask1, maskadd1);
376 _mm_maskmoveu_si128(c128, mask1, (char*)(dest + x));
377 mask2 = _mm_and_si128(mask2, maskmask2);
378 mask2 = _mm_add_epi8(mask2, maskadd2);
379 _mm_maskmoveu_si128(c128, mask2, (char*)(dest + x + 4));
380 }
381 dest += destStride;
382 src += stride;
383 }
384 } else {
385 while (--height >= 0) {
386 const quint8 s = *src;
387 if (s) {
388 __m128i mask1 = _mm_set1_epi8(s);
389 mask1 = _mm_and_si128(mask1, maskmask1);
390 mask1 = _mm_add_epi8(mask1, maskadd1);
391 _mm_maskmoveu_si128(c128, mask1, (char*)(dest));
392 }
393 dest += destStride;
394 src += stride;
395 }
396 }
397}
398
399void qt_bitmapblit32_sse2(QRasterBuffer *rasterBuffer, int x, int y,
400 const QRgba64 &color,
401 const uchar *src, int width, int height, int stride)
402{
403 qt_bitmapblit32_sse2_base(rasterBuffer, x, y, color.toArgb32(), src, width, height, stride);
404}
405
406void qt_bitmapblit8888_sse2(QRasterBuffer *rasterBuffer, int x, int y,
407 const QRgba64 &color,
408 const uchar *src, int width, int height, int stride)
409{
410 qt_bitmapblit32_sse2_base(rasterBuffer, x, y, ARGB2RGBA(color.toArgb32()), src, width, height, stride);
411}
412
413void qt_bitmapblit16_sse2(QRasterBuffer *rasterBuffer, int x, int y,
414 const QRgba64 &color,
415 const uchar *src, int width, int height, int stride)
416{
417 const quint16 c = qConvertRgb32To16(color.toArgb32());
418 quint16 *dest = reinterpret_cast<quint16*>(rasterBuffer->scanLine(y)) + x;
419 const int destStride = rasterBuffer->stride<quint32>();
420
421 const __m128i c128 = _mm_set1_epi16(c);
422QT_WARNING_DISABLE_MSVC(4309) // truncation of constant value
423 const __m128i maskmask = _mm_set_epi16(0x0101, 0x0202, 0x0404, 0x0808,
424 0x1010, 0x2020, 0x4040, 0x8080);
425 const __m128i maskadd = _mm_set_epi16(0x7f7f, 0x7e7e, 0x7c7c, 0x7878,
426 0x7070, 0x6060, 0x4040, 0x0000);
427
428 while (--height >= 0) {
429 for (int x = 0; x < width; x += 8) {
430 const quint8 s = src[x >> 3];
431 if (!s)
432 continue;
433 __m128i mask = _mm_set1_epi8(s);
434 mask = _mm_and_si128(mask, maskmask);
435 mask = _mm_add_epi8(mask, maskadd);
436 _mm_maskmoveu_si128(c128, mask, (char*)(dest + x));
437 }
438 dest += destStride;
439 src += stride;
440 }
441}
442
443class QSimdSse2
444{
445public:
446 typedef __m128i Int32x4;
447 typedef __m128 Float32x4;
448
449 union Vect_buffer_i { Int32x4 v; int i[4]; };
450 union Vect_buffer_f { Float32x4 v; float f[4]; };
451
452 static inline Float32x4 Q_DECL_VECTORCALL v_dup(float x) { return _mm_set1_ps(x); }
453 static inline Float32x4 Q_DECL_VECTORCALL v_dup(double x) { return _mm_set1_ps(x); }
454 static inline Int32x4 Q_DECL_VECTORCALL v_dup(int x) { return _mm_set1_epi32(x); }
455 static inline Int32x4 Q_DECL_VECTORCALL v_dup(uint x) { return _mm_set1_epi32(x); }
456
457 static inline Float32x4 Q_DECL_VECTORCALL v_add(Float32x4 a, Float32x4 b) { return _mm_add_ps(a, b); }
458 static inline Int32x4 Q_DECL_VECTORCALL v_add(Int32x4 a, Int32x4 b) { return _mm_add_epi32(a, b); }
459
460 static inline Float32x4 Q_DECL_VECTORCALL v_max(Float32x4 a, Float32x4 b) { return _mm_max_ps(a, b); }
461 static inline Float32x4 Q_DECL_VECTORCALL v_min(Float32x4 a, Float32x4 b) { return _mm_min_ps(a, b); }
462 static inline Int32x4 Q_DECL_VECTORCALL v_min_16(Int32x4 a, Int32x4 b) { return _mm_min_epi16(a, b); }
463
464 static inline Int32x4 Q_DECL_VECTORCALL v_and(Int32x4 a, Int32x4 b) { return _mm_and_si128(a, b); }
465
466 static inline Float32x4 Q_DECL_VECTORCALL v_sub(Float32x4 a, Float32x4 b) { return _mm_sub_ps(a, b); }
467 static inline Int32x4 Q_DECL_VECTORCALL v_sub(Int32x4 a, Int32x4 b) { return _mm_sub_epi32(a, b); }
468
469 static inline Float32x4 Q_DECL_VECTORCALL v_mul(Float32x4 a, Float32x4 b) { return _mm_mul_ps(a, b); }
470
471 static inline Float32x4 Q_DECL_VECTORCALL v_sqrt(Float32x4 x) { return _mm_sqrt_ps(x); }
472
473 static inline Int32x4 Q_DECL_VECTORCALL v_toInt(Float32x4 x) { return _mm_cvttps_epi32(x); }
474
475 static inline Int32x4 Q_DECL_VECTORCALL v_greaterOrEqual(Float32x4 a, Float32x4 b) { return _mm_castps_si128(_mm_cmpgt_ps(a, b)); }
476};
477
478const uint * QT_FASTCALL qt_fetch_radial_gradient_sse2(uint *buffer, const Operator *op, const QSpanData *data,
479 int y, int x, int length)
480{
481 return qt_fetch_radial_gradient_template<QRadialFetchSimd<QSimdSse2>,uint>(buffer, op, data, y, x, length);
482}
483
484void qt_scale_image_argb32_on_argb32_sse2(uchar *destPixels, int dbpl,
485 const uchar *srcPixels, int sbpl, int srch,
486 const QRectF &targetRect,
487 const QRectF &sourceRect,
488 const QRect &clip,
489 int const_alpha)
490{
491 // The SIMD code below assumes x-coordinates small enough to be in 16.16 fixed point format, and opaque const_alpha.
492 if (const_alpha != 256 || !q20::in_range<int16_t>((int)sourceRect.right()) || !q20::in_range<int16_t>((int)sourceRect.left())) {
493 // from qblendfunctions.cpp
494 extern void qt_scale_image_argb32_on_argb32(uchar *destPixels, int dbpl,
495 const uchar *srcPixels, int sbpl, int srch,
496 const QRectF &targetRect,
497 const QRectF &sourceRect,
498 const QRect &clip,
499 int const_alpha);
500 return qt_scale_image_argb32_on_argb32(destPixels, dbpl, srcPixels, sbpl, srch, targetRect, sourceRect, clip, const_alpha);
501 }
502
503 if (targetRect.width() == 0 || targetRect.height() == 0)
504 return;
505
506 const qreal sx = sourceRect.width() / (qreal)targetRect.width();
507 const qreal sy = sourceRect.height() / (qreal)targetRect.height();
508
509 const int ix = 0x00010000 * sx;
510 const int64_t iy = 0x00010000 * sy;
511
512 QRect tr = targetRect.normalized().toRect();
513 tr = tr.intersected(clip);
514 if (tr.isEmpty())
515 return;
516 const int tx1 = tr.left();
517 const int ty1 = tr.top();
518 int h = tr.height();
519 int w = tr.width();
520
521 quint32 basex;
522 quint64 srcy;
523
524 if (sx < 0) {
525 int dstx = qFloor((tx1 + qreal(0.5) - targetRect.right()) * sx * 65536) + 1;
526 basex = quint32(sourceRect.right() * 65536) + dstx;
527 } else {
528 int dstx = qCeil((tx1 + qreal(0.5) - targetRect.left()) * sx * 65536) - 1;
529 basex = quint32(sourceRect.left() * 65536) + dstx;
530 }
531 if (sy < 0) {
532 int64_t dsty = qFloor64((ty1 + qreal(0.5) - targetRect.bottom()) * sy * 65536) + 1;
533 srcy = quint64(sourceRect.bottom() * 65536) + dsty;
534 } else {
535 int64_t dsty = qCeil64((ty1 + qreal(0.5) - targetRect.top()) * sy * 65536) - 1;
536 srcy = quint64(sourceRect.top() * 65536) + dsty;
537 }
538
539 quint32 *dst = ((quint32 *) (destPixels + qsizetype(ty1) * dbpl)) + tx1;
540
541 const __m128i nullVector = _mm_setzero_si128();
542 const __m128i half = _mm_set1_epi16(0x80);
543 const __m128i one = _mm_set1_epi16(0xff);
544 const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
545 const __m128i alphaMask = _mm_set1_epi32(0xff000000);
546 const __m128i ixVector = _mm_set1_epi32(4*static_cast<unsigned>(ix));
547
548 // this bounds check here is required as floating point rounding above might in some cases lead to
549 // w/h values that are one pixel too large, falling outside of the valid image area.
550 const int ystart = static_cast<int>(srcy >> 16);
551 if (ystart >= srch && iy < 0) {
552 srcy += iy;
553 --h;
554 }
555 const int xstart = basex >> 16;
556 if (xstart >= (int)(sbpl/sizeof(quint32)) && ix < 0) {
557 basex += ix;
558 --w;
559 }
560 int yend = static_cast<int>((srcy + iy * (h - 1)) >> 16);
561 if (yend < 0 || yend >= srch)
562 --h;
563 int xend = (basex + ix * (w - 1)) >> 16;
564 if (xend < 0 || xend >= (int)(sbpl/sizeof(quint32)))
565 --w;
566
567 while (--h >= 0) {
568 const uint *src = (const quint32 *) (srcPixels + (srcy >> 16) * sbpl);
569 int srcx = basex;
570 int x = 0;
571
572 ALIGNMENT_PROLOGUE_16BYTES(dst, x, w) {
573 uint s = src[srcx >> 16];
574 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
575 srcx += ix;
576 }
577
578 __m128i srcxVector = _mm_set_epi32(srcx, srcx + ix, srcx + ix + ix, srcx + ix + ix + ix);
579
580 for (; x < (w - 3); x += 4) {
581 const int idx0 = _mm_extract_epi16(srcxVector, 1);
582 const int idx1 = _mm_extract_epi16(srcxVector, 3);
583 const int idx2 = _mm_extract_epi16(srcxVector, 5);
584 const int idx3 = _mm_extract_epi16(srcxVector, 7);
585 srcxVector = _mm_add_epi32(srcxVector, ixVector);
586
587 const __m128i srcVector = _mm_set_epi32(src[idx0], src[idx1], src[idx2], src[idx3]);
588 BLEND_SOURCE_OVER_ARGB32_SSE2_helper(dst, srcVector, nullVector, half, one, colorMask, alphaMask);
589 }
590
591 SIMD_EPILOGUE(x, w, 3) {
592 uint s = src[(basex + x*ix) >> 16];
593 dst[x] = s + BYTE_MUL(dst[x], qAlpha(~s));
594 }
595 dst = (quint32 *)(((uchar *) dst) + dbpl);
596 srcy += iy;
597 }
598}
599
600
601QT_END_NAMESPACE
602
603#endif // QT_COMPILER_SUPPORTS_SSE2