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