QtLottie: Migrate from Qt JSON to rapidjson.

This commit is contained in:
John Preston 2019-05-16 16:56:00 +03:00
parent a03d42daa8
commit 973c3f8838
7 changed files with 31 additions and 29 deletions

View File

@ -8,18 +8,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "lottie/lottie_animation.h" #include "lottie/lottie_animation.h"
#include "lottie/lottie_frame_renderer.h" #include "lottie/lottie_frame_renderer.h"
#include "rasterrenderer/rasterrenderer.h"
#include "json.h"
#include "base/algorithm.h" #include "base/algorithm.h"
#include "logs.h"
#include <QFile>
#include <crl/crl_async.h> #include <crl/crl_async.h>
#include <crl/crl_on_main.h> #include <crl/crl_on_main.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <rapidjson/document.h>
#include "logs.h"
#include "rasterrenderer/rasterrenderer.h"
namespace Lottie { namespace Lottie {
@ -42,40 +38,35 @@ std::unique_ptr<Animation> FromFile(const QString &path) {
if (content.isEmpty()) { if (content.isEmpty()) {
return nullptr; return nullptr;
} }
return FromData(content); return FromData(std::move(content));
} }
std::unique_ptr<Animation> FromData(const QByteArray &data) { std::unique_ptr<Animation> FromData(const QByteArray &data) {
return std::make_unique<Animation>(data); return std::make_unique<Animation>(base::duplicate(data));
} }
Animation::Animation(const QByteArray &content) Animation::Animation(QByteArray &&content)
: _timer([=] { checkNextFrame(); }) { : _timer([=] { checkNextFrame(); }) {
const auto weak = base::make_weak(this); const auto weak = base::make_weak(this);
crl::async([=] { crl::async([=, content = base::take(content)]() mutable {
const auto now = crl::now(); const auto now = crl::now();
auto error = QJsonParseError(); const auto document = JsonDocument(std::move(content));
const auto document = QJsonDocument::fromJson(content, &error);
const auto parsed = crl::now(); const auto parsed = crl::now();
auto test = rapidjson::Document(); if (const auto error = document.error()) {
test.Parse(content.data());
const auto second = crl::now();
if (error.error != QJsonParseError::NoError) {
qWarning() qWarning()
<< "Lottie Error: Parse failed with code " << "Lottie Error: Parse failed with code "
<< error.error << error;
<< "( " << error.errorString() << ")";
crl::on_main(weak, [=] { crl::on_main(weak, [=] {
parseFailed(); parseFailed();
}); });
} else { } else {
auto state = std::make_unique<SharedState>(document.object()); auto state = std::make_unique<SharedState>(document.root());
crl::on_main(weak, [this, result = std::move(state)]() mutable { crl::on_main(weak, [this, result = std::move(state)]() mutable {
parseDone(std::move(result)); parseDone(std::move(result));
}); });
} }
const auto finish = crl::now(); const auto finish = crl::now();
LOG(("INIT: %1 (PARSE %2, RAPIDJSON %3)").arg(finish - now).arg(parsed - now).arg(second - parsed)); LOG(("INIT: %1 (PARSE %2)").arg(finish - now).arg(parsed - now));
}); });
} }

View File

@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/flat_map.h" #include "base/flat_map.h"
#include "base/weak_ptr.h" #include "base/weak_ptr.h"
#include "base/timer.h" #include "base/timer.h"
#include "lottie/lottie_common.h" #include "lottie/lottie_common.h"
#include <QSize> #include <QSize>
@ -36,7 +35,7 @@ std::unique_ptr<Animation> FromData(const QByteArray &data);
class Animation final : public base::has_weak_ptr { class Animation final : public base::has_weak_ptr {
public: public:
explicit Animation(const QByteArray &content); explicit Animation(QByteArray &&content);
~Animation(); ~Animation();
//void play(const PlaybackOptions &options); //void play(const PlaybackOptions &options);

View File

@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <range/v3/algorithm/find.hpp> #include <range/v3/algorithm/find.hpp>
#include <range/v3/algorithm/count_if.hpp> #include <range/v3/algorithm/count_if.hpp>
#include <QJsonDocument>
#include <QPainter> #include <QPainter>
namespace Images { namespace Images {
@ -172,7 +171,7 @@ void FrameRendererObject::queueGenerateFrames() {
}); });
} }
SharedState::SharedState(const QJsonObject &definition) SharedState::SharedState(const JsonObject &definition)
: _scene(definition) { : _scene(definition) {
if (_scene.endFrame() > _scene.startFrame()) { if (_scene.endFrame() > _scene.startFrame()) {
auto cover = QImage(); auto cover = QImage();

View File

@ -25,6 +25,7 @@ namespace Lottie {
constexpr auto kTimeUnknown = std::numeric_limits<crl::time>::min(); constexpr auto kTimeUnknown = std::numeric_limits<crl::time>::min();
class Animation; class Animation;
class JsonObject;
struct Frame { struct Frame {
QImage original; QImage original;
@ -42,7 +43,7 @@ QImage PrepareFrameByRequest(
class SharedState { class SharedState {
public: public:
explicit SharedState(const QJsonObject &definition); explicit SharedState(const JsonObject &definition);
void start(not_null<Animation*> owner, crl::time now); void start(not_null<Animation*> owner, crl::time now);

View File

@ -7,5 +7,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/ */
#pragma once #pragma once
#include <QtGlobal> #include <vector>
#include <functional>
#include <QtMath>
#include <QDebug> #include <QDebug>
#include <QList>
#include <QPointF>
#include <QSizeF>
#include <QVector4D>
#include "json.h"
#include "beziereasing.h"

@ -1 +1 @@
Subproject commit 3a08e238de47d1c08b946ce2e1533f31ea9689e2 Subproject commit 5d99b3ed05a8f54a1b192ffb9488a000e75e1c12

View File

@ -132,6 +132,8 @@
'<(lottie_loc)/bodymovin/bmscene.h', '<(lottie_loc)/bodymovin/bmscene.h',
'<(lottie_loc)/bodymovin/bmmasks.h', '<(lottie_loc)/bodymovin/bmmasks.h',
'<(lottie_loc)/bodymovin/bmmaskshape.h', '<(lottie_loc)/bodymovin/bmmaskshape.h',
'<(lottie_loc)/bodymovin/json.h',
], ],
'conditions': [[ 'build_macold', { 'conditions': [[ 'build_macold', {
'xcode_settings': { 'xcode_settings': {