]> git.tdb.fi Git - libs/gl.git/blob - source/resourcemanager.h
Implement manual loading policy and async flag
[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
33                 ManagedResource(Resource &);
34
35                 void start_loading();
36                 void finish_loading();
37         };
38
39         class LoadingThread: public Thread
40         {
41         public:
42                 enum State
43                 {
44                         IDLE,
45                         SYNC_PENDING,
46                         BUSY,
47                         LOAD_FINISHED,
48                         TERMINATING
49                 };
50
51         private:
52                 ResourceManager &manager;
53                 Semaphore sem;
54                 ManagedResource *volatile resource;
55                 volatile State state;
56
57         public:
58                 LoadingThread(ResourceManager &);
59
60         private:
61                 virtual void main();
62
63         public:
64                 void set_resource(ManagedResource *);
65                 ManagedResource *get_resource() const { return resource; }
66                 void sync();
67                 State get_state() const { return state; }
68
69                 void terminate();
70         };
71
72         typedef std::map<const Resource *, ManagedResource> ResourceMap;
73         typedef std::list<ManagedResource *> LoadQueue;
74
75         LoadingPolicy policy;
76         bool async_loads;
77         ResourceMap resources;
78         LoadQueue queue;
79         LoadingThread thread;
80
81 public:
82         ResourceManager();
83         ~ResourceManager();
84
85         void set_loading_policy(LoadingPolicy);
86         void set_async_loads(bool);
87
88         void add_resource(Resource &);
89         void *get_data_for_resource(const Resource &);
90         void set_resource_location(Resource &, DataFile::Collection &, const std::string &);
91         void load_resource(Resource &);
92         void resource_used(const Resource &);
93         void remove_resource(Resource &);
94
95         void tick();
96 };
97
98 } // namespace GL
99 } // namespace Msp
100
101 #endif