mirror-chatterino2/src/messages/Image.hpp

131 lines
3.1 KiB
C++
Raw Normal View History

2017-06-06 21:18:05 +02:00
#pragma once
2017-01-04 15:12:31 +01:00
2017-01-13 18:59:11 +01:00
#include <QPixmap>
2017-01-11 18:52:09 +01:00
#include <QString>
2018-08-09 18:39:46 +02:00
#include <QThread>
#include <QTimer>
2018-08-10 18:56:17 +02:00
#include <QVector>
2018-08-02 14:23:27 +02:00
#include <atomic>
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
2018-08-06 20:00:04 +02:00
#include <boost/variant.hpp>
#include <chrono>
#include <map>
2018-08-02 14:23:27 +02:00
#include <memory>
#include <mutex>
2018-08-11 17:15:17 +02:00
#include <pajlada/signals/signal.hpp>
#include "common/Aliases.hpp"
2019-09-22 10:27:05 +02:00
#include "common/Common.hpp"
2018-01-19 22:45:33 +01:00
2017-01-18 21:30:23 +01:00
namespace chatterino {
2018-09-20 13:09:37 +02:00
namespace detail {
2018-08-15 22:46:20 +02:00
template <typename Image>
struct Frame {
Image image;
int duration;
};
class Frames : boost::noncopyable
{
public:
Frames();
Frames(QVector<Frame<QPixmap>> &&frames);
2018-08-15 22:46:20 +02:00
~Frames();
2018-08-06 18:25:47 +02:00
void clear();
bool empty() const;
2018-08-15 22:46:20 +02:00
bool animated() const;
void advance();
boost::optional<QPixmap> current() const;
boost::optional<QPixmap> first() const;
2018-08-06 18:25:47 +02:00
2018-08-15 22:46:20 +02:00
private:
2020-02-16 14:24:11 +01:00
void processOffset();
2018-08-15 22:46:20 +02:00
QVector<Frame<QPixmap>> items_;
int index_{0};
int durationOffset_{0};
pajlada::Signals::Connection gifTimerConnection_;
};
2018-09-20 13:09:37 +02:00
} // namespace detail
2017-01-18 21:30:23 +01:00
2018-08-02 14:23:27 +02:00
class Image;
using ImagePtr = std::shared_ptr<Image>;
2019-09-22 10:27:05 +02:00
/// This class is thread safe.
2018-08-02 14:23:27 +02:00
class Image : public std::enable_shared_from_this<Image>, boost::noncopyable
2017-01-04 15:12:31 +01:00
{
public:
2021-06-24 22:54:36 +02:00
// Maximum amount of RAM used by the image in bytes.
static constexpr int maxBytesRam = 20 * 1024 * 1024;
2019-10-07 20:03:15 +02:00
~Image();
2018-08-02 14:23:27 +02:00
static ImagePtr fromUrl(const Url &url, qreal scale = 1);
static ImagePtr fromResourcePixmap(const QPixmap &pixmap, qreal scale = 1);
2018-08-02 14:23:27 +02:00
static ImagePtr getEmpty();
2018-08-06 18:25:47 +02:00
const Url &url() const;
bool loaded() const;
// either returns the current pixmap, or triggers loading it (lazy loading)
boost::optional<QPixmap> pixmapOrLoad() const;
void load() const;
2018-08-06 18:25:47 +02:00
qreal scale() const;
2018-08-10 18:56:17 +02:00
bool isEmpty() const;
2018-08-06 18:25:47 +02:00
int width() const;
int height() const;
bool animated() const;
2017-01-11 01:08:20 +01:00
2018-08-02 14:23:27 +02:00
bool operator==(const Image &image) const;
bool operator!=(const Image &image) const;
2017-01-05 16:07:20 +01:00
private:
2018-08-02 14:23:27 +02:00
Image();
Image(const Url &url, qreal scale);
2019-08-01 13:30:58 +02:00
Image(qreal scale);
void setPixmap(const QPixmap &pixmap);
void actuallyLoad();
void expireFrames();
2018-08-06 18:25:47 +02:00
2019-09-22 10:27:05 +02:00
const Url url_{};
const qreal scale_{1};
std::atomic_bool empty_{false};
mutable std::chrono::time_point<std::chrono::steady_clock> lastUsed_;
2018-08-06 18:25:47 +02:00
bool shouldLoad_{false};
// gui thread only
2018-09-20 13:09:37 +02:00
std::unique_ptr<detail::Frames> frames_{};
friend class ImageExpirationPool;
2017-01-04 15:12:31 +01:00
};
class ImageExpirationPool
{
private:
friend class Image;
ImageExpirationPool();
static ImageExpirationPool &instance();
void addImagePtr(ImagePtr imgPtr);
void removeImagePtr(Image *rawPtr);
/**
* @brief Frees frame data for all images that ImagePool deems to have expired.
*
* Expiration is based on last accessed time of the Image, stored in Image::lastUsed_.
* Must be ran in the GUI thread.
*/
void freeOld();
private:
// Timer to periodically run freeOld()
QTimer freeTimer_;
std::map<Image *, std::weak_ptr<Image>> allImages_;
std::mutex mutex_;
};
2017-04-14 17:52:22 +02:00
} // namespace chatterino