]> git.tdb.fi Git - libs/gl.git/blob - source/resourcemanager.h
Add orthographic mode to Camera
[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                 enum State
30                 {
31                         NOT_LOADED,
32                         LOAD_QUEUED,
33                         LOADING,
34                         LOAD_FINISHED,
35                         LOADED
36                 };
37
38                 Resource *resource;
39                 DataFile::Collection *collection;
40                 std::string name;
41                 IO::Seekable *io;
42                 Resource::AsyncLoader *loader;
43                 State state;
44                 unsigned last_used;
45                 UInt64 data_size;
46                 std::vector<ResourceWatcher *> watchers;
47
48                 ManagedResource(Resource &);
49
50                 void start_loading();
51                 bool process(bool);
52                 void finish_loading(bool);
53                 void finish_loading();
54                 void unload();
55
56                 void add_watcher(ResourceWatcher &);
57                 void remove_watcher(ResourceWatcher &);
58         };
59
60         typedef std::list<ManagedResource *> LoadQueue;
61
62         class LoadingThread: public Thread
63         {
64         private:
65                 Semaphore sem;
66                 Mutex queue_mutex;
67                 LoadQueue async_queue;
68                 LoadQueue sync_queue;
69                 unsigned capacity;
70                 unsigned size;
71                 volatile bool done;
72
73         public:
74                 LoadingThread();
75
76         private:
77                 virtual void main();
78
79                 ManagedResource *front(LoadQueue &);
80
81         public:
82                 void add_resource(ManagedResource &);
83                 void remove_resource(ManagedResource &);
84         private:
85                 bool try_remove_resource(ManagedResource &);
86         public:
87                 bool sync();
88                 bool needs_work() const { return size<capacity; }
89
90                 void terminate();
91         };
92
93         typedef std::map<const Resource *, ManagedResource> ResourceMap;
94
95         LoadingPolicy policy;
96         bool async_loads;
97         ResourceMap resources;
98         LoadQueue queue;
99         UInt64 size_limit;
100         unsigned frame;
101         unsigned min_retain_frames;
102         unsigned max_retain_frames;
103         unsigned next_unload;
104         LoadingThread thread;
105
106 public:
107         ResourceManager();
108         ~ResourceManager();
109
110         void set_loading_policy(LoadingPolicy);
111         void set_async_loads(bool);
112         void set_size_limit(UInt64);
113         void set_min_retain_frames(unsigned);
114         void set_max_retain_frames(unsigned);
115
116         void add_resource(Resource &);
117         void *get_data_for_resource(const Resource &);
118         void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
119         void load_resource(Resource &);
120         void resource_used(const Resource &);
121         void remove_resource(Resource &);
122
123         void watch_resource(const Resource &, ResourceWatcher &);
124         void unwatch_resource(const Resource &, ResourceWatcher &);
125
126         void tick();
127 private:
128         void dispatch_work();
129         void unload_by_age();
130         void unload_by_size();
131 public:
132         UInt64 get_total_data_size() const;
133
134 private:
135         static bool age_order(ManagedResource *, ManagedResource *);
136 };
137
138 } // namespace GL
139 } // namespace Msp
140
141 #endif