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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <plib/ssg.h> // plib include
30 #include <simgear/bucket/newbucket.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sg_path.hxx>
34 #include <Main/globals.hxx>
35 #include <Main/viewer.hxx>
36 #include <Scenery/scenery.hxx> // for scenery.center
38 #include "newcache.hxx"
39 #include "tileentry.hxx"
42 SG_USING_NAMESPACE(std);
46 FGNewCache::FGNewCache( void ) :
54 FGNewCache::~FGNewCache( void ) {
58 // Free a tile cache entry
59 void FGNewCache::entry_free( long cache_index ) {
60 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
61 FGTileEntry *tile = tile_cache[cache_index];
62 tile->disconnect_ssg_nodes();
64 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
65 tile->sched_removal();
71 tile_cache.erase( cache_index );
75 // Initialize the tile cache subsystem
76 void FGNewCache::init( void ) {
77 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
79 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
81 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
82 << tile_cache.size() );
84 #if 0 // don't clear the cache
88 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
92 // Search for the specified "bucket" in the cache
93 bool FGNewCache::exists( const SGBucket& b ) const {
94 long tile_index = b.gen_index();
95 const_tile_map_iterator it = tile_cache.find( tile_index );
97 return ( it != tile_cache.end() );
102 // Ensure at least one entry is free in the cache
103 bool FGNewCache::make_space() {
104 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
105 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
106 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
108 if ( (int)tile_cache.size() < max_cache_size ) {
109 // space in the cache, return
113 while ( (int)tile_cache.size() >= max_cache_size ) {
114 sgdVec3 abs_view_pos;
116 double timestamp = 0.0;
118 double min_time = 2419200000.0f; // one month should be enough
121 // we need to free the furthest entry
122 tile_map_iterator current = tile_cache.begin();
123 tile_map_iterator end = tile_cache.end();
125 for ( ; current != end; ++current ) {
126 long index = current->first;
127 FGTileEntry *e = current->second;
128 // if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
129 if ( e->is_loaded() ) {
131 timestamp = e->get_timestamp();
132 if ( timestamp < min_time ) {
134 min_time = timestamp;
136 if ( timestamp > max_time ) {
137 max_time = timestamp;
141 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
142 << " pending models = " << e->get_pending_models()
143 << " time stamp = " << e->get_timestamp() );
147 // If we made it this far, then there were no open cache entries.
148 // We will instead free the oldest cache entry and return true
150 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
151 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
152 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
153 if ( max_index >= 0 ) {
154 entry_free( max_index );
157 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
158 "cache is full, but no entries available for removal." );
163 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
164 "FGNewCache::make_space()." );
170 // Return the index of the oldest tile in the cache, return -1 if
171 // nothing available to be removed.
172 long FGNewCache::get_oldest_tile() {
173 // we need to free the furthest entry
175 double timestamp = 0.0;
176 double min_time = 2419200000.0f; // one month should be enough
179 tile_map_iterator current = tile_cache.begin();
180 tile_map_iterator end = tile_cache.end();
182 for ( ; current != end; ++current ) {
183 long index = current->first;
184 FGTileEntry *e = current->second;
185 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
187 timestamp = e->get_timestamp();
188 if ( timestamp < min_time ) {
190 min_time = timestamp;
192 if ( timestamp > max_time ) {
193 max_time = timestamp;
197 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
198 << " pending models = " << e->get_pending_models()
199 << " time stamp = " << e->get_timestamp() );
203 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
204 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
205 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
211 // Clear the inner ring flag for all tiles in the cache so that the
212 // external tile scheduler can flag the inner ring correctly.
213 void FGNewCache::clear_inner_ring_flags() {
214 tile_map_iterator current = tile_cache.begin();
215 tile_map_iterator end = tile_cache.end();
217 for ( ; current != end; ++current ) {
218 FGTileEntry *e = current->second;
219 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
220 e->set_inner_ring( false );
225 // Clear a cache entry, note that the cache only holds pointers
226 // and this does not free the object which is pointed to.
227 void FGNewCache::clear_entry( long cache_index ) {
228 tile_cache.erase( cache_index );
232 // Clear all completely loaded tiles (ignores partially loaded tiles)
233 void FGNewCache::clear_cache() {
235 tile_map_iterator current = tile_cache.begin();
236 tile_map_iterator end = tile_cache.end();
238 for ( ; current != end; ++current ) {
239 long index = current->first;
240 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
241 FGTileEntry *e = current->second;
242 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
243 e->tile_bucket.make_bad();
248 // and ... just in case we missed something ...
249 globals->get_scenery()->get_terrain_branch()->removeAllKids();
254 * Create a new tile and schedule it for loading.
256 bool FGNewCache::insert_tile( FGTileEntry *e ) {
257 // set time of insertion for tracking age of tiles...
258 e->set_timestamp(globals->get_sim_time_sec());
260 // register it in the cache
261 long tile_index = e->get_tile_bucket().gen_index();
262 tile_cache[tile_index] = e;
268 // Note this is the old version of FGNewCache::make_space(), currently disabled
269 // It uses distance from a center point to determine tiles to be discarded...
271 // Ensure at least one entry is free in the cache
272 bool FGNewCache::make_space() {
273 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
274 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
275 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
277 if ( (int)tile_cache.size() < max_cache_size ) {
278 // space in the cache, return
282 while ( (int)tile_cache.size() >= max_cache_size ) {
283 sgdVec3 abs_view_pos;
285 float max_dist = 0.0;
288 // we need to free the furthest entry
289 tile_map_iterator current = tile_cache.begin();
290 tile_map_iterator end = tile_cache.end();
292 for ( ; current != end; ++current ) {
293 long index = current->first;
294 FGTileEntry *e = current->second;
296 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
297 // calculate approximate distance from view point
298 sgdCopyVec3( abs_view_pos,
299 globals->get_current_view()->get_absolute_view_pos() );
301 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
302 << abs_view_pos[0] << ","
303 << abs_view_pos[1] << ","
304 << abs_view_pos[2] );
305 SG_LOG( SG_TERRAIN, SG_DEBUG,
306 " ref point = " << e->center );
310 e->center.x(), e->center.y(), e->center.z() );
311 dist = sgdDistanceVec3( center, abs_view_pos );
313 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
315 if ( dist > max_dist ) {
320 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
321 << " pending models = " << e->get_pending_models() );
325 // If we made it this far, then there were no open cache entries.
326 // We will instead free the furthest cache entry and return true
328 SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
329 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
330 if ( max_index >= 0 ) {
331 entry_free( max_index );
334 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
335 "cache is full, but no entries available for removal." );
340 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
341 "FGNewCache::make_space()." );