ResourceManager.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Types.h"
  23. #include "ResourceManager.h"
  24. #include "String.h"
  25. #include "Hash.h"
  26. #include "Path.h"
  27. #include "Log.h"
  28. #include <algorithm>
  29. #include "TextResource.h"
  30. #include "TextureResource.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. ResourceManager::ResourceManager(ResourceArchive& archive, Allocator& allocator) :
  35. m_resource_archive(archive),
  36. m_resource_allocator(allocator),
  37. m_resources(m_allocator),
  38. m_loading_queue(m_allocator),
  39. m_loaded_queue(m_allocator),
  40. m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
  41. {
  42. // FIXME hardcoded seed
  43. m_config_hash = hash::murmur2_32("config", string::strlen("config"), 0);
  44. m_texture_hash = hash::murmur2_32("tga", string::strlen("tga"), 0);
  45. m_mesh_hash = hash::murmur2_32("mesh", string::strlen("mesh"), 0);
  46. m_txt_hash = hash::murmur2_32("txt", 3, 0);
  47. }
  48. //-----------------------------------------------------------------------------
  49. ResourceManager::~ResourceManager()
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. ResourceId ResourceManager::load(const char* name)
  54. {
  55. char basename[512];
  56. char extension[512];
  57. path::filename_without_extension(name, basename, 512);
  58. path::extension(name, extension, 512);
  59. // FIXME hardcoded seed
  60. uint32_t name_hash = hash::murmur2_32(basename, string::strlen(basename), 0);
  61. uint32_t type_hash = hash::murmur2_32(extension, string::strlen(extension), 0);
  62. return load(name_hash, type_hash);
  63. }
  64. //-----------------------------------------------------------------------------
  65. ResourceId ResourceManager::load(uint32_t name, uint32_t type)
  66. {
  67. // Search for an already existent resource
  68. ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), name);
  69. // If resource not found, create a new one
  70. if (entry == m_resources.end())
  71. {
  72. ResourceId id;
  73. id.name = name;
  74. id.type = type;
  75. id.index = m_resources.size();
  76. ResourceEntry entry;
  77. entry.id = id;
  78. entry.state = RS_UNLOADED;
  79. entry.references = 1;
  80. entry.resource = NULL;
  81. m_resources.push_back(entry);
  82. m_loading_mutex.lock();
  83. m_loading_queue.push_back(id);
  84. m_loading_mutex.unlock();
  85. return id;
  86. }
  87. // Else, increment its reference count
  88. entry->references++;
  89. return entry->id;
  90. }
  91. //-----------------------------------------------------------------------------
  92. void ResourceManager::unload(ResourceId name)
  93. {
  94. assert(has(name));
  95. ResourceEntry& entry = m_resources[name.index];
  96. entry.references--;
  97. if (entry.references == 0 && entry.state == RS_LOADED)
  98. {
  99. //m_resource_loader.unload(name, entry.resource);
  100. entry.state = RS_UNLOADED;
  101. entry.resource = NULL;
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. void ResourceManager::reload(ResourceId name)
  106. {
  107. assert(has(name));
  108. ResourceEntry& entry = m_resources[name.index];
  109. if (entry.state == RS_LOADED)
  110. {
  111. // FIXME
  112. }
  113. }
  114. //-----------------------------------------------------------------------------
  115. bool ResourceManager::has(ResourceId name) const
  116. {
  117. if (m_resources.size() > name.index)
  118. {
  119. return (m_resources[name.index].id.name == name.name);
  120. }
  121. return false;
  122. }
  123. //-----------------------------------------------------------------------------
  124. const void* ResourceManager::data(ResourceId name) const
  125. {
  126. assert(has(name));
  127. return m_resources[name.index].resource;
  128. }
  129. //-----------------------------------------------------------------------------
  130. bool ResourceManager::is_loaded(ResourceId name) const
  131. {
  132. assert(has(name));
  133. return m_resources[name.index].state == RS_LOADED;
  134. }
  135. //-----------------------------------------------------------------------------
  136. uint32_t ResourceManager::references(ResourceId name) const
  137. {
  138. assert(has(name));
  139. return m_resources[name.index].references;
  140. }
  141. //-----------------------------------------------------------------------------
  142. void ResourceManager::flush_load_queue()
  143. {
  144. m_loading_mutex.lock();
  145. if (m_loading_queue.size() > 0)
  146. {
  147. m_loading_requests.signal();
  148. }
  149. m_loading_mutex.unlock();
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ResourceManager::bring_loaded_online()
  153. {
  154. m_loaded_mutex.lock();
  155. // Update master table and bring online
  156. while (m_loaded_queue.size() > 0)
  157. {
  158. LoadedResource lr = m_loaded_queue.front();
  159. m_loaded_queue.pop_front();
  160. online(lr.resource, lr.data);
  161. }
  162. m_loaded_mutex.unlock();
  163. }
  164. //-----------------------------------------------------------------------------
  165. void ResourceManager::background_load()
  166. {
  167. // FIXME: Maybe epic crash because of concurrent access to the same allocator?
  168. while (true)
  169. {
  170. m_loading_mutex.lock();
  171. m_loading_requests.wait(m_loading_mutex);
  172. ResourceId resource = m_loading_queue.front();
  173. m_loading_queue.pop_front();
  174. m_loading_mutex.unlock();
  175. void* data = load_by_type(resource);
  176. LoadedResource lr;
  177. lr.resource = resource;
  178. lr.data = data;
  179. m_loaded_mutex.lock();
  180. m_loaded_queue.push_back(lr);
  181. m_loaded_mutex.unlock();
  182. }
  183. }
  184. //-----------------------------------------------------------------------------
  185. void* ResourceManager::load_by_type(ResourceId name) const
  186. {
  187. if (name.type == m_config_hash)
  188. {
  189. return NULL;
  190. }
  191. else if (name.type == m_texture_hash)
  192. {
  193. return TextureResource::load(m_resource_allocator, m_resource_archive, name);
  194. }
  195. else if (name.type == m_txt_hash)
  196. {
  197. return TextResource::load(m_resource_allocator, m_resource_archive, name);
  198. }
  199. return NULL;
  200. }
  201. //-----------------------------------------------------------------------------
  202. void ResourceManager::unload_by_type(ResourceId name, void* resource) const
  203. {
  204. if (name.type == m_config_hash)
  205. {
  206. return;
  207. }
  208. if (name.type == m_texture_hash)
  209. {
  210. TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
  211. }
  212. if (name.type == m_txt_hash)
  213. {
  214. TextResource::unload(m_resource_allocator, (TextResource*)resource);
  215. }
  216. return;
  217. }
  218. //-----------------------------------------------------------------------------
  219. void ResourceManager::online(ResourceId name, void* resource)
  220. {
  221. ResourceEntry& entry = m_resources[name.index];
  222. // FIXME hardcoded seed
  223. if (name.type == hash::murmur2_32("tga", 3, 0))
  224. {
  225. TextureResource::online((TextureResource*)resource);
  226. }
  227. entry.resource = resource;
  228. entry.state = RS_LOADED;
  229. }
  230. //-----------------------------------------------------------------------------
  231. void* ResourceManager::background_thread(void* thiz)
  232. {
  233. ResourceManager* mgr = (ResourceManager*)thiz;
  234. mgr->background_load();
  235. }
  236. } // namespace crown