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 | // |
5 | // W A R N I N G |
6 | // ------------- |
7 | // |
8 | // This file is not part of the Qt API. It exists purely as an |
9 | // implementation detail. This header file may change from version to |
10 | // version without notice, or even be removed. |
11 | // |
12 | // We mean it. |
13 | // |
14 | |
15 | #ifndef QOPENGLTEXTURECACHE_P_H |
16 | #define QOPENGLTEXTURECACHE_P_H |
17 | |
18 | #include <QtOpenGL/qtopenglglobal.h> |
19 | #include <QHash> |
20 | #include <QObject> |
21 | #include <QCache> |
22 | #include <private/qopenglcontext_p.h> |
23 | #include <private/qopengltextureuploader_p.h> |
24 | #include <QtCore/qmutex.h> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | class QOpenGLCachedTexture; |
29 | |
30 | class Q_OPENGL_EXPORT QOpenGLTextureCache : public QOpenGLSharedResource |
31 | { |
32 | public: |
33 | static QOpenGLTextureCache *cacheForContext(QOpenGLContext *context); |
34 | |
35 | QOpenGLTextureCache(QOpenGLContext *); |
36 | ~QOpenGLTextureCache(); |
37 | |
38 | enum class BindResultFlag : quint8 { |
39 | NewTexture = 0x01 |
40 | }; |
41 | Q_DECLARE_FLAGS(BindResultFlags, BindResultFlag) |
42 | |
43 | struct BindResult { |
44 | GLuint id; |
45 | BindResultFlags flags; |
46 | }; |
47 | |
48 | BindResult bindTexture(QOpenGLContext *context, const QPixmap &pixmap, |
49 | QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption); |
50 | BindResult bindTexture(QOpenGLContext *context, const QImage &image, |
51 | QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption); |
52 | |
53 | void invalidate(qint64 key); |
54 | |
55 | void invalidateResource() override; |
56 | void freeResource(QOpenGLContext *ctx) override; |
57 | |
58 | private: |
59 | BindResult bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options); |
60 | |
61 | QMutex m_mutex; |
62 | QCache<quint64, QOpenGLCachedTexture> m_cache; |
63 | }; |
64 | |
65 | Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLTextureCache::BindResultFlags) |
66 | |
67 | class QOpenGLCachedTexture |
68 | { |
69 | public: |
70 | QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context); |
71 | ~QOpenGLCachedTexture() { m_resource->free(); } |
72 | |
73 | GLuint id() const { return m_resource->id(); } |
74 | QOpenGLTextureUploader::BindOptions options() const { return m_options; } |
75 | |
76 | private: |
77 | QOpenGLSharedResourceGuard *m_resource; |
78 | QOpenGLTextureUploader::BindOptions m_options; |
79 | }; |
80 | |
81 | QT_END_NAMESPACE |
82 | |
83 | #endif |
84 | |
85 | |