ResourceManager.cpp 10.0 KB

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