1 // newcache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started December 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 # include <simgear_config.h>
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sg_path.hxx>
31 #include "TileEntry.hxx"
32 #include "TileCache.hxx"
34 using simgear::TileEntry;
35 using simgear::TileCache;
37 TileCache::TileCache( void ) :
44 TileCache::~TileCache( void ) {
49 // Free a tile cache entry
50 void TileCache::entry_free( long cache_index ) {
51 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
52 TileEntry *tile = tile_cache[cache_index];
53 tile->removeFromSceneGraph();
58 tile_cache.erase( cache_index );
62 // Initialize the tile cache subsystem
63 void TileCache::init( void ) {
64 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
66 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
68 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
69 << tile_cache.size() );
71 #if 0 // don't clear the cache
75 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
79 // Search for the specified "bucket" in the cache
80 bool TileCache::exists( const SGBucket& b ) const {
81 long tile_index = b.gen_index();
82 const_tile_map_iterator it = tile_cache.find( tile_index );
84 return ( it != tile_cache.end() );
88 // Return the index of the oldest tile in the cache, return -1 if
89 // nothing available to be removed.
90 long TileCache::get_oldest_tile() {
91 // we need to free the furthest entry
93 double timestamp = 0.0;
94 double min_time = DBL_MAX;
96 tile_map_iterator current = tile_cache.begin();
97 tile_map_iterator end = tile_cache.end();
99 for ( ; current != end; ++current ) {
100 long index = current->first;
101 TileEntry *e = current->second;
102 if ( e->is_loaded() ) {
103 timestamp = e->get_timestamp();
104 if ( timestamp < min_time ) {
105 min_time = timestamp;
109 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
110 << " time stamp = " << e->get_timestamp() );
114 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
115 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
121 // Clear the inner ring flag for all tiles in the cache so that the
122 // external tile scheduler can flag the inner ring correctly.
123 void TileCache::clear_inner_ring_flags() {
124 tile_map_iterator current = tile_cache.begin();
125 tile_map_iterator end = tile_cache.end();
127 for ( ; current != end; ++current ) {
128 TileEntry *e = current->second;
129 if ( e->is_loaded() ) {
130 e->set_inner_ring( false );
135 // Clear a cache entry, note that the cache only holds pointers
136 // and this does not free the object which is pointed to.
137 void TileCache::clear_entry( long cache_index ) {
138 tile_cache.erase( cache_index );
142 // Clear all completely loaded tiles (ignores partially loaded tiles)
143 void TileCache::clear_cache() {
144 std::vector<long> indexList;
145 tile_map_iterator current = tile_cache.begin();
146 tile_map_iterator end = tile_cache.end();
148 for ( ; current != end; ++current ) {
149 long index = current->first;
150 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
151 TileEntry *e = current->second;
152 if ( e->is_loaded() ) {
153 e->tile_bucket.make_bad();
154 // entry_free modifies tile_cache, so store index and call entry_free() later;
155 indexList.push_back( index);
158 for (unsigned int it = 0; it < indexList.size(); it++) {
159 entry_free( indexList[ it]);
164 * Create a new tile and schedule it for loading.
166 bool TileCache::insert_tile( TileEntry *e ) {
167 // register tile in the cache
168 long tile_index = e->get_tile_bucket().gen_index();
169 tile_cache[tile_index] = e;