1 // newcache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started December 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <plib/ssg.h> // plib include
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/viewer.hxx>
43 #include <Scenery/scenery.hxx> // for scenery.center
45 #include "newcache.hxx"
46 #include "tileentry.hxx"
49 SG_USING_NAMESPACE(std);
53 FGNewCache::FGNewCache( void ) :
61 FGNewCache::~FGNewCache( void ) {
65 // Free a tile cache entry
66 void FGNewCache::entry_free( long cache_index ) {
67 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
68 FGTileEntry *tile = tile_cache[cache_index];
69 tile->disconnect_ssg_nodes();
71 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
72 tile->sched_removal();
78 tile_cache.erase( cache_index );
82 // Initialize the tile cache subsystem
83 void FGNewCache::init( void ) {
84 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
86 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
88 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
89 << tile_cache.size() );
91 #if 0 // don't clear the cache
95 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
99 // Search for the specified "bucket" in the cache
100 bool FGNewCache::exists( const SGBucket& b ) const {
101 long tile_index = b.gen_index();
102 const_tile_map_iterator it = tile_cache.find( tile_index );
104 return ( it != tile_cache.end() );
108 // Ensure at least one entry is free in the cache
109 bool FGNewCache::make_space() {
110 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
111 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
112 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
114 if ( (int)tile_cache.size() < max_cache_size ) {
115 // space in the cache, return
119 while ( (int)tile_cache.size() >= max_cache_size ) {
120 sgdVec3 abs_view_pos;
122 double timestamp = 0.0;
124 double min_time = 2419200000.0f; // one month should be enough
127 // we need to free the furthest entry
128 tile_map_iterator current = tile_cache.begin();
129 tile_map_iterator end = tile_cache.end();
131 for ( ; current != end; ++current ) {
132 long index = current->first;
133 FGTileEntry *e = current->second;
134 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
136 timestamp = e->get_timestamp();
137 if ( timestamp < min_time ) {
139 min_time = timestamp;
141 if ( timestamp > max_time ) {
142 max_time = timestamp;
146 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
147 << " pending models = " << e->get_pending_models()
148 << " time stamp = " << e->get_timestamp() );
152 // If we made it this far, then there were no open cache entries.
153 // We will instead free the oldest cache entry and return true
155 SG_LOG( SG_TERRAIN, SG_INFO, " min_time = " << min_time );
156 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
157 SG_LOG( SG_TERRAIN, SG_INFO, " max_time = " << max_time );
158 if ( max_index >= 0 ) {
159 entry_free( max_index );
162 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
163 "cache is full, but no entries available for removal." );
168 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
169 "FGNewCache::make_space()." );
174 // Return the index of the oldest tile in the cache, return -1 if
175 // nothing available to be removed.
176 long FGNewCache::get_oldest_tile() {
177 // we need to free the furthest entry
179 double timestamp = 0.0;
180 double min_time = 2419200000.0f; // one month should be enough
183 tile_map_iterator current = tile_cache.begin();
184 tile_map_iterator end = tile_cache.end();
186 for ( ; current != end; ++current ) {
187 long index = current->first;
188 FGTileEntry *e = current->second;
189 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
191 timestamp = e->get_timestamp();
192 if ( timestamp < min_time ) {
194 min_time = timestamp;
196 if ( timestamp > max_time ) {
197 max_time = timestamp;
201 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
202 << " pending models = " << e->get_pending_models()
203 << " time stamp = " << e->get_timestamp() );
207 SG_LOG( SG_TERRAIN, SG_INFO, " min_time = " << min_time );
208 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << min_index );
209 SG_LOG( SG_TERRAIN, SG_INFO, " max_time = " << max_time );
215 // Clear a cache entry, note that the cache only holds pointers
216 // and this does not free the object which is pointed to.
217 void FGNewCache::clear_entry( long cache_index ) {
218 tile_cache.erase( cache_index );
222 // Clear all completely loaded tiles (ignores partially loaded tiles)
223 void FGNewCache::clear_cache() {
225 tile_map_iterator current = tile_cache.begin();
226 tile_map_iterator end = tile_cache.end();
228 for ( ; current != end; ++current ) {
229 long index = current->first;
230 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
231 FGTileEntry *e = current->second;
232 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
233 e->tile_bucket.make_bad();
238 // and ... just in case we missed something ...
239 globals->get_scenery()->get_terrain_branch()->removeAllKids();
244 * Create a new tile and schedule it for loading.
246 bool FGNewCache::insert_tile( FGTileEntry *e ) {
247 // set time of insertion for tracking age of tiles...
248 e->set_timestamp(globals->get_sim_time_sec());
249 // clear out a distant entry in the cache if needed.
250 if ( make_space() ) {
251 // register it in the cache
252 long tile_index = e->get_tile_bucket().gen_index();
253 tile_cache[tile_index] = e;
257 // failed to find cache space
264 // Note this is the old version of FGNewCache::make_space(), currently disabled
265 // It uses distance from a center point to determine tiles to be discarded...
267 // Ensure at least one entry is free in the cache
268 bool FGNewCache::make_space() {
269 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
270 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
271 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
273 if ( (int)tile_cache.size() < max_cache_size ) {
274 // space in the cache, return
278 while ( (int)tile_cache.size() >= max_cache_size ) {
279 sgdVec3 abs_view_pos;
281 float max_dist = 0.0;
284 // we need to free the furthest entry
285 tile_map_iterator current = tile_cache.begin();
286 tile_map_iterator end = tile_cache.end();
288 for ( ; current != end; ++current ) {
289 long index = current->first;
290 FGTileEntry *e = current->second;
292 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
293 // calculate approximate distance from view point
294 sgdCopyVec3( abs_view_pos,
295 globals->get_current_view()->get_absolute_view_pos() );
297 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
298 << abs_view_pos[0] << ","
299 << abs_view_pos[1] << ","
300 << abs_view_pos[2] );
301 SG_LOG( SG_TERRAIN, SG_DEBUG,
302 " ref point = " << e->center );
306 e->center.x(), e->center.y(), e->center.z() );
307 dist = sgdDistanceVec3( center, abs_view_pos );
309 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
311 if ( dist > max_dist ) {
316 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
317 << " pending models = " << e->get_pending_models() );
321 // If we made it this far, then there were no open cache entries.
322 // We will instead free the furthest cache entry and return true
324 SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
325 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
326 if ( max_index >= 0 ) {
327 entry_free( max_index );
330 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
331 "cache is full, but no entries available for removal." );
336 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
337 "FGNewCache::make_space()." );