|
|
@@ -40,19 +40,17 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "TextureResource.h"
|
|
|
#include "MeshResource.h"
|
|
|
#include "SoundResource.h"
|
|
|
+#include "TempAllocator.h"
|
|
|
+#include "List.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_loader(bundle),
|
|
|
m_seed(0),
|
|
|
- m_background_thread_should_run(true),
|
|
|
- m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
|
|
|
+ m_resources(m_allocator)
|
|
|
{
|
|
|
DiskFile* seed_file = device()->filesystem()->open("seed.ini", FOM_READ);
|
|
|
TextReader reader(*seed_file);
|
|
|
@@ -63,12 +61,14 @@ ResourceManager::ResourceManager(Bundle& bundle) :
|
|
|
device()->filesystem()->close(seed_file);
|
|
|
|
|
|
sscanf(tmp_buf, "%u", &m_seed);
|
|
|
+
|
|
|
+ m_loader.start();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
ResourceManager::~ResourceManager()
|
|
|
{
|
|
|
- m_background_thread_should_run = false;
|
|
|
+ m_loader.stop();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -90,8 +90,6 @@ ResourceId ResourceManager::load(const char* name)
|
|
|
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];
|
|
|
|
|
|
@@ -99,69 +97,30 @@ void ResourceManager::unload(ResourceId name)
|
|
|
|
|
|
if (entry.references == 0 && entry.state == RS_LOADED)
|
|
|
{
|
|
|
- unload_by_type(name, entry.resource);
|
|
|
+ m_loader.unload(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);
|
|
|
+ return (m_resources[name.index].id.name == name.name);
|
|
|
}
|
|
|
|
|
|
- m_resources_mutex.unlock();
|
|
|
-
|
|
|
- return has_resource;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
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;
|
|
|
+ return m_resources[name.index].resource;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -169,13 +128,7 @@ 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;
|
|
|
+ return m_resources[name.index].state == RS_LOADED;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -183,50 +136,15 @@ 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;
|
|
|
+ return m_resources[name.index].references;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
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();
|
|
|
+ while (m_loader.remaining() > 0) ;
|
|
|
|
|
|
- return;
|
|
|
- }
|
|
|
+ check_load_queue();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -238,30 +156,17 @@ uint32_t ResourceManager::seed() const
|
|
|
//-----------------------------------------------------------------------------
|
|
|
void ResourceManager::check_load_queue()
|
|
|
{
|
|
|
- m_loading_mutex.lock();
|
|
|
-
|
|
|
- if (m_loading_queue.size() > 0)
|
|
|
+ if (m_loader.num_loaded() != 0)
|
|
|
{
|
|
|
- m_loading_requests.signal();
|
|
|
- }
|
|
|
-
|
|
|
- m_loading_mutex.unlock();
|
|
|
-}
|
|
|
+ TempAllocator1024 alloc;
|
|
|
+ List<LoadedResource> loaded(alloc);
|
|
|
+ m_loader.get_loaded(loaded);
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-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);
|
|
|
+ for (uint32_t i = 0; i < loaded.size(); i++)
|
|
|
+ {
|
|
|
+ online(loaded[i].resource, loaded[i].data);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- m_loaded_mutex.unlock();
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
@@ -288,9 +193,8 @@ ResourceId ResourceManager::load(uint32_t name, uint32_t type)
|
|
|
|
|
|
m_resources.push_back(entry);
|
|
|
|
|
|
- m_loading_mutex.lock();
|
|
|
- m_loading_queue.push_back(id);
|
|
|
- m_loading_mutex.unlock();
|
|
|
+ // Issue request to resource loader
|
|
|
+ m_loader.load(id);
|
|
|
|
|
|
return id;
|
|
|
}
|
|
|
@@ -301,66 +205,6 @@ ResourceId ResourceManager::load(uint32_t name, uint32_t type)
|
|
|
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)
|
|
|
{
|
|
|
@@ -383,21 +227,9 @@ void ResourceManager::online(ResourceId name, void* resource)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- 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
|