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