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::exception &);
20 virtual ~resource_load_error() throw() { }
34 struct ResourceLocation
36 DataFile::Collection *collection;
40 ResourceLocation(DataFile::Collection &, const std::string &);
44 struct ManagedResource
57 ResourceLocation location;
59 Resource::AsyncLoader *loader;
63 std::vector<ResourceWatcher *> watchers;
65 ManagedResource(Resource &);
69 void finish_loading(bool);
70 void finish_loading();
73 void add_watcher(ResourceWatcher &);
74 void remove_watcher(ResourceWatcher &);
77 typedef std::list<ManagedResource *> LoadQueue;
79 class LoadingThread: public Thread
84 LoadQueue async_queue;
88 std::list<resource_load_error> error_queue;
97 ManagedResource *front(LoadQueue &);
100 void add_resource(ManagedResource &);
101 void remove_resource(ManagedResource &);
103 bool try_remove_resource(ManagedResource &);
106 bool needs_work() const { return size<capacity; }
111 typedef std::map<const Resource *, ManagedResource> ResourceMap;
113 LoadingPolicy policy;
115 ResourceMap resources;
119 unsigned min_retain_frames;
120 unsigned max_retain_frames;
121 unsigned next_unload;
122 LoadingThread thread;
128 void set_loading_policy(LoadingPolicy);
129 void set_async_loads(bool);
130 void set_size_limit(UInt64);
131 void set_min_retain_frames(unsigned);
132 void set_max_retain_frames(unsigned);
134 void add_resource(Resource &);
135 void *get_data_for_resource(const Resource &);
136 void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
137 void set_resource_location(Resource &, const ResourceLocation &);
138 const ResourceLocation *get_resource_location(const Resource &) const;
139 void load_resource(Resource &);
140 bool is_resource_loaded(const Resource &) const;
141 void resource_used(const Resource &);
142 void remove_resource(Resource &);
144 void watch_resource(const Resource &, ResourceWatcher &);
145 void unwatch_resource(const Resource &, ResourceWatcher &);
149 void dispatch_work();
150 void unload_by_age();
151 void unload_by_size();
153 UInt64 get_total_data_size() const;
156 static bool age_order(ManagedResource *, ManagedResource *);