]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
- Some refactoring of the configure.ac script.
[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  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <plib/ssg.h>           // plib include
29
30 #include <simgear/bucket/newbucket.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/misc/sg_path.hxx>
33
34 #include <Main/globals.hxx>
35 #include <Main/viewer.hxx>
36 #include <Scenery/scenery.hxx>  // for scenery.center
37
38 #include "newcache.hxx"
39 #include "tileentry.hxx"
40
41
42 SG_USING_NAMESPACE(std);
43
44
45 // Constructor
46 FGNewCache::FGNewCache( void ) :
47     max_cache_size(100)
48 {
49     tile_cache.clear();
50 }
51
52
53 // Destructor
54 FGNewCache::~FGNewCache( void ) {
55 }
56
57
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();
63
64 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
65     tile->sched_removal();
66 #else
67     tile->free_tile();
68     delete tile;
69 #endif
70
71     tile_cache.erase( cache_index );
72 }
73
74
75 // Initialize the tile cache subsystem
76 void FGNewCache::init( void ) {
77     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
78
79     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = " 
80             << max_cache_size );
81     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = " 
82             << tile_cache.size() );
83
84 #if 0 // don't clear the cache
85     clear_cache();
86 #endif
87
88     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
89 }
90
91
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 );
96
97     return ( it != tile_cache.end() );
98 }
99
100
101 #if 0
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 );
107
108     if ( (int)tile_cache.size() < max_cache_size ) {
109         // space in the cache, return
110         return true;
111     }
112
113     while ( (int)tile_cache.size() >= max_cache_size ) {
114         sgdVec3 abs_view_pos;
115         float dist;
116         double timestamp = 0.0;
117         int max_index = -1;
118         double min_time = 2419200000.0f;  // one month should be enough
119         double max_time = 0;
120
121         // we need to free the furthest entry
122         tile_map_iterator current = tile_cache.begin();
123         tile_map_iterator end = tile_cache.end();
124     
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) ) {
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 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 );
214 }
215
216
217 // Clear all completely loaded tiles (ignores partially loaded tiles)
218 void FGNewCache::clear_cache() {
219
220     tile_map_iterator current = tile_cache.begin();
221     tile_map_iterator end = tile_cache.end();
222     
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();
229             entry_free(index);
230         }
231     }
232
233     // and ... just in case we missed something ... 
234     globals->get_scenery()->get_terrain_branch()->removeAllKids();
235 }
236
237
238 /**
239  * Create a new tile and schedule it for loading.
240  */
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());
244
245     // register it in the cache
246     long tile_index = e->get_tile_bucket().gen_index();
247     tile_cache[tile_index] = e;
248
249     return true;
250 }
251
252
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...
255 #if 0
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 );
261
262     if ( (int)tile_cache.size() < max_cache_size ) {
263         // space in the cache, return
264         return true;
265     }
266
267     while ( (int)tile_cache.size() >= max_cache_size ) {
268         sgdVec3 abs_view_pos;
269         float dist;
270         float max_dist = 0.0;
271         int max_index = -1;
272
273         // we need to free the furthest entry
274         tile_map_iterator current = tile_cache.begin();
275         tile_map_iterator end = tile_cache.end();
276     
277         for ( ; current != end; ++current ) {
278             long index = current->first;
279             FGTileEntry *e = current->second;
280
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() );
285
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 );
292
293                 sgdVec3 center;
294                 sgdSetVec3( center,
295                             e->center.x(), e->center.y(), e->center.z() );
296                 dist = sgdDistanceVec3( center, abs_view_pos );
297
298                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    distance = " << dist );
299
300                 if ( dist > max_dist ) {
301                     max_dist = dist;
302                     max_index = index;
303                 }
304             } else {
305                 SG_LOG( SG_TERRAIN, SG_INFO, "loaded = " << e->is_loaded()
306                         << " pending models = " << e->get_pending_models() );
307             }
308         }
309
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
312         
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 );
317             return true;
318         } else {
319             SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! can't make_space(), tile "
320                     "cache is full, but no entries available for removal." );
321             return false;
322         }
323     }
324
325     SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Hit an unhandled condition in  "
326             "FGNewCache::make_space()." );
327     return false;
328 }
329 #endif
330
331