ResourceManager.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 <algorithm>
  23. #include <cstdio>
  24. #include "Types.h"
  25. #include "ResourceManager.h"
  26. #include "String.h"
  27. #include "Hash.h"
  28. #include "Path.h"
  29. #include "Log.h"
  30. #include "Device.h"
  31. #include "Filesystem.h"
  32. #include "TextReader.h"
  33. #include "DiskFile.h"
  34. #include "TextResource.h"
  35. #include "TextureResource.h"
  36. namespace crown
  37. {
  38. //-----------------------------------------------------------------------------
  39. ResourceManager::ResourceManager(ResourceArchive& archive, Allocator& allocator) :
  40. m_resource_archive(archive),
  41. m_resource_allocator(allocator),
  42. m_resources(m_allocator),
  43. m_loading_queue(m_allocator),
  44. m_loaded_queue(m_allocator),
  45. m_seed(0),
  46. m_background_thread_should_run(true),
  47. m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
  48. {
  49. DiskFile* seed_file = device()->filesystem()->open("seed.ini", FOM_READ);
  50. TextReader reader(*seed_file);
  51. char tmp_buf[32];
  52. reader.read_string(tmp_buf, 32);
  53. device()->filesystem()->close(seed_file);
  54. sscanf(tmp_buf, "%u", &m_seed);
  55. }
  56. //-----------------------------------------------------------------------------
  57. ResourceManager::~ResourceManager()
  58. {
  59. m_background_thread_should_run = false;
  60. }
  61. //-----------------------------------------------------------------------------
  62. ResourceId ResourceManager::load(const char* name)
  63. {
  64. char basename[512];
  65. char extension[512];
  66. path::filename_without_extension(name, basename, 512);
  67. path::extension(name, extension, 512);
  68. uint32_t name_hash = hash::murmur2_32(basename, string::strlen(basename), m_seed);
  69. uint32_t type_hash = hash::murmur2_32(extension, string::strlen(extension), 0);
  70. return load(name_hash, type_hash);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void ResourceManager::unload(ResourceId name)
  74. {
  75. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  76. m_resources_mutex.lock();
  77. ResourceEntry& entry = m_resources[name.index];
  78. entry.references--;
  79. if (entry.references == 0 && entry.state == RS_LOADED)
  80. {
  81. unload_by_type(name, entry.resource);
  82. entry.state = RS_UNLOADED;
  83. entry.resource = NULL;
  84. }
  85. m_resources_mutex.unlock();
  86. }
  87. //-----------------------------------------------------------------------------
  88. void ResourceManager::reload(ResourceId name)
  89. {
  90. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  91. m_resources_mutex.lock();
  92. ResourceEntry& entry = m_resources[name.index];
  93. if (entry.state == RS_LOADED)
  94. {
  95. unload_by_type(name, entry.resource);
  96. entry.state = RS_UNLOADED;
  97. entry.resource = NULL;
  98. entry.resource = load_by_type(name);
  99. entry.state = RS_LOADED;
  100. }
  101. m_resources_mutex.unlock();
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool ResourceManager::has(ResourceId name) const
  105. {
  106. bool has_resource = false;
  107. m_resources_mutex.lock();
  108. if (m_resources.size() > name.index)
  109. {
  110. has_resource = (m_resources[name.index].id.name == name.name);
  111. }
  112. m_resources_mutex.unlock();
  113. return has_resource;
  114. }
  115. //-----------------------------------------------------------------------------
  116. const void* ResourceManager::data(ResourceId name) const
  117. {
  118. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  119. m_resources_mutex.lock();
  120. void* resource = m_resources[name.index].resource;
  121. m_resources_mutex.unlock();
  122. return resource;
  123. }
  124. //-----------------------------------------------------------------------------
  125. bool ResourceManager::is_loaded(ResourceId name) const
  126. {
  127. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  128. m_resources_mutex.lock();
  129. bool loaded = m_resources[name.index].state == RS_LOADED;
  130. m_resources_mutex.unlock();
  131. return loaded;
  132. }
  133. //-----------------------------------------------------------------------------
  134. uint32_t ResourceManager::references(ResourceId name) const
  135. {
  136. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  137. m_resources_mutex.lock();
  138. bool loaded = m_resources[name.index].references;
  139. m_resources_mutex.unlock();
  140. return loaded;
  141. }
  142. //-----------------------------------------------------------------------------
  143. uint32_t ResourceManager::remaining() const
  144. {
  145. uint32_t count = 0;
  146. m_loading_mutex.lock();
  147. count = m_loading_queue.size();
  148. m_loading_mutex.unlock();
  149. return count;
  150. }
  151. //-----------------------------------------------------------------------------
  152. void ResourceManager::flush()
  153. {
  154. check_load_queue();
  155. while (true)
  156. {
  157. // Wait for all the resources to be loaded
  158. // by the background thread
  159. m_loading_mutex.lock();
  160. while (m_loading_queue.size() > 0)
  161. {
  162. m_all_loaded.wait(m_loading_mutex);
  163. }
  164. m_loading_mutex.unlock();
  165. // When all loaded, bring them online
  166. bring_loaded_online();
  167. return;
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. uint32_t ResourceManager::seed() const
  172. {
  173. return m_seed;
  174. }
  175. //-----------------------------------------------------------------------------
  176. void ResourceManager::check_load_queue()
  177. {
  178. m_loading_mutex.lock();
  179. if (m_loading_queue.size() > 0)
  180. {
  181. m_loading_requests.signal();
  182. }
  183. m_loading_mutex.unlock();
  184. }
  185. //-----------------------------------------------------------------------------
  186. void ResourceManager::bring_loaded_online()
  187. {
  188. m_loaded_mutex.lock();
  189. while (m_loaded_queue.size() > 0)
  190. {
  191. LoadedResource lr = m_loaded_queue.front();
  192. m_loaded_queue.pop_front();
  193. online(lr.resource, lr.data);
  194. }
  195. m_loaded_mutex.unlock();
  196. }
  197. //-----------------------------------------------------------------------------
  198. ResourceId ResourceManager::load(uint32_t name, uint32_t type)
  199. {
  200. // Search for an already existent resource
  201. ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), name);
  202. // If resource not found, create a new one
  203. if (entry == m_resources.end())
  204. {
  205. ResourceId id;
  206. id.name = name;
  207. id.type = type;
  208. id.index = m_resources.size();
  209. ResourceEntry entry;
  210. entry.id = id;
  211. entry.state = RS_UNLOADED;
  212. entry.references = 1;
  213. entry.resource = NULL;
  214. m_resources.push_back(entry);
  215. m_loading_mutex.lock();
  216. m_loading_queue.push_back(id);
  217. m_loading_mutex.unlock();
  218. return id;
  219. }
  220. // Else, increment its reference count
  221. entry->references++;
  222. return entry->id;
  223. }
  224. //-----------------------------------------------------------------------------
  225. void ResourceManager::background_load()
  226. {
  227. while (m_background_thread_should_run)
  228. {
  229. m_loading_mutex.lock();
  230. while (m_loading_queue.size() == 0)
  231. {
  232. m_loading_requests.wait(m_loading_mutex);
  233. }
  234. ResourceId resource = m_loading_queue.front();
  235. m_loading_queue.pop_front();
  236. m_loading_mutex.unlock();
  237. void* data = load_by_type(resource);
  238. LoadedResource lr;
  239. lr.resource = resource;
  240. lr.data = data;
  241. m_loaded_mutex.lock();
  242. m_loaded_queue.push_back(lr);
  243. m_loaded_mutex.unlock();
  244. m_loading_mutex.lock();
  245. if (m_loading_queue.size() == 0)
  246. {
  247. m_all_loaded.signal();
  248. }
  249. m_loading_mutex.unlock();
  250. }
  251. }
  252. //-----------------------------------------------------------------------------
  253. void* ResourceManager::load_by_type(ResourceId name) const
  254. {
  255. if (name.type == TEXTURE_TYPE)
  256. {
  257. return TextureResource::load(m_resource_allocator, m_resource_archive, name);
  258. }
  259. else if (name.type == TEXT_TYPE)
  260. {
  261. return TextResource::load(m_resource_allocator, m_resource_archive, name);
  262. }
  263. return NULL;
  264. }
  265. //-----------------------------------------------------------------------------
  266. void ResourceManager::unload_by_type(ResourceId name, void* resource) const
  267. {
  268. if (name.type == TEXTURE_TYPE)
  269. {
  270. TextureResource::unload(m_resource_allocator, (TextureResource*)resource);
  271. }
  272. else if (name.type == TEXT_TYPE)
  273. {
  274. TextResource::unload(m_resource_allocator, (TextResource*)resource);
  275. }
  276. return;
  277. }
  278. //-----------------------------------------------------------------------------
  279. void ResourceManager::online(ResourceId name, void* resource)
  280. {
  281. if (name.type == TEXTURE_TYPE)
  282. {
  283. TextureResource::online((TextureResource*)resource);
  284. }
  285. else if (name.type == TEXT_TYPE)
  286. {
  287. TextResource::unload(m_resource_allocator, (TextResource*)resource);
  288. }
  289. m_resources_mutex.lock();
  290. ResourceEntry& entry = m_resources[name.index];
  291. entry.resource = resource;
  292. entry.state = RS_LOADED;
  293. m_resources_mutex.unlock();
  294. }
  295. //-----------------------------------------------------------------------------
  296. void* ResourceManager::background_thread(void* thiz)
  297. {
  298. ResourceManager* mgr = (ResourceManager*)thiz;
  299. mgr->background_load();
  300. return NULL;
  301. }
  302. } // namespace crown