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