]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TileCache.cxx
Improved tile cache priority scheme.
[simgear.git] / simgear / scene / tgdb / TileCache.cxx
1 // TileCache.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 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/bucket/newbucket.hxx>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include "TileEntry.hxx"
32 #include "TileCache.hxx"
33
34 using simgear::TileEntry;
35 using simgear::TileCache;
36
37 TileCache::TileCache( void ) :
38     max_cache_size(100), current_time(0.0)
39 {
40     tile_cache.clear();
41 }
42
43
44 TileCache::~TileCache( void ) {
45     clear_cache();
46 }
47
48
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 );
55
56     delete tile;
57 }
58
59
60 // Initialize the tile cache subsystem
61 void TileCache::init( void ) {
62     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
63
64     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = "
65             << max_cache_size );
66     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = "
67             << tile_cache.size() );
68
69 #if 0 // don't clear the cache
70     clear_cache();
71 #endif
72
73     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
74 }
75
76
77 // Search for the specified "bucket" in the cache
78 bool TileCache::exists( const SGBucket& b ) const {
79     long tile_index = b.gen_index();
80     const_tile_map_iterator it = tile_cache.find( tile_index );
81
82     return ( it != tile_cache.end() );
83 }
84
85
86 // Return the index of a tile to be dropped from the cache, return -1 if
87 // nothing available to be removed.
88 long TileCache::get_drop_tile() {
89     long min_index = -1;
90     double min_time = DBL_MAX;
91     float priority = FLT_MAX;
92
93     tile_map_iterator current = tile_cache.begin();
94     tile_map_iterator end = tile_cache.end();
95
96     for ( ; current != end; ++current ) {
97         long index = current->first;
98         TileEntry *e = current->second;
99         if (( !e->is_current_view() )&&
100             ( e->is_expired(current_time) ))
101         {
102             if (e->is_expired(current_time - 1.0)&&
103                 !e->is_loaded())
104             {
105                 /* Immediately drop "empty" tiles which are no longer used/requested, and were last requested > 1 second ago...
106                  * Allow a 1 second timeout since an empty tiles may just be loaded...
107                  */
108                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    dropping an unused and empty tile");
109                 break;
110             }
111             if (( e->get_time_expired() < min_time )||
112                 (( e->get_time_expired() == min_time)&&
113                  ( priority > e->get_priority())))
114             {
115                 // drop oldest tile with lowest priority
116                 min_time = e->get_time_expired();
117                 priority = e->get_priority();
118                 min_index = index;
119             }
120         }
121     }
122
123     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
124     SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
125
126     return min_index;
127 }
128
129
130 // Clear all flags indicating tiles belonging to the current view
131 void TileCache::clear_current_view()
132 {
133     tile_map_iterator current = tile_cache.begin();
134     tile_map_iterator end = tile_cache.end();
135
136     for ( ; current != end; ++current ) {
137         TileEntry *e = current->second;
138         if (e->is_current_view())
139         {
140             // update expiry time for tiles belonging to most recent position
141             e->update_time_expired( current_time );
142             e->set_current_view( false );
143         }
144     }
145 }
146
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 TileCache::clear_entry( long cache_index ) {
150     tile_cache.erase( cache_index );
151 }
152
153
154 // Clear all completely loaded tiles (ignores partially loaded tiles)
155 void TileCache::clear_cache() {
156     std::vector<long> indexList;
157     tile_map_iterator current = tile_cache.begin();
158     tile_map_iterator end = tile_cache.end();
159
160     for ( ; current != end; ++current ) {
161         long index = current->first;
162         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
163         TileEntry *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);
168         }
169     }
170     for (unsigned int it = 0; it < indexList.size(); it++) {
171         entry_free( indexList[ it]);
172     }
173 }
174
175 /**
176  * Create a new tile and schedule it for loading.
177  */
178 bool TileCache::insert_tile( TileEntry *e ) {
179     // register tile in the cache
180     long tile_index = e->get_tile_bucket().gen_index();
181     tile_cache[tile_index] = e;
182     e->update_time_expired(current_time);
183
184     return true;
185 }
186
187 // update tile's priority and expiry time according to current request
188 void TileCache::request_tile(TileEntry* t,float priority,bool current_view,double request_time)
189 {
190     if ((!current_view)&&(request_time<=0.0))
191         return;
192
193     // update priority when hire - or old has expired
194     if ((t->is_expired(current_time))||
195          (priority > t->get_priority()))
196     {
197         t->set_priority( priority );
198     }
199
200     if (current_view)
201     {
202         t->update_time_expired( current_time );
203         t->set_current_view( true );
204     }
205     else
206     {
207         t->update_time_expired( current_time+request_time );
208     }
209 }