]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Modified Files:
[flightgear.git] / src / Scenery / newcache.cxx
1 // newcache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/bucket/newbucket.hxx>
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
31
32 #include <Main/globals.hxx>
33 #include <Main/viewer.hxx>
34 #include <Scenery/scenery.hxx>  // for scenery.center
35
36 #include "newcache.hxx"
37 #include "tileentry.hxx"
38
39
40 SG_USING_NAMESPACE(std);
41
42
43 // Constructor
44 FGNewCache::FGNewCache( void ) :
45     max_cache_size(100)
46 {
47     tile_cache.clear();
48 }
49
50
51 // Destructor
52 FGNewCache::~FGNewCache( void ) {
53 }
54
55
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();
61
62 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
63     tile->sched_removal();
64 #else
65     tile->free_tile();
66     delete tile;
67 #endif
68
69     tile_cache.erase( cache_index );
70 }
71
72
73 // Initialize the tile cache subsystem
74 void FGNewCache::init( void ) {
75     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
76
77     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
78             << max_cache_size );
79     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
80             << tile_cache.size() );
81
82 #if 0 // don't clear the cache
83     clear_cache();
84 #endif
85
86     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
87 }
88
89
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 );
94
95     return ( it != tile_cache.end() );
96 }
97
98
99 #if 0
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 );
105
106     if ( (int)tile_cache.size() < max_cache_size ) {
107         // space in the cache, return
108         return true;
109     }
110
111     while ( (int)tile_cache.size() >= max_cache_size ) {
112         sgdVec3 abs_view_pos;
113         float dist;
114         double timestamp = 0.0;
115         int max_index = -1;
116         double min_time = 2419200000.0f;  // one month should be enough
117         double max_time = 0;
118
119         // we need to free the furthest entry
120         tile_map_iterator current = tile_cache.begin();
121         tile_map_iterator end = tile_cache.end();
122     
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() ) {
128
129                 timestamp = e->get_timestamp();
130                 if ( timestamp < min_time ) {
131                     max_index = index;
132                     min_time = timestamp;
133                 }
134                 if ( timestamp > max_time ) {
135                     max_time = timestamp;
136                 }
137
138             } else {
139                 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
140                         << " pending models = " << e->get_pending_models()
141                         << " time stamp = " << e->get_timestamp() );
142             }
143         }
144
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
147         
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 );
153             return true;
154         } else {
155             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
156                     "cache is full, but no entries available for removal." );
157             return false;
158         }
159     }
160
161     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
162             "FGNewCache::make_space()." );
163     return false;
164 }
165 #endif
166
167
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
172     long min_index = -1;
173     double timestamp = 0.0;
174     double min_time = 2419200000.0f; // one month should be enough
175     double max_time = 0;
176
177     tile_map_iterator current = tile_cache.begin();
178     tile_map_iterator end = tile_cache.end();
179     
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) ) {
184             
185             timestamp = e->get_timestamp();
186             if ( timestamp < min_time ) {
187                 min_index = index;
188                 min_time = timestamp;
189             }
190             if ( timestamp > max_time ) {
191                 max_time = timestamp;
192             }
193
194         } else {
195             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
196                     << " pending models = " << e->get_pending_models()
197                     << " time stamp = " << e->get_timestamp() );
198         }
199     }
200
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 );
204
205     return min_index;
206 }
207
208
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();
214     
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 );
219         }
220     }
221 }
222
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 );
227 }
228
229
230 // Clear all completely loaded tiles (ignores partially loaded tiles)
231 void FGNewCache::clear_cache() {
232
233     tile_map_iterator current = tile_cache.begin();
234     tile_map_iterator end = tile_cache.end();
235     
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();
242             entry_free(index);
243         }
244     }
245
246     // and ... just in case we missed something ... 
247     osg::Group* group = globals->get_scenery()->get_terrain_branch();
248     group->removeChildren(0, group->getNumChildren());
249 }
250
251
252 /**
253  * Create a new tile and schedule it for loading.
254  */
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());
258
259     // register it in the cache
260     long tile_index = e->get_tile_bucket().gen_index();
261     tile_cache[tile_index] = e;
262
263     return true;
264 }
265
266
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...
269 #if 0
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 );
275
276     if ( (int)tile_cache.size() < max_cache_size ) {
277         // space in the cache, return
278         return true;
279     }
280
281     while ( (int)tile_cache.size() >= max_cache_size ) {
282         sgdVec3 abs_view_pos;
283         float dist;
284         float max_dist = 0.0;
285         int max_index = -1;
286
287         // we need to free the furthest entry
288         tile_map_iterator current = tile_cache.begin();
289         tile_map_iterator end = tile_cache.end();
290     
291         for ( ; current != end; ++current ) {
292             long index = current->first;
293             FGTileEntry *e = current->second;
294
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() );
299
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 );
306
307                 sgdVec3 center;
308                 sgdSetVec3( center,
309                             e->center.x(), e->center.y(), e->center.z() );
310                 dist = sgdDistanceVec3( center, abs_view_pos );
311
312                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
313
314                 if ( dist > max_dist ) {
315                     max_dist = dist;
316                     max_index = index;
317                 }
318             } else {
319                 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
320                         << " pending models = " << e->get_pending_models() );
321             }
322         }
323
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
326         
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 );
331             return true;
332         } else {
333             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
334                     "cache is full, but no entries available for removal." );
335             return false;
336         }
337     }
338
339     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
340             "FGNewCache::make_space()." );
341     return false;
342 }
343 #endif
344
345