Panda3D
Loading...
Searching...
No Matches
bamCache.h
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.h
10 * @author drose
11 * @date 2006-06-09
12 */
13
14#ifndef BAMCACHE_H
15#define BAMCACHE_H
16
17#include "pandabase.h"
18#include "bamCacheRecord.h"
19#include "pointerTo.h"
20#include "filename.h"
21#include "pmap.h"
22#include "pvector.h"
23#include "reMutex.h"
24#include "reMutexHolder.h"
25
26#include <time.h>
27
28class BamCacheIndex;
29
30/**
31 * This class maintains a cache of Bam and/or Txo objects generated from model
32 * files and texture images (as well as possibly other kinds of loadable
33 * objects that can be stored in bam file format).
34 *
35 * This class also maintains a persistent index that lists all of the cached
36 * objects (see BamCacheIndex). We go through some considerable effort to make
37 * sure this index gets saved correctly to disk, even in the presence of
38 * multiple different processes writing to the same index, and without relying
39 * too heavily on low-level os-provided file locks (which work poorly with C++
40 * iostreams).
41 */
42class EXPCL_PANDA_PUTIL BamCache {
43PUBLISHED:
44 BamCache();
45 ~BamCache();
46
47 INLINE void set_active(bool flag);
48 INLINE bool get_active() const;
49
50 INLINE void set_cache_models(bool flag);
51 INLINE bool get_cache_models() const;
52
53 INLINE void set_cache_textures(bool flag);
54 INLINE bool get_cache_textures() const;
55
56 INLINE void set_cache_compressed_textures(bool flag);
57 INLINE bool get_cache_compressed_textures() const;
58
59 INLINE void set_cache_compiled_shaders(bool flag);
60 INLINE bool get_cache_compiled_shaders() const;
61
62 void set_root(const Filename &root);
63 INLINE Filename get_root() const;
64
65 INLINE void set_flush_time(int flush_time);
66 INLINE int get_flush_time() const;
67
68 INLINE void set_cache_max_kbytes(int max_kbytes);
69 INLINE int get_cache_max_kbytes() const;
70
71 INLINE void set_read_only(bool ro);
72 INLINE bool get_read_only() const;
73
74 PT(BamCacheRecord) lookup(const Filename &source_filename,
75 const std::string &cache_extension);
76 bool store(BamCacheRecord *record);
77
79 void flush_index();
80
81 void list_index(std::ostream &out, int indent_level = 0) const;
82
83 INLINE static BamCache *get_global_ptr();
84 INLINE static void consider_flush_global_index();
85 INLINE static void flush_global_index();
86
87PUBLISHED:
88 MAKE_PROPERTY(active, get_active, set_active);
89 MAKE_PROPERTY(cache_models, get_cache_models, set_cache_models);
90 MAKE_PROPERTY(cache_textures, get_cache_textures, set_cache_textures);
91 MAKE_PROPERTY(cache_compressed_textures, get_cache_compressed_textures,
93 MAKE_PROPERTY(cache_compiled_shaders, get_cache_compiled_shaders,
95 MAKE_PROPERTY(root, get_root, set_root);
96 MAKE_PROPERTY(flush_time, get_flush_time, set_flush_time);
97 MAKE_PROPERTY(cache_max_kbytes, get_cache_max_kbytes, set_cache_max_kbytes);
98 MAKE_PROPERTY(read_only, get_read_only, set_read_only);
99
100private:
101 void read_index();
102 bool read_index_pathname(Filename &index_pathname,
103 std::string &index_ref_contents) const;
104 void merge_index(BamCacheIndex *new_index);
105 void rebuild_index();
106 INLINE void mark_index_stale();
107
108 void add_to_index(const BamCacheRecord *record);
109 void remove_from_index(const Filename &source_filename);
110
111 void check_cache_size();
112
113 void emergency_read_only();
114
115 static BamCacheIndex *do_read_index(const Filename &index_pathname);
116 static bool do_write_index(const Filename &index_pathname, const BamCacheIndex *index);
117
118 PT(BamCacheRecord) find_and_read_record(const Filename &source_pathname,
119 const Filename &cache_filename);
120 PT(BamCacheRecord) read_record(const Filename &source_pathname,
121 const Filename &cache_filename,
122 int pass);
123 static PT(BamCacheRecord) do_read_record(const Filename &cache_pathname,
124 bool read_data);
125
126 static std::string hash_filename(const std::string &filename);
127 static void make_global();
128
129 bool _active;
130 bool _cache_models;
131 bool _cache_textures;
132 bool _cache_compressed_textures;
133 bool _cache_compiled_shaders;
134 bool _read_only;
135 Filename _root;
136 int _flush_time;
137 int _max_kbytes;
138 static BamCache *_global_ptr;
139
140 BamCacheIndex *_index;
141 time_t _index_stale_since;
142
143 Filename _index_pathname;
144 std::string _index_ref_contents;
145
146 ReMutex _lock;
147};
148
149#include "bamCache.I"
150
151#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This represents the in-memory index that records the list of files stored in the BamCache.
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
static void consider_flush_global_index()
If there is a global BamCache object, calls consider_flush_index() on it.
Definition bamCache.I:234
get_active
Returns true if the BamCache is currently active, false if it is not.
Definition bamCache.h:88
get_cache_models
Returns 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
void consider_flush_index()
Flushes the index if enough time has elapsed since the index was last flushed.
Definition bamCache.cxx:323
static void flush_global_index()
If there is a global BamCache object, calls flush_index() on it.
Definition bamCache.I:244
get_cache_textures
Returns whether texture files (e.g.
Definition bamCache.h:90
get_root
Returns the current root pathname of the cache.
Definition bamCache.h:95
void list_index(std::ostream &out, int indent_level=0) const
Writes the contents of the index to standard output.
Definition bamCache.cxx:400
bool store(BamCacheRecord *record)
Flushes a cache entry to disk.
Definition bamCache.cxx:194
set_flush_time
Specifies the time in seconds between automatic flushes of the cache index.
Definition bamCache.h:96
get_cache_max_kbytes
Returns the maximum size, in kilobytes, which the cache is allowed to grow to.
Definition bamCache.h:97
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 flush_index()
Ensures the index is written to disk.
Definition bamCache.cxx:348
set_active
Changes the state of the active flag.
Definition bamCache.h:88
set_cache_compiled_shaders
Indicates whether compiled shader programs will be stored in the cache, as binary ....
Definition bamCache.h:94
get_flush_time
Returns the time in seconds between automatic flushes of the cache index.
Definition bamCache.h:96
get_cache_compressed_textures
Returns whether compressed texture files will be stored in the cache, as compressed txo files.
Definition bamCache.h:92
set_cache_max_kbytes
Specifies the maximum size, in kilobytes, which the cache is allowed to grow to.
Definition bamCache.h:97
set_cache_models
Indicates whether model files (e.g.
Definition bamCache.h:89
set_cache_textures
Indicates whether texture files will be stored in the cache, as uncompressed txo files.
Definition bamCache.h:90
get_cache_compiled_shaders
Returns whether compiled shader programs will be stored in the cache, as binary .txo files.
Definition bamCache.h:94
set_root
Changes the current root pathname of the cache.
Definition bamCache.h:95
set_cache_compressed_textures
Indicates whether compressed texture files will be stored in the cache, as compressed txo files.
Definition bamCache.h:92
get_read_only
Returns true if the cache is in read-only mode.
Definition bamCache.h:98
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
A reentrant mutex.
Definition reMutex.h:34
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.