1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef BATCHRENDERER_H
5#define BATCHRENDERER_H
6
7#include <QHash>
8#include <QThread>
9#include <QMutex>
10#include <QWaitCondition>
11
12QT_BEGIN_NAMESPACE
13
14class BMBase;
15class QImage;
16class QVersionNumber;
17class LottieAnimation;
18
19class BatchRenderer : public QThread
20{
21 Q_OBJECT
22
23 struct Entry
24 {
25 LottieAnimation* animator = nullptr;
26 BMBase *bmTreeBlueprint = nullptr;
27 int startFrame = 0;
28 int endFrame = 0;
29 int currentFrame = 0;
30 int animDir = 1;
31 QHash<int, BMBase*> frameCache;
32 };
33
34public:
35 ~BatchRenderer() override;
36
37 BatchRenderer(BatchRenderer const &) = delete;
38 void operator=(BatchRenderer const&) = delete;
39
40 static BatchRenderer *instance();
41 static void deleteInstance();
42
43 BMBase *getFrame(LottieAnimation *animator, int frameNumber);
44
45signals:
46 void frameReady(LottieAnimation *animator, int frameNumber);
47
48public slots:
49 void registerAnimator(LottieAnimation *animator);
50 void deregisterAnimator(LottieAnimation *animator);
51
52 bool gotoFrame(LottieAnimation *animator, int frame);
53
54 void frameRendered(LottieAnimation *animator, int frameNumber);
55
56protected:
57 void run() override;
58
59 int parse(BMBase *rootElement, const QByteArray &jsonSource,
60 const QVersionNumber &version) const;
61
62 void prerender(Entry *animEntry);
63
64private:
65 BatchRenderer();
66
67 void pruneFrameCache(Entry* e);
68
69private:
70 static BatchRenderer *m_rendererInstance;
71
72 QMutex m_mutex;
73 QWaitCondition m_waitCondition;
74
75 int m_cacheSize = 2;
76 QHash<LottieAnimation *, Entry *> m_animData;
77 int m_lastRenderedFrame = -1;
78};
79
80QT_END_NAMESPACE
81
82#endif // BATCHRENDERER_H
83

source code of qtlottie/src/imports/rasterrenderer/batchrenderer.h