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