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() );
99 // Return the index of the oldest tile in the cache, return -1 if
100 // nothing available to be removed.
101 long FGNewCache::get_oldest_tile() {
102 // we need to free the furthest entry
104 double timestamp = 0.0;
107 tile_map_iterator current = tile_cache.begin();
108 tile_map_iterator end = tile_cache.end();
110 for ( ; current != end; ++current ) {
111 long index = current->first;
112 FGTileEntry *e = current->second;
113 if ( e->is_loaded() ) {
115 timestamp = e->get_timestamp();
116 if ( timestamp > max_time ) {
117 max_time = timestamp;
121 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
122 << " time stamp = " << e->get_timestamp() );
126 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
127 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_time = " << max_time );
133 // Clear the inner ring flag for all tiles in the cache so that the
134 // external tile scheduler can flag the inner ring correctly.
135 void FGNewCache::clear_inner_ring_flags() {
136 tile_map_iterator current = tile_cache.begin();
137 tile_map_iterator end = tile_cache.end();
139 for ( ; current != end; ++current ) {
140 FGTileEntry *e = current->second;
141 if ( e->is_loaded() ) {
142 e->set_inner_ring( false );
147 // Clear a cache entry, note that the cache only holds pointers
148 // and this does not free the object which is pointed to.
149 void FGNewCache::clear_entry( long cache_index ) {
150 tile_cache.erase( cache_index );
154 // Clear all completely loaded tiles (ignores partially loaded tiles)
155 void FGNewCache::clear_cache() {
156 std::vector<long> indexList;
157 tile_map_iterator current = tile_cache.begin();
158 tile_map_iterator end = tile_cache.end();
160 for ( ; current != end; ++current ) {
161 long index = current->first;
162 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
163 FGTileEntry *e = current->second;
164 if ( e->is_loaded() ) {
165 e->tile_bucket.make_bad();
166 // entry_free modifies tile_cache, so store index and call entry_free() later;
167 indexList.push_back( index);
170 for (unsigned int it = 0; it < indexList.size(); it++) {
171 entry_free( indexList[ it]);
173 // and ... just in case we missed something ...
174 osg::Group* group = globals->get_scenery()->get_terrain_branch();
175 group->removeChildren(0, group->getNumChildren());
180 * Create a new tile and schedule it for loading.
182 bool FGNewCache::insert_tile( FGTileEntry *e ) {
183 // register tile in the cache
184 long tile_index = e->get_tile_bucket().gen_index();
185 tile_cache[tile_index] = e;