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