]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/TileCache.cxx
Thorsten Renk's fix for tile manager problems
[simgear.git] / simgear / scene / tgdb / TileCache.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 #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
55     delete tile;
56
57     tile_cache.erase( cache_index );
58 }
59
60
61 // Initialize the tile cache subsystem
62 void TileCache::init( void ) {
63     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
64
65     SG_LOG( SG_TERRAIN, SG_INFO, "  max cache size = "
66             << max_cache_size );
67     SG_LOG( SG_TERRAIN, SG_INFO, "  current cache size = "
68             << tile_cache.size() );
69
70 #if 0 // don't clear the cache
71     clear_cache();
72 #endif
73
74     SG_LOG( SG_TERRAIN, SG_INFO, "  done with init()"  );
75 }
76
77
78 // Search for the specified "bucket" in the cache
79 bool TileCache::exists( const SGBucket& b ) const {
80     long tile_index = b.gen_index();
81     const_tile_map_iterator it = tile_cache.find( tile_index );
82
83     return ( it != tile_cache.end() );
84 }
85
86
87 // Return the index of the oldest tile in the cache, return -1 if
88 // nothing available to be removed.
89 long TileCache::get_oldest_tile() {
90     // we need to free the furthest entry
91     long min_index = -1;
92     double timestamp = 0.0;
93     double min_time = DBL_MAX;
94
95     tile_map_iterator current = tile_cache.begin();
96     tile_map_iterator end = tile_cache.end();
97
98     for ( ; current != end; ++current ) {
99         long index = current->first;
100         TileEntry *e = current->second;
101         if (e->get_cache_lock())
102         {
103             // tile locked to cache => must not be dropped
104         }
105         else
106         if ( e->is_loaded() ) {
107             timestamp = e->get_timestamp();
108             if ( timestamp < min_time ) {
109                 min_time = timestamp;
110                 min_index = index;
111             }
112         } else {
113             SG_LOG( SG_TERRAIN, SG_DEBUG, "loaded = " << e->is_loaded()
114                     << " time stamp = " << e->get_timestamp() );
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 the inner ring flag for all tiles in the cache so that the
126 // external tile scheduler can flag the inner ring correctly.
127 void TileCache::clear_inner_ring_flags() {
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_loaded() ) {
134             e->set_inner_ring( false );
135         //}
136     }
137 }
138
139 // Clear all locked flags for all tiles in the cache.
140 // (Tiles belonging to the current position are locked to
141 //  the cache to prevent them from being dropped).
142 void TileCache::clear_cache_lock_flags()
143 {
144     tile_map_iterator current = tile_cache.begin();
145     tile_map_iterator end = tile_cache.end();
146
147     for ( ; current != end; ++current ) {
148         TileEntry *e = current->second;
149         if (e->get_cache_lock())
150         {
151             // update timestamps for tiles belonging to most recent position
152             e->set_timestamp( current_time );
153             e->set_cache_lock( false );
154         }
155     }
156 }
157
158 // Clear a cache entry, note that the cache only holds pointers
159 // and this does not free the object which is pointed to.
160 void TileCache::clear_entry( long cache_index ) {
161     tile_cache.erase( cache_index );
162 }
163
164
165 // Clear all completely loaded tiles (ignores partially loaded tiles)
166 void TileCache::clear_cache() {
167     std::vector<long> indexList;
168     tile_map_iterator current = tile_cache.begin();
169     tile_map_iterator end = tile_cache.end();
170
171     for ( ; current != end; ++current ) {
172         long index = current->first;
173         SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
174         TileEntry *e = current->second;
175         if ( e->is_loaded() ) {
176             e->tile_bucket.make_bad();
177             // entry_free modifies tile_cache, so store index and call entry_free() later;
178             indexList.push_back( index);
179         }
180     }
181     for (unsigned int it = 0; it < indexList.size(); it++) {
182         entry_free( indexList[ it]);
183     }
184 }
185
186 /**
187  * Create a new tile and schedule it for loading.
188  */
189 bool TileCache::insert_tile( TileEntry *e ) {
190     // register tile in the cache
191     long tile_index = e->get_tile_bucket().gen_index();
192     tile_cache[tile_index] = e;
193     e->set_timestamp(current_time);
194
195     return true;
196 }
197