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