From 47abe7c9e1633ca65f910a4db340724117a6f6e5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 7 Sep 2014 20:16:36 +0300 Subject: [PATCH] Implement manual loading policy and async flag --- source/resource.cpp | 5 +++ source/resource.h | 2 ++ source/resourcemanager.cpp | 66 ++++++++++++++++++++++++++++++-------- source/resourcemanager.h | 8 ++++- source/texture.cpp | 6 ++-- 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/source/resource.cpp b/source/resource.cpp index c2cf94d5..e63341c8 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -20,7 +20,12 @@ void Resource::set_manager(ResourceManager *m) manager->remove_resource(*this); manager = m; if(manager) + { manager->add_resource(*this); + manager_data = manager->get_data_for_resource(*this); + } + else + manager_data = 0; } } // namespace GL diff --git a/source/resource.h b/source/resource.h index c41b8849..625c9f93 100644 --- a/source/resource.h +++ b/source/resource.h @@ -24,12 +24,14 @@ public: protected: ResourceManager *manager; + void *manager_data; Resource(); public: virtual ~Resource(); void set_manager(ResourceManager *); + void *get_manager_data() const { return manager_data; } virtual AsyncLoader *load(IO::Seekable &) = 0; virtual void unload() = 0; }; diff --git a/source/resourcemanager.cpp b/source/resourcemanager.cpp index b778175e..cc69b895 100644 --- a/source/resourcemanager.cpp +++ b/source/resourcemanager.cpp @@ -20,11 +20,26 @@ ResourceManager::~ResourceManager() i->second.resource->set_manager(0); } +void ResourceManager::set_loading_policy(LoadingPolicy p) +{ + policy = p; +} + +void ResourceManager::set_async_loads(bool a) +{ + async_loads = a; +} + void ResourceManager::add_resource(Resource &r) { insert_unique(resources, &r, ManagedResource(r)); } +void *ResourceManager::get_data_for_resource(const Resource &r) +{ + return &get_item(resources, &r); +} + void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c, const string &n) { ManagedResource &managed = get_item(resources, &r); @@ -35,7 +50,7 @@ void ResourceManager::set_resource_location(Resource &r, DataFile::Collection &c load_resource(r); } -void ResourceManager::load_resource(const Resource &r) +void ResourceManager::load_resource(Resource &r) { ManagedResource &managed = get_item(resources, &r); if(!managed.collection) @@ -44,16 +59,22 @@ void ResourceManager::load_resource(const Resource &r) if(managed.loader) return; - managed.io = managed.collection->open_raw(managed.name); - managed.loader = managed.resource->load(*managed.io); - if(!managed.loader) + managed.start_loading(); + + if(async_loads) + queue.push_back(&managed); + else { - delete managed.io; - managed.io = 0; - throw logic_error("no loader created"); + while(!managed.loader->process()) ; + managed.finish_loading(); } +} - queue.push_back(&managed); +void ResourceManager::resource_used(const Resource &r) +{ + ManagedResource *managed = reinterpret_cast(r.get_manager_data()); + if(!managed->loaded && !managed->loader && policy!=LOAD_MANUALLY) + load_resource(*managed->resource); } void ResourceManager::remove_resource(Resource &r) @@ -83,9 +104,31 @@ ResourceManager::ManagedResource::ManagedResource(Resource &r): resource(&r), collection(0), io(0), - loader(0) + loader(0), + loaded(false) { } +void ResourceManager::ManagedResource::start_loading() +{ + io = collection->open_raw(name); + loader = resource->load(*io); + if(!loader) + { + delete io; + io = 0; + throw logic_error("no loader created"); + } +} + +void ResourceManager::ManagedResource::finish_loading() +{ + delete loader; + loader = 0; + loaded = true; + delete io; + io = 0; +} + ResourceManager::LoadingThread::LoadingThread(ResourceManager &m): manager(m), @@ -152,10 +195,7 @@ void ResourceManager::LoadingThread::sync() if(finished) { - delete resource->loader; - resource->loader = 0; - delete resource->io; - resource->io = 0; + resource->finish_loading(); resource = 0; state = IDLE; } diff --git a/source/resourcemanager.h b/source/resourcemanager.h index 49a4c0bd..1e3cb54f 100644 --- a/source/resourcemanager.h +++ b/source/resourcemanager.h @@ -28,8 +28,12 @@ private: std::string name; IO::Seekable *io; Resource::AsyncLoader *loader; + bool loaded; ManagedResource(Resource &); + + void start_loading(); + void finish_loading(); }; class LoadingThread: public Thread @@ -82,8 +86,10 @@ public: void set_async_loads(bool); 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 &); + void resource_used(const Resource &); void remove_resource(Resource &); void tick(); diff --git a/source/texture.cpp b/source/texture.cpp index a23e57c7..aec1bc92 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -178,13 +178,13 @@ void Texture::set_compare_func(Predicate f) void Texture::bind_to(unsigned i) const { - if(!id) - manager->load_resource(*this); - TexUnit &unit = TexUnit::get_unit(i); const Texture *cur = unit.get_texture(); if(unit.set_texture(this)) { + if(manager) + manager->resource_used(*this); + unit.bind(); if(cur && cur->target!=target) glDisable(cur->target); -- 2.45.2