]> git.tdb.fi Git - libs/gl.git/blob - source/resourcemanager.h
Add a ResourceWatcher interface
[libs/gl.git] / source / resourcemanager.h
1 #ifndef MSP_GL_RESOURCEMANAGER_H_
2 #define MSP_GL_RESOURCEMANAGER_H_
3
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>
9 #include "resource.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class ResourceWatcher;
15
16 class ResourceManager
17 {
18 public:
19         enum LoadingPolicy
20         {
21                 LOAD_IMMEDIATELY,
22                 LOAD_ON_DEMAND,
23                 LOAD_MANUALLY
24         };
25
26 private:
27         struct ManagedResource
28         {
29                 Resource *resource;
30                 DataFile::Collection *collection;
31                 std::string name;
32                 IO::Seekable *io;
33                 Resource::AsyncLoader *loader;
34                 bool loaded;
35                 unsigned last_used;
36                 UInt64 data_size;
37                 std::vector<ResourceWatcher *> watchers;
38
39                 ManagedResource(Resource &);
40
41                 void start_loading();
42                 void finish_loading();
43                 void unload();
44
45                 void add_watcher(ResourceWatcher &);
46                 void remove_watcher(ResourceWatcher &);
47         };
48
49         class LoadingThread: public Thread
50         {
51         public:
52                 enum State
53                 {
54                         IDLE,
55                         SYNC_PENDING,
56                         BUSY,
57                         LOAD_FINISHED,
58                         TERMINATING
59                 };
60
61         private:
62                 ResourceManager &manager;
63                 Semaphore sem;
64                 ManagedResource *volatile resource;
65                 volatile State state;
66
67         public:
68                 LoadingThread(ResourceManager &);
69
70         private:
71                 virtual void main();
72
73         public:
74                 void set_resource(ManagedResource *);
75                 ManagedResource *get_resource() const { return resource; }
76                 void sync();
77                 State get_state() const { return state; }
78
79                 void terminate();
80         };
81
82         typedef std::map<const Resource *, ManagedResource> ResourceMap;
83         typedef std::list<ManagedResource *> LoadQueue;
84
85         LoadingPolicy policy;
86         bool async_loads;
87         ResourceMap resources;
88         LoadQueue queue;
89         UInt64 size_limit;
90         unsigned frame;
91         unsigned min_retain_frames;
92         unsigned max_retain_frames;
93         unsigned next_unload;
94         LoadingThread thread;
95
96 public:
97         ResourceManager();
98         ~ResourceManager();
99
100         void set_loading_policy(LoadingPolicy);
101         void set_async_loads(bool);
102         void set_size_limit(UInt64);
103         void set_min_retain_frames(unsigned);
104         void set_max_retain_frames(unsigned);
105
106         void add_resource(Resource &);
107         void *get_data_for_resource(const Resource &);
108         void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
109         void load_resource(Resource &);
110         void resource_used(const Resource &);
111         void remove_resource(Resource &);
112
113         void watch_resource(const Resource &, ResourceWatcher &);
114         void unwatch_resource(const Resource &, ResourceWatcher &);
115
116         void tick();
117         UInt64 get_total_data_size() const;
118 };
119
120 } // namespace GL
121 } // namespace Msp
122
123 #endif