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