1 // TileCache.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.
24 # include <simgear_config.h>
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sg_path.hxx>
31 #include "TileEntry.hxx"
32 #include "TileCache.hxx"
34 using simgear::TileEntry;
35 using simgear::TileCache;
37 TileCache::TileCache( void ) :
38 max_cache_size(100), current_time(0.0)
44 TileCache::~TileCache( void ) {
49 // Free a tile cache entry
50 void TileCache::entry_free( long cache_index ) {
51 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << cache_index );
52 TileEntry *tile = tile_cache[cache_index];
53 tile->removeFromSceneGraph();
54 tile_cache.erase( cache_index );
60 // Initialize the tile cache subsystem
61 void TileCache::init( void ) {
62 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
64 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
66 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
67 << tile_cache.size() );
71 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
75 // Search for the specified "bucket" in the cache
76 bool TileCache::exists( const SGBucket& b ) const {
77 long tile_index = b.gen_index();
78 const_tile_map_iterator it = tile_cache.find( tile_index );
80 return ( it != tile_cache.end() );
84 // Return the index of a tile to be dropped from the cache, return -1 if
85 // nothing available to be removed.
86 long TileCache::get_drop_tile() {
88 double min_time = DBL_MAX;
89 float priority = FLT_MAX;
91 tile_map_iterator current = tile_cache.begin();
92 tile_map_iterator end = tile_cache.end();
94 for ( ; current != end; ++current ) {
95 long index = current->first;
96 TileEntry *e = current->second;
97 if (( !e->is_current_view() )&&
98 ( e->is_expired(current_time) ))
100 if (e->is_expired(current_time - 1.0)&&
103 /* Immediately drop "empty" tiles which are no longer used/requested, and were last requested > 1 second ago...
104 * Allow a 1 second timeout since an empty tiles may just be loaded...
106 SG_LOG( SG_TERRAIN, SG_DEBUG, " dropping an unused and empty tile");
109 if (( e->get_time_expired() < min_time )||
110 (( e->get_time_expired() == min_time)&&
111 ( priority > e->get_priority())))
113 // drop oldest tile with lowest priority
114 min_time = e->get_time_expired();
115 priority = e->get_priority();
121 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << min_index );
122 SG_LOG( SG_TERRAIN, SG_DEBUG, " min_time = " << min_time );
128 // Clear all flags indicating tiles belonging to the current view
129 void TileCache::clear_current_view()
131 tile_map_iterator current = tile_cache.begin();
132 tile_map_iterator end = tile_cache.end();
134 for ( ; current != end; ++current ) {
135 TileEntry *e = current->second;
136 if (e->is_current_view())
138 // update expiry time for tiles belonging to most recent position
139 e->update_time_expired( current_time );
140 e->set_current_view( false );
145 // Clear a cache entry, note that the cache only holds pointers
146 // and this does not free the object which is pointed to.
147 void TileCache::clear_entry( long cache_index ) {
148 tile_cache.erase( cache_index );
152 // Clear all completely loaded tiles (ignores partially loaded tiles)
153 void TileCache::clear_cache() {
154 std::vector<long> indexList;
155 tile_map_iterator current = tile_cache.begin();
156 tile_map_iterator end = tile_cache.end();
158 for ( ; current != end; ++current ) {
159 long index = current->first;
160 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
161 TileEntry *e = current->second;
162 if ( e->is_loaded() ) {
163 e->tile_bucket.make_bad();
164 // entry_free modifies tile_cache, so store index and call entry_free() later;
165 indexList.push_back( index);
168 for (unsigned int it = 0; it < indexList.size(); it++) {
169 entry_free( indexList[ it]);
174 * Create a new tile and schedule it for loading.
176 bool TileCache::insert_tile( TileEntry *e ) {
177 // register tile in the cache
178 long tile_index = e->get_tile_bucket().gen_index();
179 tile_cache[tile_index] = e;
180 e->update_time_expired(current_time);
185 // update tile's priority and expiry time according to current request
186 void TileCache::request_tile(TileEntry* t,float priority,bool current_view,double request_time)
188 if ((!current_view)&&(request_time<=0.0))
191 // update priority when hire - or old has expired
192 if ((t->is_expired(current_time))||
193 (priority > t->get_priority()))
195 t->set_priority( priority );
200 t->update_time_expired( current_time );
201 t->set_current_view( true );
205 t->update_time_expired( current_time+request_time );