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() { }
35 struct ManagedResource
48 DataFile::Collection *collection;
51 Resource::AsyncLoader *loader;
55 std::vector<ResourceWatcher *> watchers;
57 ManagedResource(Resource &);
61 void finish_loading(bool);
62 void finish_loading();
65 void add_watcher(ResourceWatcher &);
66 void remove_watcher(ResourceWatcher &);
69 typedef std::list<ManagedResource *> LoadQueue;
71 class LoadingThread: public Thread
76 LoadQueue async_queue;
80 std::list<resource_load_error> error_queue;
89 ManagedResource *front(LoadQueue &);
92 void add_resource(ManagedResource &);
93 void remove_resource(ManagedResource &);
95 bool try_remove_resource(ManagedResource &);
98 bool needs_work() const { return size<capacity; }
103 typedef std::map<const Resource *, ManagedResource> ResourceMap;
105 LoadingPolicy policy;
107 ResourceMap resources;
111 unsigned min_retain_frames;
112 unsigned max_retain_frames;
113 unsigned next_unload;
114 LoadingThread thread;
120 void set_loading_policy(LoadingPolicy);
121 void set_async_loads(bool);
122 void set_size_limit(UInt64);
123 void set_min_retain_frames(unsigned);
124 void set_max_retain_frames(unsigned);
126 void add_resource(Resource &);
127 void *get_data_for_resource(const Resource &);
128 void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
129 void load_resource(Resource &);
130 bool is_resource_loaded(const Resource &);
131 void resource_used(const Resource &);
132 void remove_resource(Resource &);
134 void watch_resource(const Resource &, ResourceWatcher &);
135 void unwatch_resource(const Resource &, ResourceWatcher &);
139 void dispatch_work();
140 void unload_by_age();
141 void unload_by_size();
143 UInt64 get_total_data_size() const;
146 static bool age_order(ManagedResource *, ManagedResource *);