1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QBATCHRENDERER_H
5#define QBATCHRENDERER_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 <QHash>
19#include <QThread>
20#include <QMutex>
21#include <QWaitCondition>
22
23QT_BEGIN_NAMESPACE
24
25class QLottieBase;
26class QImage;
27class QLottieAnimation;
28
29class QBatchRenderer : public QThread
30{
31 Q_OBJECT
32
33 struct Entry
34 {
35 QLottieAnimation* animator = nullptr;
36 QLottieBase *lottieTreeBlueprint = nullptr;
37 int startFrame = 0;
38 int endFrame = 0;
39 int currentFrame = 0;
40 int animDir = 1;
41 QHash<int, QLottieBase*> frameCache;
42 };
43
44public:
45 ~QBatchRenderer() override;
46
47 QBatchRenderer(QBatchRenderer const &) = delete;
48 void operator=(QBatchRenderer const&) = delete;
49
50 static QBatchRenderer *instance();
51 static void deleteInstance();
52
53 QLottieBase *getFrame(QLottieAnimation *animator, int frameNumber);
54
55Q_SIGNALS:
56 void frameReady(QLottieAnimation *animator, int frameNumber);
57
58public Q_SLOTS:
59 void registerAnimator(QLottieAnimation *animator);
60 void deregisterAnimator(QLottieAnimation *animator);
61
62 bool gotoFrame(QLottieAnimation *animator, int frame);
63
64 void frameRendered(QLottieAnimation *animator, int frameNumber);
65
66protected:
67 void run() override;
68
69 void prerender(Entry *animEntry);
70
71private:
72 QBatchRenderer();
73
74 void pruneFrameCache(Entry* e);
75
76private:
77 static QBatchRenderer *m_rendererInstance;
78
79 QMutex m_mutex;
80 QWaitCondition m_waitCondition;
81
82 int m_cacheSize = 2;
83 QHash<QLottieAnimation *, Entry *> m_animData;
84 int m_lastRenderedFrame = -1;
85};
86
87QT_END_NAMESPACE
88
89#endif // QBATCHRENDERER_H
90

source code of qtlottie/src/lottie/renderer/qbatchrenderer_p.h