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.
28 #include <simgear/bucket/newbucket.hxx>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
32 #include <Main/globals.hxx>
33 #include <Main/viewer.hxx>
34 #include <Scenery/scenery.hxx> // for scenery.center
36 #include "newcache.hxx"
37 #include "tileentry.hxx"
40 SG_USING_NAMESPACE(std);
44 FGNewCache::FGNewCache( void ) :
52 FGNewCache::~FGNewCache( void ) {
57 // Free a tile cache entry
58 void FGNewCache::entry_free( long cache_index ) {
59 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
60 FGTileEntry *tile = tile_cache[cache_index];
61 tile->disconnect_ssg_nodes();
63 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
64 tile->sched_removal();
70 tile_cache.erase( cache_index );
74 // Initialize the tile cache subsystem
75 void FGNewCache::init( void ) {
76 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
78 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
80 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
81 << tile_cache.size() );
83 #if 0 // don't clear the cache
87 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
91 // Search for the specified "bucket" in the cache
92 bool FGNewCache::exists( const SGBucket& b ) const {
93 long tile_index = b.gen_index();
94 const_tile_map_iterator it = tile_cache.find( tile_index );
96 return ( it != tile_cache.end() );
101 // Ensure at least one entry is free in the cache
102 bool FGNewCache::make_space() {
103 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
104 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
105 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
107 if ( (int)tile_cache.size() < max_cache_size ) {
108 // space in the cache, return
112 while ( (int)tile_cache.size() >= max_cache_size ) {
113 sgdVec3 abs_view_pos;
115 double timestamp = 0.0;
117 double min_time = 2419200000.0f; // one month should be enough
120 // we need to free the furthest entry
121 tile_map_iterator current = tile_cache.begin();
122 tile_map_iterator end = tile_cache.end();
124 for ( ; current != end; ++current ) {
125 long index = current->first;
126 FGTileEntry *e = current->second;
127 // if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
128 if ( e->is_loaded() ) {
130 timestamp = e->get_timestamp();
131 if ( timestamp < min_time ) {
133 min_time = timestamp;
135 if ( timestamp > max_time ) {
136 max_time = timestamp;
140 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
141 << " pending models = " << e->get_pending_models()
142 << " time stamp = " << e->get_timestamp() );
146 // If we made it this far, then there were no open cache entries.
147 // We will instead free the oldest cache entry and return true
149 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
150 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
151 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
152 if ( max_index >= 0 ) {
153 entry_free( max_index );
156 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
157 "cache is full, but no entries available for removal." );
162 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
163 "FGNewCache::make_space()." );
169 // Return the index of the oldest tile in the cache, return -1 if
170 // nothing available to be removed.
171 long FGNewCache::get_oldest_tile() {
172 // we need to free the furthest entry
174 double timestamp = 0.0;
175 double min_time = 2419200000.0f; // one month should be enough
178 tile_map_iterator current = tile_cache.begin();
179 tile_map_iterator end = tile_cache.end();
181 for ( ; current != end; ++current ) {
182 long index = current->first;
183 FGTileEntry *e = current->second;
184 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
186 timestamp = e->get_timestamp();
187 if ( timestamp < min_time ) {
189 min_time = timestamp;
191 if ( timestamp > max_time ) {
192 max_time = timestamp;
196 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
197 << " pending models = " << e->get_pending_models()
198 << " time stamp = " << e->get_timestamp() );
202 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
203 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
204 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
210 // Clear the inner ring flag for all tiles in the cache so that the
211 // external tile scheduler can flag the inner ring correctly.
212 void FGNewCache::clear_inner_ring_flags() {
213 tile_map_iterator current = tile_cache.begin();
214 tile_map_iterator end = tile_cache.end();
216 for ( ; current != end; ++current ) {
217 FGTileEntry *e = current->second;
218 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
219 e->set_inner_ring( false );
224 // Clear a cache entry, note that the cache only holds pointers
225 // and this does not free the object which is pointed to.
226 void FGNewCache::clear_entry( long cache_index ) {
227 tile_cache.erase( cache_index );
231 // Clear all completely loaded tiles (ignores partially loaded tiles)
232 void FGNewCache::clear_cache() {
233 std::vector<long> indexList;
234 tile_map_iterator current = tile_cache.begin();
235 tile_map_iterator end = tile_cache.end();
237 for ( ; current != end; ++current ) {
238 long index = current->first;
239 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
240 FGTileEntry *e = current->second;
241 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
242 e->tile_bucket.make_bad();
243 // entry_free modifies tile_cache, so store index and call entry_free() later;
244 indexList.push_back( index);
247 for (unsigned int it = 0; it < indexList.size(); it++) {
248 entry_free( indexList[ it]);
250 // and ... just in case we missed something ...
251 osg::Group* group = globals->get_scenery()->get_terrain_branch();
252 group->removeChildren(0, group->getNumChildren());
257 * Create a new tile and schedule it for loading.
259 bool FGNewCache::insert_tile( FGTileEntry *e ) {
260 // set time of insertion for tracking age of tiles...
261 e->set_timestamp(globals->get_sim_time_sec());
263 // register it in the cache
264 long tile_index = e->get_tile_bucket().gen_index();
265 tile_cache[tile_index] = e;
271 // Note this is the old version of FGNewCache::make_space(), currently disabled
272 // It uses distance from a center point to determine tiles to be discarded...
274 // Ensure at least one entry is free in the cache
275 bool FGNewCache::make_space() {
276 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
277 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
278 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
280 if ( (int)tile_cache.size() < max_cache_size ) {
281 // space in the cache, return
285 while ( (int)tile_cache.size() >= max_cache_size ) {
286 sgdVec3 abs_view_pos;
288 float max_dist = 0.0;
291 // we need to free the furthest entry
292 tile_map_iterator current = tile_cache.begin();
293 tile_map_iterator end = tile_cache.end();
295 for ( ; current != end; ++current ) {
296 long index = current->first;
297 FGTileEntry *e = current->second;
299 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
300 // calculate approximate distance from view point
301 sgdCopyVec3( abs_view_pos,
302 globals->get_current_view()->get_absolute_view_pos() );
304 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
305 << abs_view_pos[0] << ","
306 << abs_view_pos[1] << ","
307 << abs_view_pos[2] );
308 SG_LOG( SG_TERRAIN, SG_DEBUG,
309 " ref point = " << e->center );
313 e->center.x(), e->center.y(), e->center.z() );
314 dist = sgdDistanceVec3( center, abs_view_pos );
316 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
318 if ( dist > max_dist ) {
323 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
324 << " pending models = " << e->get_pending_models() );
328 // If we made it this far, then there were no open cache entries.
329 // We will instead free the furthest cache entry and return true
331 SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
332 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
333 if ( max_index >= 0 ) {
334 entry_free( max_index );
337 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
338 "cache is full, but no entries available for removal." );
343 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
344 "FGNewCache::make_space()." );