00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #  include <simgear_config.h>
00025 #endif
00026 
00027 #include <simgear/bucket/newbucket.hxx>
00028 #include <simgear/debug/logstream.hxx>
00029 #include <simgear/misc/sg_path.hxx>
00030 
00031 #include "TileEntry.hxx"
00032 #include "TileCache.hxx"
00033 
00034 using simgear::TileEntry;
00035 using simgear::TileCache;
00036 
00037 TileCache::TileCache( void ) :
00038     max_cache_size(100), current_time(0.0)
00039 {
00040     tile_cache.clear();
00041 }
00042 
00043 
00044 TileCache::~TileCache( void ) {
00045     clear_cache();
00046 }
00047 
00048 
00049 
00050 void TileCache::entry_free( long cache_index ) {
00051     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
00052     TileEntry *tile = tile_cache[cache_index];
00053     tile->removeFromSceneGraph();
00054 
00055     delete tile;
00056 
00057     tile_cache.erase( cache_index );
00058 }
00059 
00060 
00061 
00062 void TileCache::init( void ) {
00063     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
00064 
00065     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = "
00066             << max_cache_size );
00067     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = "
00068             << tile_cache.size() );
00069 
00070 #if 0 // don't clear the cache
00071     clear_cache();
00072 #endif
00073 
00074     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
00075 }
00076 
00077 
00078 
00079 bool TileCache::exists( const SGBucket& b ) const {
00080     long tile_index = b.gen_index();
00081     const_tile_map_iterator it = tile_cache.find( tile_index );
00082 
00083     return ( it != tile_cache.end() );
00084 }
00085 
00086 
00087 
00088 
00089 long TileCache::get_oldest_tile() {
00090     
00091     long min_index = -1;
00092     double timestamp = 0.0;
00093     double min_time = DBL_MAX;
00094 
00095     tile_map_iterator current = tile_cache.begin();
00096     tile_map_iterator end = tile_cache.end();
00097 
00098     for ( ; current != end; ++current ) {
00099         long index = current->first;
00100         TileEntry *e = current->second;
00101         if ( e->is_loaded() ) {
00102             timestamp = e->get_timestamp();
00103             if ( timestamp < min_time ) {
00104                 min_time = timestamp;
00105                 min_index = index;
00106             }
00107         } else {
00108             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
00109                     << " time stamp = " << e->get_timestamp() );
00110         }
00111     }
00112 
00113     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
00114     SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
00115 
00116     return min_index;
00117 }
00118 
00119 
00120 
00121 
00122 void TileCache::clear_inner_ring_flags() {
00123     tile_map_iterator current = tile_cache.begin();
00124     tile_map_iterator end = tile_cache.end();
00125 
00126     for ( ; current != end; ++current ) {
00127         TileEntry *e = current->second;
00128         if ( e->is_loaded() ) {
00129             e->set_inner_ring( false );
00130         }
00131     }
00132 }
00133 
00134 
00135 
00136 void TileCache::clear_entry( long cache_index ) {
00137     tile_cache.erase( cache_index );
00138 }
00139 
00140 
00141 
00142 void TileCache::clear_cache() {
00143     std::vector<long> indexList;
00144     tile_map_iterator current = tile_cache.begin();
00145     tile_map_iterator end = tile_cache.end();
00146 
00147     for ( ; current != end; ++current ) {
00148         long index = current->first;
00149         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
00150         TileEntry *e = current->second;
00151         if ( e->is_loaded() ) {
00152             e->tile_bucket.make_bad();
00153             
00154             indexList.push_back( index);
00155         }
00156     }
00157     for (unsigned int it = 0; it < indexList.size(); it++) {
00158         entry_free( indexList[ it]);
00159     }
00160 }
00161 
00165 bool TileCache::insert_tile( TileEntry *e ) {
00166     
00167     long tile_index = e->get_tile_bucket().gen_index();
00168     tile_cache[tile_index] = e;
00169     e->set_timestamp(current_time);
00170 
00171     return true;
00172 }
00173