2019-06-28 11:33:47 +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_single_player.h"
|
|
|
|
|
|
|
|
#include "lottie/lottie_frame_renderer.h"
|
|
|
|
|
|
|
|
namespace Lottie {
|
|
|
|
|
|
|
|
SinglePlayer::SinglePlayer(
|
|
|
|
const QByteArray &content,
|
2019-07-02 11:46:37 +00:00
|
|
|
const FrameRequest &request,
|
2019-07-05 17:15:25 +00:00
|
|
|
Quality quality,
|
2019-08-07 14:22:51 +00:00
|
|
|
const ColorReplacements *replacements,
|
2019-07-02 11:46:37 +00:00
|
|
|
std::shared_ptr<FrameRenderer> renderer)
|
2019-08-07 14:22:51 +00:00
|
|
|
: _animation(this, content, request, quality, replacements)
|
2019-06-28 11:33:47 +00:00
|
|
|
, _timer([=] { checkNextFrameRender(); })
|
2019-07-02 11:46:37 +00:00
|
|
|
, _renderer(renderer ? renderer : FrameRenderer::Instance()) {
|
2019-06-28 11:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SinglePlayer::SinglePlayer(
|
|
|
|
FnMut<void(FnMut<void(QByteArray &&cached)>)> get, // Main thread.
|
|
|
|
FnMut<void(QByteArray &&cached)> put, // Unknown thread.
|
|
|
|
const QByteArray &content,
|
2019-07-02 11:46:37 +00:00
|
|
|
const FrameRequest &request,
|
2019-07-05 17:15:25 +00:00
|
|
|
Quality quality,
|
2019-08-07 14:22:51 +00:00
|
|
|
const ColorReplacements *replacements,
|
2019-07-02 11:46:37 +00:00
|
|
|
std::shared_ptr<FrameRenderer> renderer)
|
2019-08-07 14:22:51 +00:00
|
|
|
: _animation(
|
|
|
|
this,
|
|
|
|
std::move(get),
|
|
|
|
std::move(put),
|
|
|
|
content,
|
|
|
|
request,
|
|
|
|
quality,
|
|
|
|
replacements)
|
2019-06-28 11:33:47 +00:00
|
|
|
, _timer([=] { checkNextFrameRender(); })
|
2019-07-02 11:46:37 +00:00
|
|
|
, _renderer(renderer ? renderer : FrameRenderer::Instance()) {
|
2019-06-28 11:33:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:24:33 +00:00
|
|
|
SinglePlayer::~SinglePlayer() {
|
|
|
|
if (_state) {
|
|
|
|
_renderer->remove(_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:33:47 +00:00
|
|
|
void SinglePlayer::start(
|
|
|
|
not_null<Animation*> animation,
|
|
|
|
std::unique_ptr<SharedState> state) {
|
|
|
|
Expects(animation == &_animation);
|
|
|
|
|
|
|
|
_state = state.get();
|
|
|
|
auto information = state->information();
|
|
|
|
state->start(this, crl::now());
|
2019-07-05 14:43:21 +00:00
|
|
|
const auto request = state->frameForPaint()->request;
|
|
|
|
_renderer->append(std::move(state), request);
|
2019-06-28 11:33:47 +00:00
|
|
|
_updates.fire({ std::move(information) });
|
|
|
|
|
|
|
|
crl::on_main_update_requests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
checkStep();
|
|
|
|
}, _lifetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SinglePlayer::failed(not_null<Animation*> animation, Error error) {
|
|
|
|
Expects(animation == &_animation);
|
|
|
|
|
|
|
|
_updates.fire_error(std::move(error));
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:24:33 +00:00
|
|
|
rpl::producer<Update, Error> SinglePlayer::updates() const {
|
2019-06-28 11:33:47 +00:00
|
|
|
return _updates.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SinglePlayer::ready() const {
|
|
|
|
return _animation.ready();
|
|
|
|
}
|
|
|
|
|
2019-07-02 13:20:04 +00:00
|
|
|
QImage SinglePlayer::frame() const {
|
|
|
|
return _animation.frame();
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:33:47 +00:00
|
|
|
QImage SinglePlayer::frame(const FrameRequest &request) const {
|
|
|
|
return _animation.frame(request);
|
|
|
|
}
|
|
|
|
|
2019-08-01 11:42:24 +00:00
|
|
|
Animation::FrameInfo SinglePlayer::frameInfo(
|
|
|
|
const FrameRequest &request) const {
|
|
|
|
return _animation.frameInfo(request);
|
|
|
|
}
|
|
|
|
|
2019-06-28 11:33:47 +00:00
|
|
|
void SinglePlayer::checkStep() {
|
2019-06-29 12:24:46 +00:00
|
|
|
if (_nextFrameTime == kFrameDisplayTimeAlreadyDone) {
|
|
|
|
return;
|
|
|
|
} else if (_nextFrameTime != kTimeUnknown) {
|
2019-06-28 11:33:47 +00:00
|
|
|
checkNextFrameRender();
|
|
|
|
} else {
|
|
|
|
checkNextFrameAvailability();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SinglePlayer::checkNextFrameAvailability() {
|
|
|
|
Expects(_state != nullptr);
|
2019-06-29 12:24:46 +00:00
|
|
|
Expects(_nextFrameTime == kTimeUnknown);
|
2019-06-28 11:33:47 +00:00
|
|
|
|
|
|
|
_nextFrameTime = _state->nextFrameDisplayTime();
|
2019-06-29 12:24:46 +00:00
|
|
|
Assert(_nextFrameTime != kFrameDisplayTimeAlreadyDone);
|
2019-06-28 11:33:47 +00:00
|
|
|
if (_nextFrameTime != kTimeUnknown) {
|
2019-06-28 13:24:33 +00:00
|
|
|
checkNextFrameRender();
|
2019-06-28 11:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SinglePlayer::checkNextFrameRender() {
|
|
|
|
Expects(_nextFrameTime != kTimeUnknown);
|
|
|
|
|
|
|
|
const auto now = crl::now();
|
|
|
|
if (now < _nextFrameTime) {
|
|
|
|
if (!_timer.isActive()) {
|
|
|
|
_timer.callOnce(_nextFrameTime - now);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_timer.cancel();
|
|
|
|
|
2019-07-01 11:20:53 +00:00
|
|
|
_state->markFrameDisplayed(now);
|
2019-06-30 13:19:57 +00:00
|
|
|
_state->addTimelineDelay(now - _nextFrameTime);
|
|
|
|
|
|
|
|
_nextFrameTime = kFrameDisplayTimeAlreadyDone;
|
2019-07-01 11:20:53 +00:00
|
|
|
_updates.fire({ DisplayFrameRequest() });
|
2019-06-28 11:33:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SinglePlayer::updateFrameRequest(
|
|
|
|
not_null<const Animation*> animation,
|
|
|
|
const FrameRequest &request) {
|
|
|
|
Expects(animation == &_animation);
|
|
|
|
Expects(_state != nullptr);
|
|
|
|
|
|
|
|
_renderer->updateFrameRequest(_state, request);
|
|
|
|
}
|
|
|
|
|
2019-08-01 11:42:24 +00:00
|
|
|
bool SinglePlayer::markFrameShown() {
|
2019-06-28 11:33:47 +00:00
|
|
|
Expects(_state != nullptr);
|
|
|
|
|
2019-06-29 12:24:46 +00:00
|
|
|
if (_nextFrameTime == kFrameDisplayTimeAlreadyDone) {
|
|
|
|
_nextFrameTime = kTimeUnknown;
|
|
|
|
}
|
2019-07-01 11:20:53 +00:00
|
|
|
if (_state->markFrameShown()) {
|
2019-06-29 12:24:46 +00:00
|
|
|
_renderer->frameShown();
|
2019-08-01 11:42:24 +00:00
|
|
|
return true;
|
2019-06-29 12:24:46 +00:00
|
|
|
}
|
2019-08-01 11:42:24 +00:00
|
|
|
return false;
|
2019-06-28 11:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lottie
|