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