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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2019-05-13 21:57:59 +00:00
|
|
|
#include "base/basic_types.h"
|
|
|
|
#include "base/weak_ptr.h"
|
|
|
|
#include "lottie/lottie_common.h"
|
|
|
|
|
|
|
|
#include <QImage>
|
2019-06-26 08:29:19 +00:00
|
|
|
#include <QSize>
|
2019-05-13 21:57:59 +00:00
|
|
|
#include <crl/crl_time.h>
|
|
|
|
#include <crl/crl_object_on_queue.h>
|
|
|
|
#include <limits>
|
2019-04-27 09:12:53 +00:00
|
|
|
|
2019-06-25 14:16:38 +00:00
|
|
|
namespace rlottie {
|
|
|
|
class Animation;
|
|
|
|
} // namespace rlottie
|
|
|
|
|
2019-04-27 09:12:53 +00:00
|
|
|
namespace Lottie {
|
|
|
|
|
2019-06-26 14:18:00 +00:00
|
|
|
inline constexpr auto kMaxFrameRate = 120;
|
|
|
|
inline constexpr auto kMaxSize = 3096;
|
|
|
|
inline constexpr auto kMaxFramesCount = 600;
|
|
|
|
|
2019-04-27 09:12:53 +00:00
|
|
|
class Animation;
|
2019-06-27 14:36:01 +00:00
|
|
|
class Cache;
|
2019-05-13 21:57:59 +00:00
|
|
|
|
|
|
|
struct Frame {
|
|
|
|
QImage original;
|
|
|
|
crl::time position = kTimeUnknown;
|
|
|
|
crl::time displayed = kTimeUnknown;
|
|
|
|
crl::time display = kTimeUnknown;
|
|
|
|
|
2019-06-26 08:55:08 +00:00
|
|
|
FrameRequest request;
|
2019-05-13 21:57:59 +00:00
|
|
|
QImage prepared;
|
|
|
|
};
|
|
|
|
|
|
|
|
QImage PrepareFrameByRequest(
|
|
|
|
not_null<Frame*> frame,
|
|
|
|
bool useExistingPrepared);
|
|
|
|
|
|
|
|
class SharedState {
|
|
|
|
public:
|
2019-06-25 14:16:38 +00:00
|
|
|
explicit SharedState(std::unique_ptr<rlottie::Animation> animation);
|
2019-06-26 17:14:46 +00:00
|
|
|
SharedState(
|
|
|
|
const QByteArray &content,
|
|
|
|
std::unique_ptr<rlottie::Animation> animation,
|
2019-06-27 14:36:01 +00:00
|
|
|
std::unique_ptr<Cache> cache,
|
2019-06-26 17:14:46 +00:00
|
|
|
const FrameRequest &request);
|
2019-05-13 21:57:59 +00:00
|
|
|
|
|
|
|
void start(not_null<Animation*> owner, crl::time now);
|
|
|
|
|
|
|
|
[[nodiscard]] Information information() const;
|
|
|
|
[[nodiscard]] bool initialized() const;
|
|
|
|
|
|
|
|
[[nodiscard]] not_null<Frame*> frameForPaint();
|
|
|
|
[[nodiscard]] crl::time nextFrameDisplayTime() const;
|
|
|
|
crl::time markFrameDisplayed(crl::time now);
|
|
|
|
crl::time markFrameShown();
|
|
|
|
|
|
|
|
void renderFrame(QImage &image, const FrameRequest &request, int index);
|
|
|
|
[[nodiscard]] bool renderNextFrame(const FrameRequest &request);
|
|
|
|
|
2019-06-25 14:16:38 +00:00
|
|
|
~SharedState();
|
|
|
|
|
2019-05-13 21:57:59 +00:00
|
|
|
private:
|
2019-06-26 17:14:46 +00:00
|
|
|
void construct(const FrameRequest &request);
|
2019-06-26 08:29:19 +00:00
|
|
|
void calculateProperties();
|
2019-06-25 14:16:38 +00:00
|
|
|
bool isValid() const;
|
2019-06-26 17:14:46 +00:00
|
|
|
void init(QImage cover, const FrameRequest &request);
|
2019-05-13 21:57:59 +00:00
|
|
|
void renderNextFrame(
|
|
|
|
not_null<Frame*> frame,
|
|
|
|
const FrameRequest &request);
|
|
|
|
[[nodiscard]] not_null<Frame*> getFrame(int index);
|
|
|
|
[[nodiscard]] not_null<const Frame*> getFrame(int index) const;
|
|
|
|
[[nodiscard]] int counter() const;
|
|
|
|
|
2019-06-26 17:14:46 +00:00
|
|
|
QByteArray _content;
|
2019-06-25 14:16:38 +00:00
|
|
|
std::unique_ptr<rlottie::Animation> _animation;
|
2019-05-13 21:57:59 +00:00
|
|
|
|
|
|
|
static constexpr auto kCounterUninitialized = -1;
|
|
|
|
std::atomic<int> _counter = kCounterUninitialized;
|
|
|
|
|
|
|
|
static constexpr auto kFramesCount = 4;
|
|
|
|
std::array<Frame, kFramesCount> _frames;
|
|
|
|
|
|
|
|
base::weak_ptr<Animation> _owner;
|
|
|
|
crl::time _started = kTimeUnknown;
|
2019-05-14 10:04:33 +00:00
|
|
|
crl::time _duration = kTimeUnknown;
|
2019-05-13 21:57:59 +00:00
|
|
|
int _frameIndex = 0;
|
2019-05-14 10:04:33 +00:00
|
|
|
int _framesCount = 0;
|
2019-06-26 08:29:19 +00:00
|
|
|
int _frameRate = 0;
|
|
|
|
QSize _size;
|
2019-05-14 10:04:33 +00:00
|
|
|
std::atomic<int> _accumulatedDelayMs = 0;
|
2019-05-13 21:57:59 +00:00
|
|
|
|
2019-06-26 17:14:46 +00:00
|
|
|
std::unique_ptr<Cache> _cache;
|
|
|
|
|
2019-05-13 21:57:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FrameRendererObject;
|
|
|
|
|
|
|
|
class FrameRenderer final {
|
|
|
|
public:
|
|
|
|
static std::shared_ptr<FrameRenderer> Instance();
|
|
|
|
|
|
|
|
void append(std::unique_ptr<SharedState> entry);
|
|
|
|
void updateFrameRequest(
|
|
|
|
not_null<SharedState*> entry,
|
|
|
|
const FrameRequest &request);
|
|
|
|
void frameShown(not_null<SharedState*> entry);
|
|
|
|
void remove(not_null<SharedState*> state);
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Implementation = FrameRendererObject;
|
|
|
|
crl::object_on_queue<Implementation> _wrapped;
|
|
|
|
|
|
|
|
};
|
2019-04-27 09:12:53 +00:00
|
|
|
|
|
|
|
} // namespace Lottie
|