2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 14:45:14 +00:00

First attempt to use QtLottie.

This commit is contained in:
John Preston
2019-04-27 13:12:53 +04:00
parent b2e5ab36d4
commit 22c2054dcf
40 changed files with 3383 additions and 0 deletions

View File

@@ -0,0 +1,196 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "lottie/lottie_animation.h"
#include "lottie/lottie_frame_renderer.h"
#include "base/algorithm.h"
#include <range/v3/view/reverse.hpp>
#include <QtMath>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QFile>
#include <QPointF>
#include <QPainter>
#include <QImage>
#include <QTimer>
#include <QMetaObject>
#include <QLoggingCategory>
#include <QThread>
#include <math.h>
#include <QtBodymovin/private/bmbase_p.h>
#include <QtBodymovin/private/bmlayer_p.h>
#include "rasterrenderer/lottierasterrenderer.h"
namespace Lottie {
bool ValidateFile(const QString &path) {
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)) {
return false;
}
return true;
}
std::unique_ptr<Animation> FromFile(const QString &path) {
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)) {
return nullptr;
}
auto f = QFile(path);
if (!f.open(QIODevice::ReadOnly)) {
return nullptr;
}
const auto content = f.readAll();
if (content.isEmpty()) {
return nullptr;
}
return std::make_unique<Lottie::Animation>(content);
}
Animation::Animation(const QByteArray &content) {
parse(content);
}
Animation::~Animation() {
}
QImage Animation::frame(crl::time now) const {
if (_startFrame == _endFrame || _realWidth <= 0 || _realHeight <= 0) {
return QImage();
}
auto result = QImage(
qCeil(_realWidth),
qCeil(_realHeight),
QImage::Format_ARGB32_Premultiplied);
result.fill(Qt::transparent);
{
QPainter p(&result);
p.setRenderHints(QPainter::Antialiasing);
p.setRenderHints(QPainter::SmoothPixmapTransform);
const auto position = now;
const auto elapsed = int((_frameRate * position + 500) / 1000);
const auto frames = (_endFrame - _startFrame);
const auto frame = _options.loop
? (_startFrame + (elapsed % frames))
: std::min(_startFrame + elapsed, _endFrame);
auto tree = BMBase(*_treeBlueprint);
for (const auto element : tree.children()) {
if (element->active(frame)) {
element->updateProperties(frame);
}
}
LottieRasterRenderer renderer(&p);
for (const auto element : tree.children()) {
if (element->active(frame)) {
element->render(renderer);
}
}
}
return result;
}
int Animation::frameRate() const {
return _frameRate;
}
void Animation::play(const PlaybackOptions &options) {
_options = options;
_started = crl::now();
}
void Animation::parse(const QByteArray &content) {
const auto document = QJsonDocument::fromJson(content);
const auto root = document.object();
if (root.empty()) {
_failed = true;
return;
}
_startFrame = root.value(qstr("ip")).toVariant().toInt();
_endFrame = root.value(qstr("op")).toVariant().toInt();
_frameRate = root.value(qstr("fr")).toVariant().toInt();
_realWidth = root.value(qstr("w")).toVariant().toReal();
_realHeight = root.value(qstr("h")).toVariant().toReal();
const auto markers = root.value(qstr("markers")).toArray();
for (const auto &entry : markers) {
const auto object = entry.toObject();
const auto name = object.value(qstr("cm")).toString();
const auto frame = object.value(qstr("tm")).toInt();
_markers.emplace(name, frame);
if (object.value(qstr("dr")).toInt()) {
_unsupported = true;
}
}
if (root.value(qstr("assets")).toArray().count()) {
_unsupported = true;
}
if (root.value(qstr("chars")).toArray().count()) {
_unsupported = true;
}
_treeBlueprint = std::make_unique<BMBase>();
const auto blueprint = _treeBlueprint.get();
const auto layers = root.value(QLatin1String("layers")).toArray();
//for (const auto &entry : ranges::view::reverse(layers)) {
// if (const auto layer = BMLayer::construct(entry.toObject())) {
// layer->setParent(blueprint);
// // 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
// if (layer->isMaskLayer()) {
// blueprint->prependChild(layer);
// } else {
// blueprint->appendChild(layer);
// }
// } else {
// _unsupported = true;
// }
//}
for (const auto &entry : ranges::view::reverse(layers)) {
if (const auto layer = BMLayer::construct(entry.toObject())) {
layer->setParent(blueprint);
blueprint->addChild(layer);
} else {
_unsupported = true;
}
}
// 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
auto &children = blueprint->children();
auto moveTo = -1;
for (int i = 0; i < children.count(); i++) {
const auto layer = static_cast<BMLayer*>(children.at(i));
if (layer->isClippedLayer())
moveTo = i;
if (layer->isMaskLayer()) {
qCDebug(lcLottieQtBodymovinParser()) << "Move mask layer"
<< children.at(i)->name()
<< "before" << children.at(moveTo)->name();
children.move(i, moveTo);
}
}
}
} // namespace Lottie

View File

@@ -0,0 +1,83 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include "base/basic_types.h"
#include "base/flat_map.h"
#include <crl/crl_time.h>
#include <QString>
#include <QHash>
#include <QPainter>
class BMBase;
class BMLayer;
namespace Lottie {
class Animation;
bool ValidateFile(const QString &path);
std::unique_ptr<Animation> FromFile(const QString &path);
struct PlaybackOptions {
float64 speed = 1.;
bool loop = true;
};
class Animation final {
public:
explicit Animation(const QByteArray &content);
~Animation();
void play(const PlaybackOptions &options);
QImage frame(crl::time now) const;
int frameRate() const;
crl::time duration() const;
void play();
void pause();
void resume();
void stop();
[[nodiscard]] bool active() const;
[[nodiscard]] bool ready() const;
[[nodiscard]] bool unsupported() const;
[[nodiscard]] float64 speed() const;
void setSpeed(float64 speed); // 0.5 <= speed <= 2.
[[nodiscard]] bool playing() const;
[[nodiscard]] bool buffering() const;
[[nodiscard]] bool paused() const;
[[nodiscard]] bool finished() const;
private:
void parse(const QByteArray &content);
int _startFrame = 0;
int _endFrame = 0;
int _frameRate = 30;
qreal _realWidth = 0;
qreal _realHeight = 0;
base::flat_map<QString, int> _markers;
bool _initialized = false;
bool _unsupported = false;
bool _failed = false;
bool _paused = false;
crl::time _started = 0;
PlaybackOptions _options;
std::unique_ptr<BMBase> _treeBlueprint;
};
} // namespace Lottie

View File

@@ -0,0 +1,229 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "lottie/lottie_frame_renderer.h"
#include "lottie/lottie_animation.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 "rasterrenderer/lottierasterrenderer.h"
Q_LOGGING_CATEGORY(lcLottieQtBodymovinRenderThread, "qt.lottieqt.bodymovin.render.thread");
namespace Lottie {
//
//FrameRenderer *FrameRenderer::_rendererInstance = nullptr;
//
//FrameRenderer::~FrameRenderer()
//{
// QMutexLocker mlocker(&_mutex);
// qDeleteAll(_animData);
// qDeleteAll(_frameCache);
//}
//
//FrameRenderer *FrameRenderer::instance()
//{
// if (!_rendererInstance)
// _rendererInstance = new FrameRenderer;
//
// return _rendererInstance;
//}
//
//void FrameRenderer::deleteInstance()
//{
// delete _rendererInstance;
// _rendererInstance = nullptr;
//}
//
//void FrameRenderer::registerAnimator(Animation *animator)
//{
// QMutexLocker mlocker(&_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();
// entry->animDir = animator->direction();
// entry->bmTreeBlueprint = new BMBase;
// parse(entry->bmTreeBlueprint, animator->jsonSource());
// _animData.insert(animator, entry);
// _waitCondition.wakeAll();
//}
//
//void FrameRenderer::deregisterAnimator(Animation *animator)
//{
// QMutexLocker mlocker(&_mutex);
//
// qCDebug(lcLottieQtBodymovinRenderThread) << "Deregister Animator:"
// << static_cast<void*>(animator);
//
// Entry *entry = _animData.value(animator, nullptr);
// if (entry) {
// qDeleteAll(entry->frameCache);
// delete entry->bmTreeBlueprint;
// delete entry;
// _animData.remove(animator);
// }
//}
//
//bool FrameRenderer::gotoFrame(Animation *animator, int frame)
//{
// QMutexLocker mlocker(&_mutex);
// Entry *entry = _animData.value(animator, nullptr);
// if (entry) {
// qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:"
// << static_cast<void*>(animator)
// << "Goto frame:" << frame;
// entry->currentFrame = frame;
// entry->animDir = animator->direction();
// pruneFrameCache(entry);
// _waitCondition.wakeAll();
// return true;
// }
// return false;
//}
//
//FrameRenderer::FrameRenderer() : QThread() {
// const QByteArray cacheStr = qgetenv("QLOTTIE_RENDER_CACHE_SIZE");
// int cacheSize = cacheStr.toInt();
// if (cacheSize > 0) {
// qCDebug(lcLottieQtBodymovinRenderThread) << "Setting frame cache size to" << cacheSize;
// _cacheSize = cacheSize;
// }
//}
//
//void FrameRenderer::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 *FrameRenderer::getFrame(Animation *animator, int frameNumber)
//{
// QMutexLocker mlocker(&_mutex);
//
// Entry *entry = _animData.value(animator, nullptr);
// if (entry)
// return entry->frameCache.value(frameNumber, nullptr);
// else
// return nullptr;
//}
//
//void FrameRenderer::prerender(Entry *animEntry)
//{
// while (animEntry->frameCache.count() < _cacheSize) {
// 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(animEntry->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 FrameRenderer::frameRendered(Animation *animator, int frameNumber)
//{
// QMutexLocker mlocker(&_mutex);
// Entry *entry = _animData.value(animator, nullptr);
// if (entry) {
// qCDebug(lcLottieQtBodymovinRenderThread) << "Animator:" << static_cast<void*>(animator)
// << "Remove frame from cache" << frameNumber;
//
// BMBase *root = entry->frameCache.value(frameNumber, nullptr);
// delete root;
// entry->frameCache.remove(frameNumber);
// _waitCondition.wakeAll();
// }
//}
//
//void FrameRenderer::run()
//{
// qCDebug(lcLottieQtBodymovinRenderThread) << "rendering thread" << QThread::currentThread();
//
// while (!isInterruptionRequested()) {
// QMutexLocker mlocker(&_mutex);
//
// for (Entry *e : qAsConst(_animData))
// prerender(e);
//
// _waitCondition.wait(&_mutex);
// }
//}
//
//int FrameRenderer::parse(BMBase* rootElement, const 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);
// // 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
// if (layer->isMaskLayer())
// rootElement->prependChild(layer);
// else
// rootElement->appendChild(layer);
// }
// }
//
// return 0;
//}
} // namespace Lottie

View File

@@ -0,0 +1,84 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#include <QHash>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
class BMBase;
class QImage;
namespace Lottie {
class Animation;
//
//class FrameRenderer : public QThread {
// Q_OBJECT
//
// struct Entry {
// Animation* animator = nullptr;
// BMBase *bmTreeBlueprint = nullptr;
// int startFrame = 0;
// int endFrame = 0;
// int currentFrame = 0;
// int animDir = 1;
// QHash<int, BMBase*> frameCache;
// };
//
//public:
// ~FrameRenderer();
//
// FrameRenderer(const FrameRenderer &other) = delete;
// void operator=(const FrameRenderer &other) = delete;
//
// static FrameRenderer *instance();
// static void deleteInstance();
//
// BMBase *getFrame(Animation *animator, int frameNumber);
//
//signals:
// void frameReady(Animation *animator, int frameNumber);
//
//public slots:
// void registerAnimator(Animation *animator);
// void deregisterAnimator(Animation *animator);
//
// bool gotoFrame(Animation *animator, int frame);
//
// void frameRendered(Animation *animator, int frameNumber);
//
//protected:
// void run() override;
//
// int parse(BMBase* rootElement, const QByteArray &jsonSource);
//
// void prerender(Entry *animEntry);
//
//protected:
// QHash<Animation*, Entry*> _animData;
// int _cacheSize = 2;
// int _currentFrame = 0;
//
// Animation *_animation = nullptr;
// QHash<int, QImage*> _frameCache;
//
//private:
// FrameRenderer();
//
// void pruneFrameCache(Entry* e);
//
//private:
// static FrameRenderer *_rendererInstance;
//
// QMutex _mutex;
// QWaitCondition _waitCondition;
//};
} // namespace Lottie

View File

@@ -0,0 +1,9 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "lottie/lottie_pch.h"

View File

@@ -0,0 +1,10 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
#define qAsConst(X) std::as_const(X)

View File

@@ -38,6 +38,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "auth_session.h"
#include "layout.h"
#include "storage/file_download.h"
#include "lottie/lottie_animation.h"
#include "calls/calls_instance.h"
#include "styles/style_mediaview.h"
#include "styles/style_history.h"