|
degate 0.1.1
|
The TileCache class handles caching of image tiles. More...
#include <TileCache.h>

Public Member Functions | |
| TileCache (std::string const &_directory, unsigned int _tile_width_exp, bool _persistent, unsigned int _min_cache_tiles=4) | |
| Create a TileCache object. | |
| ~TileCache () | |
| Destroy a TileCache object. | |
| void | print () const |
| std::tr1::shared_ptr < MemoryMap< typename PixelPolicy::pixel_type > > | get_tile (unsigned int x, unsigned int y) |
| Get a tile. | |
Protected Member Functions | |
| void | cleanup_cache () |
| Remove the oldest entry from the cache. | |
Private Types | |
| typedef std::tr1::shared_ptr < MemoryMap< typename PixelPolicy::pixel_type > > | MemoryMap_shptr |
| typedef std::map< std::string, std::pair< MemoryMap_shptr, struct timespec > > | cache_type |
Private Member Functions | |
| size_t | get_image_size () const |
| Get image size in bytes. | |
| std::tr1::shared_ptr < MemoryMap< typename PixelPolicy::pixel_type > > | load (std::string const &filename) const |
| Load a tile from an image file. | |
Private Attributes | |
| const std::string | directory |
| const unsigned int | tile_width_exp |
| const bool | persistent |
| cache_type | cache |
| MemoryMap_shptr | current_tile |
| unsigned | curr_tile_num_x |
| unsigned | curr_tile_num_y |
Friends | |
| class | GlobalTileCache |
The TileCache class handles caching of image tiles.
The implementation keeps track how old the cached tile is. If new tiles become loaded, old tiles are removed from the cache. You can control the numer of cached tiles via the constructor parameter _min_cache_tiles. The memory requirement is around _min_cache_tiles*sizeof(PixelPolicy::pixel_type)*(2^_tile_width_exp)^2 , where sizeof(PixelPolicy::pixel_type) is the size of a pixel.
Definition at line 239 of file TileCache.h.
typedef std::map< std::string, std::pair<MemoryMap_shptr, struct timespec> > degate::TileCache< PixelPolicy >::cache_type [private] |
Definition at line 247 of file TileCache.h.
typedef std::tr1::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::MemoryMap_shptr [private] |
Definition at line 245 of file TileCache.h.
| degate::TileCache< PixelPolicy >::TileCache | ( | std::string const & | _directory, |
| unsigned int | _tile_width_exp, | ||
| bool | _persistent, | ||
| unsigned int | _min_cache_tiles = 4 |
||
| ) | [inline] |
Create a TileCache object.
| _directory | The directory where all the tiles are for a TileImage. |
| _tile_width_exp | |
| _persistent |
Definition at line 270 of file TileCache.h.
:
directory(_directory),
tile_width_exp(_tile_width_exp),
persistent(_persistent) {}
| degate::TileCache< PixelPolicy >::~TileCache | ( | ) | [inline] |
Destroy a TileCache object.
Definition at line 282 of file TileCache.h.
References degate::TileCache< PixelPolicy >::cache, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), and degate::GlobalTileCache::release_cache_memory().
{
if(cache.size() > 0) {
GlobalTileCache & gtc = GlobalTileCache::get_instance();
gtc.release_cache_memory(this, cache.size() * get_image_size());
}
}

| void degate::TileCache< PixelPolicy >::cleanup_cache | ( | ) | [inline, protected, virtual] |
Remove the oldest entry from the cache.
Implements degate::TileCacheBase.
Definition at line 356 of file TileCache.h.
References degate::TileCache< PixelPolicy >::cache, debug(), GET_CLOCK, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), and TM.
{
if(cache.size() == 0) return;
struct timespec oldest_clock_val;
GET_CLOCK(oldest_clock_val);
typename cache_type::iterator oldest = cache.begin();
for(typename cache_type::iterator iter = cache.begin();
iter != cache.end(); ++iter) {
struct timespec clock_val = (*iter).second.second;
if(clock_val < oldest_clock_val) {
oldest_clock_val.tv_sec = clock_val.tv_sec;
oldest_clock_val.tv_nsec = clock_val.tv_nsec;
oldest = iter;
}
}
assert(oldest != cache.end());
(*oldest).second.first.reset(); // explicit reset of smart pointer
cache.erase(oldest);
#ifdef TILECACHE_DEBUG
debug(TM, "local cache: %d entries after remove\n", cache.size());
#endif
GlobalTileCache & gtc = GlobalTileCache::get_instance();
gtc.release_cache_memory(this, get_image_size());
}

| size_t degate::TileCache< PixelPolicy >::get_image_size | ( | ) | const [inline, private] |
Get image size in bytes.
Definition at line 393 of file TileCache.h.
References degate::TileCache< PixelPolicy >::tile_width_exp.
Referenced by degate::TileCache< PixelPolicy >::cleanup_cache(), degate::TileCache< PixelPolicy >::get_tile(), and degate::TileCache< PixelPolicy >::~TileCache().
{
return sizeof(typename PixelPolicy::pixel_type) * (1<< tile_width_exp) * (1<< tile_width_exp);
}

| std::tr1::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::get_tile | ( | unsigned int | x, |
| unsigned int | y | ||
| ) | [inline] |
Get a tile.
If the tile is not in the cache, the tile is loaded.
| x | Absolut pixel coordinate. |
| y | Absolut pixel coordinate. |
Definition at line 311 of file TileCache.h.
References degate::TileCache< PixelPolicy >::cache, degate::TileCache< PixelPolicy >::curr_tile_num_x, degate::TileCache< PixelPolicy >::curr_tile_num_y, degate::TileCache< PixelPolicy >::current_tile, GET_CLOCK, degate::TileCache< PixelPolicy >::get_image_size(), degate::SingletonBase< GlobalTileCache >::get_instance(), degate::TileCache< PixelPolicy >::load(), degate::GlobalTileCache::print_table(), degate::GlobalTileCache::request_cache_memory(), and degate::TileCache< PixelPolicy >::tile_width_exp.
{
unsigned int tile_num_x = x >> tile_width_exp;
unsigned int tile_num_y = y >> tile_width_exp;
if(!(current_tile != NULL &&
tile_num_x == curr_tile_num_x &&
tile_num_y == curr_tile_num_y)) {
// create a file name from tile number
char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "%d_%d.dat", tile_num_x, tile_num_y);
//debug(TM, "filename is: [%s]", filename);
// if filename/ object is not in cache, load the tile
typename cache_type::const_iterator iter = cache.find(filename);
if(iter == cache.end()) {
//cleanup_cache();
GlobalTileCache & gtc = GlobalTileCache::get_instance();
bool ok = gtc.request_cache_memory(this, get_image_size());
assert(ok == true);
struct timespec now;
GET_CLOCK(now);
cache[filename] = std::make_pair(load(filename), now);
#ifdef TILECACHE_DEBUG
gtc.print_table();
#endif
}
current_tile = cache[filename].first;
curr_tile_num_x = tile_num_x;
curr_tile_num_y = tile_num_y;
}
return current_tile;
}

| std::tr1::shared_ptr<MemoryMap<typename PixelPolicy::pixel_type> > degate::TileCache< PixelPolicy >::load | ( | std::string const & | filename | ) | const [inline, private] |
Load a tile from an image file.
| filename | Just the name of the file to load. The filename is relative to the directory. |
Definition at line 403 of file TileCache.h.
References degate::TileCache< PixelPolicy >::directory, degate::join_pathes(), degate::MAP_STORAGE_TYPE_PERSISTENT_FILE, and degate::TileCache< PixelPolicy >::tile_width_exp.
Referenced by degate::TileCache< PixelPolicy >::get_tile().
{
//debug(TM, "directory: [%s] file: [%s]", directory.c_str(), filename.c_str());
MemoryMap_shptr mem(new MemoryMap<typename PixelPolicy::pixel_type>
(1 << tile_width_exp,
1 << tile_width_exp,
MAP_STORAGE_TYPE_PERSISTENT_FILE,
join_pathes(directory, filename)));
return mem;
}


| void degate::TileCache< PixelPolicy >::print | ( | ) | const [inline, virtual] |
Implements degate::TileCacheBase.
Definition at line 289 of file TileCache.h.
References degate::TileCache< PixelPolicy >::cache, and degate::TileCache< PixelPolicy >::directory.
friend class GlobalTileCache [friend] |
Definition at line 241 of file TileCache.h.
cache_type degate::TileCache< PixelPolicy >::cache [private] |
Definition at line 253 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::cleanup_cache(), degate::TileCache< PixelPolicy >::get_tile(), degate::TileCache< PixelPolicy >::print(), and degate::TileCache< PixelPolicy >::~TileCache().
unsigned degate::TileCache< PixelPolicy >::curr_tile_num_x [mutable, private] |
Definition at line 257 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::get_tile().
unsigned degate::TileCache< PixelPolicy >::curr_tile_num_y [mutable, private] |
Definition at line 258 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::get_tile().
MemoryMap_shptr degate::TileCache< PixelPolicy >::current_tile [mutable, private] |
Definition at line 256 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::get_tile().
const std::string degate::TileCache< PixelPolicy >::directory [private] |
Definition at line 249 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::load(), and degate::TileCache< PixelPolicy >::print().
const bool degate::TileCache< PixelPolicy >::persistent [private] |
Definition at line 251 of file TileCache.h.
const unsigned int degate::TileCache< PixelPolicy >::tile_width_exp [private] |
Definition at line 250 of file TileCache.h.
Referenced by degate::TileCache< PixelPolicy >::get_image_size(), degate::TileCache< PixelPolicy >::get_tile(), and degate::TileCache< PixelPolicy >::load().
1.7.4