| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QDRAWHELPER_P_H |
| 5 | #define QDRAWHELPER_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtGui/private/qtguiglobal_p.h> |
| 19 | #include "QtCore/qmath.h" |
| 20 | #include "QtGui/qcolor.h" |
| 21 | #include "QtGui/qpainter.h" |
| 22 | #include "QtGui/qimage.h" |
| 23 | #include "QtGui/qrgba64.h" |
| 24 | #ifndef QT_FT_BEGIN_HEADER |
| 25 | #define |
| 26 | #define |
| 27 | #endif |
| 28 | #include "private/qpixellayout_p.h" |
| 29 | #include "private/qrasterdefs_p.h" |
| 30 | #include <private/qsimd_p.h> |
| 31 | |
| 32 | #include <memory> |
| 33 | |
| 34 | QT_BEGIN_NAMESPACE |
| 35 | |
| 36 | #if defined(Q_CC_GNU) |
| 37 | # define Q_DECL_RESTRICT __restrict__ |
| 38 | # if defined(Q_PROCESSOR_X86_32) && defined(Q_CC_GNU) && !defined(Q_CC_CLANG) |
| 39 | # define Q_DECL_VECTORCALL __attribute__((sseregparm,regparm(3))) |
| 40 | # else |
| 41 | # define Q_DECL_VECTORCALL |
| 42 | # endif |
| 43 | #elif defined(Q_CC_MSVC) |
| 44 | # define Q_DECL_RESTRICT __restrict |
| 45 | # define Q_DECL_VECTORCALL __vectorcall |
| 46 | #else |
| 47 | # define Q_DECL_RESTRICT |
| 48 | # define Q_DECL_VECTORCALL |
| 49 | #endif |
| 50 | |
| 51 | static const uint AMASK = 0xff000000; |
| 52 | static const uint RMASK = 0x00ff0000; |
| 53 | static const uint GMASK = 0x0000ff00; |
| 54 | static const uint BMASK = 0x000000ff; |
| 55 | |
| 56 | struct QSolidData; |
| 57 | struct QTextureData; |
| 58 | struct QGradientData; |
| 59 | struct QLinearGradientData; |
| 60 | struct QRadialGradientData; |
| 61 | struct QConicalGradientData; |
| 62 | struct QSpanData; |
| 63 | class QGradient; |
| 64 | class QRasterBuffer; |
| 65 | class QClipData; |
| 66 | class QRasterPaintEngineState; |
| 67 | |
| 68 | template<typename F> class QRgbaFloat; |
| 69 | typedef QRgbaFloat<float> QRgbaFloat32; |
| 70 | |
| 71 | typedef QT_FT_SpanFunc ProcessSpans; |
| 72 | typedef void (*BitmapBlitFunc)(QRasterBuffer *rasterBuffer, |
| 73 | int x, int y, const QRgba64 &color, |
| 74 | const uchar *bitmap, |
| 75 | int mapWidth, int mapHeight, int mapStride); |
| 76 | |
| 77 | typedef void (*AlphamapBlitFunc)(QRasterBuffer *rasterBuffer, |
| 78 | int x, int y, const QRgba64 &color, |
| 79 | const uchar *bitmap, |
| 80 | int mapWidth, int mapHeight, int mapStride, |
| 81 | const QClipData *clip, bool useGammaCorrection); |
| 82 | |
| 83 | typedef void (*AlphaRGBBlitFunc)(QRasterBuffer *rasterBuffer, |
| 84 | int x, int y, const QRgba64 &color, |
| 85 | const uint *rgbmask, |
| 86 | int mapWidth, int mapHeight, int mapStride, |
| 87 | const QClipData *clip, bool useGammaCorrection); |
| 88 | |
| 89 | typedef void (*RectFillFunc)(QRasterBuffer *rasterBuffer, |
| 90 | int x, int y, int width, int height, |
| 91 | const QRgba64 &color); |
| 92 | |
| 93 | typedef void (*SrcOverBlendFunc)(uchar *destPixels, int dbpl, |
| 94 | const uchar *src, int spbl, |
| 95 | int w, int h, |
| 96 | int const_alpha); |
| 97 | |
| 98 | typedef void (*SrcOverScaleFunc)(uchar *destPixels, int dbpl, |
| 99 | const uchar *src, int spbl, int srch, |
| 100 | const QRectF &targetRect, |
| 101 | const QRectF &sourceRect, |
| 102 | const QRect &clipRect, |
| 103 | int const_alpha); |
| 104 | |
| 105 | typedef void (*SrcOverTransformFunc)(uchar *destPixels, int dbpl, |
| 106 | const uchar *src, int spbl, |
| 107 | const QRectF &targetRect, |
| 108 | const QRectF &sourceRect, |
| 109 | const QRect &clipRect, |
| 110 | const QTransform &targetRectTransform, |
| 111 | int const_alpha); |
| 112 | |
| 113 | struct DrawHelper { |
| 114 | ProcessSpans blendColor; |
| 115 | BitmapBlitFunc bitmapBlit; |
| 116 | AlphamapBlitFunc alphamapBlit; |
| 117 | AlphaRGBBlitFunc alphaRGBBlit; |
| 118 | RectFillFunc fillRect; |
| 119 | }; |
| 120 | |
| 121 | extern SrcOverBlendFunc qBlendFunctions[QImage::NImageFormats][QImage::NImageFormats]; |
| 122 | extern SrcOverScaleFunc qScaleFunctions[QImage::NImageFormats][QImage::NImageFormats]; |
| 123 | extern SrcOverTransformFunc qTransformFunctions[QImage::NImageFormats][QImage::NImageFormats]; |
| 124 | |
| 125 | extern DrawHelper qDrawHelper[QImage::NImageFormats]; |
| 126 | |
| 127 | struct quint24 { |
| 128 | quint24() = default; |
| 129 | quint24(uint value) |
| 130 | { |
| 131 | data[0] = uchar(value >> 16); |
| 132 | data[1] = uchar(value >> 8); |
| 133 | data[2] = uchar(value); |
| 134 | } |
| 135 | operator uint() const |
| 136 | { |
| 137 | return data[2] | (data[1] << 8) | (data[0] << 16); |
| 138 | } |
| 139 | |
| 140 | uchar data[3]; |
| 141 | }; |
| 142 | |
| 143 | void qBlendGradient(int count, const QT_FT_Span *spans, void *userData); |
| 144 | void qBlendTexture(int count, const QT_FT_Span *spans, void *userData); |
| 145 | #ifdef Q_PROCESSOR_X86 |
| 146 | extern void (*qt_memfill64)(quint64 *dest, quint64 value, qsizetype count); |
| 147 | extern void (*qt_memfill32)(quint32 *dest, quint32 value, qsizetype count); |
| 148 | #else |
| 149 | extern void qt_memfill64(quint64 *dest, quint64 value, qsizetype count); |
| 150 | extern void qt_memfill32(quint32 *dest, quint32 value, qsizetype count); |
| 151 | #endif |
| 152 | extern void qt_memfill24(quint24 *dest, quint24 value, qsizetype count); |
| 153 | extern void qt_memfill16(quint16 *dest, quint16 value, qsizetype count); |
| 154 | |
| 155 | typedef void (QT_FASTCALL *CompositionFunction)(uint *Q_DECL_RESTRICT dest, const uint *Q_DECL_RESTRICT src, int length, uint const_alpha); |
| 156 | typedef void (QT_FASTCALL *CompositionFunction64)(QRgba64 *Q_DECL_RESTRICT dest, const QRgba64 *Q_DECL_RESTRICT src, int length, uint const_alpha); |
| 157 | typedef void (QT_FASTCALL *CompositionFunctionFP)(QRgbaFloat32 *Q_DECL_RESTRICT dest, const QRgbaFloat32 *Q_DECL_RESTRICT src, int length, uint const_alpha); |
| 158 | typedef void (QT_FASTCALL *CompositionFunctionSolid)(uint *dest, int length, uint color, uint const_alpha); |
| 159 | typedef void (QT_FASTCALL *CompositionFunctionSolid64)(QRgba64 *dest, int length, QRgba64 color, uint const_alpha); |
| 160 | typedef void (QT_FASTCALL *CompositionFunctionSolidFP)(QRgbaFloat32 *dest, int length, QRgbaFloat32 color, uint const_alpha); |
| 161 | |
| 162 | struct LinearGradientValues |
| 163 | { |
| 164 | qreal dx; |
| 165 | qreal dy; |
| 166 | qreal l; |
| 167 | qreal off; |
| 168 | }; |
| 169 | |
| 170 | struct RadialGradientValues |
| 171 | { |
| 172 | qreal dx; |
| 173 | qreal dy; |
| 174 | qreal dr; |
| 175 | qreal sqrfr; |
| 176 | qreal a; |
| 177 | bool extended; |
| 178 | }; |
| 179 | |
| 180 | struct Operator; |
| 181 | typedef uint* (QT_FASTCALL *DestFetchProc)(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length); |
| 182 | typedef QRgba64* (QT_FASTCALL *DestFetchProc64)(QRgba64 *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length); |
| 183 | typedef QRgbaFloat32* (QT_FASTCALL *DestFetchProcFP)(QRgbaFloat32 *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length); |
| 184 | typedef void (QT_FASTCALL *DestStoreProc)(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length); |
| 185 | typedef void (QT_FASTCALL *DestStoreProc64)(QRasterBuffer *rasterBuffer, int x, int y, const QRgba64 *buffer, int length); |
| 186 | typedef void (QT_FASTCALL *DestStoreProcFP)(QRasterBuffer *rasterBuffer, int x, int y, const QRgbaFloat32 *buffer, int length); |
| 187 | typedef const uint* (QT_FASTCALL *SourceFetchProc)(uint *buffer, const Operator *o, const QSpanData *data, int y, int x, int length); |
| 188 | typedef const QRgba64* (QT_FASTCALL *SourceFetchProc64)(QRgba64 *buffer, const Operator *o, const QSpanData *data, int y, int x, int length); |
| 189 | typedef const QRgbaFloat32* (QT_FASTCALL *SourceFetchProcFP)(QRgbaFloat32 *buffer, const Operator *o, const QSpanData *data, int y, int x, int length); |
| 190 | |
| 191 | struct Operator |
| 192 | { |
| 193 | QPainter::CompositionMode mode; |
| 194 | DestFetchProc destFetch; |
| 195 | DestStoreProc destStore; |
| 196 | SourceFetchProc srcFetch; |
| 197 | CompositionFunctionSolid funcSolid; |
| 198 | CompositionFunction func; |
| 199 | |
| 200 | DestFetchProc64 destFetch64; |
| 201 | DestStoreProc64 destStore64; |
| 202 | SourceFetchProc64 srcFetch64; |
| 203 | CompositionFunctionSolid64 funcSolid64; |
| 204 | CompositionFunction64 func64; |
| 205 | |
| 206 | DestFetchProcFP destFetchFP; |
| 207 | DestStoreProcFP destStoreFP; |
| 208 | SourceFetchProcFP srcFetchFP; |
| 209 | CompositionFunctionSolidFP funcSolidFP; |
| 210 | CompositionFunctionFP funcFP; |
| 211 | |
| 212 | union { |
| 213 | LinearGradientValues linear; |
| 214 | RadialGradientValues radial; |
| 215 | }; |
| 216 | }; |
| 217 | |
| 218 | class QRasterPaintEngine; |
| 219 | |
| 220 | struct QLinearGradientData |
| 221 | { |
| 222 | struct { |
| 223 | qreal x; |
| 224 | qreal y; |
| 225 | } origin; |
| 226 | struct { |
| 227 | qreal x; |
| 228 | qreal y; |
| 229 | } end; |
| 230 | }; |
| 231 | |
| 232 | struct QRadialGradientData |
| 233 | { |
| 234 | struct { |
| 235 | qreal x; |
| 236 | qreal y; |
| 237 | qreal radius; |
| 238 | } center; |
| 239 | struct { |
| 240 | qreal x; |
| 241 | qreal y; |
| 242 | qreal radius; |
| 243 | } focal; |
| 244 | }; |
| 245 | |
| 246 | struct QConicalGradientData |
| 247 | { |
| 248 | struct { |
| 249 | qreal x; |
| 250 | qreal y; |
| 251 | } center; |
| 252 | qreal angle; |
| 253 | }; |
| 254 | |
| 255 | struct QGradientData |
| 256 | { |
| 257 | QGradient::Spread spread; |
| 258 | |
| 259 | union { |
| 260 | QLinearGradientData linear; |
| 261 | QRadialGradientData radial; |
| 262 | QConicalGradientData conical; |
| 263 | }; |
| 264 | |
| 265 | #define GRADIENT_STOPTABLE_SIZE 1024 |
| 266 | #define GRADIENT_STOPTABLE_SIZE_SHIFT 10 |
| 267 | |
| 268 | #if QT_CONFIG(raster_64bit) || QT_CONFIG(raster_fp) |
| 269 | const QRgba64 *colorTable64; //[GRADIENT_STOPTABLE_SIZE]; |
| 270 | #endif |
| 271 | const QRgb *colorTable32; //[GRADIENT_STOPTABLE_SIZE]; |
| 272 | |
| 273 | uint alphaColor : 1; |
| 274 | }; |
| 275 | |
| 276 | struct QTextureData |
| 277 | { |
| 278 | const uchar *imageData; |
| 279 | const uchar *scanLine(int y) const { return imageData + y*bytesPerLine; } |
| 280 | |
| 281 | int width; |
| 282 | int height; |
| 283 | // clip rect |
| 284 | int x1; |
| 285 | int y1; |
| 286 | int x2; |
| 287 | int y2; |
| 288 | qsizetype bytesPerLine; |
| 289 | QImage::Format format; |
| 290 | const QList<QRgb> *colorTable; |
| 291 | bool hasAlpha; |
| 292 | enum Type { |
| 293 | Plain, |
| 294 | Tiled, |
| 295 | Pattern |
| 296 | }; |
| 297 | Type type; |
| 298 | int const_alpha; |
| 299 | }; |
| 300 | |
| 301 | struct QSpanData |
| 302 | { |
| 303 | QSpanData() : tempImage(nullptr) {} |
| 304 | ~QSpanData() { delete tempImage; } |
| 305 | |
| 306 | QRasterBuffer *rasterBuffer; |
| 307 | ProcessSpans blend; |
| 308 | ProcessSpans unclipped_blend; |
| 309 | BitmapBlitFunc bitmapBlit; |
| 310 | AlphamapBlitFunc alphamapBlit; |
| 311 | AlphaRGBBlitFunc alphaRGBBlit; |
| 312 | RectFillFunc fillRect; |
| 313 | qreal m11, m12, m13, m21, m22, m23, m33, dx, dy; // inverse xform matrix |
| 314 | const QClipData *clip; |
| 315 | enum Type { |
| 316 | None, |
| 317 | Solid, |
| 318 | LinearGradient, |
| 319 | RadialGradient, |
| 320 | ConicalGradient, |
| 321 | Texture |
| 322 | } type : 8; |
| 323 | signed int txop : 8; |
| 324 | uint fast_matrix : 1; |
| 325 | bool bilinear; |
| 326 | QImage *tempImage; |
| 327 | QColor solidColor; |
| 328 | union { |
| 329 | QGradientData gradient; |
| 330 | QTextureData texture; |
| 331 | }; |
| 332 | std::shared_ptr<const void> cachedGradient; |
| 333 | |
| 334 | |
| 335 | void init(QRasterBuffer *rb, const QRasterPaintEngine *pe); |
| 336 | void setup(const QBrush &brush, int alpha, QPainter::CompositionMode compositionMode, bool isCosmetic); |
| 337 | void setupMatrix(const QTransform &matrix, int bilinear); |
| 338 | void initTexture(const QImage *image, int alpha, QTextureData::Type = QTextureData::Plain, const QRect &sourceRect = QRect()); |
| 339 | void adjustSpanMethods(); |
| 340 | }; |
| 341 | |
| 342 | static inline uint qt_gradient_clamp(const QGradientData *data, int ipos) |
| 343 | { |
| 344 | if (ipos < 0 || ipos >= GRADIENT_STOPTABLE_SIZE) { |
| 345 | if (data->spread == QGradient::RepeatSpread) { |
| 346 | ipos = ipos % GRADIENT_STOPTABLE_SIZE; |
| 347 | ipos = ipos < 0 ? GRADIENT_STOPTABLE_SIZE + ipos : ipos; |
| 348 | } else if (data->spread == QGradient::ReflectSpread) { |
| 349 | const int limit = GRADIENT_STOPTABLE_SIZE * 2; |
| 350 | ipos = ipos % limit; |
| 351 | ipos = ipos < 0 ? limit + ipos : ipos; |
| 352 | ipos = ipos >= GRADIENT_STOPTABLE_SIZE ? limit - 1 - ipos : ipos; |
| 353 | } else { |
| 354 | if (ipos < 0) |
| 355 | ipos = 0; |
| 356 | else if (ipos >= GRADIENT_STOPTABLE_SIZE) |
| 357 | ipos = GRADIENT_STOPTABLE_SIZE-1; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | Q_ASSERT(ipos >= 0); |
| 362 | Q_ASSERT(ipos < GRADIENT_STOPTABLE_SIZE); |
| 363 | |
| 364 | return ipos; |
| 365 | } |
| 366 | |
| 367 | static inline uint qt_gradient_pixel(const QGradientData *data, qreal pos) |
| 368 | { |
| 369 | int ipos = int(pos * (GRADIENT_STOPTABLE_SIZE - 1) + qreal(0.5)); |
| 370 | return data->colorTable32[qt_gradient_clamp(data, ipos)]; |
| 371 | } |
| 372 | |
| 373 | #if QT_CONFIG(raster_64bit) |
| 374 | static inline const QRgba64& qt_gradient_pixel64(const QGradientData *data, qreal pos) |
| 375 | { |
| 376 | int ipos = int(pos * (GRADIENT_STOPTABLE_SIZE - 1) + qreal(0.5)); |
| 377 | return data->colorTable64[qt_gradient_clamp(data, ipos)]; |
| 378 | } |
| 379 | #endif |
| 380 | |
| 381 | static inline qreal qRadialDeterminant(qreal a, qreal b, qreal c) |
| 382 | { |
| 383 | return (b * b) - (4 * a * c); |
| 384 | } |
| 385 | |
| 386 | template <class RadialFetchFunc, typename BlendType> static |
| 387 | const BlendType * QT_FASTCALL qt_fetch_radial_gradient_template(BlendType *buffer, const Operator *op, |
| 388 | const QSpanData *data, int y, int x, int length) |
| 389 | { |
| 390 | // avoid division by zero |
| 391 | if (qFuzzyIsNull(d: op->radial.a)) { |
| 392 | RadialFetchFunc::memfill(buffer, RadialFetchFunc::null(), length); |
| 393 | return buffer; |
| 394 | } |
| 395 | |
| 396 | const BlendType *b = buffer; |
| 397 | qreal rx = data->m21 * (y + qreal(0.5)) |
| 398 | + data->dx + data->m11 * (x + qreal(0.5)); |
| 399 | qreal ry = data->m22 * (y + qreal(0.5)) |
| 400 | + data->dy + data->m12 * (x + qreal(0.5)); |
| 401 | bool affine = !data->m13 && !data->m23; |
| 402 | |
| 403 | BlendType *end = buffer + length; |
| 404 | qreal inv_a = 1 / qreal(2 * op->radial.a); |
| 405 | |
| 406 | if (affine) { |
| 407 | rx -= data->gradient.radial.focal.x; |
| 408 | ry -= data->gradient.radial.focal.y; |
| 409 | |
| 410 | const qreal delta_rx = data->m11; |
| 411 | const qreal delta_ry = data->m12; |
| 412 | |
| 413 | qreal b = 2*(op->radial.dr*data->gradient.radial.focal.radius + rx * op->radial.dx + ry * op->radial.dy); |
| 414 | qreal delta_b = 2*(delta_rx * op->radial.dx + delta_ry * op->radial.dy); |
| 415 | const qreal b_delta_b = 2 * b * delta_b; |
| 416 | const qreal delta_b_delta_b = 2 * delta_b * delta_b; |
| 417 | |
| 418 | const qreal bb = b * b; |
| 419 | const qreal delta_bb = delta_b * delta_b; |
| 420 | |
| 421 | b *= inv_a; |
| 422 | delta_b *= inv_a; |
| 423 | |
| 424 | const qreal rxrxryry = rx * rx + ry * ry; |
| 425 | const qreal delta_rxrxryry = delta_rx * delta_rx + delta_ry * delta_ry; |
| 426 | const qreal rx_plus_ry = 2*(rx * delta_rx + ry * delta_ry); |
| 427 | const qreal delta_rx_plus_ry = 2 * delta_rxrxryry; |
| 428 | |
| 429 | inv_a *= inv_a; |
| 430 | |
| 431 | qreal det = (bb - 4 * op->radial.a * (op->radial.sqrfr - rxrxryry)) * inv_a; |
| 432 | qreal delta_det = (b_delta_b + delta_bb + 4 * op->radial.a * (rx_plus_ry + delta_rxrxryry)) * inv_a; |
| 433 | const qreal delta_delta_det = (delta_b_delta_b + 4 * op->radial.a * delta_rx_plus_ry) * inv_a; |
| 434 | |
| 435 | if (std::isfinite(x: float(det)) && std::isfinite(x: float(delta_det)) |
| 436 | && std::isfinite(x: float(delta_delta_det))) |
| 437 | RadialFetchFunc::fetch(buffer, end, op, data, det, delta_det, delta_delta_det, b, delta_b); |
| 438 | else |
| 439 | RadialFetchFunc::memfill(buffer, RadialFetchFunc::null(), length); |
| 440 | } else { |
| 441 | qreal rw = data->m23 * (y + qreal(0.5)) |
| 442 | + data->m33 + data->m13 * (x + qreal(0.5)); |
| 443 | |
| 444 | while (buffer < end) { |
| 445 | if (rw == 0) { |
| 446 | *buffer = RadialFetchFunc::null(); |
| 447 | } else { |
| 448 | qreal invRw = 1 / rw; |
| 449 | qreal gx = rx * invRw - data->gradient.radial.focal.x; |
| 450 | qreal gy = ry * invRw - data->gradient.radial.focal.y; |
| 451 | qreal b = 2*(op->radial.dr*data->gradient.radial.focal.radius + gx*op->radial.dx + gy*op->radial.dy); |
| 452 | qreal det = qRadialDeterminant(a: op->radial.a, b, c: op->radial.sqrfr - (gx*gx + gy*gy)); |
| 453 | |
| 454 | BlendType result = RadialFetchFunc::null(); |
| 455 | if (det >= 0) { |
| 456 | qreal detSqrt = qSqrt(v: det); |
| 457 | |
| 458 | qreal s0 = (-b - detSqrt) * inv_a; |
| 459 | qreal s1 = (-b + detSqrt) * inv_a; |
| 460 | |
| 461 | qreal s = qMax(a: s0, b: s1); |
| 462 | |
| 463 | if (data->gradient.radial.focal.radius + op->radial.dr * s >= 0) |
| 464 | result = RadialFetchFunc::fetchSingle(data->gradient, s); |
| 465 | } |
| 466 | |
| 467 | *buffer = result; |
| 468 | } |
| 469 | |
| 470 | rx += data->m11; |
| 471 | ry += data->m12; |
| 472 | rw += data->m13; |
| 473 | |
| 474 | ++buffer; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return b; |
| 479 | } |
| 480 | |
| 481 | template <class Simd> |
| 482 | class QRadialFetchSimd |
| 483 | { |
| 484 | public: |
| 485 | static uint null() { return 0; } |
| 486 | static uint fetchSingle(const QGradientData& gradient, qreal v) |
| 487 | { |
| 488 | return qt_gradient_pixel(data: &gradient, pos: v); |
| 489 | } |
| 490 | static void memfill(uint *buffer, uint fill, int length) |
| 491 | { |
| 492 | qt_memfill32(buffer, fill, length); |
| 493 | } |
| 494 | static void fetch(uint *buffer, uint *end, const Operator *op, const QSpanData *data, qreal det, |
| 495 | qreal delta_det, qreal delta_delta_det, qreal b, qreal delta_b) |
| 496 | { |
| 497 | typename Simd::Vect_buffer_f det_vec; |
| 498 | typename Simd::Vect_buffer_f delta_det4_vec; |
| 499 | typename Simd::Vect_buffer_f b_vec; |
| 500 | |
| 501 | for (int i = 0; i < 4; ++i) { |
| 502 | det_vec.f[i] = det; |
| 503 | delta_det4_vec.f[i] = 4 * delta_det; |
| 504 | b_vec.f[i] = b; |
| 505 | |
| 506 | det += delta_det; |
| 507 | delta_det += delta_delta_det; |
| 508 | b += delta_b; |
| 509 | } |
| 510 | |
| 511 | const typename Simd::Float32x4 v_delta_delta_det16 = Simd::v_dup(16 * delta_delta_det); |
| 512 | const typename Simd::Float32x4 v_delta_delta_det6 = Simd::v_dup(6 * delta_delta_det); |
| 513 | const typename Simd::Float32x4 v_delta_b4 = Simd::v_dup(4 * delta_b); |
| 514 | |
| 515 | const typename Simd::Float32x4 v_r0 = Simd::v_dup(data->gradient.radial.focal.radius); |
| 516 | const typename Simd::Float32x4 v_dr = Simd::v_dup(op->radial.dr); |
| 517 | |
| 518 | #if defined(__ARM_NEON__) |
| 519 | // NEON doesn't have SIMD sqrt, but uses rsqrt instead that can't be taken of 0. |
| 520 | const typename Simd::Float32x4 v_min = Simd::v_dup(std::numeric_limits<float>::epsilon()); |
| 521 | #else |
| 522 | const typename Simd::Float32x4 v_min = Simd::v_dup(0.0f); |
| 523 | #endif |
| 524 | const typename Simd::Float32x4 v_max = Simd::v_dup(float(GRADIENT_STOPTABLE_SIZE-1)); |
| 525 | const typename Simd::Float32x4 v_half = Simd::v_dup(0.5f); |
| 526 | |
| 527 | const typename Simd::Int32x4 v_repeat_mask = Simd::v_dup(~(uint(0xffffff) << GRADIENT_STOPTABLE_SIZE_SHIFT)); |
| 528 | const typename Simd::Int32x4 v_reflect_mask = Simd::v_dup(~(uint(0xffffff) << (GRADIENT_STOPTABLE_SIZE_SHIFT+1))); |
| 529 | |
| 530 | const typename Simd::Int32x4 v_reflect_limit = Simd::v_dup(2 * GRADIENT_STOPTABLE_SIZE - 1); |
| 531 | |
| 532 | const int extended_mask = op->radial.extended ? 0x0 : ~0x0; |
| 533 | |
| 534 | #define FETCH_RADIAL_LOOP_PROLOGUE \ |
| 535 | while (buffer < end) { \ |
| 536 | typename Simd::Vect_buffer_i v_buffer_mask; \ |
| 537 | v_buffer_mask.v = Simd::v_greaterOrEqual(det_vec.v, v_min); \ |
| 538 | const typename Simd::Float32x4 v_index_local = Simd::v_sub(Simd::v_sqrt(Simd::v_max(v_min, det_vec.v)), b_vec.v); \ |
| 539 | const typename Simd::Float32x4 v_index = Simd::v_add(Simd::v_mul(v_index_local, v_max), v_half); \ |
| 540 | v_buffer_mask.v = Simd::v_and(v_buffer_mask.v, Simd::v_greaterOrEqual(Simd::v_add(v_r0, Simd::v_mul(v_dr, v_index_local)), v_min)); \ |
| 541 | typename Simd::Vect_buffer_i index_vec; |
| 542 | #define FETCH_RADIAL_LOOP_CLAMP_REPEAT \ |
| 543 | index_vec.v = Simd::v_and(v_repeat_mask, Simd::v_toInt(v_index)); |
| 544 | #define FETCH_RADIAL_LOOP_CLAMP_REFLECT \ |
| 545 | const typename Simd::Int32x4 v_index_i = Simd::v_and(v_reflect_mask, Simd::v_toInt(v_index)); \ |
| 546 | const typename Simd::Int32x4 v_index_i_inv = Simd::v_sub(v_reflect_limit, v_index_i); \ |
| 547 | index_vec.v = Simd::v_min_16(v_index_i, v_index_i_inv); |
| 548 | #define FETCH_RADIAL_LOOP_CLAMP_PAD \ |
| 549 | index_vec.v = Simd::v_toInt(Simd::v_min(v_max, Simd::v_max(v_min, v_index))); |
| 550 | #define FETCH_RADIAL_LOOP_EPILOGUE \ |
| 551 | det_vec.v = Simd::v_add(Simd::v_add(det_vec.v, delta_det4_vec.v), v_delta_delta_det6); \ |
| 552 | delta_det4_vec.v = Simd::v_add(delta_det4_vec.v, v_delta_delta_det16); \ |
| 553 | b_vec.v = Simd::v_add(b_vec.v, v_delta_b4); \ |
| 554 | for (int i = 0; i < 4; ++i) \ |
| 555 | *buffer++ = (extended_mask | v_buffer_mask.i[i]) & data->gradient.colorTable32[index_vec.i[i]]; \ |
| 556 | } |
| 557 | |
| 558 | #define FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP) \ |
| 559 | FETCH_RADIAL_LOOP_PROLOGUE \ |
| 560 | FETCH_RADIAL_LOOP_CLAMP \ |
| 561 | FETCH_RADIAL_LOOP_EPILOGUE |
| 562 | |
| 563 | switch (data->gradient.spread) { |
| 564 | case QGradient::RepeatSpread: |
| 565 | FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_REPEAT) |
| 566 | break; |
| 567 | case QGradient::ReflectSpread: |
| 568 | FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_REFLECT) |
| 569 | break; |
| 570 | case QGradient::PadSpread: |
| 571 | FETCH_RADIAL_LOOP(FETCH_RADIAL_LOOP_CLAMP_PAD) |
| 572 | break; |
| 573 | default: |
| 574 | Q_UNREACHABLE(); |
| 575 | } |
| 576 | } |
| 577 | }; |
| 578 | |
| 579 | static inline uint INTERPOLATE_PIXEL_255(uint x, uint a, uint y, uint b) { |
| 580 | uint t = (x & 0xff00ff) * a + (y & 0xff00ff) * b; |
| 581 | t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8; |
| 582 | t &= 0xff00ff; |
| 583 | |
| 584 | x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b; |
| 585 | x = (x + ((x >> 8) & 0xff00ff) + 0x800080); |
| 586 | x &= 0xff00ff00; |
| 587 | x |= t; |
| 588 | return x; |
| 589 | } |
| 590 | |
| 591 | #if Q_PROCESSOR_WORDSIZE == 8 // 64-bit versions |
| 592 | |
| 593 | static inline uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b) { |
| 594 | quint64 t = (((quint64(x)) | ((quint64(x)) << 24)) & 0x00ff00ff00ff00ff) * a; |
| 595 | t += (((quint64(y)) | ((quint64(y)) << 24)) & 0x00ff00ff00ff00ff) * b; |
| 596 | t >>= 8; |
| 597 | t &= 0x00ff00ff00ff00ff; |
| 598 | return (uint(t)) | (uint(t >> 24)); |
| 599 | } |
| 600 | |
| 601 | static inline uint BYTE_MUL(uint x, uint a) { |
| 602 | quint64 t = (((quint64(x)) | ((quint64(x)) << 24)) & 0x00ff00ff00ff00ff) * a; |
| 603 | t = (t + ((t >> 8) & 0xff00ff00ff00ff) + 0x80008000800080) >> 8; |
| 604 | t &= 0x00ff00ff00ff00ff; |
| 605 | return (uint(t)) | (uint(t >> 24)); |
| 606 | } |
| 607 | |
| 608 | #else // 32-bit versions |
| 609 | |
| 610 | static inline uint INTERPOLATE_PIXEL_256(uint x, uint a, uint y, uint b) { |
| 611 | uint t = (x & 0xff00ff) * a + (y & 0xff00ff) * b; |
| 612 | t >>= 8; |
| 613 | t &= 0xff00ff; |
| 614 | |
| 615 | x = ((x >> 8) & 0xff00ff) * a + ((y >> 8) & 0xff00ff) * b; |
| 616 | x &= 0xff00ff00; |
| 617 | x |= t; |
| 618 | return x; |
| 619 | } |
| 620 | |
| 621 | static inline uint BYTE_MUL(uint x, uint a) { |
| 622 | uint t = (x & 0xff00ff) * a; |
| 623 | t = (t + ((t >> 8) & 0xff00ff) + 0x800080) >> 8; |
| 624 | t &= 0xff00ff; |
| 625 | |
| 626 | x = ((x >> 8) & 0xff00ff) * a; |
| 627 | x = (x + ((x >> 8) & 0xff00ff) + 0x800080); |
| 628 | x &= 0xff00ff00; |
| 629 | x |= t; |
| 630 | return x; |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | static inline void blend_pixel(quint32 &dst, const quint32 src) |
| 635 | { |
| 636 | if (src >= 0xff000000) |
| 637 | dst = src; |
| 638 | else if (src != 0) |
| 639 | dst = src + BYTE_MUL(x: dst, a: qAlpha(rgb: ~src)); |
| 640 | } |
| 641 | |
| 642 | static inline void blend_pixel(quint32 &dst, const quint32 src, const int const_alpha) |
| 643 | { |
| 644 | if (const_alpha == 255) |
| 645 | return blend_pixel(dst, src); |
| 646 | if (src != 0) { |
| 647 | const quint32 s = BYTE_MUL(x: src, a: const_alpha); |
| 648 | dst = s + BYTE_MUL(x: dst, a: qAlpha(rgb: ~s)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | #if defined(__SSE2__) |
| 653 | static inline uint Q_DECL_VECTORCALL interpolate_4_pixels_sse2(__m128i vt, __m128i vb, uint distx, uint disty) |
| 654 | { |
| 655 | // First interpolate top and bottom pixels in parallel. |
| 656 | vt = _mm_unpacklo_epi8(a: vt, b: _mm_setzero_si128()); |
| 657 | vb = _mm_unpacklo_epi8(a: vb, b: _mm_setzero_si128()); |
| 658 | vt = _mm_mullo_epi16(a: vt, b: _mm_set1_epi16(w: 256 - disty)); |
| 659 | vb = _mm_mullo_epi16(a: vb, b: _mm_set1_epi16(w: disty)); |
| 660 | __m128i vlr = _mm_add_epi16(a: vt, b: vb); |
| 661 | vlr = _mm_srli_epi16(a: vlr, count: 8); |
| 662 | // vlr now contains the result of the first two interpolate calls vlr = unpacked((xright << 64) | xleft) |
| 663 | |
| 664 | // Now the last interpolate between left and right.. |
| 665 | const __m128i vidistx = _mm_shufflelo_epi16(_mm_cvtsi32_si128(256 - distx), _MM_SHUFFLE(0, 0, 0, 0)); |
| 666 | const __m128i vdistx = _mm_shufflelo_epi16(_mm_cvtsi32_si128(distx), _MM_SHUFFLE(0, 0, 0, 0)); |
| 667 | const __m128i vmulx = _mm_unpacklo_epi16(a: vidistx, b: vdistx); |
| 668 | vlr = _mm_unpacklo_epi16(a: vlr, _mm_srli_si128(vlr, 8)); |
| 669 | // vlr now contains the colors of left and right interleaved { la, ra, lr, rr, lg, rg, lb, rb } |
| 670 | vlr = _mm_madd_epi16(a: vlr, b: vmulx); // Multiply and horizontal add. |
| 671 | vlr = _mm_srli_epi32(a: vlr, count: 8); |
| 672 | vlr = _mm_packs_epi32(a: vlr, b: vlr); |
| 673 | vlr = _mm_packus_epi16(a: vlr, b: vlr); |
| 674 | return _mm_cvtsi128_si32(a: vlr); |
| 675 | } |
| 676 | |
| 677 | static inline uint interpolate_4_pixels(uint tl, uint tr, uint bl, uint br, uint distx, uint disty) |
| 678 | { |
| 679 | __m128i vt = _mm_unpacklo_epi32(a: _mm_cvtsi32_si128(a: tl), b: _mm_cvtsi32_si128(a: tr)); |
| 680 | __m128i vb = _mm_unpacklo_epi32(a: _mm_cvtsi32_si128(a: bl), b: _mm_cvtsi32_si128(a: br)); |
| 681 | return interpolate_4_pixels_sse2(vt, vb, distx, disty); |
| 682 | } |
| 683 | |
| 684 | static inline uint interpolate_4_pixels(const uint t[], const uint b[], uint distx, uint disty) |
| 685 | { |
| 686 | __m128i vt = _mm_loadl_epi64(p: (const __m128i*)t); |
| 687 | __m128i vb = _mm_loadl_epi64(p: (const __m128i*)b); |
| 688 | return interpolate_4_pixels_sse2(vt, vb, distx, disty); |
| 689 | } |
| 690 | |
| 691 | static constexpr inline bool hasFastInterpolate4() { return true; } |
| 692 | |
| 693 | #elif defined(__ARM_NEON__) |
| 694 | static inline uint interpolate_4_pixels_neon(uint32x2_t vt32, uint32x2_t vb32, uint distx, uint disty) |
| 695 | { |
| 696 | uint16x8_t vt16 = vmovl_u8(vreinterpret_u8_u32(vt32)); |
| 697 | uint16x8_t vb16 = vmovl_u8(vreinterpret_u8_u32(vb32)); |
| 698 | vt16 = vmulq_n_u16(vt16, 256 - disty); |
| 699 | vt16 = vmlaq_n_u16(vt16, vb16, disty); |
| 700 | vt16 = vshrq_n_u16(vt16, 8); |
| 701 | uint16x4_t vl16 = vget_low_u16(vt16); |
| 702 | uint16x4_t vr16 = vget_high_u16(vt16); |
| 703 | vl16 = vmul_n_u16(vl16, 256 - distx); |
| 704 | vl16 = vmla_n_u16(vl16, vr16, distx); |
| 705 | vl16 = vshr_n_u16(vl16, 8); |
| 706 | uint8x8_t vr = vmovn_u16(vcombine_u16(vl16, vl16)); |
| 707 | return vget_lane_u32(vreinterpret_u32_u8(vr), 0); |
| 708 | } |
| 709 | |
| 710 | static inline uint interpolate_4_pixels(uint tl, uint tr, uint bl, uint br, uint distx, uint disty) |
| 711 | { |
| 712 | uint32x2_t vt32 = vmov_n_u32(tl); |
| 713 | uint32x2_t vb32 = vmov_n_u32(bl); |
| 714 | vt32 = vset_lane_u32(tr, vt32, 1); |
| 715 | vb32 = vset_lane_u32(br, vb32, 1); |
| 716 | return interpolate_4_pixels_neon(vt32, vb32, distx, disty); |
| 717 | } |
| 718 | |
| 719 | static inline uint interpolate_4_pixels(const uint t[], const uint b[], uint distx, uint disty) |
| 720 | { |
| 721 | uint32x2_t vt32 = vld1_u32(t); |
| 722 | uint32x2_t vb32 = vld1_u32(b); |
| 723 | return interpolate_4_pixels_neon(vt32, vb32, distx, disty); |
| 724 | } |
| 725 | |
| 726 | static constexpr inline bool hasFastInterpolate4() { return true; } |
| 727 | |
| 728 | #else |
| 729 | static inline uint interpolate_4_pixels(uint tl, uint tr, uint bl, uint br, uint distx, uint disty) |
| 730 | { |
| 731 | uint idistx = 256 - distx; |
| 732 | uint idisty = 256 - disty; |
| 733 | uint xtop = INTERPOLATE_PIXEL_256(tl, idistx, tr, distx); |
| 734 | uint xbot = INTERPOLATE_PIXEL_256(bl, idistx, br, distx); |
| 735 | return INTERPOLATE_PIXEL_256(xtop, idisty, xbot, disty); |
| 736 | } |
| 737 | |
| 738 | static inline uint interpolate_4_pixels(const uint t[], const uint b[], uint distx, uint disty) |
| 739 | { |
| 740 | return interpolate_4_pixels(t[0], t[1], b[0], b[1], distx, disty); |
| 741 | } |
| 742 | |
| 743 | static constexpr inline bool hasFastInterpolate4() { return false; } |
| 744 | |
| 745 | #endif |
| 746 | |
| 747 | static inline QRgba64 multiplyAlpha256(QRgba64 rgba64, uint alpha256) |
| 748 | { |
| 749 | return QRgba64::fromRgba64(red: (rgba64.red() * alpha256) >> 8, |
| 750 | green: (rgba64.green() * alpha256) >> 8, |
| 751 | blue: (rgba64.blue() * alpha256) >> 8, |
| 752 | alpha: (rgba64.alpha() * alpha256) >> 8); |
| 753 | } |
| 754 | static inline QRgba64 interpolate256(QRgba64 x, uint alpha1, QRgba64 y, uint alpha2) |
| 755 | { |
| 756 | return QRgba64::fromRgba64(c: multiplyAlpha256(rgba64: x, alpha256: alpha1) + multiplyAlpha256(rgba64: y, alpha256: alpha2)); |
| 757 | } |
| 758 | |
| 759 | #ifdef __SSE2__ |
| 760 | static inline QRgba64 interpolate_4_pixels_rgb64(const QRgba64 t[], const QRgba64 b[], uint distx, uint disty) |
| 761 | { |
| 762 | __m128i vt = _mm_loadu_si128(p: (const __m128i*)t); |
| 763 | if (disty) { |
| 764 | __m128i vb = _mm_loadu_si128(p: (const __m128i*)b); |
| 765 | vt = _mm_mulhi_epu16(a: vt, b: _mm_set1_epi16(w: 0x10000 - disty)); |
| 766 | vb = _mm_mulhi_epu16(a: vb, b: _mm_set1_epi16(w: disty)); |
| 767 | vt = _mm_add_epi16(a: vt, b: vb); |
| 768 | } |
| 769 | if (distx) { |
| 770 | const __m128i vdistx = _mm_shufflelo_epi16(_mm_cvtsi32_si128(distx), _MM_SHUFFLE(0, 0, 0, 0)); |
| 771 | const __m128i vidistx = _mm_shufflelo_epi16(_mm_cvtsi32_si128(0x10000 - distx), _MM_SHUFFLE(0, 0, 0, 0)); |
| 772 | vt = _mm_mulhi_epu16(a: vt, b: _mm_unpacklo_epi64(a: vidistx, b: vdistx)); |
| 773 | vt = _mm_add_epi16(a: vt, _mm_srli_si128(vt, 8)); |
| 774 | } |
| 775 | #ifdef Q_PROCESSOR_X86_64 |
| 776 | return QRgba64::fromRgba64(c: _mm_cvtsi128_si64(a: vt)); |
| 777 | #else |
| 778 | QRgba64 out; |
| 779 | _mm_storel_epi64((__m128i*)&out, vt); |
| 780 | return out; |
| 781 | #endif // Q_PROCESSOR_X86_64 |
| 782 | } |
| 783 | #elif defined(__ARM_NEON__) |
| 784 | static inline QRgba64 interpolate_4_pixels_rgb64(const QRgba64 t[], const QRgba64 b[], uint distx, uint disty) |
| 785 | { |
| 786 | uint64x1x2_t vt = vld2_u64(reinterpret_cast<const uint64_t *>(t)); |
| 787 | if (disty) { |
| 788 | uint64x1x2_t vb = vld2_u64(reinterpret_cast<const uint64_t *>(b)); |
| 789 | uint32x4_t vt0 = vmull_n_u16(vreinterpret_u16_u64(vt.val[0]), 0x10000 - disty); |
| 790 | uint32x4_t vt1 = vmull_n_u16(vreinterpret_u16_u64(vt.val[1]), 0x10000 - disty); |
| 791 | vt0 = vmlal_n_u16(vt0, vreinterpret_u16_u64(vb.val[0]), disty); |
| 792 | vt1 = vmlal_n_u16(vt1, vreinterpret_u16_u64(vb.val[1]), disty); |
| 793 | vt.val[0] = vreinterpret_u64_u16(vshrn_n_u32(vt0, 16)); |
| 794 | vt.val[1] = vreinterpret_u64_u16(vshrn_n_u32(vt1, 16)); |
| 795 | } |
| 796 | if (distx) { |
| 797 | uint32x4_t vt0 = vmull_n_u16(vreinterpret_u16_u64(vt.val[0]), 0x10000 - distx); |
| 798 | vt0 = vmlal_n_u16(vt0, vreinterpret_u16_u64(vt.val[1]), distx); |
| 799 | vt.val[0] = vreinterpret_u64_u16(vshrn_n_u32(vt0, 16)); |
| 800 | } |
| 801 | QRgba64 out; |
| 802 | vst1_u64(reinterpret_cast<uint64_t *>(&out), vt.val[0]); |
| 803 | return out; |
| 804 | } |
| 805 | #else |
| 806 | static inline QRgba64 interpolate_4_pixels_rgb64(const QRgba64 t[], const QRgba64 b[], uint distx, uint disty) |
| 807 | { |
| 808 | const uint dx = distx>>8; |
| 809 | const uint dy = disty>>8; |
| 810 | const uint idx = 256 - dx; |
| 811 | const uint idy = 256 - dy; |
| 812 | QRgba64 xtop = interpolate256(t[0], idx, t[1], dx); |
| 813 | QRgba64 xbot = interpolate256(b[0], idx, b[1], dx); |
| 814 | return interpolate256(xtop, idy, xbot, dy); |
| 815 | } |
| 816 | #endif // __SSE2__ |
| 817 | |
| 818 | #if QT_CONFIG(raster_fp) |
| 819 | static inline QRgbaFloat32 multiplyAlpha_rgba32f(QRgbaFloat32 c, float a) |
| 820 | { |
| 821 | return QRgbaFloat32 { .r: c.r * a, .g: c.g * a, .b: c.b * a, .a: c.a * a }; |
| 822 | } |
| 823 | |
| 824 | static inline QRgbaFloat32 interpolate_rgba32f(QRgbaFloat32 x, float alpha1, QRgbaFloat32 y, float alpha2) |
| 825 | { |
| 826 | x = multiplyAlpha_rgba32f(c: x, a: alpha1); |
| 827 | y = multiplyAlpha_rgba32f(c: y, a: alpha2); |
| 828 | return QRgbaFloat32 { .r: x.r + y.r, .g: x.g + y.g, .b: x.b + y.b, .a: x.a + y.a }; |
| 829 | } |
| 830 | #ifdef __SSE2__ |
| 831 | static inline __m128 Q_DECL_VECTORCALL interpolate_rgba32f(__m128 x, __m128 alpha1, __m128 y, __m128 alpha2) |
| 832 | { |
| 833 | return _mm_add_ps(a: _mm_mul_ps(a: x, b: alpha1), b: _mm_mul_ps(a: y, b: alpha2)); |
| 834 | } |
| 835 | #endif |
| 836 | |
| 837 | static inline QRgbaFloat32 interpolate_4_pixels_rgba32f(const QRgbaFloat32 t[], const QRgbaFloat32 b[], uint distx, uint disty) |
| 838 | { |
| 839 | constexpr float f = 1.0f / 65536.0f; |
| 840 | const float dx = distx * f; |
| 841 | const float dy = disty * f; |
| 842 | const float idx = 1.0f - dx; |
| 843 | const float idy = 1.0f - dy; |
| 844 | #ifdef __SSE2__ |
| 845 | const __m128 vtl = _mm_load_ps(p: (const float *)&t[0]); |
| 846 | const __m128 vtr = _mm_load_ps(p: (const float *)&t[1]); |
| 847 | const __m128 vbl = _mm_load_ps(p: (const float *)&b[0]); |
| 848 | const __m128 vbr = _mm_load_ps(p: (const float *)&b[1]); |
| 849 | |
| 850 | const __m128 vdx = _mm_set1_ps(w: dx); |
| 851 | const __m128 vidx = _mm_set1_ps(w: idx); |
| 852 | __m128 vt = interpolate_rgba32f(x: vtl, alpha1: vidx, y: vtr, alpha2: vdx); |
| 853 | __m128 vb = interpolate_rgba32f(x: vbl, alpha1: vidx, y: vbr, alpha2: vdx); |
| 854 | const __m128 vdy = _mm_set1_ps(w: dy); |
| 855 | const __m128 vidy = _mm_set1_ps(w: idy); |
| 856 | vt = interpolate_rgba32f(x: vt, alpha1: vidy, y: vb, alpha2: vdy); |
| 857 | QRgbaFloat32 res; |
| 858 | _mm_store_ps(p: (float*)&res, a: vt); |
| 859 | return res; |
| 860 | #else |
| 861 | QRgbaFloat32 xtop = interpolate_rgba32f(t[0], idx, t[1], dx); |
| 862 | QRgbaFloat32 xbot = interpolate_rgba32f(b[0], idx, b[1], dx); |
| 863 | xtop = interpolate_rgba32f(xtop, idy, xbot, dy); |
| 864 | return xtop; |
| 865 | #endif |
| 866 | } |
| 867 | #endif // QT_CONFIG(raster_fp) |
| 868 | |
| 869 | static inline uint BYTE_MUL_RGB16(uint x, uint a) { |
| 870 | a += 1; |
| 871 | uint t = (((x & 0x07e0)*a) >> 8) & 0x07e0; |
| 872 | t |= (((x & 0xf81f)*(a>>2)) >> 6) & 0xf81f; |
| 873 | return t; |
| 874 | } |
| 875 | |
| 876 | static inline uint BYTE_MUL_RGB16_32(uint x, uint a) { |
| 877 | uint t = (((x & 0xf81f07e0) >> 5)*a) & 0xf81f07e0; |
| 878 | t |= (((x & 0x07e0f81f)*a) >> 5) & 0x07e0f81f; |
| 879 | return t; |
| 880 | } |
| 881 | |
| 882 | // qt_div_255 is a fast rounded division by 255 using an approximation that is accurate for all positive 16-bit integers |
| 883 | static constexpr inline int qt_div_255(int x) { return (x + (x>>8) + 0x80) >> 8; } |
| 884 | static constexpr inline uint qt_div_257_floor(uint x) { return (x - (x >> 8)) >> 8; } |
| 885 | static constexpr inline uint qt_div_257(uint x) { return qt_div_257_floor(x: x + 128); } |
| 886 | static constexpr inline uint qt_div_65535(uint x) { return (x + (x>>16) + 0x8000U) >> 16; } |
| 887 | |
| 888 | template <class T> inline void qt_memfill_template(T *dest, T color, qsizetype count) |
| 889 | { |
| 890 | if (!count) |
| 891 | return; |
| 892 | |
| 893 | qsizetype n = (count + 7) / 8; |
| 894 | switch (count & 0x07) |
| 895 | { |
| 896 | case 0: do { *dest++ = color; Q_FALLTHROUGH(); |
| 897 | case 7: *dest++ = color; Q_FALLTHROUGH(); |
| 898 | case 6: *dest++ = color; Q_FALLTHROUGH(); |
| 899 | case 5: *dest++ = color; Q_FALLTHROUGH(); |
| 900 | case 4: *dest++ = color; Q_FALLTHROUGH(); |
| 901 | case 3: *dest++ = color; Q_FALLTHROUGH(); |
| 902 | case 2: *dest++ = color; Q_FALLTHROUGH(); |
| 903 | case 1: *dest++ = color; |
| 904 | } while (--n > 0); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | template <class T> inline void qt_memfill(T *dest, T value, qsizetype count) |
| 909 | { |
| 910 | qt_memfill_template(dest, value, count); |
| 911 | } |
| 912 | |
| 913 | template<> inline void qt_memfill(quint64 *dest, quint64 color, qsizetype count) |
| 914 | { |
| 915 | qt_memfill64(dest, color, count); |
| 916 | } |
| 917 | |
| 918 | template<> inline void qt_memfill(quint32 *dest, quint32 color, qsizetype count) |
| 919 | { |
| 920 | qt_memfill32(dest, color, count); |
| 921 | } |
| 922 | |
| 923 | template<> inline void qt_memfill(quint24 *dest, quint24 color, qsizetype count) |
| 924 | { |
| 925 | qt_memfill24(dest, value: color, count); |
| 926 | } |
| 927 | |
| 928 | template<> inline void qt_memfill(quint16 *dest, quint16 color, qsizetype count) |
| 929 | { |
| 930 | qt_memfill16(dest, value: color, count); |
| 931 | } |
| 932 | |
| 933 | template<> inline void qt_memfill(quint8 *dest, quint8 color, qsizetype count) |
| 934 | { |
| 935 | memset(s: dest, c: color, n: count); |
| 936 | } |
| 937 | |
| 938 | template <class T> static |
| 939 | inline void qt_rectfill(T *dest, T value, |
| 940 | int x, int y, int width, int height, qsizetype stride) |
| 941 | { |
| 942 | char *d = reinterpret_cast<char*>(dest + x) + y * stride; |
| 943 | if (uint(stride) == (width * sizeof(T))) { |
| 944 | qt_memfill(reinterpret_cast<T*>(d), value, qsizetype(width) * height); |
| 945 | } else { |
| 946 | for (int j = 0; j < height; ++j) { |
| 947 | dest = reinterpret_cast<T*>(d); |
| 948 | qt_memfill(dest, value, width); |
| 949 | d += stride; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | inline ushort qConvertRgb32To16(uint c) |
| 955 | { |
| 956 | return (((c) >> 3) & 0x001f) |
| 957 | | (((c) >> 5) & 0x07e0) |
| 958 | | (((c) >> 8) & 0xf800); |
| 959 | } |
| 960 | |
| 961 | inline QRgb qConvertRgb16To32(uint c) |
| 962 | { |
| 963 | return 0xff000000 |
| 964 | | ((((c) << 3) & 0xf8) | (((c) >> 2) & 0x7)) |
| 965 | | ((((c) << 5) & 0xfc00) | (((c) >> 1) & 0x300)) |
| 966 | | ((((c) << 8) & 0xf80000) | (((c) << 3) & 0x70000)); |
| 967 | } |
| 968 | |
| 969 | const uint qt_bayer_matrix[16][16] = { |
| 970 | { 0x1, 0xc0, 0x30, 0xf0, 0xc, 0xcc, 0x3c, 0xfc, |
| 971 | 0x3, 0xc3, 0x33, 0xf3, 0xf, 0xcf, 0x3f, 0xff}, |
| 972 | { 0x80, 0x40, 0xb0, 0x70, 0x8c, 0x4c, 0xbc, 0x7c, |
| 973 | 0x83, 0x43, 0xb3, 0x73, 0x8f, 0x4f, 0xbf, 0x7f}, |
| 974 | { 0x20, 0xe0, 0x10, 0xd0, 0x2c, 0xec, 0x1c, 0xdc, |
| 975 | 0x23, 0xe3, 0x13, 0xd3, 0x2f, 0xef, 0x1f, 0xdf}, |
| 976 | { 0xa0, 0x60, 0x90, 0x50, 0xac, 0x6c, 0x9c, 0x5c, |
| 977 | 0xa3, 0x63, 0x93, 0x53, 0xaf, 0x6f, 0x9f, 0x5f}, |
| 978 | { 0x8, 0xc8, 0x38, 0xf8, 0x4, 0xc4, 0x34, 0xf4, |
| 979 | 0xb, 0xcb, 0x3b, 0xfb, 0x7, 0xc7, 0x37, 0xf7}, |
| 980 | { 0x88, 0x48, 0xb8, 0x78, 0x84, 0x44, 0xb4, 0x74, |
| 981 | 0x8b, 0x4b, 0xbb, 0x7b, 0x87, 0x47, 0xb7, 0x77}, |
| 982 | { 0x28, 0xe8, 0x18, 0xd8, 0x24, 0xe4, 0x14, 0xd4, |
| 983 | 0x2b, 0xeb, 0x1b, 0xdb, 0x27, 0xe7, 0x17, 0xd7}, |
| 984 | { 0xa8, 0x68, 0x98, 0x58, 0xa4, 0x64, 0x94, 0x54, |
| 985 | 0xab, 0x6b, 0x9b, 0x5b, 0xa7, 0x67, 0x97, 0x57}, |
| 986 | { 0x2, 0xc2, 0x32, 0xf2, 0xe, 0xce, 0x3e, 0xfe, |
| 987 | 0x1, 0xc1, 0x31, 0xf1, 0xd, 0xcd, 0x3d, 0xfd}, |
| 988 | { 0x82, 0x42, 0xb2, 0x72, 0x8e, 0x4e, 0xbe, 0x7e, |
| 989 | 0x81, 0x41, 0xb1, 0x71, 0x8d, 0x4d, 0xbd, 0x7d}, |
| 990 | { 0x22, 0xe2, 0x12, 0xd2, 0x2e, 0xee, 0x1e, 0xde, |
| 991 | 0x21, 0xe1, 0x11, 0xd1, 0x2d, 0xed, 0x1d, 0xdd}, |
| 992 | { 0xa2, 0x62, 0x92, 0x52, 0xae, 0x6e, 0x9e, 0x5e, |
| 993 | 0xa1, 0x61, 0x91, 0x51, 0xad, 0x6d, 0x9d, 0x5d}, |
| 994 | { 0xa, 0xca, 0x3a, 0xfa, 0x6, 0xc6, 0x36, 0xf6, |
| 995 | 0x9, 0xc9, 0x39, 0xf9, 0x5, 0xc5, 0x35, 0xf5}, |
| 996 | { 0x8a, 0x4a, 0xba, 0x7a, 0x86, 0x46, 0xb6, 0x76, |
| 997 | 0x89, 0x49, 0xb9, 0x79, 0x85, 0x45, 0xb5, 0x75}, |
| 998 | { 0x2a, 0xea, 0x1a, 0xda, 0x26, 0xe6, 0x16, 0xd6, |
| 999 | 0x29, 0xe9, 0x19, 0xd9, 0x25, 0xe5, 0x15, 0xd5}, |
| 1000 | { 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56, |
| 1001 | 0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55} |
| 1002 | }; |
| 1003 | |
| 1004 | #define ARGB_COMBINE_ALPHA(argb, alpha) \ |
| 1005 | ((((argb >> 24) * alpha) >> 8) << 24) | (argb & 0x00ffffff) |
| 1006 | |
| 1007 | |
| 1008 | #if Q_PROCESSOR_WORDSIZE == 8 // 64-bit versions |
| 1009 | #define AMIX(mask) (qMin(((quint64(s)&mask) + (quint64(d)&mask)), quint64(mask))) |
| 1010 | #define MIX(mask) (qMin(((quint64(s)&mask) + (quint64(d)&mask)), quint64(mask))) |
| 1011 | #else // 32 bits |
| 1012 | // The mask for alpha can overflow over 32 bits |
| 1013 | #define AMIX(mask) quint32(qMin(((quint64(s)&mask) + (quint64(d)&mask)), quint64(mask))) |
| 1014 | #define MIX(mask) (qMin(((quint32(s)&mask) + (quint32(d)&mask)), quint32(mask))) |
| 1015 | #endif |
| 1016 | |
| 1017 | inline uint comp_func_Plus_one_pixel_const_alpha(uint d, const uint s, const uint const_alpha, const uint one_minus_const_alpha) |
| 1018 | { |
| 1019 | const uint result = uint(AMIX(AMASK) | MIX(RMASK) | MIX(GMASK) | MIX(BMASK)); |
| 1020 | return INTERPOLATE_PIXEL_255(x: result, a: const_alpha, y: d, b: one_minus_const_alpha); |
| 1021 | } |
| 1022 | |
| 1023 | inline uint comp_func_Plus_one_pixel(uint d, const uint s) |
| 1024 | { |
| 1025 | const uint result = uint(AMIX(AMASK) | MIX(RMASK) | MIX(GMASK) | MIX(BMASK)); |
| 1026 | return result; |
| 1027 | } |
| 1028 | |
| 1029 | #undef MIX |
| 1030 | #undef AMIX |
| 1031 | |
| 1032 | // must be multiple of 4 for easier SIMD implementations |
| 1033 | static constexpr int BufferSize = 2048; |
| 1034 | |
| 1035 | // A buffer of intermediate results used by simple bilinear scaling. |
| 1036 | struct IntermediateBuffer |
| 1037 | { |
| 1038 | // The idea is first to do the interpolation between the row s1 and the row s2 |
| 1039 | // into this intermediate buffer, then later interpolate between two pixel of this buffer. |
| 1040 | // |
| 1041 | // buffer_rb is a buffer of red-blue component of the pixel, in the form 0x00RR00BB |
| 1042 | // buffer_ag is the alpha-green component of the pixel, in the form 0x00AA00GG |
| 1043 | // +1 for the last pixel to interpolate with, and +1 for rounding errors. |
| 1044 | quint32 buffer_rb[BufferSize+2]; |
| 1045 | quint32 buffer_ag[BufferSize+2]; |
| 1046 | }; |
| 1047 | |
| 1048 | QT_END_NAMESPACE |
| 1049 | |
| 1050 | #endif // QDRAWHELPER_P_H |
| 1051 | |