| 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 QQUICKPIXMAPCACHE_H |
| 5 | #define QQUICKPIXMAPCACHE_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 <QtCore/qmutex.h> |
| 19 | #include <QtCore/qurl.h> |
| 20 | #include <QtQuick/qquickimageprovider.h> |
| 21 | #include <private/qquickpixmap_p.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QQuickPixmapData; |
| 26 | |
| 27 | /*! \internal |
| 28 | A composite key to identify a QQuickPixmapData instance in a QHash. |
| 29 | */ |
| 30 | struct Q_AUTOTEST_EXPORT QQuickPixmapKey |
| 31 | { |
| 32 | const QUrl *url; |
| 33 | const QRect *region; |
| 34 | const QSize *size; |
| 35 | int frame; |
| 36 | QQuickImageProviderOptions options; |
| 37 | }; |
| 38 | |
| 39 | /*! \internal |
| 40 | A singleton managing the storage and garbage collection of QQuickPixmapData |
| 41 | instances. It's exported only for the sake of autotests, and should not be |
| 42 | manipulated outside qquickpixmap.cpp and qquickpixmapcache.cpp. |
| 43 | */ |
| 44 | class Q_AUTOTEST_EXPORT QQuickPixmapCache : public QObject |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | |
| 48 | public: |
| 49 | static QQuickPixmapCache *instance(); |
| 50 | ~QQuickPixmapCache() override; |
| 51 | |
| 52 | void unreferencePixmap(QQuickPixmapData *); |
| 53 | void referencePixmap(QQuickPixmapData *); |
| 54 | |
| 55 | void purgeCache(); |
| 56 | |
| 57 | protected: |
| 58 | void timerEvent(QTimerEvent *) override; |
| 59 | |
| 60 | private: |
| 61 | QQuickPixmapCache() = default; |
| 62 | Q_DISABLE_COPY(QQuickPixmapCache) |
| 63 | |
| 64 | void shrinkCache(int remove); |
| 65 | int destroyCache(); |
| 66 | qsizetype referencedCost() const; |
| 67 | |
| 68 | private: |
| 69 | QHash<QQuickPixmapKey, QQuickPixmapData *> m_cache; |
| 70 | mutable QMutex m_cacheMutex; // avoid simultaneous iteration and modification |
| 71 | |
| 72 | QQuickPixmapData *m_unreferencedPixmaps = nullptr; |
| 73 | QQuickPixmapData *m_lastUnreferencedPixmap = nullptr; |
| 74 | |
| 75 | int m_unreferencedCost = 0; |
| 76 | int m_timerId = -1; |
| 77 | bool m_destroying = false; |
| 78 | |
| 79 | friend class QQuickPixmap; |
| 80 | friend class QQuickPixmapData; |
| 81 | friend class tst_qquickpixmapcache; |
| 82 | friend class tst_qquicktext; |
| 83 | friend class tst_qquicktextedit; |
| 84 | }; |
| 85 | |
| 86 | QT_END_NAMESPACE |
| 87 | |
| 88 | #endif // QQUICKPIXMAPCACHE_H |
| 89 |
