From 973c3f8838a8e5e38f7b56aeabe3c0fa2c369179 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Thu, 16 May 2019 16:56:00 +0300
Subject: [PATCH] QtLottie: Migrate from Qt JSON to rapidjson.

---
 .../SourceFiles/lottie/lottie_animation.cpp   | 35 +++++++------------
 .../SourceFiles/lottie/lottie_animation.h     |  3 +-
 .../lottie/lottie_frame_renderer.cpp          |  3 +-
 .../lottie/lottie_frame_renderer.h            |  3 +-
 Telegram/SourceFiles/lottie/lottie_pch.h      | 12 ++++++-
 Telegram/ThirdParty/qtlottie                  |  2 +-
 Telegram/gyp/lib_lottie.gyp                   |  2 ++
 7 files changed, 31 insertions(+), 29 deletions(-)

diff --git a/Telegram/SourceFiles/lottie/lottie_animation.cpp b/Telegram/SourceFiles/lottie/lottie_animation.cpp
index 0ee37c15b..bd890aaa1 100644
--- a/Telegram/SourceFiles/lottie/lottie_animation.cpp
+++ b/Telegram/SourceFiles/lottie/lottie_animation.cpp
@@ -8,18 +8,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "lottie/lottie_animation.h"
 
 #include "lottie/lottie_frame_renderer.h"
+#include "rasterrenderer/rasterrenderer.h"
+#include "json.h"
 #include "base/algorithm.h"
+#include "logs.h"
 
+#include <QFile>
 #include <crl/crl_async.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 {
 
@@ -42,40 +38,35 @@ std::unique_ptr<Animation> FromFile(const QString &path) {
 	if (content.isEmpty()) {
 		return nullptr;
 	}
-	return FromData(content);
+	return FromData(std::move(content));
 }
 
 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(); }) {
 	const auto weak = base::make_weak(this);
-	crl::async([=] {
+	crl::async([=, content = base::take(content)]() mutable {
 		const auto now = crl::now();
-		auto error = QJsonParseError();
-		const auto document = QJsonDocument::fromJson(content, &error);
+		const auto document = JsonDocument(std::move(content));
 		const auto parsed = crl::now();
-		auto test = rapidjson::Document();
-		test.Parse(content.data());
-		const auto second = crl::now();
-		if (error.error != QJsonParseError::NoError) {
+		if (const auto error = document.error()) {
 			qWarning()
 				<< "Lottie Error: Parse failed with code "
-				<< error.error
-				<< "( " << error.errorString() << ")";
+				<< error;
 			crl::on_main(weak, [=] {
 				parseFailed();
 			});
 		} 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 {
 				parseDone(std::move(result));
 			});
 		}
 		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));
 	});
 }
 
diff --git a/Telegram/SourceFiles/lottie/lottie_animation.h b/Telegram/SourceFiles/lottie/lottie_animation.h
index 264bd9781..686f0fad1 100644
--- a/Telegram/SourceFiles/lottie/lottie_animation.h
+++ b/Telegram/SourceFiles/lottie/lottie_animation.h
@@ -11,7 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 #include "base/flat_map.h"
 #include "base/weak_ptr.h"
 #include "base/timer.h"
-
 #include "lottie/lottie_common.h"
 
 #include <QSize>
@@ -36,7 +35,7 @@ std::unique_ptr<Animation> FromData(const QByteArray &data);
 
 class Animation final : public base::has_weak_ptr {
 public:
-	explicit Animation(const QByteArray &content);
+	explicit Animation(QByteArray &&content);
 	~Animation();
 
 	//void play(const PlaybackOptions &options);
diff --git a/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp b/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp
index 0777788cf..5a35c66f9 100644
--- a/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp
+++ b/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp
@@ -13,7 +13,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 
 #include <range/v3/algorithm/find.hpp>
 #include <range/v3/algorithm/count_if.hpp>
-#include <QJsonDocument>
 #include <QPainter>
 
 namespace Images {
@@ -172,7 +171,7 @@ void FrameRendererObject::queueGenerateFrames() {
 	});
 }
 
-SharedState::SharedState(const QJsonObject &definition)
+SharedState::SharedState(const JsonObject &definition)
 : _scene(definition) {
 	if (_scene.endFrame() > _scene.startFrame()) {
 		auto cover = QImage();
diff --git a/Telegram/SourceFiles/lottie/lottie_frame_renderer.h b/Telegram/SourceFiles/lottie/lottie_frame_renderer.h
index 3fcf24a8d..454739aad 100644
--- a/Telegram/SourceFiles/lottie/lottie_frame_renderer.h
+++ b/Telegram/SourceFiles/lottie/lottie_frame_renderer.h
@@ -25,6 +25,7 @@ namespace Lottie {
 constexpr auto kTimeUnknown = std::numeric_limits<crl::time>::min();
 
 class Animation;
+class JsonObject;
 
 struct Frame {
 	QImage original;
@@ -42,7 +43,7 @@ QImage PrepareFrameByRequest(
 
 class SharedState {
 public:
-	explicit SharedState(const QJsonObject &definition);
+	explicit SharedState(const JsonObject &definition);
 
 	void start(not_null<Animation*> owner, crl::time now);
 
diff --git a/Telegram/SourceFiles/lottie/lottie_pch.h b/Telegram/SourceFiles/lottie/lottie_pch.h
index 6d62ab815..6ff729778 100644
--- a/Telegram/SourceFiles/lottie/lottie_pch.h
+++ b/Telegram/SourceFiles/lottie/lottie_pch.h
@@ -7,5 +7,15 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #pragma once
 
-#include <QtGlobal>
+#include <vector>
+#include <functional>
+
+#include <QtMath>
 #include <QDebug>
+#include <QList>
+#include <QPointF>
+#include <QSizeF>
+#include <QVector4D>
+
+#include "json.h"
+#include "beziereasing.h"
diff --git a/Telegram/ThirdParty/qtlottie b/Telegram/ThirdParty/qtlottie
index 3a08e238d..5d99b3ed0 160000
--- a/Telegram/ThirdParty/qtlottie
+++ b/Telegram/ThirdParty/qtlottie
@@ -1 +1 @@
-Subproject commit 3a08e238de47d1c08b946ce2e1533f31ea9689e2
+Subproject commit 5d99b3ed05a8f54a1b192ffb9488a000e75e1c12
diff --git a/Telegram/gyp/lib_lottie.gyp b/Telegram/gyp/lib_lottie.gyp
index 886eb999d..c83e09e54 100644
--- a/Telegram/gyp/lib_lottie.gyp
+++ b/Telegram/gyp/lib_lottie.gyp
@@ -132,6 +132,8 @@
       '<(lottie_loc)/bodymovin/bmscene.h',
       '<(lottie_loc)/bodymovin/bmmasks.h',
       '<(lottie_loc)/bodymovin/bmmaskshape.h',
+
+	  '<(lottie_loc)/bodymovin/json.h',
     ],
     'conditions': [[ 'build_macold', {
       'xcode_settings': {