X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresourcemanager.h;h=e83a61328e590c528d1c05f7157dea0d523e995d;hb=89d3d10b75a42f86b224feb00b20283af66c0b01;hp=fb4353350f4c2138a1b4a4d1d541b08bf1016f61;hpb=7db48f2836ec42c6e2dbeff336634a03bc6d7a62;p=libs%2Fgl.git diff --git a/source/resourcemanager.h b/source/resourcemanager.h index fb435335..e83a6132 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -1,6 +1,7 @@ #ifndef MSP_GL_RESOURCEMANAGER_H_ #define MSP_GL_RESOURCEMANAGER_H_ +#include #include #include #include @@ -10,6 +11,16 @@ namespace Msp { namespace GL { +class ResourceWatcher; + +class resource_load_error: public std::runtime_error +{ +public: + resource_load_error(const std::string &, const std::exception &); + virtual ~resource_load_error() throw() { } +}; + + class ResourceManager { public: @@ -23,54 +34,83 @@ public: private: struct ManagedResource { + enum State + { + NOT_LOADED, + LOAD_QUEUED, + LOADING, + LOAD_FINISHED, + LOADED, + LOAD_ERROR + }; + Resource *resource; DataFile::Collection *collection; std::string name; IO::Seekable *io; Resource::AsyncLoader *loader; + State state; + unsigned last_used; + UInt64 data_size; + std::vector watchers; ManagedResource(Resource &); + + void start_loading(); + bool process(bool); + void finish_loading(bool); + void finish_loading(); + void unload(); + + void add_watcher(ResourceWatcher &); + void remove_watcher(ResourceWatcher &); }; + typedef std::list LoadQueue; + class LoadingThread: public Thread { - public: - enum State - { - IDLE, - SYNC_PENDING, - BUSY, - LOAD_FINISHED, - TERMINATING - }; - private: - ResourceManager &manager; Semaphore sem; - ManagedResource *volatile resource; - volatile State state; + Mutex queue_mutex; + LoadQueue async_queue; + LoadQueue sync_queue; + unsigned capacity; + unsigned size; + std::list error_queue; + volatile bool done; public: - LoadingThread(ResourceManager &); + LoadingThread(); private: virtual void main(); + ManagedResource *front(LoadQueue &); + public: - void set_resource(ManagedResource *); - ManagedResource *get_resource() const { return resource; } - void sync(); - State get_state() const { return state; } + void add_resource(ManagedResource &); + void remove_resource(ManagedResource &); + private: + bool try_remove_resource(ManagedResource &); + public: + bool sync(); + bool needs_work() const { return size LoadQueue; + typedef std::map ResourceMap; LoadingPolicy policy; bool async_loads; - std::map resources; - std::list queue; + ResourceMap resources; + LoadQueue queue; + UInt64 size_limit; + unsigned frame; + unsigned min_retain_frames; + unsigned max_retain_frames; + unsigned next_unload; LoadingThread thread; public: @@ -79,13 +119,31 @@ public: void set_loading_policy(LoadingPolicy); void set_async_loads(bool); + void set_size_limit(UInt64); + void set_min_retain_frames(unsigned); + void set_max_retain_frames(unsigned); void add_resource(Resource &); + void *get_data_for_resource(const Resource &); void set_resource_location(Resource &, DataFile::Collection &, const std::string &); - void load_resource(const Resource &); + void load_resource(Resource &); + bool is_resource_loaded(const Resource &); + void resource_used(const Resource &); void remove_resource(Resource &); + void watch_resource(const Resource &, ResourceWatcher &); + void unwatch_resource(const Resource &, ResourceWatcher &); + void tick(); +private: + void dispatch_work(); + void unload_by_age(); + void unload_by_size(); +public: + UInt64 get_total_data_size() const; + +private: + static bool age_order(ManagedResource *, ManagedResource *); }; } // namespace GL