X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fresourcemanager.h;h=9dcd9b997a56a25214ea691d27f2f616197ae55f;hp=afa2c2d073e45b32b25332a91a3be9c7c8568461;hb=HEAD;hpb=ab28eedc313d54a037b4d62bce0d3d243423dcf7 diff --git a/source/resourcemanager.h b/source/resourcemanager.h deleted file mode 100644 index afa2c2d0..00000000 --- a/source/resourcemanager.h +++ /dev/null @@ -1,173 +0,0 @@ -#ifndef MSP_GL_RESOURCEMANAGER_H_ -#define MSP_GL_RESOURCEMANAGER_H_ - -#include -#include -#include -#include -#include -#include "resource.h" - -namespace Msp { -namespace GL { - -class ResourceWatcher; - -class resource_load_error: public std::runtime_error -{ -public: - resource_load_error(const std::string &, const std::string &); - resource_load_error(const std::string &, const std::exception &); - virtual ~resource_load_error() throw() { } -}; - - -class ResourceManager -{ -public: - enum LoadingPolicy - { - LOAD_IMMEDIATELY, - LOAD_ON_DEMAND, - LOAD_MANUALLY - }; - - struct ResourceLocation - { - DataFile::Collection *collection; - std::string name; - - ResourceLocation(); - ResourceLocation(DataFile::Collection &, const std::string &); - }; - -private: - struct ManagedResource - { - enum State - { - NOT_LOADED, - LOAD_QUEUED, - LOADING, - LOAD_FINISHED, - LOADED, - LOAD_ERROR - }; - - Resource *resource; - ResourceLocation location; - bool load_priority; - 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 - { - private: - Semaphore sem; - Mutex queue_mutex; - LoadQueue async_queue; - LoadQueue sync_queue; - unsigned capacity; - unsigned size; - std::list error_queue; - Mutex data_size_mutex; - UInt64 loaded_data_size; - volatile bool done; - - public: - LoadingThread(); - - private: - virtual void main(); - - ManagedResource *front(LoadQueue &); - - public: - void add_resource(ManagedResource &); - void remove_resource(ManagedResource &); - private: - bool try_remove_resource(ManagedResource &); - public: - bool sync(); - bool needs_work() const { return size ResourceMap; - - LoadingPolicy policy; - bool async_loads; - mutable Mutex map_mutex; - ResourceMap resources; - LoadQueue queue; - UInt64 total_data_size; - UInt64 size_limit; - unsigned frame; - unsigned min_retain_frames; - unsigned max_retain_frames; - unsigned next_unload; - LoadingThread thread; - -public: - ResourceManager(); - ~ResourceManager(); - - 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 &); -private: - const ManagedResource &get_managed_resource(const Resource &) const; - ManagedResource &get_managed_resource(const Resource &); -public: - void *get_data_for_resource(const Resource &r) { return &get_managed_resource(r); } - void set_resource_location(Resource &, DataFile::Collection &, const std::string &); - void set_resource_location(Resource &, const ResourceLocation &); - const ResourceLocation *get_resource_location(const Resource &) const; - void load_resource(Resource &); - bool is_resource_loaded(const Resource &) const; - 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 { return total_data_size; } - -private: - static bool age_order(ManagedResource *, ManagedResource *); -}; - -} // namespace GL -} // namespace Msp - -#endif