Panda3D
 All Classes Functions Variables Enumerations
bamCache.I
1 // Filename: bamCache.I
2 // Created by: drose (09Jun06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: BamCache::set_active
18 // Access: Published
19 // Description: Changes the state of the active flag. "active" means
20 // that the cache should be consulted automatically on
21 // loads, "not active" means that objects should be
22 // loaded directly without consulting the cache.
23 //
24 // This represents the global flag. Also see the
25 // individual cache_models, cache_textures,
26 // cache_compressed_textures flags.
27 ////////////////////////////////////////////////////////////////////
28 INLINE void BamCache::
29 set_active(bool active) {
30  ReMutexHolder holder(_lock);
31  _active = active;
32 }
33 
34 ////////////////////////////////////////////////////////////////////
35 // Function: BamCache::get_active
36 // Access: Published
37 // Description: Returns true if the BamCache is currently active,
38 // false if it is not. "active" means that the cache
39 // should be consulted automatically on loads, "not
40 // active" means that objects should be loaded directly
41 // without consulting the cache.
42 //
43 // This represents the global flag. Also see the
44 // individual cache_models, cache_textures,
45 // cache_compressed_textures flags.
46 ////////////////////////////////////////////////////////////////////
47 INLINE bool BamCache::
48 get_active() const {
49  ReMutexHolder holder(_lock);
50  return _active;
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: BamCache::set_cache_models
55 // Access: Published
56 // Description: Indicates whether model files (e.g. egg files and bam
57 // files) will be stored in the cache, as bam files.
58 ////////////////////////////////////////////////////////////////////
59 INLINE void BamCache::
60 set_cache_models(bool flag) {
61  ReMutexHolder holder(_lock);
62  _cache_models = flag;
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: BamCache::get_cache_models
67 // Access: Published
68 // Description: Returns whether model files (e.g. egg files and bam
69 // files) will be stored in the cache, as bam files.
70 //
71 // This also returns false if get_active() is false.
72 ////////////////////////////////////////////////////////////////////
73 INLINE bool BamCache::
75  ReMutexHolder holder(_lock);
76  return _cache_models && _active;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: BamCache::set_cache_textures
81 // Access: Published
82 // Description: Indicates whether texture files will be stored in the
83 // cache, as uncompressed txo files.
84 ////////////////////////////////////////////////////////////////////
85 INLINE void BamCache::
86 set_cache_textures(bool flag) {
87  ReMutexHolder holder(_lock);
88  _cache_textures = flag;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: BamCache::get_cache_textures
93 // Access: Published
94 // Description: Returns whether texture files (e.g. egg files and bam
95 // files) will be stored in the cache, as txo files.
96 //
97 // This also returns false if get_active() is false.
98 ////////////////////////////////////////////////////////////////////
99 INLINE bool BamCache::
101  ReMutexHolder holder(_lock);
102  return _cache_textures && _active;
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: BamCache::set_cache_compressed_textures
107 // Access: Published
108 // Description: Indicates whether compressed texture files will be
109 // stored in the cache, as compressed txo files. The
110 // compressed data may either be generated in-CPU, via
111 // the squish library, or it may be extracted from the
112 // GSG after the texture has been loaded.
113 //
114 // This may be set in conjunction with
115 // set_cache_textures(), or independently of it. If
116 // set_cache_textures() is true and this is false, all
117 // textures will be cached in their uncompressed form.
118 // If set_cache_textures() is false and this is true,
119 // only compressed textures will be cached, and they
120 // will be cached in their compressed form. If both are
121 // true, all textures will be cached, in their
122 // uncompressed or compressed form appropriately.
123 ////////////////////////////////////////////////////////////////////
124 INLINE void BamCache::
126  ReMutexHolder holder(_lock);
127  _cache_compressed_textures = flag;
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: BamCache::get_cache_compressed_textures
132 // Access: Published
133 // Description: Returns whether compressed texture files will be
134 // stored in the cache, as compressed txo files. See
135 // set_cache_compressed_textures().
136 //
137 // This also returns false if get_active() is false.
138 ////////////////////////////////////////////////////////////////////
139 INLINE bool BamCache::
141  ReMutexHolder holder(_lock);
142  return _cache_compressed_textures && _active;
143 }
144 
145 ////////////////////////////////////////////////////////////////////
146 // Function: BamCache::get_root
147 // Access: Published
148 // Description: Returns the current root pathname of the cache. See
149 // set_root().
150 ////////////////////////////////////////////////////////////////////
151 INLINE Filename BamCache::
152 get_root() const {
153  ReMutexHolder holder(_lock);
154  return _root;
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: BamCache::set_flush_time
159 // Access: Published
160 // Description: Specifies the time in seconds between automatic
161 // flushes of the cache index.
162 ////////////////////////////////////////////////////////////////////
163 INLINE void BamCache::
164 set_flush_time(int flush_time) {
165  ReMutexHolder holder(_lock);
166  _flush_time = flush_time;
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: BamCache::get_flush_time
171 // Access: Published
172 // Description: Returns the time in seconds between automatic
173 // flushes of the cache index.
174 ////////////////////////////////////////////////////////////////////
175 INLINE int BamCache::
176 get_flush_time() const {
177  ReMutexHolder holder(_lock);
178  return _flush_time;
179 }
180 
181 ////////////////////////////////////////////////////////////////////
182 // Function: BamCache::set_cache_max_kbytes
183 // Access: Published
184 // Description: Specifies the maximum size, in kilobytes, which the
185 // cache is allowed to grow to. If a newly cached file
186 // would exceed this size, an older file is removed from
187 // the cache.
188 //
189 // Note that in the case of multiple different processes
190 // simultaneously operating on the same cache directory,
191 // the actual cache size may slightly exceed this value
192 // from time to time due to latency in checking between
193 // the processes.
194 ////////////////////////////////////////////////////////////////////
195 INLINE void BamCache::
196 set_cache_max_kbytes(int max_kbytes) {
197  ReMutexHolder holder(_lock);
198  _max_kbytes = max_kbytes;
199  check_cache_size();
200 }
201 
202 ////////////////////////////////////////////////////////////////////
203 // Function: BamCache::get_cache_max_kbytes
204 // Access: Published
205 // Description: Returns the maximum size, in kilobytes, which the
206 // cache is allowed to grow to. See
207 // set_cache_max_kbytes().
208 ////////////////////////////////////////////////////////////////////
209 INLINE int BamCache::
211  ReMutexHolder holder(_lock);
212  return _max_kbytes;
213 }
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: BamCache::set_read_only
217 // Access: Published
218 // Description: Can be used to put the cache in read-only mode,
219 // or take it out of read-only mode. Note that if you
220 // put it into read-write mode, and it discovers that
221 // it does not have write access, it will put itself
222 // right back into read-only mode.
223 ////////////////////////////////////////////////////////////////////
224 INLINE void BamCache::
225 set_read_only(bool ro) {
226  ReMutexHolder holder(_lock);
227  _read_only = ro;
228 }
229 
230 ////////////////////////////////////////////////////////////////////
231 // Function: BamCache::get_read_only
232 // Access: Published
233 // Description: Returns true if the cache is in read-only mode.
234 // Normally, the cache starts in read-write mode. It
235 // can put itself into read-only mode automatically if
236 // it discovers that it does not have write access to
237 // the cache.
238 ////////////////////////////////////////////////////////////////////
239 INLINE bool BamCache::
240 get_read_only() const {
241  ReMutexHolder holder(_lock);
242  return _read_only;
243 }
244 
245 ////////////////////////////////////////////////////////////////////
246 // Function: BamCache::get_global_ptr
247 // Access: Published, Static
248 // Description: Returns a pointer to the global BamCache object,
249 // which is used automatically by the ModelPool and
250 // TexturePool.
251 ////////////////////////////////////////////////////////////////////
252 INLINE BamCache *BamCache::
254  if (_global_ptr == (BamCache *)NULL) {
255  make_global();
256  }
257  return _global_ptr;
258 }
259 
260 ////////////////////////////////////////////////////////////////////
261 // Function: BamCache::consider_flush_global_index
262 // Access: Published, Static
263 // Description: If there is a global BamCache object, calls
264 // consider_flush_index() on it.
265 ////////////////////////////////////////////////////////////////////
266 INLINE void BamCache::
268  if (_global_ptr != (BamCache *)NULL) {
269  _global_ptr->consider_flush_index();
270  }
271 }
272 
273 ////////////////////////////////////////////////////////////////////
274 // Function: BamCache::flush_global_index
275 // Access: Published, Static
276 // Description: If there is a global BamCache object, calls
277 // flush_index() on it.
278 ////////////////////////////////////////////////////////////////////
279 INLINE void BamCache::
281  if (_global_ptr != (BamCache *)NULL) {
282  _global_ptr->flush_index();
283  }
284 }
285 
286 ////////////////////////////////////////////////////////////////////
287 // Function: BamCache::mark_index_stale
288 // Access: Private
289 // Description: Indicates that the index has been modified and will
290 // need to be written to disk eventually.
291 ////////////////////////////////////////////////////////////////////
292 INLINE void BamCache::
293 mark_index_stale() {
294  if (_index_stale_since == 0) {
295  _index_stale_since = time(NULL);
296  }
297 }
static void flush_global_index()
If there is a global BamCache object, calls flush_index() on it.
Definition: bamCache.I:280
void set_active(bool flag)
Changes the state of the active flag.
Definition: bamCache.I:29
bool get_active() const
Returns true if the BamCache is currently active, false if it is not.
Definition: bamCache.I:48
Filename get_root() const
Returns the current root pathname of the cache.
Definition: bamCache.I:152
This class maintains a cache of Bam and/or Txo objects generated from model files and texture images ...
Definition: bamCache.h:47
static void consider_flush_global_index()
If there is a global BamCache object, calls consider_flush_index() on it.
Definition: bamCache.I:267
void set_cache_models(bool flag)
Indicates whether model files (e.g.
Definition: bamCache.I:60
void set_cache_compressed_textures(bool flag)
Indicates whether compressed texture files will be stored in the cache, as compressed txo files...
Definition: bamCache.I:125
void set_cache_textures(bool flag)
Indicates whether texture files will be stored in the cache, as uncompressed txo files.
Definition: bamCache.I:86
void flush_index()
Ensures the index is written to disk.
Definition: bamCache.cxx:339
int get_flush_time() const
Returns the time in seconds between automatic flushes of the cache index.
Definition: bamCache.I:176
bool get_cache_textures() const
Returns whether texture files (e.g.
Definition: bamCache.I:100
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
int get_cache_max_kbytes() const
Returns the maximum size, in kilobytes, which the cache is allowed to grow to.
Definition: bamCache.I:210
Similar to MutexHolder, but for a reentrant mutex.
Definition: reMutexHolder.h:27
bool get_cache_compressed_textures() const
Returns whether compressed texture files will be stored in the cache, as compressed txo files...
Definition: bamCache.I:140
bool get_read_only() const
Returns true if the cache is in read-only mode.
Definition: bamCache.I:240
bool get_cache_models() const
Returns whether model files (e.g.
Definition: bamCache.I:74
void set_flush_time(int flush_time)
Specifies the time in seconds between automatic flushes of the cache index.
Definition: bamCache.I:164
void set_cache_max_kbytes(int max_kbytes)
Specifies the maximum size, in kilobytes, which the cache is allowed to grow to.
Definition: bamCache.I:196
void set_read_only(bool ro)
Can be used to put the cache in read-only mode, or take it out of read-only mode. ...
Definition: bamCache.I:225
static BamCache * get_global_ptr()
Returns a pointer to the global BamCache object, which is used automatically by the ModelPool and Tex...
Definition: bamCache.I:253
void consider_flush_index()
Flushes the index if enough time has elapsed since the index was last flushed.
Definition: bamCache.cxx:323