summaryrefslogtreecommitdiffstats
path: root/src/imports/rasterrenderer/batchrenderer.cpp
blob: ca7b00eb391eb07cb2e1e60fee6933c39ec89fe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the lottie-qt module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "batchrenderer.h"

#include <QImage>
#include <QPainter>
#include <QHash>
#include <QMutexLocker>
#include <QLoggingCategory>
#include <QThread>

#include <QJsonDocument>
#include <QJsonArray>

#include <QtBodymovin/private/bmconstants_p.h>
#include <QtBodymovin/private/bmbase_p.h>
#include <QtBodymovin/private/bmlayer_p.h>

#include "lottieanimation.h"
#include "lottierasterrenderer.h"

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcLottieQtBodymovinRenderThread, "qt.lottieqt.bodymovin.render.thread");

BatchRenderer *BatchRenderer::m_rendererInstance = nullptr;

BatchRenderer::~BatchRenderer()
{
    qDeleteAll(m_animData);
    qDeleteAll(m_frameCache);
}

BatchRenderer *BatchRenderer::instance()
{
    if (!m_rendererInstance)
        m_rendererInstance = new BatchRenderer;

    return m_rendererInstance;
}

void BatchRenderer::deleteInstance()
{
    delete m_rendererInstance;
    m_rendererInstance = nullptr;
}

void BatchRenderer::registerAnimator(LottieAnimation *animator)
{
    QMutexLocker mlocker(&m_mutex);

    qCDebug(lcLottieQtBodymovinRenderThread) << "Register Animator:"
                                       << static_cast<void*>(animator);

    Entry *entry = new Entry;
    entry->animator = animator;
    entry->startFrame = animator->startFrame();
    entry->endFrame = animator->endFrame();
    entry->currentFrame = animator->startFrame();
    if (animator->direction() == LottieAnimation::Reverse)
        entry->animDir = -1;
    // animDir == 1 by default
    entry->bmTreeBlueprint = new BMBase;
    parse(entry->bmTreeBlueprint, animator->jsonSource());
    m_animData.insert(animator, entry);
    m_waitCondition.wakeAll();
}

void BatchRenderer::deregisterAnimator(LottieAnimation *animator)
{
    QMutexLocker mlocker(&m_mutex);

    qCDebug(lcLottieQtBodymovinRenderThread) << "Deregister Animator:"
                                       << static_cast<void*>(animator);

    Entry *entry = m_animData.value(animator, nullptr);
    if (entry) {
        qDeleteAll(entry->frameCache);
        delete entry->bmTreeBlueprint;
        delete entry;
        m_animData.remove(animator);
    }
}

bool BatchRenderer::gotoFrame(LottieAnimation *animator, int frame)
{
    Entry *entry = m_animData.value(animator, nullptr);
    if (entry) {
        QMutexLocker mlocker(&m_mutex);
        qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:"
                                           << static_cast<void*>(animator)
                                           << "Goto frame:" << frame;
        entry->currentFrame = frame;
        pruneFrameCache(entry);
        m_waitCondition.wakeAll();
        return true;
    }
    return false;
}

void BatchRenderer::pruneFrameCache(Entry* e)
{
    QHash<int, BMBase*>::iterator it = e->frameCache.begin();

    while (it != e->frameCache.end()) {
        if (it.key() == e->currentFrame) {
            ++it;
        } else {
            delete it.value();
            it = e->frameCache.erase(it);
        }
    }
}

BMBase *BatchRenderer::getFrame(LottieAnimation *animator, int frameNumber)
{
    QMutexLocker mlocker(&m_mutex);

    Entry *entry = m_animData.value(animator, nullptr);
    if (entry)
        return entry->frameCache.value(frameNumber, nullptr);
    else
        return nullptr;
}

void BatchRenderer::setCacheSize(int size)
{
    if (size < 1 || isRunning())
        return;

    qCDebug(lcLottieQtBodymovinRenderThread) << "Setting frame cache size to" << size;
    m_cacheSize = size;
}

void BatchRenderer::prerender(Entry *animEntry)
{
    LottieAnimation *animator = animEntry->animator;

    if (animEntry->frameCache.count() == m_cacheSize) {
        qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:" << static_cast<void*>(animEntry->animator)
                                                        << "Cache full, cannot render more";
        return;
    }

    while (animEntry->frameCache.count() < m_cacheSize) {
        // It may be that the animator has deregistered itself while
        // te mutex was locked. In that case we cannot render here anymore
        if (!animEntry->bmTreeBlueprint)
            break;

        if (!animEntry->frameCache.contains(animEntry->currentFrame)) {
            BMBase *bmTree = new BMBase(*animEntry->bmTreeBlueprint);

            for (BMBase *elem : bmTree->children()) {
                if (elem->active(animEntry->currentFrame))
                    elem->updateProperties( animEntry->currentFrame);
            }

            animEntry->frameCache.insert( animEntry->currentFrame, bmTree);
        }

        qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:"
                                           << static_cast<void*>(animEntry->animator)
                                           << "Frame drawn to cache. FN:"
                                           << animEntry->currentFrame;
        emit frameReady(animator,  animEntry->currentFrame);

        animEntry->currentFrame += animEntry->animDir;

        if (animEntry->currentFrame > animEntry->endFrame) {
            animEntry->currentFrame = animEntry->startFrame;
        } else if (animEntry->currentFrame < animEntry->startFrame) {
            animEntry->currentFrame = animEntry->endFrame;
        }
    }
}

void BatchRenderer::prerender()
{
    QMutexLocker mlocker(&m_mutex);
    bool wait = true;

    foreach (Entry *e, m_animData) {
        if (e->frameCache.size() < m_cacheSize) {
            wait = false;
            break;
        }
    }

    if (wait)
        m_waitCondition.wait(&m_mutex);

    QHash<LottieAnimation*, Entry*>::iterator it = m_animData.begin();
    while (it != m_animData.end()) {
        Entry *e = *it;
        if (e && e->frameCache.size() < m_cacheSize)
            prerender(e);
        ++it;
    }
}

void BatchRenderer::frameRendered(LottieAnimation *animator, int frameNumber)
{
    Entry *entry = m_animData.value(animator, nullptr);
    if (entry) {
        qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:" << static_cast<void*>(animator)
                                           << "Remove frame from cache" << frameNumber;

        QMutexLocker mlocker(&m_mutex);
        BMBase *root = entry->frameCache.value(frameNumber, nullptr);
        delete root;
        entry->frameCache.remove(frameNumber);
        m_waitCondition.wakeAll();
    }
}

void BatchRenderer::run()
{
    qCDebug(lcLottieQtBodymovinRenderThread) << "rendering thread" << QThread::currentThread();

    while (-1) {
        if (QThread::currentThread()->isInterruptionRequested())
            return;

        prerender();
    }
}

int BatchRenderer::parse(BMBase* rootElement, QByteArray jsonSource)
{
    QJsonDocument doc = QJsonDocument::fromJson(jsonSource);
    QJsonObject rootObj = doc.object();

    if (rootObj.empty())
        return -1;

    QJsonArray jsonLayers = rootObj.value(QLatin1String("layers")).toArray();
    QJsonArray::const_iterator jsonLayerIt = jsonLayers.constEnd();
    while (jsonLayerIt != jsonLayers.constBegin()) {
        jsonLayerIt--;
        QJsonObject jsonLayer = (*jsonLayerIt).toObject();
        BMLayer *layer = BMLayer::construct(jsonLayer);
        if (layer) {
            layer->setParent(rootElement);
            rootElement->addChild(layer);
        }
    }

    // Mask layers must be rendered before the layers they affect to
    // although they appear before in layer hierarchy. For this reason
    // move a mask after the affected layers, so it will be rendered first
    QList<BMBase *> &layers = rootElement->children();
    int moveTo = -1;
    for (int i = 0; i < layers.count(); i++) {
        BMLayer *layer = static_cast<BMLayer*>(layers.at(i));
        if (layer->isClippedLayer())
            moveTo = i;
        if (layer->isMaskLayer()) {
            qCDebug(lcLottieQtBodymovinParser()) << "Move mask layer"
                                                 <<  layers.at(i)->name()
                                                 << "before" << layers.at(moveTo)->name();
            layers.move(i, moveTo);
        }
    }

    return 0;
}

QT_END_NAMESPACE