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