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 QOPENGLGRADIENTCACHE_P_H
5#define QOPENGLGRADIENTCACHE_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 <QMultiHash>
19#include <QObject>
20#include <private/qopenglcontext_p.h>
21#include <QtCore/qmutex.h>
22#include <QGradient>
23#include <qrgba64.h>
24
25QT_BEGIN_NAMESPACE
26
27class QOpenGL2GradientCache : public QOpenGLSharedResource
28{
29 struct CacheInfo
30 {
31 inline CacheInfo(QGradientStops s, qreal op, QGradient::InterpolationMode mode) :
32 stops(std::move(s)), opacity(op), interpolationMode(mode) {}
33
34 GLuint texId;
35 QGradientStops stops;
36 qreal opacity;
37 QGradient::InterpolationMode interpolationMode;
38 };
39
40 typedef QMultiHash<quint64, CacheInfo> QOpenGLGradientColorTableHash;
41
42public:
43 static QOpenGL2GradientCache *cacheForContext(QOpenGLContext *context);
44
45 QOpenGL2GradientCache(QOpenGLContext *);
46 ~QOpenGL2GradientCache();
47
48 GLuint getBuffer(const QGradient &gradient, qreal opacity);
49 inline int paletteSize() const { return 1024; }
50
51 void invalidateResource() override;
52 void freeResource(QOpenGLContext *ctx) override;
53
54private:
55 inline int maxCacheSize() const { return 60; }
56 inline void generateGradientColorTable(const QGradient& gradient,
57 QRgba64 *colorTable,
58 int size, qreal opacity) const;
59 inline void generateGradientColorTable(const QGradient& gradient,
60 uint *colorTable,
61 int size, qreal opacity) const;
62 GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity);
63 void cleanCache();
64
65 QOpenGLGradientColorTableHash cache;
66 QMutex m_mutex;
67};
68
69QT_END_NAMESPACE
70
71#endif // QOPENGLGRADIENTCACHE_P_H
72

source code of qtbase/src/opengl/qopenglgradientcache_p.h