ResourceManager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <algorithm>
  24. #include <cstdio>
  25. #include "Types.h"
  26. #include "ResourceManager.h"
  27. #include "StringUtils.h"
  28. #include "Hash.h"
  29. #include "Path.h"
  30. #include "Log.h"
  31. #include "Device.h"
  32. #include "Filesystem.h"
  33. #include "TextReader.h"
  34. #include "DiskFile.h"
  35. #include "TextResource.h"
  36. #include "TextureResource.h"
  37. #include "VertexShaderResource.h"
  38. #include "PixelShaderResource.h"
  39. namespace crown
  40. {
  41. //-----------------------------------------------------------------------------
  42. ResourceManager::ResourceManager(Bundle& bundle) :
  43. m_resource_bundle(bundle),
  44. m_resources(m_allocator),
  45. m_loading_queue(m_allocator),
  46. m_loaded_queue(m_allocator),
  47. m_seed(0),
  48. m_background_thread_should_run(true),
  49. m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
  50. {
  51. DiskFile* seed_file = device()->filesystem()->open("seed.ini", FOM_READ);
  52. TextReader reader(*seed_file);
  53. char tmp_buf[32];
  54. reader.read_string(tmp_buf, 32);
  55. device()->filesystem()->close(seed_file);
  56. sscanf(tmp_buf, "%u", &m_seed);
  57. }
  58. //-----------------------------------------------------------------------------
  59. ResourceManager::~ResourceManager()
  60. {
  61. m_background_thread_should_run = false;
  62. }
  63. //-----------------------------------------------------------------------------
  64. ResourceId ResourceManager::load(const char* name)
  65. {
  66. char basename[512];
  67. char extension[512];
  68. path::filename_without_extension(name, basename, 512);
  69. path::extension(name, extension, 512);
  70. uint32_t name_hash = hash::murmur2_32(basename, string::strlen(basename), m_seed);
  71. uint32_t type_hash = hash::murmur2_32(extension, string::strlen(extension), 0);
  72. return load(name_hash, type_hash);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ResourceManager::unload(ResourceId name)
  76. {
  77. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  78. m_resources_mutex.lock();
  79. ResourceEntry& entry = m_resources[name.index];
  80. entry.references--;
  81. if (entry.references == 0 && entry.state == RS_LOADED)
  82. {
  83. unload_by_type(name, entry.resource);
  84. entry.state = RS_UNLOADED;
  85. entry.resource = NULL;
  86. }
  87. m_resources_mutex.unlock();
  88. }
  89. //-----------------------------------------------------------------------------
  90. void ResourceManager::reload(ResourceId name)
  91. {
  92. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  93. m_resources_mutex.lock();
  94. ResourceEntry& entry = m_resources[name.index];
  95. if (entry.state == RS_LOADED)
  96. {
  97. unload_by_type(name, entry.resource);
  98. entry.state = RS_UNLOADED;
  99. entry.resource = NULL;
  100. entry.resource = load_by_type(name);
  101. entry.state = RS_LOADED;
  102. }
  103. m_resources_mutex.unlock();
  104. }
  105. //-----------------------------------------------------------------------------
  106. bool ResourceManager::has(ResourceId name) const
  107. {
  108. bool has_resource = false;
  109. m_resources_mutex.lock();
  110. if (m_resources.size() > name.index)
  111. {
  112. has_resource = (m_resources[name.index].id.name == name.name);
  113. }
  114. m_resources_mutex.unlock();
  115. return has_resource;
  116. }
  117. //-----------------------------------------------------------------------------
  118. const void* ResourceManager::data(ResourceId name) const
  119. {
  120. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  121. m_resources_mutex.lock();
  122. void* resource = m_resources[name.index].resource;
  123. m_resources_mutex.unlock();
  124. return resource;
  125. }
  126. //-----------------------------------------------------------------------------
  127. bool ResourceManager::is_loaded(ResourceId name) const
  128. {
  129. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  130. m_resources_mutex.lock();
  131. bool loaded = m_resources[name.index].state == RS_LOADED;
  132. m_resources_mutex.unlock();
  133. return loaded;
  134. }
  135. //-----------------------------------------------------------------------------
  136. uint32_t ResourceManager::references(ResourceId name) const
  137. {
  138. CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
  139. m_resources_mutex.lock();
  140. bool loaded = m_resources[name.index].references;
  141. m_resources_mutex.unlock();
  142. return loaded;
  143. }
  144. //-----------------------------------------------------------------------------
  145. uint32_t ResourceManager::remaining() const
  146. {
  147. uint32_t count = 0;
  148. m_loading_mutex.lock();
  149. count = m_loading_queue.size();
  150. m_loading_mutex.unlock();
  151. return count;
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ResourceManager::flush()
  155. {
  156. check_load_queue();
  157. while (true)
  158. {
  159. // Wait for all the resources to be loaded
  160. // by the background thread
  161. m_loading_mutex.lock();
  162. while (m_loading_queue.size() > 0)
  163. {
  164. m_all_loaded.wait(m_loading_mutex);
  165. }
  166. m_loading_mutex.unlock();
  167. // When all loaded, bring them online
  168. bring_loaded_online();
  169. return;
  170. }
  171. }
  172. //-----------------------------------------------------------------------------
  173. uint32_t ResourceManager::seed() const
  174. {
  175. return m_seed;
  176. }
  177. //-----------------------------------------------------------------------------
  178. void ResourceManager::check_load_queue()
  179. {
  180. m_loading_mutex.lock();
  181. if (m_loading_queue.size() > 0)
  182. {
  183. m_loading_requests.signal();
  184. }
  185. m_loading_mutex.unlock();
  186. }
  187. //-----------------------------------------------------------------------------
  188. void ResourceManager::bring_loaded_online()
  189. {
  190. m_loaded_mutex.lock();
  191. while (m_loaded_queue.size() > 0)
  192. {
  193. LoadedResource lr = m_loaded_queue.front();
  194. m_loaded_queue.pop_front();
  195. online(lr.resource, lr.data);
  196. }
  197. m_loaded_mutex.unlock();
  198. }
  199. //-----------------------------------------------------------------------------
  200. ResourceId ResourceManager::load(uint32_t name, uint32_t type)
  201. {
  202. ResourceId id;
  203. id.name = name;
  204. id.type = type;
  205. // Search for an already existent resource
  206. ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
  207. // If resource not found, create a new one
  208. if (entry == m_resources.end())
  209. {
  210. id.index = m_resources.size();
  211. ResourceEntry entry;
  212. entry.id = id;
  213. entry.state = RS_UNLOADED;
  214. entry.references = 1;
  215. entry.resource = NULL;
  216. m_resources.push_back(entry);
  217. m_loading_mutex.lock();
  218. m_loading_queue.push_back(id);
  219. m_loading_mutex.unlock();
  220. return id;
  221. }
  222. // Else, increment its reference count
  223. entry->references++;
  224. return entry->id;
  225. }
  226. //-----------------------------------------------------------------------------
  227. void ResourceManager::background_load()
  228. {
  229. while (m_background_thread_should_run)
  230. {
  231. m_loading_mutex.lock();
  232. while (m_loading_queue.size() == 0)
  233. {
  234. m_loading_requests.wait(m_loading_mutex);
  235. }
  236. ResourceId resource = m_loading_queue.front();
  237. m_loading_queue.pop_front();
  238. m_loading_mutex.unlock();
  239. void* data = load_by_type(resource);
  240. LoadedResource lr;
  241. lr.resource = resource;
  242. lr.data = data;
  243. m_loaded_mutex.lock();
  244. m_loaded_queue.push_back(lr);
  245. m_loaded_mutex.unlock();
  246. m_loading_mutex.lock();
  247. if (m_loading_queue.size() == 0)
  248. {
  249. m_all_loaded.signal();
  250. }
  251. m_loading_mutex.unlock();
  252. }
  253. }
  254. //-----------------------------------------------------------------------------
  255. void* ResourceManager::load_by_type(ResourceId name)
  256. {
  257. if (name.type == TEXTURE_TYPE)
  258. {
  259. return TextureResource::load(m_resource_allocator, m_resource_bundle, name);
  260. }
  261. else if (name.type == TEXT_TYPE)
  262. {
  263. return TextResource::load(m_resource_allocator, m_resource_bundle, name);
  264. }
  265. else if (name.type == VERTEX_SHADER_TYPE)
  266. {
  267. return VertexShaderResource::load(m_resource_allocator, m_resource_bundle, name);
  268. }
  269. else if (name.type == PIXEL_SHADER_TYPE)
  270. {
  271. return PixelShaderResource::load(m_resource_allocator, m_resource_bundle, name);
  272. }
  273. return NULL;
  274. }
  275. //-----------------------------------------------------------------------------
  276. void ResourceManager::unload_by_type(ResourceId name, void* resource)
  277. {
  278. if (name.type == TEXTURE_TYPE)
  279. {
  280. TextureResource::unload(m_resource_allocator, resource);
  281. }
  282. else if (name.type == TEXT_TYPE)
  283. {
  284. TextResource::unload(m_resource_allocator, resource);
  285. }
  286. else if (name.type == VERTEX_SHADER_TYPE)
  287. {
  288. VertexShaderResource::unload(m_resource_allocator, resource);
  289. }
  290. else if (name.type == PIXEL_SHADER_TYPE)
  291. {
  292. PixelShaderResource::unload(m_resource_allocator, resource);
  293. }
  294. return;
  295. }
  296. //-----------------------------------------------------------------------------
  297. void ResourceManager::online(ResourceId name, void* resource)
  298. {
  299. if (name.type == TEXTURE_TYPE)
  300. {
  301. TextureResource::online(resource);
  302. }
  303. else if (name.type == TEXT_TYPE)
  304. {
  305. TextResource::online(resource);
  306. }
  307. else if (name.type == VERTEX_SHADER_TYPE)
  308. {
  309. VertexShaderResource::online(resource);
  310. }
  311. else if (name.type == PIXEL_SHADER_TYPE)
  312. {
  313. PixelShaderResource::online(resource);
  314. }
  315. m_resources_mutex.lock();
  316. ResourceEntry& entry = m_resources[name.index];
  317. entry.resource = resource;
  318. entry.state = RS_LOADED;
  319. m_resources_mutex.unlock();
  320. }
  321. //-----------------------------------------------------------------------------
  322. void* ResourceManager::background_thread(void* thiz)
  323. {
  324. ResourceManager* mgr = (ResourceManager*)thiz;
  325. mgr->background_load();
  326. return NULL;
  327. }
  328. } // namespace crown