| 1 | // Copyright (C) 2023 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 | #include "qsggradientcache_p.h" |
| 5 | |
| 6 | #include <QtGui/private/qdrawhelper_p.h> |
| 7 | #include <QtGui/rhi/qrhi.h> |
| 8 | |
| 9 | #include <QtQuick/qsgtexture.h> |
| 10 | #include <QtQuick/private/qsgplaintexture_p.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | static void generateGradientColorTable(const QSGGradientCacheKey &gradient, |
| 15 | uint *colorTable, int size, float opacity) |
| 16 | { |
| 17 | int pos = 0; |
| 18 | const QGradientStops &s = gradient.stops; |
| 19 | Q_ASSERT(!s.isEmpty()); |
| 20 | const bool colorInterpolation = true; |
| 21 | |
| 22 | uint alpha = qRound(f: opacity * 256); |
| 23 | uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha); |
| 24 | qreal incr = 1.0 / qreal(size); |
| 25 | qreal fpos = 1.5 * incr; |
| 26 | colorTable[pos++] = ARGB2RGBA(x: qPremultiply(x: current_color)); |
| 27 | |
| 28 | while (fpos <= s.first().first) { |
| 29 | colorTable[pos] = colorTable[pos - 1]; |
| 30 | pos++; |
| 31 | fpos += incr; |
| 32 | } |
| 33 | |
| 34 | if (colorInterpolation) |
| 35 | current_color = qPremultiply(x: current_color); |
| 36 | |
| 37 | const int sLast = s.size() - 1; |
| 38 | for (int i = 0; i < sLast; ++i) { |
| 39 | qreal delta = 1/(s[i+1].first - s[i].first); |
| 40 | uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha); |
| 41 | if (colorInterpolation) |
| 42 | next_color = qPremultiply(x: next_color); |
| 43 | |
| 44 | while (fpos < s[i+1].first && pos < size) { |
| 45 | int dist = int(256 * ((fpos - s[i].first) * delta)); |
| 46 | int idist = 256 - dist; |
| 47 | if (colorInterpolation) |
| 48 | colorTable[pos] = ARGB2RGBA(x: INTERPOLATE_PIXEL_256(x: current_color, a: idist, y: next_color, b: dist)); |
| 49 | else |
| 50 | colorTable[pos] = ARGB2RGBA(x: qPremultiply(x: INTERPOLATE_PIXEL_256(x: current_color, a: idist, y: next_color, b: dist))); |
| 51 | ++pos; |
| 52 | fpos += incr; |
| 53 | } |
| 54 | current_color = next_color; |
| 55 | } |
| 56 | |
| 57 | uint last_color = ARGB2RGBA(x: qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha))); |
| 58 | for ( ; pos < size; ++pos) |
| 59 | colorTable[pos] = last_color; |
| 60 | |
| 61 | colorTable[size-1] = last_color; |
| 62 | } |
| 63 | |
| 64 | QSGGradientCache::~QSGGradientCache() |
| 65 | { |
| 66 | qDeleteAll(c: m_textures); |
| 67 | } |
| 68 | |
| 69 | QSGGradientCache *QSGGradientCache::cacheForRhi(QRhi *rhi) |
| 70 | { |
| 71 | static QHash<QRhi *, QSGGradientCache *> caches; |
| 72 | auto it = caches.constFind(key: rhi); |
| 73 | if (it != caches.constEnd()) |
| 74 | return *it; |
| 75 | |
| 76 | QSGGradientCache *cache = new QSGGradientCache; |
| 77 | rhi->addCleanupCallback(callback: [cache](QRhi *rhi) { |
| 78 | caches.remove(key: rhi); |
| 79 | delete cache; |
| 80 | }); |
| 81 | caches.insert(key: rhi, value: cache); |
| 82 | return cache; |
| 83 | } |
| 84 | |
| 85 | QSGTexture *QSGGradientCache::get(const QSGGradientCacheKey &grad) |
| 86 | { |
| 87 | QSGPlainTexture *tx = m_textures[grad]; |
| 88 | if (!tx) { |
| 89 | static const int W = 1024; // texture size is 1024x1 |
| 90 | QImage gradTab(W, 1, QImage::Format_RGBA8888_Premultiplied); |
| 91 | if (!grad.stops.isEmpty()) |
| 92 | generateGradientColorTable(gradient: grad, colorTable: reinterpret_cast<uint *>(gradTab.bits()), size: W, opacity: 1.0f); |
| 93 | else |
| 94 | gradTab.fill(color: Qt::black); |
| 95 | tx = new QSGPlainTexture; |
| 96 | tx->setImage(gradTab); |
| 97 | switch (grad.spread) { |
| 98 | case QGradient::PadSpread: |
| 99 | tx->setHorizontalWrapMode(QSGTexture::ClampToEdge); |
| 100 | tx->setVerticalWrapMode(QSGTexture::ClampToEdge); |
| 101 | break; |
| 102 | case QGradient::RepeatSpread: |
| 103 | tx->setHorizontalWrapMode(QSGTexture::Repeat); |
| 104 | tx->setVerticalWrapMode(QSGTexture::Repeat); |
| 105 | break; |
| 106 | case QGradient::ReflectSpread: |
| 107 | tx->setHorizontalWrapMode(QSGTexture::MirroredRepeat); |
| 108 | tx->setVerticalWrapMode(QSGTexture::MirroredRepeat); |
| 109 | break; |
| 110 | default: |
| 111 | qWarning(msg: "Unknown gradient spread mode %d" , grad.spread); |
| 112 | break; |
| 113 | } |
| 114 | tx->setFiltering(QSGTexture::Linear); |
| 115 | m_textures[grad] = tx; |
| 116 | } |
| 117 | return tx; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | QT_END_NAMESPACE |
| 122 | |