1 #ifndef MSP_GL_RESOURCEMANAGER_H_
2 #define MSP_GL_RESOURCEMANAGER_H_
4 #include <msp/core/inttypes.h>
5 #include <msp/core/mutex.h>
6 #include <msp/core/semaphore.h>
7 #include <msp/core/thread.h>
8 #include <msp/datafile/collection.h>
14 class ResourceWatcher;
16 class resource_load_error: public std::runtime_error
19 resource_load_error(const std::string &, const std::string &);
20 resource_load_error(const std::string &, const std::exception &);
21 virtual ~resource_load_error() throw() { }
35 struct ResourceLocation
37 DataFile::Collection *collection;
41 ResourceLocation(DataFile::Collection &, const std::string &);
45 struct ManagedResource
58 ResourceLocation location;
60 Resource::AsyncLoader *loader;
64 std::vector<ResourceWatcher *> watchers;
66 ManagedResource(Resource &);
70 void finish_loading(bool);
71 void finish_loading();
74 void add_watcher(ResourceWatcher &);
75 void remove_watcher(ResourceWatcher &);
78 typedef std::list<ManagedResource *> LoadQueue;
80 class LoadingThread: public Thread
85 LoadQueue async_queue;
89 std::list<resource_load_error> error_queue;
98 ManagedResource *front(LoadQueue &);
101 void add_resource(ManagedResource &);
102 void remove_resource(ManagedResource &);
104 bool try_remove_resource(ManagedResource &);
107 bool needs_work() const { return size<capacity; }
112 typedef std::map<const Resource *, ManagedResource> ResourceMap;
114 LoadingPolicy policy;
116 ResourceMap resources;
120 unsigned min_retain_frames;
121 unsigned max_retain_frames;
122 unsigned next_unload;
123 LoadingThread thread;
129 void set_loading_policy(LoadingPolicy);
130 void set_async_loads(bool);
131 void set_size_limit(UInt64);
132 void set_min_retain_frames(unsigned);
133 void set_max_retain_frames(unsigned);
135 void add_resource(Resource &);
136 void *get_data_for_resource(const Resource &);
137 void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
138 void set_resource_location(Resource &, const ResourceLocation &);
139 const ResourceLocation *get_resource_location(const Resource &) const;
140 void load_resource(Resource &);
141 bool is_resource_loaded(const Resource &) const;
142 void resource_used(const Resource &);
143 void remove_resource(Resource &);
145 void watch_resource(const Resource &, ResourceWatcher &);
146 void unwatch_resource(const Resource &, ResourceWatcher &);
150 void dispatch_work();
151 void unload_by_age();
152 void unload_by_size();
154 UInt64 get_total_data_size() const;
157 static bool age_order(ManagedResource *, ManagedResource *);