Toshihiro Shimizu 890ddd
#ifndef TFXCACHEMANAGER_H
Toshihiro Shimizu 890ddd
#define TFXCACHEMANAGER_H
Toshihiro Shimizu 890ddd
Shinya Kitaoka 262a92
#include <memory></memory>
Shinya Kitaoka 262a92
Toshihiro Shimizu 890ddd
#include "trenderresourcemanager.h"
Toshihiro Shimizu 890ddd
#include "tcacheresource.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  Forward declarations
Toshihiro Shimizu 890ddd
struct ResourceDeclaration;
Toshihiro Shimizu 890ddd
typedef std::pair<resourcedeclaration *,="" tcacheresourcep=""> ResourceData;</resourcedeclaration>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TFxCacheManager;
Toshihiro Shimizu 890ddd
class TFxCacheManagerDelegate;
Toshihiro Shimizu 890ddd
//class TFxCacheManagerListener;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=======================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=====================================
Toshihiro Shimizu 890ddd
//    ResourceBuilder class
Toshihiro Shimizu 890ddd
//-------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
The ResourceBuilder class is the preferential base interface that must be used to
Toshihiro Shimizu 890ddd
by eventual users to interact with the Toonz cache during render processes.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Caching can be used to specify that some intermediate rendering result is useful,
Toshihiro Shimizu 890ddd
and efforts should be taken to ensure that it is stored in memory rather than
Toshihiro Shimizu 890ddd
repeatedly rebuilt, when it is sufficiently convenient.
Toshihiro Shimizu 890ddd
\n
Toshihiro Shimizu 890ddd
These kind of operations are automatically performed by Toonz on the overall results of
Toshihiro Shimizu 890ddd
fx nodes - users should definitely implement hidden fx nodes rather than a specialized
Toshihiro Shimizu 890ddd
ResourceBuilder class. However, ResourceBuilder provides a more simple and generic API
Toshihiro Shimizu 890ddd
than TRasterFx's, and does not force code splitting into several fxs.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
This class already works out most of the implementation details necessary
Toshihiro Shimizu 890ddd
to deal with cache management and predictability issues.
Toshihiro Shimizu 890ddd
\n \n
Toshihiro Shimizu 890ddd
It just requires the implementation of two essential functions:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  • The compute() method, which performs the effective calculation of the passed tile.
  • Toshihiro Shimizu 890ddd
  • The simCompute() method, which is a dummy simulation of the above compute(),
  • Toshihiro Shimizu 890ddd
    used by the builder for predictive purposes.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    Please refer to their descriptions for implementation details.
    Toshihiro Shimizu 890ddd
    \n \n
    Toshihiro Shimizu 890ddd
    Once the necessary compute() and simCompute() functions have been supplied, the
    Toshihiro Shimizu 890ddd
    ResourceBuilder can be used to obtain the resource data associated with passed TTile
    Toshihiro Shimizu 890ddd
    simply by invoking the build() method. The build() invocation performs all the
    Toshihiro Shimizu 890ddd
    cache resource availability checkings, retrieval, and the eventual resource calculation
    Toshihiro Shimizu 890ddd
    on its own.
    Toshihiro Shimizu 890ddd
    \n \n
    Toshihiro Shimizu 890ddd
    The simBuild() method is the dummy counterpart for build(), which must be used in the
    Toshihiro Shimizu 890ddd
    simCompute() simulations whenever some father resource requires that a child resource
    Toshihiro Shimizu 890ddd
    is built first.
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    class DVAPI ResourceBuilder
    Toshihiro Shimizu 890ddd
    {
    Toshihiro Shimizu 890ddd
    	TFxCacheManager *m_cacheManager;
    Toshihiro Shimizu 890ddd
    	ResourceData m_data;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    protected:
    Toshihiro Shimizu 890ddd
    	virtual void simCompute(const TRectD &rect) = 0;
    Toshihiro Shimizu 890ddd
    	virtual void compute(const TRectD &rect) = 0;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	virtual void upload(TCacheResourceP &resource) = 0;
    Toshihiro Shimizu 890ddd
    	virtual bool download(TCacheResourceP &resource) = 0;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Toshihiro Shimizu 890ddd
    	ResourceBuilder(const string &resourceName,
    Toshihiro Shimizu 890ddd
    					const TFxP &fx, double frame, const TRenderSettings &rs);
    Toshihiro Shimizu 890ddd
    	virtual ~ResourceBuilder() {}
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	static void declareResource(
    Toshihiro Shimizu 890ddd
    		const string &alias, const TFxP &fx,
    Toshihiro Shimizu 890ddd
    		const TRectD &rect, double frame, const TRenderSettings &rs,
    Toshihiro Shimizu 890ddd
    		bool subtileable = true);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	void simBuild(const TRectD &tile);
    Toshihiro Shimizu 890ddd
    	void build(const TRectD &tile);
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //************************************************************************************************
    Toshihiro Shimizu 890ddd
    //    Cache Management internals
    Toshihiro Shimizu 890ddd
    //************************************************************************************************
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //! The ResourceDeclaration structure contains the informations retrieved in the
    Toshihiro Shimizu 890ddd
    //! predictive processes performed before a render process. It can be retrieved
    Toshihiro Shimizu 890ddd
    //! via the TFxCacheManager::getResource() method or passed directly by it to
    Toshihiro Shimizu 890ddd
    //! TFxCacheManagerDelegate instances.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    struct ResourceDeclaration {
    Toshihiro Shimizu 890ddd
    	//! ResourceDeclaration's Raw data is gathered in the first predictive run for
    Toshihiro Shimizu 890ddd
    	//! a render process. It is destroyed before the actual process begins due to
    Toshihiro Shimizu 890ddd
    	//! its considerable size.
    Toshihiro Shimizu 890ddd
    	struct RawData {
    Toshihiro Shimizu 890ddd
    		//Requested infos for calculation
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    		//!Fx who generated the data
    Toshihiro Shimizu 890ddd
    		TFxP m_fx;
    Toshihiro Shimizu 890ddd
    		//!Frame at which the data was generated
    Toshihiro Shimizu 890ddd
    		double m_frame;
    Toshihiro Shimizu 890ddd
    		//!Render settings associated with the data
    Toshihiro Shimizu 890ddd
    		TRenderSettings m_rs;
    Toshihiro Shimizu 890ddd
    		//!Tiles declared in the prediction process.
    Toshihiro Shimizu 890ddd
    		//!May differ from those actually calculated by the renderer.
    Toshihiro Shimizu 890ddd
    		std::vector<trectd> m_tiles;</trectd>
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    		//Useful infos
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    		//!Bounding box associated with the considered fx calculation
    Toshihiro Shimizu 890ddd
    		//TRectD m_bbox;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    		//!Whether the resource may be subdivided for calculation
    Toshihiro Shimizu 890ddd
    		bool m_subtileable;
    Toshihiro Shimizu 890ddd
    	};
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	//! The ResourceDeclaration's Raw data is processed into a vector of
    Toshihiro Shimizu 890ddd
    	//! TileDatas, representing the actual tiles to be computed in the render process.
    Toshihiro Shimizu 890ddd
    	struct TileData {
    Toshihiro Shimizu 890ddd
    		TRectD m_rect;
    Toshihiro Shimizu 890ddd
    		int m_refCount;
    Toshihiro Shimizu 890ddd
    		bool m_calculated;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    		TileData(const TRectD &rect) : m_rect(rect), m_refCount(0), m_calculated(false) {}
    Toshihiro Shimizu 890ddd
    	};
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Toshihiro Shimizu 890ddd
    	RawData *m_rawData;
    Toshihiro Shimizu 890ddd
    	std::vector<tiledata> m_tiles;</tiledata>
    Toshihiro Shimizu 890ddd
    	int m_tilesCount;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	ResourceDeclaration() : m_rawData(0), m_tilesCount(0) {}
    Toshihiro Shimizu 890ddd
    	~ResourceDeclaration() {}
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=======================================================================
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    typedef std::pair<resourcedeclaration *,="" tcacheresourcep=""> ResourceData;</resourcedeclaration>
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=======================================================================
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //======================
    Toshihiro Shimizu 890ddd
    //    Cache Manager
    Toshihiro Shimizu 890ddd
    //----------------------
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    /*!
    Toshihiro Shimizu 890ddd
    The TFxCacheManager is the main resource manager that deals with fx nodes caching
    Toshihiro Shimizu 890ddd
    inside Toonz rendering processes. During a render process, a considerable amount of time
    Toshihiro Shimizu 890ddd
    can be saved if any intermediate rendered result used more than once is saved in cache
    Toshihiro Shimizu 890ddd
    and reused multiple times rather than rebuilding it every time. In some cases, the user may even
    Toshihiro Shimizu 890ddd
    know that some specific intermediate result is useful for rendering purposes, and could
    Toshihiro Shimizu 890ddd
    explicitly require that it needs to be cached.
    Toshihiro Shimizu 890ddd
    \n \n
    Toshihiro Shimizu 890ddd
    In Toonz's rendering workflow, these operations are managed by the TFxCacheManager class,
    Toshihiro Shimizu 890ddd
    which delegates specific caching procedures to a group of TFxCacheManagerDelegate instances.
    Toshihiro Shimizu 890ddd
    \n \n
    Toshihiro Shimizu 890ddd
    Access to a cache resource is performed through the getResource() method, which requires
    Toshihiro Shimizu 890ddd
    a description of the resource (as specified in the TCacheResource class)
    Toshihiro Shimizu 890ddd
    and some informations - namely, the fx, render settings and frame - about the circumstances
    Toshihiro Shimizu 890ddd
    of the request. If the resource is managed by some manager delegate, it will be returned.
    Toshihiro Shimizu 890ddd
    \n \n
    Toshihiro Shimizu 890ddd
    Resource uploads and downloads must be notified through the apposite methods to inform the
    Toshihiro Shimizu 890ddd
    manager delegates appropriately. These notifications can be invoked without need that
    Toshihiro Shimizu 890ddd
    the associated method is actually invoked for the resource - this is especially required
    Toshihiro Shimizu 890ddd
    when the TRenderer instance is simulating the actual render process for predictive purposes.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    \sa TCacheResource, TPassiveCacheManager, TPredictiveCacheManager and TRenderer classes
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    class DVAPI TFxCacheManager : public TRenderResourceManager
    Toshihiro Shimizu 890ddd
    {
    Toshihiro Shimizu 890ddd
    	T_RENDER_RESOURCE_MANAGER
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Toshihiro Shimizu 890ddd
    	//std::set<tfxcachemanagerlistener*> m_listeners;</tfxcachemanagerlistener*>
    Toshihiro Shimizu 890ddd
    	std::set<tfxcachemanagerdelegate *=""> m_delegates;</tfxcachemanagerdelegate>
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	std::set<std::string> m_staticCacheIds;</std::string>
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	class Imp;
    Shinya Kitaoka 262a92
    	std::unique_ptr<imp> m_imp;</imp>
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Toshihiro Shimizu 890ddd
    	TFxCacheManager();
    Toshihiro Shimizu 890ddd
    	~TFxCacheManager();
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	static TFxCacheManager *instance();
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	void add(const std::string &cacheId, TImageP img);
    Toshihiro Shimizu 890ddd
    	void remove(const std::string &cacheId);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	void onRenderStatusStart(int renderStatus);
    Toshihiro Shimizu 890ddd
    	void onRenderStatusEnd(int renderStatus);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	//void install(TFxCacheManagerListener* listener);
    Toshihiro Shimizu 890ddd
    	//void uninstall(TFxCacheManagerListener* listener);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Toshihiro Shimizu 890ddd
    	friend class ResourceBuilder;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	void declareResource(
    Toshihiro Shimizu 890ddd
    		const string &alias, const TFxP &fx,
    Toshihiro Shimizu 890ddd
    		const TRectD &rect, double frame, const TRenderSettings &rs,
    Toshihiro Shimizu 890ddd
    		bool subtileable);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	ResourceData getResource(const string &resourceName,
    Toshihiro Shimizu 890ddd
    							 const TFxP &fx, double frame, const TRenderSettings &rs);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	/*void notifyResourceUpload(const TCacheResourceP& resource, const TRect& rect);
    Toshihiro Shimizu 890ddd
      void notifyResourceDownload(const TCacheResourceP& resource, const TRect& rect);
    Toshihiro Shimizu 890ddd
      void notifyPredictedRelease(const TCacheResourceP& resource);*/
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Toshihiro Shimizu 890ddd
    	friend class TFxCacheManagerDelegate;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	void install(TFxCacheManagerDelegate *managerDelegate);
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=======================================================================
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //===============================
    Toshihiro Shimizu 890ddd
    //    Cache Manager Delegate
    Toshihiro Shimizu 890ddd
    //-------------------------------
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    /*!
    Toshihiro Shimizu 890ddd
    TFxCacheManagerDelegate is the base class to implement cache managers used
    Toshihiro Shimizu 890ddd
    in Toonz render processes.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    A TFxCacheManagerDelegate instance automatically receives access notifications
    Toshihiro Shimizu 890ddd
    through the TFxCacheManager associated with a render process.
    Toshihiro Shimizu 890ddd
    These may either concern a request of a certain cache resource, or the storage
    Toshihiro Shimizu 890ddd
    proposal of a calculated node result.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    \warning Delegates are expected to be dependent on TFxCacheManager - use the
    Toshihiro Shimizu 890ddd
    MANAGER_FILESCOPE_DECLARATION_DEP macro to achieve that.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    \sa TCacheResource and TFxCacheManager classes
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    class TFxCacheManagerDelegate : public TRenderResourceManager
    Toshihiro Shimizu 890ddd
    {
    Toshihiro Shimizu 890ddd
    public:
    Toshihiro Shimizu 890ddd
    	TFxCacheManagerDelegate() {}
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	virtual void getResource(
    Toshihiro Shimizu 890ddd
    		TCacheResourceP &resource, const string &alias,
    Toshihiro Shimizu 890ddd
    		const TFxP &fx, double frame, const TRenderSettings &rs,
    Toshihiro Shimizu 890ddd
    		ResourceDeclaration *resData) = 0;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    	virtual void onRenderInstanceStart(unsigned long renderId)
    Toshihiro Shimizu 890ddd
    	{
    Toshihiro Shimizu 890ddd
    		assert(TFxCacheManager::instance());
    Toshihiro Shimizu 890ddd
    		TFxCacheManager::instance()->install(this);
    Toshihiro Shimizu 890ddd
    	}
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=======================================================================
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //===============================
    Toshihiro Shimizu 890ddd
    //    Cache Manager Listener
    Toshihiro Shimizu 890ddd
    //-------------------------------
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    /*class TFxCacheManagerListener
    Toshihiro Shimizu 890ddd
    {
    Toshihiro Shimizu 890ddd
    public:
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
      TFxCacheManagerListener() {}
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
      virtual void onResourceUpload(const TCacheResourceP& resource, const TRect& rect) {}
    Toshihiro Shimizu 890ddd
      virtual void onResourceDownload(const TCacheResourceP& resource, const TRect& rect) {}
    Toshihiro Shimizu 890ddd
      virtual void onPredictedRelease(const TCacheResourceP& resource) {}
    Toshihiro Shimizu 890ddd
    };*/
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    #endif //TFXCACHEMANAGER_H