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.
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) ) {
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 a cache entry, note that the cache only holds pointers
211 // and this does not free the object which is pointed to.
212 void FGNewCache::clear_entry( long cache_index ) {
213 tile_cache.erase( cache_index );
217 // Clear all completely loaded tiles (ignores partially loaded tiles)
218 void FGNewCache::clear_cache() {
220 tile_map_iterator current = tile_cache.begin();
221 tile_map_iterator end = tile_cache.end();
223 for ( ; current != end; ++current ) {
224 long index = current->first;
225 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
226 FGTileEntry *e = current->second;
227 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
228 e->tile_bucket.make_bad();
233 // and ... just in case we missed something ...
234 globals->get_scenery()->get_terrain_branch()->removeAllKids();
239 * Create a new tile and schedule it for loading.
241 bool FGNewCache::insert_tile( FGTileEntry *e ) {
242 // set time of insertion for tracking age of tiles...
243 e->set_timestamp(globals->get_sim_time_sec());
245 // register it in the cache
246 long tile_index = e->get_tile_bucket().gen_index();
247 tile_cache[tile_index] = e;
253 // Note this is the old version of FGNewCache::make_space(), currently disabled
254 // It uses distance from a center point to determine tiles to be discarded...
256 // Ensure at least one entry is free in the cache
257 bool FGNewCache::make_space() {
258 SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
259 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
260 SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
262 if ( (int)tile_cache.size() < max_cache_size ) {
263 // space in the cache, return
267 while ( (int)tile_cache.size() >= max_cache_size ) {
268 sgdVec3 abs_view_pos;
270 float max_dist = 0.0;
273 // we need to free the furthest entry
274 tile_map_iterator current = tile_cache.begin();
275 tile_map_iterator end = tile_cache.end();
277 for ( ; current != end; ++current ) {
278 long index = current->first;
279 FGTileEntry *e = current->second;
281 if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
282 // calculate approximate distance from view point
283 sgdCopyVec3( abs_view_pos,
284 globals->get_current_view()->get_absolute_view_pos() );
286 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
287 << abs_view_pos[0] << ","
288 << abs_view_pos[1] << ","
289 << abs_view_pos[2] );
290 SG_LOG( SG_TERRAIN, SG_DEBUG,
291 " ref point = " << e->center );
295 e->center.x(), e->center.y(), e->center.z() );
296 dist = sgdDistanceVec3( center, abs_view_pos );
298 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
300 if ( dist > max_dist ) {
305 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
306 << " pending models = " << e->get_pending_models() );
310 // If we made it this far, then there were no open cache entries.
311 // We will instead free the furthest cache entry and return true
313 SG_LOG( SG_TERRAIN, SG_INFO, " max_dist = " << max_dist );
314 SG_LOG( SG_TERRAIN, SG_INFO, " index = " << max_index );
315 if ( max_index >= 0 ) {
316 entry_free( max_index );
319 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
320 "cache is full, but no entries available for removal." );
325 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in "
326 "FGNewCache::make_space()." );