]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
initialize release_keys array
[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     clear_cache();
54 }
55
56
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();
62
63 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
64     tile->sched_removal();
65 #else
66     tile->free_tile();
67     delete tile;
68 #endif
69
70     tile_cache.erase( cache_index );
71 }
72
73
74 // Initialize the tile cache subsystem
75 void FGNewCache::init( void ) {
76     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
77
78     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
79             << max_cache_size );
80     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
81             << tile_cache.size() );
82
83 #if 0 // don't clear the cache
84     clear_cache();
85 #endif
86
87     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
88 }
89
90
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 );
95
96     return ( it != tile_cache.end() );
97 }
98
99
100 #if 0
101 // Ensure at least one entry is free in the cache
102 bool FGNewCache::make_space() {
103     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
104     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
105     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
106
107     if ( (int)tile_cache.size() < max_cache_size ) {
108         // space in the cache, return
109         return true;
110     }
111
112     while ( (int)tile_cache.size() >= max_cache_size ) {
113         sgdVec3 abs_view_pos;
114         float dist;
115         double timestamp = 0.0;
116         int max_index = -1;
117         double min_time = 2419200000.0f;  // one month should be enough
118         double max_time = 0;
119
120         // we need to free the furthest entry
121         tile_map_iterator current = tile_cache.begin();
122         tile_map_iterator end = tile_cache.end();
123     
124         for ( ; current != end; ++current ) {
125             long index = current->first;
126             FGTileEntry *e = current->second;
127             // if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
128             if ( e->is_loaded() ) {
129
130                 timestamp = e->get_timestamp();
131                 if ( timestamp < min_time ) {
132                     max_index = index;
133                     min_time = timestamp;
134                 }
135                 if ( timestamp > max_time ) {
136                     max_time = timestamp;
137                 }
138
139             } else {
140                 SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
141                         << " pending models = " << e->get_pending_models()
142                         << " time stamp = " << e->get_timestamp() );
143             }
144         }
145
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
148         
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 );
154             return true;
155         } else {
156             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
157                     "cache is full, but no entries available for removal." );
158             return false;
159         }
160     }
161
162     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
163             "FGNewCache::make_space()." );
164     return false;
165 }
166 #endif
167
168
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
173     long min_index = -1;
174     double timestamp = 0.0;
175     double min_time = 2419200000.0f; // one month should be enough
176     double max_time = 0;
177
178     tile_map_iterator current = tile_cache.begin();
179     tile_map_iterator end = tile_cache.end();
180     
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) ) {
185             
186             timestamp = e->get_timestamp();
187             if ( timestamp < min_time ) {
188                 min_index = index;
189                 min_time = timestamp;
190             }
191             if ( timestamp > max_time ) {
192                 max_time = timestamp;
193             }
194
195         } else {
196             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
197                     << " pending models = " << e->get_pending_models()
198                     << " time stamp = " << e->get_timestamp() );
199         }
200     }
201
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 );
205
206     return min_index;
207 }
208
209
210 // Clear the inner ring flag for all tiles in the cache so that the
211 // external tile scheduler can flag the inner ring correctly.
212 void FGNewCache::clear_inner_ring_flags() {
213     tile_map_iterator current = tile_cache.begin();
214     tile_map_iterator end = tile_cache.end();
215     
216     for ( ; current != end; ++current ) {
217         FGTileEntry *e = current->second;
218         if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
219             e->set_inner_ring( false );
220         }
221     }
222 }
223
224 // Clear a cache entry, note that the cache only holds pointers
225 // and this does not free the object which is pointed to.
226 void FGNewCache::clear_entry( long cache_index ) {
227     tile_cache.erase( cache_index );
228 }
229
230
231 // Clear all completely loaded tiles (ignores partially loaded tiles)
232 void FGNewCache::clear_cache() {
233     std::vector<long> indexList;
234     tile_map_iterator current = tile_cache.begin();
235     tile_map_iterator end = tile_cache.end();
236     
237     for ( ; current != end; ++current ) {
238         long index = current->first;
239         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
240         FGTileEntry *e = current->second;
241         if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
242             e->tile_bucket.make_bad();
243             // entry_free modifies tile_cache, so store index and call entry_free() later;
244             indexList.push_back( index);
245         }
246     }
247     for (unsigned int it = 0; it < indexList.size(); it++) {
248         entry_free( indexList[ it]);
249     }
250     // and ... just in case we missed something ... 
251     osg::Group* group = globals->get_scenery()->get_terrain_branch();
252     group->removeChildren(0, group->getNumChildren());
253 }
254
255
256 /**
257  * Create a new tile and schedule it for loading.
258  */
259 bool FGNewCache::insert_tile( FGTileEntry *e ) {
260     // set time of insertion for tracking age of tiles...
261     e->set_timestamp(globals->get_sim_time_sec());
262
263     // register it in the cache
264     long tile_index = e->get_tile_bucket().gen_index();
265     tile_cache[tile_index] = e;
266
267     return true;
268 }
269
270
271 // Note this is the old version of FGNewCache::make_space(), currently disabled
272 // It uses distance from a center point to determine tiles to be discarded...
273 #if 0
274 // Ensure at least one entry is free in the cache
275 bool FGNewCache::make_space() {
276     SG_LOG( SG_TERRAIN, SG_DEBUG, "Make space in cache" );
277     SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
278     SG_LOG( SG_TERRAIN, SG_DEBUG, "max size = " << max_cache_size );
279
280     if ( (int)tile_cache.size() < max_cache_size ) {
281         // space in the cache, return
282         return true;
283     }
284
285     while ( (int)tile_cache.size() >= max_cache_size ) {
286         sgdVec3 abs_view_pos;
287         float dist;
288         float max_dist = 0.0;
289         int max_index = -1;
290
291         // we need to free the furthest entry
292         tile_map_iterator current = tile_cache.begin();
293         tile_map_iterator end = tile_cache.end();
294     
295         for ( ; current != end; ++current ) {
296             long index = current->first;
297             FGTileEntry *e = current->second;
298
299             if ( e->is_loaded() && (e->get_pending_models() == 0) ) {
300                 // calculate approximate distance from view point
301                 sgdCopyVec3( abs_view_pos,
302                              globals->get_current_view()->get_absolute_view_pos() );
303
304                 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = " 
305                         << abs_view_pos[0] << ","
306                         << abs_view_pos[1] << ","
307                         << abs_view_pos[2] );
308                 SG_LOG( SG_TERRAIN, SG_DEBUG,
309                         "    ref point = " << e->center );
310
311                 sgdVec3 center;
312                 sgdSetVec3( center,
313                             e->center.x(), e->center.y(), e->center.z() );
314                 dist = sgdDistanceVec3( center, abs_view_pos );
315
316                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
317
318                 if ( dist > max_dist ) {
319                     max_dist = dist;
320                     max_index = index;
321                 }
322             } else {
323                 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
324                         << " pending models = " << e->get_pending_models() );
325             }
326         }
327
328         // If we made it this far, then there were no open cache entries.
329         // We will instead free the furthest cache entry and return true
330         
331         SG_LOG( SG_TERRAIN, SG_INFO, "    max_dist = " << max_dist );
332         SG_LOG( SG_TERRAIN, SG_INFO, "    index = " << max_index );
333         if ( max_index >= 0 ) {
334             entry_free( max_index );
335             return true;
336         } else {
337             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
338                     "cache is full, but no entries available for removal." );
339             return false;
340         }
341     }
342
343     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
344             "FGNewCache::make_space()." );
345     return false;
346 }
347 #endif
348
349