Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCACHERESOURCE_INCLUDED
Toshihiro Shimizu 890ddd
#define TCACHERESOURCE_INCLUDED
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "tsmartpointer.h"
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qmutex></qmutex>
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#include <qregion></qregion>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TFX_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
class TTile;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================
Toshihiro Shimizu 890ddd
//    Cache Resource class
Toshihiro Shimizu 890ddd
//----------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
The TCacheResource class models generic tile objects suitable for caching
Toshihiro Shimizu 890ddd
in render processes.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
Whereas common TTile instances can be used to represent images bounded on a
Shinya Kitaoka 120a6e
specific
Toshihiro Shimizu 890ddd
rect of the plane, the caching procedures in a render process require certain
Shinya Kitaoka 120a6e
generalizations of the tile model that are fulfilled by the TCacheResource
Shinya Kitaoka 120a6e
class:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  • A cache resource has a textual description of its content that we'll call
  • Shinya Kitaoka 120a6e
    'name'.
    Shinya Kitaoka 120a6e
    It is the associative key used to retrieve a resource among all the others
    Shinya Kitaoka 120a6e
    stored
    Toshihiro Shimizu 890ddd
    in the cache. <\li>
    Shinya Kitaoka 120a6e
  • The spatial domain of a cache resource is the entire plane, meaning that
  • Shinya Kitaoka 120a6e
    access
    Toshihiro Shimizu 890ddd
    methods are not bound to work on a specific rect.
    Shinya Kitaoka 120a6e
    This is necessary since the image data accessed on non-consecutive renders could
    Shinya Kitaoka 120a6e
    cover
    Toshihiro Shimizu 890ddd
    sparse regions of the plane. <\li>
    Shinya Kitaoka 120a6e
  • For simplicity, the pixel geometry of a cache is coherent to the plane
  • Shinya Kitaoka 120a6e
    origin - that is,
    Toshihiro Shimizu 890ddd
    pixel corners always have integer coordinates. <\li>
    Shinya Kitaoka 120a6e
  • Filled resources have a raster type <\b>, specifying the type of
  • Shinya Kitaoka 120a6e
    rasters that are
    Shinya Kitaoka 120a6e
    currently storing. It is forbidden to upload rasters to resources when their
    Shinya Kitaoka 120a6e
    raster type differs.
    Toshihiro Shimizu 890ddd
    Empty resources are compatible to all possible raster types. <\li>
    Shinya Kitaoka 120a6e
  • Cache resources provide reference counters to either the entire resource
  • Shinya Kitaoka 120a6e
    and each
    Shinya Kitaoka 120a6e
    piece of its internal memory model, in order to perform automatic release of
    Shinya Kitaoka 120a6e
    unnecessary
    Toshihiro Shimizu 890ddd
    data. <\li>
    Shinya Kitaoka 120a6e
  • The resource provides an accessible resume of its currently filled areas,
  • Shinya Kitaoka 120a6e
    in the form of
    Toshihiro Shimizu 890ddd
    a QRegion. <\li>
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    In order to use or retrieve a cache resource, users must instantiate a smart
    Shinya Kitaoka 120a6e
    pointer reference
    Shinya Kitaoka 120a6e
    to the resource, providing its name. Please refer to the TCacheResourceP
    Shinya Kitaoka 120a6e
    documentation for this
    Toshihiro Shimizu 890ddd
    topic.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    \sa TRaster, TTile classes.
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    class DVAPI TCacheResource {
    Shinya Kitaoka 120a6e
      TAtomicVar m_refCount;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      struct PointLess {
    Shinya Kitaoka 120a6e
        int x, y;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
        PointLess(int x_, int y_) : x(x_), y(y_) {}
    Shinya Kitaoka 120a6e
        bool operator<(const PointLess &p) const {
    Shinya Kitaoka 120a6e
          return x < p.x ? true : x > p.x ? false : y < p.y;
    Shinya Kitaoka 120a6e
        }
    Shinya Kitaoka 120a6e
      };
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      struct CellData {
    Shinya Kitaoka 120a6e
        int m_refsCount;
    Shinya Kitaoka 120a6e
        bool m_referenced;
    Shinya Kitaoka 120a6e
        bool m_modified;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
        CellData() : m_refsCount(0), m_modified(false), m_referenced(false) {}
    Shinya Kitaoka 120a6e
      };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      std::map<std::string, *="" tcacheresource="">::iterator m_pos;</std::string,>
    Shinya Kitaoka 120a6e
      TFilePath m_path;
    Shinya Kitaoka 120a6e
      unsigned long m_id;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      QRegion m_region;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      int m_tileType;
    Shinya Kitaoka 120a6e
      int m_cellsCount;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      TPaletteP m_palette;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      std::map<pointless, celldata=""> m_cellDatas;</pointless,>
    Shinya Kitaoka 120a6e
      int m_locksCount;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      QMutex m_mutex;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool m_backEnabled;
    Shinya Kitaoka 120a6e
      bool m_invalidated;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      friend class TCacheResourcePool;
    Shinya Kitaoka 120a6e
      friend class THDCacheResourcePool;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      TCacheResource();
    Shinya Kitaoka 120a6e
      ~TCacheResource();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      friend class TCacheResourceP;
    Shinya Kitaoka 120a6e
      inline void addRef() { ++m_refCount; }
    Shinya Kitaoka 120a6e
      void release();
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Shinya Kitaoka 120a6e
      const std::string &getName() const { return m_pos->first; }
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      QMutex *getMutex() { return &m_mutex; }
    Toshihiro Shimizu 890ddd
    shun-iwasawa 481b59
      enum Type { NONE, RGBM32, RGBM64, RGBMFloat, CM32 };
    Shinya Kitaoka 120a6e
      int getRasterType() const { return m_tileType; }
    Shinya Kitaoka 120a6e
      TRasterP buildCompatibleRaster(const TDimension &size);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      QRegion getAvailableRegion() const { return m_region; }
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool upload(const TPoint &pos, TRasterP ras);
    Shinya Kitaoka 120a6e
      bool upload(const TTile &tile);
    Shinya Kitaoka 120a6e
      QRegion download(const TPoint &pos, TRasterP ras);
    Shinya Kitaoka 120a6e
      QRegion download(TTile &tile);
    Shinya Kitaoka 120a6e
      bool downloadAll(const TPoint &pos, TRasterP ras);
    Shinya Kitaoka 120a6e
      bool downloadAll(TTile &tile);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool uploadPalette(TPaletteP palette);
    Shinya Kitaoka 120a6e
      void downloadPalette(TPaletteP &palette);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void clear(QRegion region);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool canUpload(const TTile &tile) const;
    Shinya Kitaoka 120a6e
      bool canDownloadSome(const TTile &tile) const;
    Shinya Kitaoka 120a6e
      bool canDownloadAll(const TTile &tile) const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool canUpload(int rasType) const {
    Shinya Kitaoka 120a6e
        return m_tileType == NONE || m_tileType == rasType;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
      bool canDownloadSome(const TRect &rect) const;
    Shinya Kitaoka 120a6e
      bool canDownloadAll(const TRect &rect) const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void addRef2(const TRect &rect);
    Shinya Kitaoka 120a6e
      void release2(const TRect &rect);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void addLock();
    Shinya Kitaoka 120a6e
      void releaseLock();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      int size() const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void enableBackup();
    Shinya Kitaoka 120a6e
      void invalidate();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void save(const TFilePath &fp);
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      bool checkRasterType(const TRasterP &ras, int &rasType) const;
    Shinya Kitaoka 120a6e
      bool checkTile(const TTile &tile) const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      TRasterP createCellRaster(int rasterType, const std::string &cacheId);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      inline TPoint getCellPos(const TPoint &pos) const;
    Shinya Kitaoka 120a6e
      inline TPoint getCellPos(const TPointD &pos) const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      inline PointLess getCellIndex(const TPoint &pos) const;
    Shinya Kitaoka 120a6e
      inline TPoint getCellPos(const PointLess &cellIndex) const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      inline std::string getCellName(int idxX, int idxY) const;
    Shinya Kitaoka 120a6e
      inline std::string getCellCacheId(const TPoint &cellPos) const;
    Shinya Kitaoka 120a6e
      inline std::string getCellCacheId(int idxX, int idxY) const;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    private:
    Shinya Kitaoka 120a6e
      void setPath(const TFilePath &path);
    Shinya Kitaoka 120a6e
      const TFilePath &getPath() const;
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      void clear();
    Shinya Kitaoka 120a6e
      void save();
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool save(const PointLess &cellIndex, TRasterP cellRas = 0) const;
    Shinya Kitaoka 120a6e
      TRasterP load(const PointLess &cellIndex);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      std::pair<trasterp, *="" celldata=""> touch(const PointLess &cellIndex);</trasterp,>
    Shinya Kitaoka 120a6e
      void releaseCell(const QRect &cellQRect, const PointLess &cellIndex,
    Shinya Kitaoka 120a6e
                       bool save);
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
      bool addMark(QString flag);
    Shinya Kitaoka 120a6e
      bool removeMark(QString flag);
    Shinya Kitaoka 120a6e
      bool hasMark(QString flag);
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=========================================================================
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    //=====================================
    Toshihiro Shimizu 890ddd
    //    Cache Resource Smart pointer
    Toshihiro Shimizu 890ddd
    //-------------------------------------
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    /*!
    Shinya Kitaoka 120a6e
    The TCacheResourceP class implements 'smart' referenced pointers to
    Shinya Kitaoka 120a6e
    TCacheResource
    Toshihiro Shimizu 890ddd
    instances.
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    Smart pointers are typically used to hold references to data that is
    Shinya Kitaoka 120a6e
    automatically freed
    Toshihiro Shimizu 890ddd
    once it is no more referenced by any smart pointer instance.
    Shinya Kitaoka 120a6e
    The TCacheResourceP is a specialized smart pointer-like class that references
    Shinya Kitaoka 120a6e
    TCacheResource
    Toshihiro Shimizu 890ddd
    instances.
    Toshihiro Shimizu 890ddd
    \n \n
    Shinya Kitaoka 120a6e
    In order to retrieve a Cache resource, it is sufficient to know the resource
    Shinya Kitaoka 120a6e
    name and invoke the
    Shinya Kitaoka 120a6e
    \c TCacheResourceP(const std::string&, bool) constructor. The bool optionally
    Shinya Kitaoka 120a6e
    specified as second
    Shinya Kitaoka 120a6e
    argument specifies whether the resource has to be created if none is found with
    Shinya Kitaoka 120a6e
    passed name.
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    \sa TCacheResource, TSmartPointer classes.
    Toshihiro Shimizu 890ddd
    */
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    class DVAPI TCacheResourceP {
    Shinya Kitaoka 120a6e
      TCacheResource *m_pointer;
    Toshihiro Shimizu 890ddd
    Toshihiro Shimizu 890ddd
    public:
    Shinya Kitaoka 120a6e
      TCacheResourceP() : m_pointer(0) {}
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResourceP(const TCacheResourceP &src) : m_pointer(src.m_pointer) {
    Shinya Kitaoka 120a6e
        if (m_pointer) m_pointer->addRef();
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResourceP(const std::string &resourceName, bool createIfNone = false);
    Shinya Kitaoka 120a6e
      ~TCacheResourceP();
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResourceP &operator=(const TCacheResourceP &src) {
    Shinya Kitaoka 120a6e
        TCacheResource *old = m_pointer;
    Shinya Kitaoka 120a6e
        m_pointer           = src.m_pointer;
    Shinya Kitaoka 120a6e
        if (m_pointer) m_pointer->addRef();
    Shinya Kitaoka 120a6e
        if (old) old->release();
    Shinya Kitaoka 120a6e
        return *this;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResource *operator->() const {
    Shinya Kitaoka 120a6e
        assert(m_pointer);
    Shinya Kitaoka 120a6e
        return m_pointer;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResource &operator*() const {
    Shinya Kitaoka 120a6e
        assert(m_pointer);
    Shinya Kitaoka 120a6e
        return *m_pointer;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      TCacheResource *getPointer() const { return m_pointer; }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      bool operator!() const { return m_pointer == 0; }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      operator bool() const { return m_pointer != 0; }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      bool operator<(const TCacheResourceP &res) const {
    Shinya Kitaoka 120a6e
        return m_pointer < res.m_pointer;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      bool operator==(const TCacheResourceP &p) const {
    Shinya Kitaoka 120a6e
        return m_pointer == p.m_pointer;
    Shinya Kitaoka 120a6e
      }
    Shinya Kitaoka 120a6e
    Shinya Kitaoka 120a6e
      bool operator!=(const TCacheResourceP &p) const { return !(operator==(p)); }
    Toshihiro Shimizu 890ddd
    };
    Toshihiro Shimizu 890ddd
    Shinya Kitaoka 120a6e
    #endif  // TCACHERESOURCE_INCLUDED