| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <algorithm>
- #include <cstdio>
- #include "Types.h"
- #include "ResourceManager.h"
- #include "StringUtils.h"
- #include "Hash.h"
- #include "Path.h"
- #include "Log.h"
- #include "Device.h"
- #include "Filesystem.h"
- #include "TextReader.h"
- #include "DiskFile.h"
- #include "TextureResource.h"
- #include "MeshResource.h"
- #include "SoundResource.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- ResourceManager::ResourceManager(Bundle& bundle) :
- m_resource_bundle(bundle),
- m_resources(m_allocator),
- m_loading_queue(m_allocator),
- m_loaded_queue(m_allocator),
- m_seed(0),
- m_background_thread_should_run(true),
- m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
- {
- DiskFile* seed_file = device()->filesystem()->open("seed.ini", FOM_READ);
- TextReader reader(*seed_file);
- char tmp_buf[32];
- reader.read_string(tmp_buf, 32);
- device()->filesystem()->close(seed_file);
- sscanf(tmp_buf, "%u", &m_seed);
- }
- //-----------------------------------------------------------------------------
- ResourceManager::~ResourceManager()
- {
- m_background_thread_should_run = false;
- }
- //-----------------------------------------------------------------------------
- ResourceId ResourceManager::load(const char* name)
- {
- char basename[512];
- char extension[512];
- path::filename_without_extension(name, basename, 512);
- path::extension(name, extension, 512);
- uint32_t name_hash = hash::murmur2_32(basename, string::strlen(basename), m_seed);
- uint32_t type_hash = hash::murmur2_32(extension, string::strlen(extension), 0);
- return load(name_hash, type_hash);
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::unload(ResourceId name)
- {
- CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-
- m_resources_mutex.lock();
- ResourceEntry& entry = m_resources[name.index];
-
- entry.references--;
-
- if (entry.references == 0 && entry.state == RS_LOADED)
- {
- unload_by_type(name, entry.resource);
- entry.state = RS_UNLOADED;
- entry.resource = NULL;
- }
- m_resources_mutex.unlock();
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::reload(ResourceId name)
- {
- CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-
- m_resources_mutex.lock();
- ResourceEntry& entry = m_resources[name.index];
-
- if (entry.state == RS_LOADED)
- {
- unload_by_type(name, entry.resource);
- entry.state = RS_UNLOADED;
- entry.resource = NULL;
- entry.resource = load_by_type(name);
- entry.state = RS_LOADED;
- }
- m_resources_mutex.unlock();
- }
- //-----------------------------------------------------------------------------
- bool ResourceManager::has(ResourceId name) const
- {
- bool has_resource = false;
- m_resources_mutex.lock();
- if (m_resources.size() > name.index)
- {
- has_resource = (m_resources[name.index].id.name == name.name);
- }
- m_resources_mutex.unlock();
-
- return has_resource;
- }
- //-----------------------------------------------------------------------------
- const void* ResourceManager::data(ResourceId name) const
- {
- CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-
- m_resources_mutex.lock();
- void* resource = m_resources[name.index].resource;
- m_resources_mutex.unlock();
- return resource;
- }
- //-----------------------------------------------------------------------------
- bool ResourceManager::is_loaded(ResourceId name) const
- {
- CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
- m_resources_mutex.lock();
- bool loaded = m_resources[name.index].state == RS_LOADED;
- m_resources_mutex.unlock();
- return loaded;
- }
- //-----------------------------------------------------------------------------
- uint32_t ResourceManager::references(ResourceId name) const
- {
- CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
- m_resources_mutex.lock();
- bool loaded = m_resources[name.index].references;
- m_resources_mutex.unlock();
- return loaded;
- }
- //-----------------------------------------------------------------------------
- uint32_t ResourceManager::remaining() const
- {
- uint32_t count = 0;
- m_loading_mutex.lock();
- count = m_loading_queue.size();
- m_loading_mutex.unlock();
- return count;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::flush()
- {
- check_load_queue();
- while (true)
- {
- // Wait for all the resources to be loaded
- // by the background thread
- m_loading_mutex.lock();
- while (m_loading_queue.size() > 0)
- {
- m_all_loaded.wait(m_loading_mutex);
- }
- m_loading_mutex.unlock();
- // When all loaded, bring them online
- bring_loaded_online();
- return;
- }
- }
- //-----------------------------------------------------------------------------
- uint32_t ResourceManager::seed() const
- {
- return m_seed;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::check_load_queue()
- {
- m_loading_mutex.lock();
- if (m_loading_queue.size() > 0)
- {
- m_loading_requests.signal();
- }
- m_loading_mutex.unlock();
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::bring_loaded_online()
- {
- m_loaded_mutex.lock();
- while (m_loaded_queue.size() > 0)
- {
- LoadedResource lr = m_loaded_queue.front();
- m_loaded_queue.pop_front();
- online(lr.resource, lr.data);
- }
- m_loaded_mutex.unlock();
- }
- //-----------------------------------------------------------------------------
- ResourceId ResourceManager::load(uint32_t name, uint32_t type)
- {
- ResourceId id;
- id.name = name;
- id.type = type;
- // Search for an already existent resource
- ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
- // If resource not found, create a new one
- if (entry == m_resources.end())
- {
- id.index = m_resources.size();
- ResourceEntry entry;
- entry.id = id;
- entry.state = RS_UNLOADED;
- entry.references = 1;
- entry.resource = NULL;
- m_resources.push_back(entry);
- m_loading_mutex.lock();
- m_loading_queue.push_back(id);
- m_loading_mutex.unlock();
- return id;
- }
- // Else, increment its reference count
- entry->references++;
-
- return entry->id;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::background_load()
- {
- while (m_background_thread_should_run)
- {
- m_loading_mutex.lock();
- while (m_loading_queue.size() == 0)
- {
- m_loading_requests.wait(m_loading_mutex);
- }
- ResourceId resource = m_loading_queue.front();
- m_loading_queue.pop_front();
- m_loading_mutex.unlock();
- void* data = load_by_type(resource);
- LoadedResource lr;
- lr.resource = resource;
- lr.data = data;
- m_loaded_mutex.lock();
- m_loaded_queue.push_back(lr);
- m_loaded_mutex.unlock();
- m_loading_mutex.lock();
- if (m_loading_queue.size() == 0)
- {
- m_all_loaded.signal();
- }
- m_loading_mutex.unlock();
- }
- }
- //-----------------------------------------------------------------------------
- void* ResourceManager::load_by_type(ResourceId name)
- {
- switch (name.type)
- {
- case TEXTURE_TYPE: return TextureResource::load(m_resource_allocator, m_resource_bundle, name);
- case MESH_TYPE: return MeshResource::load(m_resource_allocator, m_resource_bundle, name);
- case SOUND_TYPE: return SoundResource::load(m_resource_allocator, m_resource_bundle, name);
- default: return NULL;
- }
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::unload_by_type(ResourceId name, void* resource)
- {
- switch (name.type)
- {
- case TEXTURE_TYPE: return TextureResource::unload(m_resource_allocator, resource);
- case MESH_TYPE: return MeshResource::unload(m_resource_allocator, resource);
- case SOUND_TYPE: return SoundResource::unload(m_resource_allocator, resource);
- }
- return;
- }
- //-----------------------------------------------------------------------------
- void ResourceManager::online(ResourceId name, void* resource)
- {
- switch (name.type)
- {
- case TEXTURE_TYPE:
- {
- TextureResource::online(resource);
- break;
- }
- case MESH_TYPE:
- {
- MeshResource::online(resource);
- break;
- }
- case SOUND_TYPE:
- {
- SoundResource::online(resource);
- break;
- }
- }
- m_resources_mutex.lock();
- ResourceEntry& entry = m_resources[name.index];
- entry.resource = resource;
- entry.state = RS_LOADED;
- m_resources_mutex.unlock();
- }
- //-----------------------------------------------------------------------------
- void* ResourceManager::background_thread(void* thiz)
- {
- ((ResourceManager*)thiz)->background_load();
- return NULL;
- }
- } // namespace crown
|