tdesktop/Telegram/SourceFiles/lottie/lottie_animation.cpp

229 lines
5.4 KiB
C++
Raw Normal View History

2019-04-27 09:12:53 +00:00
/*
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 "rasterrenderer/rasterrenderer.h"
#include "json.h"
2019-04-27 09:12:53 +00:00
#include "base/algorithm.h"
#include "zlib.h"
#include "logs.h"
2019-04-27 09:12:53 +00:00
#include <QFile>
#include <crl/crl_async.h>
#include <crl/crl_on_main.h>
2019-04-27 09:12:53 +00:00
namespace Lottie {
namespace {
constexpr auto kMaxSize = 1024 * 1024;
QByteArray UnpackGzip(const QByteArray &bytes) {
z_stream stream;
stream.zalloc = nullptr;
stream.zfree = nullptr;
stream.opaque = nullptr;
stream.avail_in = 0;
stream.next_in = nullptr;
int res = inflateInit2(&stream, 16 + MAX_WBITS);
if (res != Z_OK) {
return bytes;
}
const auto guard = gsl::finally([&] { inflateEnd(&stream); });
auto result = QByteArray(kMaxSize + 1, Qt::Uninitialized);
stream.avail_in = bytes.size();
stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(bytes.data()));
stream.avail_out = 0;
while (!stream.avail_out) {
stream.avail_out = result.size();
stream.next_out = reinterpret_cast<Bytef*>(result.data());
int res = inflate(&stream, Z_NO_FLUSH);
if (res != Z_OK && res != Z_STREAM_END) {
return bytes;
} else if (!stream.avail_out) {
return bytes;
}
}
result.resize(result.size() - stream.avail_out);
return result;
}
} // namespace
2019-04-27 09:12:53 +00:00
bool ValidateFile(const QString &path) {
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)
&& !path.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) {
2019-04-27 09:12:53 +00:00
return false;
}
return true;
}
std::unique_ptr<Animation> FromFile(const QString &path) {
if (!path.endsWith(qstr(".json"), Qt::CaseInsensitive)
&& !path.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) {
2019-04-27 09:12:53 +00:00
return nullptr;
}
auto f = QFile(path);
if (!f.open(QIODevice::ReadOnly)) {
return nullptr;
}
const auto content = f.readAll();
if (content.isEmpty()) {
return nullptr;
}
return FromData(std::move(content));
}
std::unique_ptr<Animation> FromData(const QByteArray &data) {
return std::make_unique<Animation>(base::duplicate(data));
2019-04-27 09:12:53 +00:00
}
Animation::Animation(QByteArray &&content)
2019-05-28 11:39:38 +00:00
: _timer([=] { checkNextFrameRender(); }) {
const auto weak = base::make_weak(this);
crl::async([=, content = base::take(content)]() mutable {
content = UnpackGzip(content);
if (content.size() > kMaxSize) {
qWarning()
<< "Lottie Error: Too large file: "
<< content.size();
crl::on_main(weak, [=] {
parseFailed();
});
return;
}
const auto document = JsonDocument(std::move(content));
if (const auto error = document.error()) {
qWarning()
<< "Lottie Error: Parse failed with code: "
<< error;
crl::on_main(weak, [=] {
parseFailed();
});
} else {
auto state = std::make_unique<SharedState>(document.root());
crl::on_main(weak, [this, result = std::move(state)]() mutable {
parseDone(std::move(result));
});
}
});
2019-04-27 09:12:53 +00:00
}
Animation::~Animation() {
if (_renderer) {
Assert(_state != nullptr);
_renderer->remove(_state);
}
2019-04-27 09:12:53 +00:00
}
void Animation::parseDone(std::unique_ptr<SharedState> state) {
Expects(state != nullptr);
auto information = state->information();
if (!information.frameRate
|| information.framesCount <= 0
|| information.size.isEmpty()) {
_updates.fire_error(Error::NotSupported);
2019-05-28 11:39:38 +00:00
return;
2019-04-27 09:12:53 +00:00
}
2019-05-28 11:39:38 +00:00
_state = state.get();
_state->start(this, crl::now());
_renderer = FrameRenderer::Instance();
_renderer->append(std::move(state));
_updates.fire({ std::move(information) });
crl::on_main_update_requests(
) | rpl::start_with_next([=] {
checkStep();
}, _lifetime);
}
void Animation::parseFailed() {
_updates.fire_error(Error::ParseFailed);
}
QImage Animation::frame(const FrameRequest &request) const {
Expects(_renderer != nullptr);
const auto frame = _state->frameForPaint();
const auto changed = (frame->request != request)
&& (request.strict || !frame->request.strict);
if (changed) {
frame->request = request;
_renderer->updateFrameRequest(_state, request);
2019-04-27 09:12:53 +00:00
}
return PrepareFrameByRequest(frame, !changed);
2019-04-27 09:12:53 +00:00
}
rpl::producer<Update, Error> Animation::updates() const {
return _updates.events();
2019-04-27 09:12:53 +00:00
}
bool Animation::ready() const {
return (_renderer != nullptr);
2019-05-02 12:42:47 +00:00
}
crl::time Animation::markFrameDisplayed(crl::time now) {
Expects(_renderer != nullptr);
const auto result = _state->markFrameDisplayed(now);
return result;
2019-04-27 09:12:53 +00:00
}
crl::time Animation::markFrameShown() {
Expects(_renderer != nullptr);
const auto result = _state->markFrameShown();
_renderer->frameShown(_state);
return result;
}
2019-04-27 09:12:53 +00:00
2019-05-28 11:39:38 +00:00
void Animation::checkStep() {
if (_nextFrameTime != kTimeUnknown) {
checkNextFrameRender();
} else {
checkNextFrameAvailability();
}
}
void Animation::checkNextFrameAvailability() {
Expects(_renderer != nullptr);
2019-05-28 11:39:38 +00:00
_nextFrameTime = _state->nextFrameDisplayTime();
if (_nextFrameTime != kTimeUnknown) {
checkStep();
2019-04-27 09:12:53 +00:00
}
2019-05-28 11:39:38 +00:00
}
void Animation::checkNextFrameRender() {
Expects(_nextFrameTime != kTimeUnknown);
2019-04-27 09:12:53 +00:00
const auto now = crl::now();
2019-05-28 11:39:38 +00:00
if (now < _nextFrameTime) {
if (!_timer.isActive()) {
_timer.callOnce(_nextFrameTime - now);
}
} else {
_timer.cancel();
2019-05-28 11:39:38 +00:00
_nextFrameTime = kTimeUnknown;
const auto position = markFrameDisplayed(now);
_updates.fire({ DisplayFrameRequest{ position } });
}
2019-04-27 09:12:53 +00:00
}
//void Animation::play(const PlaybackOptions &options) {
// _options = options;
// _started = crl::now();
//}
2019-04-27 09:12:53 +00:00
} // namespace Lottie