]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TileCache.cxx
Remove using std:: from the metar header, remove HTTP support, add very basic unit...
[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 tile_index ) {
51     SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING CACHE ENTRY = " << tile_index );
52     TileEntry *tile = tile_cache[tile_index];
53     tile->removeFromSceneGraph();
54     tile_cache.erase( tile_index );
55     delete tile;
56 }
57
58
59 // Initialize the tile cache subsystem
60 void TileCache::init( void ) {
61     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
62
63     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = "
64             << max_cache_size );
65     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = "
66             << tile_cache.size() );
67
68     clear_cache();
69
70     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
71 }
72
73
74 // Search for the specified "bucket" in the cache
75 bool TileCache::exists( const SGBucket& b ) const {
76     long tile_index = b.gen_index();
77     const_tile_map_iterator it = tile_cache.find( tile_index );
78
79     return ( it != tile_cache.end() );
80 }
81
82
83 // Return the index of a tile to be dropped from the cache, return -1 if
84 // nothing available to be removed.
85 long TileCache::get_drop_tile() {
86     long min_index = -1;
87     double min_time = DBL_MAX;
88     float priority = FLT_MAX;
89
90     tile_map_iterator current = tile_cache.begin();
91     tile_map_iterator end = tile_cache.end();
92
93     for ( ; current != end; ++current ) {
94         long index = current->first;
95         TileEntry *e = current->second;
96         if (( !e->is_current_view() )&&
97             ( e->is_expired(current_time) ))
98         {
99             if (e->is_expired(current_time - 1.0)&&
100                 !e->is_loaded())
101             {
102                 /* Immediately drop "empty" tiles which are no longer used/requested, and were last requested > 1 second ago...
103                  * Allow a 1 second timeout since an empty tiles may just be loaded...
104                  */
105                 SG_LOG( SG_TERRAIN, SG_DEBUG, "    dropping an unused and empty tile");
106                 break;
107             }
108             if (( e->get_time_expired() < min_time )||
109                 (( e->get_time_expired() == min_time)&&
110                  ( priority > e->get_priority())))
111             {
112                 // drop oldest tile with lowest priority
113                 min_time = e->get_time_expired();
114                 priority = e->get_priority();
115                 min_index = index;
116             }
117         }
118     }
119
120     SG_LOG( SG_TERRAIN, SG_DEBUG, "    index = " << min_index );
121     SG_LOG( SG_TERRAIN, SG_DEBUG, "    min_time = " << min_time );
122
123     return min_index;
124 }
125
126
127 // Clear all flags indicating tiles belonging to the current view
128 void TileCache::clear_current_view()
129 {
130     tile_map_iterator current = tile_cache.begin();
131     tile_map_iterator end = tile_cache.end();
132
133     for ( ; current != end; ++current ) {
134         TileEntry *e = current->second;
135         if (e->is_current_view())
136         {
137             // update expiry time for tiles belonging to most recent position
138             e->update_time_expired( current_time );
139             e->set_current_view( false );
140         }
141     }
142 }
143
144 // Clear a cache entry, note that the cache only holds pointers
145 // and this does not free the object which is pointed to.
146 void TileCache::clear_entry( long tile_index ) {
147     tile_cache.erase( tile_index );
148 }
149
150
151 // Clear all completely loaded tiles (ignores partially loaded tiles)
152 void TileCache::clear_cache() {
153     std::vector<long> indexList;
154     tile_map_iterator current = tile_cache.begin();
155     tile_map_iterator end = tile_cache.end();
156
157     for ( ; current != end; ++current ) {
158         long index = current->first;
159         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
160         TileEntry *e = current->second;
161         if ( e->is_loaded() ) {
162             e->tile_bucket.make_bad();
163             // entry_free modifies tile_cache, so store index and call entry_free() later;
164             indexList.push_back( index);
165         }
166     }
167     for (unsigned int it = 0; it < indexList.size(); it++) {
168         entry_free( indexList[ it]);
169     }
170 }
171
172 /**
173  * Create a new tile and schedule it for loading.
174  */
175 bool TileCache::insert_tile( TileEntry *e ) {
176     // register tile in the cache
177     long tile_index = e->get_tile_bucket().gen_index();
178     tile_cache[tile_index] = e;
179     e->update_time_expired(current_time);
180
181     return true;
182 }
183
184 /**
185  * Reloads a tile when it's already in memory.
186  */
187 void TileCache::refresh_tile(long tile_index)
188 {
189     const_tile_map_iterator it = tile_cache.find( tile_index );
190     if ( it == tile_cache.end() )
191         return;
192
193     SG_LOG( SG_TERRAIN, SG_DEBUG, "REFRESHING CACHE ENTRY = " << tile_index );
194
195     if (it->second)
196         it->second->refresh();
197 }
198
199 // update tile's priority and expiry time according to current request
200 void TileCache::request_tile(TileEntry* t,float priority,bool current_view,double request_time)
201 {
202     if ((!current_view)&&(request_time<=0.0))
203         return;
204
205     // update priority when higher - or old request has expired
206     if ((t->is_expired(current_time))||
207          (priority > t->get_priority()))
208     {
209         t->set_priority( priority );
210     }
211
212     if (current_view)
213     {
214         t->update_time_expired( current_time );
215         t->set_current_view( true );
216     }
217     else
218     {
219         t->update_time_expired( current_time+request_time );
220     }
221 }