tdesktop/Telegram/SourceFiles/lottie/lottie_cache.h

105 lines
2.2 KiB
C
Raw Normal View History

2019-06-26 14:18:00 +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
#include <QImage>
#include <QSize>
#include <QByteArray>
namespace Lottie {
2019-06-26 17:14:46 +00:00
struct FrameRequest;
2019-06-26 14:18:00 +00:00
class AlignedStorage {
public:
void allocate(int packedBytesPerLine, int lines);
int lines() const;
int rawSize() const;
// Gives a pointer to packedBytesPerLine * lines bytes of memory.
void *raw();
const void *raw() const;
// Gives a stride value in the aligned storage (% 16 == 0).
int bytesPerLine() const;
// Gives a pointer to the aligned memory (% 16 == 0).
void *aligned();
const void *aligned() const;
void copyRawToAligned();
void copyAlignedToRaw();
private:
void reallocate();
int _packedBytesPerLine = 0;
int _lines = 0;
QByteArray _raw;
QByteArray _buffer;
};
class CacheState {
public:
2019-06-27 09:57:48 +00:00
enum class Encoder : qint8 {
2019-06-26 14:18:00 +00:00
YUV420A4_LZ4,
};
2019-06-26 17:14:46 +00:00
CacheState(const QByteArray &data, const FrameRequest &request);
2019-06-26 14:18:00 +00:00
2019-06-26 17:14:46 +00:00
void init(
QSize original,
int frameRate,
int framesCount,
const FrameRequest &request);
2019-06-26 14:18:00 +00:00
[[nodiscard]] int frameRate() const;
[[nodiscard]] int framesReady() const;
[[nodiscard]] int framesCount() const;
2019-06-26 17:14:46 +00:00
[[nodiscard]] QSize originalSize() const;
2019-06-26 14:18:00 +00:00
[[nodiscard]] QImage takeFirstFrame();
2019-06-26 17:14:46 +00:00
[[nodiscard]] bool renderFrame(
QImage &to,
const FrameRequest &request,
int index);
void appendFrame(
const QImage &frame,
const FrameRequest &request,
int index);
2019-06-26 14:18:00 +00:00
private:
2019-06-27 09:57:48 +00:00
int headerSize() const;
2019-06-26 14:18:00 +00:00
void prepareBuffers();
2019-06-27 09:57:48 +00:00
void finalizeEncoding();
2019-06-26 17:14:46 +00:00
void writeHeader();
[[nodiscard]] bool readHeader(const FrameRequest &request);
void writeCompressedDelta();
[[nodiscard]] bool readCompressedDelta();
2019-06-26 14:18:00 +00:00
QByteArray _data;
2019-06-27 09:57:48 +00:00
std::vector<QByteArray> _compressedFrames;
QByteArray _compressBuffer;
2019-06-26 14:18:00 +00:00
QSize _size;
QSize _original;
AlignedStorage _uncompressed;
AlignedStorage _previous;
QImage _firstFrame;
int _frameRate = 0;
int _framesCount = 0;
int _framesReady = 0;
int _offset = 0;
2019-06-26 17:14:46 +00:00
int _offsetFrameIndex = 0;
2019-06-26 14:18:00 +00:00
Encoder _encoder = Encoder::YUV420A4_LZ4;
};
} // namespace Lottie