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 ) {
56 // Free a tile cache entry
57 void FGNewCache::entry_free( long cache_index ) {
58 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
59 FGTileEntry *tile = tile_cache[cache_index];
60 tile->disconnect_ssg_nodes();
62 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
63 tile->sched_removal();
69 tile_cache.erase( cache_index );
73 // Initialize the tile cache subsystem
74 void FGNewCache::init( void ) {
75 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
77 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
79 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
80 << tile_cache.size() );
82 #if 0 // don't clear the cache
86 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
90 // Search for the specified "bucket" in the cache
91 bool FGNewCache::exists( const SGBucket& b ) const {
92 long tile_index = b.gen_index();
93 const_tile_map_iterator it = tile_cache.find( tile_index );
95 return ( it != tile_cache.end() );
100 // Ensure at least one entry is free in the cache
101 bool FGNewCache::make_space() {
102 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
103 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
104 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
106 if ( (int)tile_cache.size() < max_cache_size ) {
107 // space in the cache, return
111 while ( (int)tile_cache.size() >= max_cache_size ) {
112 sgdVec3 abs_view_pos;
114 double timestamp = 0.0;
116 double min_time = 2419200000.0f; // one month should be enough
119 // we need to free the furthest entry
120 tile_map_iterator current = tile_cache.begin();
121 tile_map_iterator end = tile_cache.end();
123 for ( ; current != end; ++current ) {
124 long index = current->first;
125 FGTileEntry *e = current->second;
126 // if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
127 if ( e->is_loaded() ) {
129 timestamp = e->get_timestamp();
130 if ( timestamp < min_time ) {
132 min_time = timestamp;
134 if ( timestamp > max_time ) {
135 max_time = timestamp;
139 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
140 << " pending models = " << e->get_pending_models()
141 << " time stamp = " << e->get_timestamp() );
145 // If we made it this far, then there were no open cache entries.
146 // We will instead free the oldest cache entry and return true
148 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
149 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
150 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
151 if ( max_index >= 0 ) {
152 entry_free( max_index );
155 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
156 "cache is full, but no entries available for removal." );
161 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
162 "FGNewCache::make_space()." );
168 // Return the index of the oldest tile in the cache, return -1 if
169 // nothing available to be removed.
170 long FGNewCache::get_oldest_tile() {
171 // we need to free the furthest entry
173 double timestamp = 0.0;
174 double min_time = 2419200000.0f; // one month should be enough
177 tile_map_iterator current = tile_cache.begin();
178 tile_map_iterator end = tile_cache.end();
180 for ( ; current != end; ++current ) {
181 long index = current->first;
182 FGTileEntry *e = current->second;
183 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
185 timestamp = e->get_timestamp();
186 if ( timestamp < min_time ) {
188 min_time = timestamp;
190 if ( timestamp > max_time ) {
191 max_time = timestamp;
195 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
196 << " pending models = " << e->get_pending_models()
197 << " time stamp = " << e->get_timestamp() );
201 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
202 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
203 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
209 // Clear the inner ring flag for all tiles in the cache so that the
210 // external tile scheduler can flag the inner ring correctly.
211 void FGNewCache::clear_inner_ring_flags() {
212 tile_map_iterator current = tile_cache.begin();
213 tile_map_iterator end = tile_cache.end();
215 for ( ; current != end; ++current ) {
216 FGTileEntry *e = current->second;
217 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
218 e->set_inner_ring( false );
223 // Clear a cache entry, note that the cache only holds pointers
224 // and this does not free the object which is pointed to.
225 void FGNewCache::clear_entry( long cache_index ) {
226 tile_cache.erase( cache_index );
230 // Clear all completely loaded tiles (ignores partially loaded tiles)
231 void FGNewCache::clear_cache() {
233 tile_map_iterator current = tile_cache.begin();
234 tile_map_iterator end = tile_cache.end();
236 for ( ; current != end; ++current ) {
237 long index = current->first;
238 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
239 FGTileEntry *e = current->second;
240 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
241 e->tile_bucket.make_bad();
246 // and ... just in case we missed something ...
247 osg::Group* group = globals->get_scenery()->get_terrain_branch();
248 group->removeChildren(0, group->getNumChildren());
253 * Create a new tile and schedule it for loading.
255 bool FGNewCache::insert_tile( FGTileEntry *e ) {
256 // set time of insertion for tracking age of tiles...
257 e->set_timestamp(globals->get_sim_time_sec());
259 // register it in the cache
260 long tile_index = e->get_tile_bucket().gen_index();
261 tile_cache[tile_index] = e;
267 // Note this is the old version of FGNewCache::make_space(), currently disabled
268 // It uses distance from a center point to determine tiles to be discarded...
270 // Ensure at least one entry is free in the cache
271 bool FGNewCache::make_space() {
272 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
273 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
274 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
276 if ( (int)tile_cache.size() < max_cache_size ) {
277 // space in the cache, return
281 while ( (int)tile_cache.size() >= max_cache_size ) {
282 sgdVec3 abs_view_pos;
284 float max_dist = 0.0;
287 // we need to free the furthest entry
288 tile_map_iterator current = tile_cache.begin();
289 tile_map_iterator end = tile_cache.end();
291 for ( ; current != end; ++current ) {
292 long index = current->first;
293 FGTileEntry *e = current->second;
295 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
296 // calculate approximate distance from view point
297 sgdCopyVec3( abs_view_pos,
298 globals->get_current_view()->get_absolute_view_pos() );
300 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
301 << abs_view_pos[0] << ","
302 << abs_view_pos[1] << ","
303 << abs_view_pos[2] );
304 SG_LOG( SG_TERRAIN, SG_DEBUG,
305 " ref point = " << e->center );
309 e->center.x(), e->center.y(), e->center.z() );
310 dist = sgdDistanceVec3( center, abs_view_pos );
312 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
314 if ( dist > max_dist ) {
319 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
320 << " pending models = " << e->get_pending_models() );
324 // If we made it this far, then there were no open cache entries.
325 // We will instead free the furthest cache entry and return true
327 SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
328 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
329 if ( max_index >= 0 ) {
330 entry_free( max_index );
333 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
334 "cache is full, but no entries available for removal." );
339 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
340 "FGNewCache::make_space()." );