Shinya Kitaoka 810553
#pragma once
Shinya Kitaoka 810553
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;
Shinya Kitaoka 120a6e
// 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
/*!
Shinya Kitaoka 120a6e
The ResourceBuilder class is the preferential base interface that must be used
Shinya Kitaoka 120a6e
to
Toshihiro Shimizu 890ddd
by eventual users to interact with the Toonz cache during render processes.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Caching can be used to specify that some intermediate rendering result is
Shinya Kitaoka 120a6e
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
Shinya Kitaoka 120a6e
These kind of operations are automatically performed by Toonz on the overall
Shinya Kitaoka 120a6e
results of
Shinya Kitaoka 120a6e
fx nodes - users should definitely implement hidden fx nodes rather than a
Shinya Kitaoka 120a6e
specialized
Shinya Kitaoka 120a6e
ResourceBuilder class. However, ResourceBuilder provides a more simple and
Shinya Kitaoka 120a6e
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
Shinya Kitaoka 120a6e
  • The compute() method, which performs the effective calculation of the
  • Shinya Kitaoka 120a6e
    passed tile.
    Shinya Kitaoka 120a6e
  • The simCompute() method, which is a dummy simulation of the above
  • Shinya Kitaoka 120a6e
    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
    Shinya Kitaoka 120a6e
    ResourceBuilder can be used to obtain the resource data associated with passed
    Shinya Kitaoka 120a6e
    TTile
    Toshihiro Shimizu 890ddd
    simply by invoking the build() method. The build() invocation performs all the
    Shinya Kitaoka 120a6e
    cache resource availability checkings, retrieval, and the eventual resource
    Shinya Kitaoka 120a6e
    calculation
    Toshihiro Shimizu 890ddd
    on its own.
    Toshihiro Shimizu 890ddd
    \n \n
    Shinya Kitaoka 120a6e
    The simBuild() method is the dummy counterpart for build(), which must be used
    Shinya Kitaoka 120a6e
    in the
    Shinya Kitaoka 120a6e
    simCompute() simulations whenever some father resource requires that a child
    Shinya Kitaoka 120a6e
    resource
    Toshihiro Shimizu 890ddd
    is built first.
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    class DVAPI ResourceBuilder {
    Shinya Kitaoka 120a6e
      TFxCacheManager *m_cacheManager;
    Shinya Kitaoka 120a6e
      ResourceData m_data;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    protected:
    Shinya Kitaoka 120a6e
      virtual void simCompute(const TRectD &rect) = 0;
    Shinya Kitaoka 120a6e
      virtual void compute(const TRectD &rect)    = 0;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      virtual void upload(TCacheResourceP &resource)   = 0;
    Shinya Kitaoka 120a6e
      virtual bool download(TCacheResourceP &resource) = 0;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Shinya Kitaoka 120a6e
      ResourceBuilder(const std::string &resourceName, const TFxP &fx, double frame,
    Shinya Kitaoka 120a6e
                      const TRenderSettings &rs);
    Shinya Kitaoka 120a6e
      virtual ~ResourceBuilder() {}
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      static void declareResource(const std::string &alias, const TFxP &fx,
    Shinya Kitaoka 120a6e
                                  const TRectD &rect, double frame,
    Shinya Kitaoka 120a6e
                                  const TRenderSettings &rs,
    Shinya Kitaoka 120a6e
                                  bool subtileable = true);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void simBuild(const TRectD &tile);
    Shinya Kitaoka 120a6e
      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
    luz paz 6454c4
    //! The ResourceDeclaration structure contains the information 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 {
    Shinya Kitaoka 120a6e
      //! ResourceDeclaration's Raw data is gathered in the first predictive run for
    Shinya Kitaoka 120a6e
      //! a render process. It is destroyed before the actual process begins due to
    Shinya Kitaoka 120a6e
      //! its considerable size.
    Shinya Kitaoka 120a6e
      struct RawData {
    Shinya Kitaoka 120a6e
        // Requested infos for calculation
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
        //! Fx who generated the data
    Shinya Kitaoka 120a6e
        TFxP m_fx;
    Shinya Kitaoka 120a6e
        //! Frame at which the data was generated
    Shinya Kitaoka 120a6e
        double m_frame;
    Shinya Kitaoka 120a6e
        //! Render settings associated with the data
    Shinya Kitaoka 120a6e
        TRenderSettings m_rs;
    Shinya Kitaoka 120a6e
        //! Tiles declared in the prediction process.
    Shinya Kitaoka 120a6e
        //! May differ from those actually calculated by the renderer.
    Shinya Kitaoka 120a6e
        std::vector<trectd> m_tiles;</trectd>
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
        // Useful infos
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
        //! Bounding box associated with the considered fx calculation
    Shinya Kitaoka 120a6e
        // TRectD m_bbox;
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
        //! Whether the resource may be subdivided for calculation
    Shinya Kitaoka 120a6e
        bool m_subtileable;
    Shinya Kitaoka 120a6e
      };
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      //! The ResourceDeclaration's Raw data is processed into a vector of
    Shinya Kitaoka 120a6e
      //! TileDatas, representing the actual tiles to be computed in the render
    Shinya Kitaoka 120a6e
      //! process.
    Shinya Kitaoka 120a6e
      struct TileData {
    Shinya Kitaoka 120a6e
        TRectD m_rect;
    Shinya Kitaoka 120a6e
        int m_refCount;
    Shinya Kitaoka 120a6e
        bool m_calculated;
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
        TileData(const TRectD &rect)
    Shinya Kitaoka 120a6e
            : m_rect(rect), m_refCount(0), m_calculated(false) {}
    Shinya Kitaoka 120a6e
      };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Shinya Kitaoka 120a6e
      RawData *m_rawData;
    Shinya Kitaoka 120a6e
      std::vector<tiledata> m_tiles;</tiledata>
    Shinya Kitaoka 120a6e
      int m_tilesCount;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      ResourceDeclaration() : m_rawData(0), m_tilesCount(0) {}
    Shinya Kitaoka 120a6e
      ~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
    /*!
    Shinya Kitaoka 120a6e
    The TFxCacheManager is the main resource manager that deals with fx nodes
    Shinya Kitaoka 120a6e
    caching
    Shinya Kitaoka 120a6e
    inside Toonz rendering processes. During a render process, a considerable amount
    Shinya Kitaoka 120a6e
    of time
    Shinya Kitaoka 120a6e
    can be saved if any intermediate rendered result used more than once is saved in
    Shinya Kitaoka 120a6e
    cache
    Shinya Kitaoka 120a6e
    and reused multiple times rather than rebuilding it every time. In some cases,
    Shinya Kitaoka 120a6e
    the user may even
    Shinya Kitaoka 120a6e
    know that some specific intermediate result is useful for rendering purposes,
    Shinya Kitaoka 120a6e
    and could
    Toshihiro Shimizu 890ddd
    explicitly require that it needs to be cached.
    Toshihiro Shimizu 890ddd
    \n \n
    Shinya Kitaoka 120a6e
    In Toonz's rendering workflow, these operations are managed by the
    Shinya Kitaoka 120a6e
    TFxCacheManager class,
    Shinya Kitaoka 120a6e
    which delegates specific caching procedures to a group of
    Shinya Kitaoka 120a6e
    TFxCacheManagerDelegate instances.
    Toshihiro Shimizu 890ddd
    \n \n
    Shinya Kitaoka 120a6e
    Access to a cache resource is performed through the getResource() method, which
    Shinya Kitaoka 120a6e
    requires
    Toshihiro Shimizu 890ddd
    a description of the resource (as specified in the TCacheResource class)
    luz paz 6454c4
    and some information - namely, the fx, render settings and frame - about the
    Shinya Kitaoka 120a6e
    circumstances
    Shinya Kitaoka 120a6e
    of the request. If the resource is managed by some manager delegate, it will be
    Shinya Kitaoka 120a6e
    returned.
    Toshihiro Shimizu 890ddd
    \n \n
    Shinya Kitaoka 120a6e
    Resource uploads and downloads must be notified through the apposite methods to
    Shinya Kitaoka 120a6e
    inform the
    Shinya Kitaoka 120a6e
    manager delegates appropriately. These notifications can be invoked without need
    Shinya Kitaoka 120a6e
    that
    Shinya Kitaoka 120a6e
    the associated method is actually invoked for the resource - this is especially
    Shinya Kitaoka 120a6e
    required
    Shinya Kitaoka 120a6e
    when the TRenderer instance is simulating the actual render process for
    Shinya Kitaoka 120a6e
    predictive purposes.
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
    \sa TCacheResource, TPassiveCacheManager, TPredictiveCacheManager and TRenderer
    Shinya Kitaoka 120a6e
    classes
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka d1f6c4
    class DVAPI TFxCacheManager final : public TRenderResourceManager {
    Shinya Kitaoka 120a6e
      T_RENDER_RESOURCE_MANAGER
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      // std::set<tfxcachemanagerlistener*> m_listeners;</tfxcachemanagerlistener*>
    Shinya Kitaoka 120a6e
      std::set<tfxcachemanagerdelegate *=""> m_delegates;</tfxcachemanagerdelegate>
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      std::set<std::string> m_staticCacheIds;</std::string>
    Toshihiro Shimizu 890ddd
    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
      TFxCacheManager();
    Shinya Kitaoka 120a6e
      ~TFxCacheManager();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      static TFxCacheManager *instance();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void add(const std::string &cacheId, TImageP img);
    Shinya Kitaoka 120a6e
      void remove(const std::string &cacheId);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 473e70
      void onRenderStatusStart(int renderStatus) override;
    Shinya Kitaoka 473e70
      void onRenderStatusEnd(int renderStatus) override;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      // void install(TFxCacheManagerListener* listener);
    Shinya Kitaoka 120a6e
      // void uninstall(TFxCacheManagerListener* listener);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      friend class ResourceBuilder;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void declareResource(const std::string &alias, const TFxP &fx,
    Shinya Kitaoka 120a6e
                           const TRectD &rect, double frame,
    Shinya Kitaoka 120a6e
                           const TRenderSettings &rs, bool subtileable);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      ResourceData getResource(const std::string &resourceName, const TFxP &fx,
    Shinya Kitaoka 120a6e
                               double frame, const TRenderSettings &rs);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      /*void notifyResourceUpload(const TCacheResourceP& resource, const TRect&
    Shinya Kitaoka 120a6e
    rect);
    Shinya Kitaoka 120a6e
    void notifyResourceDownload(const TCacheResourceP& resource, const TRect& rect);
    Shinya Kitaoka 120a6e
    void notifyPredictedRelease(const TCacheResourceP& resource);*/
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      friend class TFxCacheManagerDelegate;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      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
    Shinya Kitaoka 120a6e
    class TFxCacheManagerDelegate : public TRenderResourceManager {
    Toshihiro Shimizu 890ddd
    public:
    Shinya Kitaoka 120a6e
      TFxCacheManagerDelegate() {}
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      virtual void getResource(TCacheResourceP &resource, const std::string &alias,
    Shinya Kitaoka 120a6e
                               const TFxP &fx, double frame,
    Shinya Kitaoka 120a6e
                               const TRenderSettings &rs,
    Shinya Kitaoka 120a6e
                               ResourceDeclaration *resData) = 0;
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 473e70
      void onRenderInstanceStart(unsigned long renderId) override {
    Shinya Kitaoka 120a6e
        assert(TFxCacheManager::instance());
    Shinya Kitaoka 120a6e
        TFxCacheManager::instance()->install(this);
    Shinya Kitaoka 120a6e
      }
    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
    Shinya Kitaoka 120a6e
      virtual void onResourceUpload(const TCacheResourceP& resource, const TRect&
    Shinya Kitaoka 120a6e
    rect) {}
    Shinya Kitaoka 120a6e
      virtual void onResourceDownload(const TCacheResourceP& resource, const TRect&
    Shinya Kitaoka 120a6e
    rect) {}
    Toshihiro Shimizu 890ddd
      virtual void onPredictedRelease(const TCacheResourceP& resource) {}
    Toshihiro Shimizu 890ddd
    };*/
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    #endif  // TFXCACHEMANAGER_H