2019-04-27 13:12:53 +04: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-14 00:57:59 +03:00
|
|
|
#include "base/basic_types.h"
|
|
|
|
#include "base/weak_ptr.h"
|
|
|
|
#include "lottie/lottie_common.h"
|
|
|
|
|
|
|
|
#include <QImage>
|
2019-06-26 10:29:19 +02:00
|
|
|
#include <QSize>
|
2019-05-14 00:57:59 +03:00
|
|
|
#include <crl/crl_time.h>
|
|
|
|
#include <crl/crl_object_on_queue.h>
|
|
|
|
#include <limits>
|
2019-04-27 13:12:53 +04:00
|
|
|
|
2019-06-25 16:16:38 +02:00
|
|
|
namespace rlottie {
|
|
|
|
class Animation;
|
|
|
|
} // namespace rlottie
|
|
|
|
|
2019-04-27 13:12:53 +04:00
|
|
|
namespace Lottie {
|
|
|
|
|
2019-07-07 15:34:35 +02:00
|
|
|
inline constexpr auto kMaxFrameRate = 60;
|
|
|
|
inline constexpr auto kMaxSize = 512;
|
|
|
|
inline constexpr auto kMaxFramesCount = 180;
|
2019-06-29 14:24:46 +02:00
|
|
|
inline constexpr auto kFrameDisplayTimeAlreadyDone
|
|
|
|
= std::numeric_limits<crl::time>::max();
|
2019-07-01 13:20:53 +02:00
|
|
|
inline constexpr auto kDisplayedInitial = crl::time(-1);
|
2019-06-26 16:18:00 +02:00
|
|
|
|
2019-06-28 13:33:47 +02:00
|
|
|
class Player;
|
2019-06-27 16:36:01 +02:00
|
|
|
class Cache;
|
2019-05-14 00:57:59 +03:00
|
|
|
|
|
|
|
struct Frame {
|
|
|
|
QImage original;
|
2019-07-01 13:20:53 +02:00
|
|
|
crl::time displayed = kDisplayedInitial;
|
2019-05-14 00:57:59 +03:00
|
|
|
crl::time display = kTimeUnknown;
|
2019-07-01 13:20:53 +02:00
|
|
|
int index = 0;
|
2019-05-14 00:57:59 +03:00
|
|
|
|
2019-06-26 10:55:08 +02:00
|
|
|
FrameRequest request;
|
2019-05-14 00:57:59 +03:00
|
|
|
QImage prepared;
|
|
|
|
};
|
|
|
|
|
|
|
|
QImage PrepareFrameByRequest(
|
|
|
|
not_null<Frame*> frame,
|
|
|
|
bool useExistingPrepared);
|
|
|
|
|
|
|
|
class SharedState {
|
|
|
|
public:
|
2019-06-27 18:57:32 +02:00
|
|
|
SharedState(
|
|
|
|
std::unique_ptr<rlottie::Animation> animation,
|
2019-07-05 19:15:25 +02:00
|
|
|
const FrameRequest &request,
|
|
|
|
Quality quality);
|
2019-06-26 19:14:46 +02:00
|
|
|
SharedState(
|
|
|
|
const QByteArray &content,
|
|
|
|
std::unique_ptr<rlottie::Animation> animation,
|
2019-06-27 16:36:01 +02:00
|
|
|
std::unique_ptr<Cache> cache,
|
2019-07-05 19:15:25 +02:00
|
|
|
const FrameRequest &request,
|
|
|
|
Quality quality);
|
2019-05-14 00:57:59 +03:00
|
|
|
|
2019-06-30 15:19:57 +02:00
|
|
|
void start(
|
|
|
|
not_null<Player*> owner,
|
|
|
|
crl::time now,
|
|
|
|
crl::time delay = 0,
|
|
|
|
int skippedFrames = 0);
|
2019-05-14 00:57:59 +03:00
|
|
|
|
|
|
|
[[nodiscard]] Information information() const;
|
|
|
|
[[nodiscard]] bool initialized() const;
|
|
|
|
|
|
|
|
[[nodiscard]] not_null<Frame*> frameForPaint();
|
|
|
|
[[nodiscard]] crl::time nextFrameDisplayTime() const;
|
2019-07-01 13:20:53 +02:00
|
|
|
void addTimelineDelay(crl::time delayed, int skippedFrames = 0);
|
|
|
|
void markFrameDisplayed(crl::time now);
|
|
|
|
bool markFrameShown();
|
2019-05-14 00:57:59 +03:00
|
|
|
|
|
|
|
void renderFrame(QImage &image, const FrameRequest &request, int index);
|
2019-06-29 14:24:46 +02:00
|
|
|
|
|
|
|
struct RenderResult {
|
|
|
|
bool rendered = false;
|
|
|
|
base::weak_ptr<Player> notify;
|
|
|
|
};
|
|
|
|
[[nodiscard]] RenderResult renderNextFrame(const FrameRequest &request);
|
2019-05-14 00:57:59 +03:00
|
|
|
|
2019-06-25 16:16:38 +02:00
|
|
|
~SharedState();
|
|
|
|
|
2019-05-14 00:57:59 +03:00
|
|
|
private:
|
2019-06-26 19:14:46 +02:00
|
|
|
void construct(const FrameRequest &request);
|
2019-06-26 10:29:19 +02:00
|
|
|
void calculateProperties();
|
2019-06-25 16:16:38 +02:00
|
|
|
bool isValid() const;
|
2019-06-26 19:14:46 +02:00
|
|
|
void init(QImage cover, const FrameRequest &request);
|
2019-05-14 00:57:59 +03:00
|
|
|
void renderNextFrame(
|
|
|
|
not_null<Frame*> frame,
|
|
|
|
const FrameRequest &request);
|
2019-07-01 13:20:53 +02:00
|
|
|
[[nodiscard]] crl::time countFrameDisplayTime(int index) const;
|
2019-05-14 00:57:59 +03:00
|
|
|
[[nodiscard]] not_null<Frame*> getFrame(int index);
|
|
|
|
[[nodiscard]] not_null<const Frame*> getFrame(int index) const;
|
|
|
|
[[nodiscard]] int counter() const;
|
|
|
|
|
2019-06-26 19:14:46 +02:00
|
|
|
QByteArray _content;
|
2019-06-25 16:16:38 +02:00
|
|
|
std::unique_ptr<rlottie::Animation> _animation;
|
2019-07-05 19:15:25 +02:00
|
|
|
Quality _quality = Quality::Default;
|
2019-05-14 00:57:59 +03:00
|
|
|
|
2019-06-30 15:19:57 +02:00
|
|
|
// crl::queue changes 0,2,4,6 to 1,3,5,7.
|
|
|
|
// main thread changes 1,3,5,7 to 2,4,6,0.
|
2019-05-14 00:57:59 +03:00
|
|
|
static constexpr auto kCounterUninitialized = -1;
|
|
|
|
std::atomic<int> _counter = kCounterUninitialized;
|
|
|
|
|
|
|
|
static constexpr auto kFramesCount = 4;
|
|
|
|
std::array<Frame, kFramesCount> _frames;
|
|
|
|
|
2019-06-28 13:33:47 +02:00
|
|
|
base::weak_ptr<Player> _owner;
|
2019-05-14 00:57:59 +03:00
|
|
|
crl::time _started = kTimeUnknown;
|
2019-06-30 15:19:57 +02:00
|
|
|
|
|
|
|
// (_counter % 2) == 1 main thread can write _delay.
|
|
|
|
// (_counter % 2) == 0 crl::queue can read _delay.
|
|
|
|
crl::time _delay = kTimeUnknown;
|
|
|
|
|
2019-05-14 00:57:59 +03:00
|
|
|
int _frameIndex = 0;
|
2019-06-30 15:19:57 +02:00
|
|
|
int _skippedFrames = 0;
|
2019-05-14 13:04:33 +03:00
|
|
|
int _framesCount = 0;
|
2019-06-26 10:29:19 +02:00
|
|
|
int _frameRate = 0;
|
|
|
|
QSize _size;
|
2019-05-14 00:57:59 +03:00
|
|
|
|
2019-06-26 19:14:46 +02:00
|
|
|
std::unique_ptr<Cache> _cache;
|
|
|
|
|
2019-05-14 00:57:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class FrameRendererObject;
|
|
|
|
|
|
|
|
class FrameRenderer final {
|
|
|
|
public:
|
2019-06-28 15:24:33 +02:00
|
|
|
static std::shared_ptr<FrameRenderer> CreateIndependent();
|
2019-05-14 00:57:59 +03:00
|
|
|
static std::shared_ptr<FrameRenderer> Instance();
|
|
|
|
|
2019-07-05 16:43:21 +02:00
|
|
|
void append(
|
|
|
|
std::unique_ptr<SharedState> entry,
|
|
|
|
const FrameRequest &request);
|
2019-06-28 13:33:47 +02:00
|
|
|
|
2019-05-14 00:57:59 +03:00
|
|
|
void updateFrameRequest(
|
|
|
|
not_null<SharedState*> entry,
|
|
|
|
const FrameRequest &request);
|
2019-06-28 15:24:33 +02:00
|
|
|
void frameShown();
|
2019-05-14 00:57:59 +03:00
|
|
|
void remove(not_null<SharedState*> state);
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Implementation = FrameRendererObject;
|
|
|
|
crl::object_on_queue<Implementation> _wrapped;
|
|
|
|
|
|
|
|
};
|
2019-04-27 13:12:53 +04:00
|
|
|
|
|
|
|
} // namespace Lottie
|