2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
#ifndef TIMAGECACHE_H
|
|
|
|
#define TIMAGECACHE_H
|
|
|
|
|
2016-04-14 22:15:09 +12:00
|
|
|
#include <memory>
|
|
|
|
|
2016-03-19 06:57:51 +13:00
|
|
|
// TnzCore includes
|
|
|
|
#include "tcommon.h"
|
|
|
|
#include "timage.h"
|
|
|
|
#include "tsmartpointer.h"
|
|
|
|
|
|
|
|
// Qt includes
|
|
|
|
#ifndef TNZCORE_LIGHT
|
|
|
|
#include <QString>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef DVAPI
|
|
|
|
#undef DVVAR
|
|
|
|
#ifdef TSYSTEM_EXPORTS
|
|
|
|
#define DVAPI DV_EXPORT_API
|
|
|
|
#define DVVAR DV_EXPORT_VAR
|
|
|
|
#else
|
|
|
|
#define DVAPI DV_IMPORT_API
|
|
|
|
#define DVVAR DV_IMPORT_VAR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//=====================================================
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
|
|
|
|
class TFilePath;
|
|
|
|
|
|
|
|
//=====================================================
|
|
|
|
|
|
|
|
//************************************************************************************************
|
|
|
|
// Toonz Image Cache declaration
|
|
|
|
//************************************************************************************************
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! The Image Cache is the global container used throughout Toonz to store
|
|
|
|
//! images that need to be
|
2016-03-19 06:57:51 +13:00
|
|
|
//! retrieved at later times.
|
|
|
|
/*!
|
2016-06-15 18:43:10 +12:00
|
|
|
TImageCache is Toonz's low-level manager singleton that deals with image
|
|
|
|
storage.
|
2016-03-19 06:57:51 +13:00
|
|
|
\n\n
|
2016-06-15 18:43:10 +12:00
|
|
|
The main task of TImageCache is that of acting as an associative container
|
|
|
|
that maps
|
|
|
|
a name (of std::string type) into an image smart pointer. Many-to-one
|
|
|
|
relationships
|
2016-03-19 06:57:51 +13:00
|
|
|
where multiple names map to the same image \a are supported.
|
2016-06-15 18:43:10 +12:00
|
|
|
Thus, users can add() an image and get() it later, until comes the time to
|
|
|
|
remove() it.
|
2016-03-19 06:57:51 +13:00
|
|
|
\n\n
|
2016-06-15 18:43:10 +12:00
|
|
|
An important reason to use TImageCache instead of direct storage of TImageP or
|
|
|
|
TRasterP instances
|
|
|
|
is that it takes care of verifying memory shortages in the process' virtual
|
|
|
|
address space, and
|
|
|
|
in case either compresses or ships to disk unreferenced images with
|
|
|
|
last-access precedence.
|
|
|
|
|
|
|
|
\warning Memory-hungry tasks should always use TImageCache to store images,
|
|
|
|
since it prevents abuses
|
2016-03-19 06:57:51 +13:00
|
|
|
of system memory. This is especially true on 32-bit OSes.
|
|
|
|
*/
|
2016-06-15 18:43:10 +12:00
|
|
|
class DVAPI TImageCache {
|
|
|
|
class Imp;
|
|
|
|
std::unique_ptr<Imp> m_imp;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
static TImageCache *instance();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string getUniqueId();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Enables or disables the image cache on current thread.
|
|
|
|
//! When the cache is disabled, images can't be added to the cache.
|
|
|
|
void setEnabled(bool enable);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Returns true or false whether the cache is active or not.
|
|
|
|
bool isEnabled();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Sets the hard disk swap directory. It is set by default in the
|
|
|
|
//! \it{stuff\cache} folder.
|
|
|
|
void setRootDir(const TFilePath &fp);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Adds the passed image to the cache, under the specified id. The optional
|
|
|
|
//! \b overwrite parameter
|
|
|
|
//! may be specified in case an image with the same id has already been
|
|
|
|
//! cached.
|
|
|
|
void add(const std::string &id, const TImageP &img, bool overwrite = true);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Removes from the image cache the image associated to passed id.
|
|
|
|
void remove(const std::string &id);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Rebinds the association between a previously cached image and its id.
|
|
|
|
void remap(const std::string &dstId, const std::string &srcId);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// provvisorio? Bisogna ripensarci
|
|
|
|
// l'idea e' rimappare tutte gli id della forma srcId:xxxxx
|
|
|
|
void remapIcons(const std::string &dstId, const std::string &srcId);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Clears the image cache. After calling this method, every image in the
|
|
|
|
//! cache
|
|
|
|
//! is released - and optionally the cache swap folder is deleted.
|
|
|
|
void clear(bool deleteFolder = false);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Clears the image cache of all images which belong to current scene. This
|
|
|
|
//! spares
|
|
|
|
//! images (typically file browser icons) which are scene-independent.
|
|
|
|
//! \n \n
|
|
|
|
//! \b{NOTE:} Scene-independent images are simply assumed to have the "$:" id
|
|
|
|
//! prefix.
|
|
|
|
void clearSceneImages();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Returns true or false whether an image under the passed id is found in the
|
|
|
|
//! cache.
|
|
|
|
bool isCached(const std::string &id) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Returns the subsampling level of the image under the specified id.
|
|
|
|
bool getSubsampling(const std::string &id, int &subs) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Retrieves the image associated to input \b id. Returns an empty pointer if
|
|
|
|
//! no image was found.
|
|
|
|
TImageP get(const std::string &id, bool toBeModified) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Returns the RAM memory size (KB) occupied by the image cache.
|
|
|
|
UINT getMemUsage() const;
|
|
|
|
//! Returns the swap files size (KB) currently allocated by the image cache.
|
|
|
|
//! \n \n \b{NOTE:} This function is not implemented yet!
|
|
|
|
UINT getDiskUsage() const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
UINT getUncompressedMemUsage(const std::string &id) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Returns the RAM memory size (KB) of the image associated to passed id.
|
|
|
|
UINT getMemUsage(const std::string &id) const;
|
|
|
|
UINT getDiskUsage(const std::string &id) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void dump(std::ostream &os) const; // per debug
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
UCHAR *compressAndMalloc(TUINT32 requestedSize); // compress in the cache
|
|
|
|
// till it can allocate the
|
|
|
|
// requested memory
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// for debug
|
|
|
|
void outputMap(UINT chunkRequested, std::string filename);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
bool hasBeenModified(const std::string &id, bool reset) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifndef TNZCORE_LIGHT
|
2016-06-15 18:43:10 +12:00
|
|
|
void add(const QString &id, const TImageP &img, bool overwrite = true);
|
|
|
|
void remove(const QString &id);
|
|
|
|
TImageP get(const QString &id, bool toBeModified) const;
|
2016-03-19 06:57:51 +13:00
|
|
|
#endif
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// compress id (in memory)
|
|
|
|
void compress(const std::string &id);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
private:
|
2016-06-15 18:43:10 +12:00
|
|
|
TImageCache();
|
|
|
|
~TImageCache();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// not implemented
|
|
|
|
TImageCache(const TImageCache &);
|
|
|
|
void operator=(const TImageCache &);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//************************************************************************************************
|
|
|
|
// Toonz Image Cache declaration
|
|
|
|
//************************************************************************************************
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! The TCachedImage class implements a shared reference to an image that is
|
|
|
|
//! stored under TImageCache.
|
2016-06-29 18:17:12 +12:00
|
|
|
class DVAPI TCachedImage final : public TSmartObject {
|
2016-06-15 18:43:10 +12:00
|
|
|
DECLARE_CLASS_CODE
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
std::string m_ref;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
TCachedImage();
|
|
|
|
TCachedImage(const TImageP &img);
|
|
|
|
~TCachedImage();
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Stores the supplied image in the cache
|
|
|
|
void setImage(const TImageP &img, bool overwrite = true);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
//! Retrieves the associated image from the cache
|
|
|
|
TImageP image(bool toBeModified = true);
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
2016-04-15 17:11:23 +12:00
|
|
|
#ifdef _WIN32
|
2016-03-19 06:57:51 +13:00
|
|
|
template class DVAPI TSmartPointerT<TCachedImage>;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef TSmartPointerT<TCachedImage> TCachedImageP;
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TIMAGECACHE_H
|