ResourceManager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #pragma once
  24. #include "Types.h"
  25. #include "List.h"
  26. #include "Queue.h"
  27. #include "Resource.h"
  28. #include "HeapAllocator.h"
  29. #include "Thread.h"
  30. #include "Mutex.h"
  31. #include "Cond.h"
  32. namespace crown
  33. {
  34. struct ResourceEntry
  35. {
  36. ResourceId id;
  37. ResourceState state;
  38. uint32_t references;
  39. void* resource;
  40. bool operator==(const ResourceId& resource)
  41. {
  42. return id == resource;
  43. }
  44. bool operator==(const ResourceEntry& b)
  45. {
  46. return id == b.id;
  47. }
  48. };
  49. struct LoadedResource
  50. {
  51. ResourceId resource;
  52. void* data;
  53. };
  54. class Bundle;
  55. /// Resource manager.
  56. class ResourceManager
  57. {
  58. public:
  59. /// Read resources from @a bundle and store resource data using @a allocator.
  60. ResourceManager(Bundle& bundle);
  61. ~ResourceManager();
  62. /// Loads the resource by @a name and returns its ResourceId.
  63. /// @note
  64. /// The resource data may be not immediately available,
  65. /// the resource gets pushed in a queue of load requests and loadead as
  66. /// soon as possible by the ResourceLoader.
  67. /// You have to explicitly call is_loaded() method to check if the
  68. /// loading process is actually completed.
  69. ResourceId load(const char* name);
  70. /// Unloads the @a resource, freeing up all the memory associated by it
  71. /// and eventually any global object associated with it.
  72. /// (Such as texture objects, vertex buffers etc.)
  73. void unload(ResourceId name);
  74. /// Reloads the @a resource
  75. void reload(ResourceId name);
  76. /// Returns whether the manager has the @a name resource into
  77. /// its list of resources.
  78. /// @note
  79. /// Having a resource does not mean that the resource is
  80. /// available for using; instead, you have to check is_loaded() to
  81. /// obtain the resource availability status.
  82. bool has(ResourceId name) const;
  83. /// Returns the data associated with the @a name resource.
  84. /// The resource data contains resource-specific metadata
  85. /// and the actual resource data. In order to correctly use
  86. /// it, you have to know which type of data @a name refers to
  87. /// and cast accordingly.
  88. const void* data(ResourceId name) const;
  89. /// Returns whether the @a name resource is loaded (i.e. whether
  90. /// you can use the data associated with it).
  91. bool is_loaded(ResourceId name) const;
  92. /// Returns the number of references to the @a resource
  93. uint32_t references(ResourceId name) const;
  94. /// Returns the number of resources still waiting to load.
  95. uint32_t remaining() const;
  96. /// Forces all the loading requests to complete before preceeding.
  97. void flush();
  98. /// Returns the seed used to generate resource name hashes.
  99. uint32_t seed() const;
  100. private:
  101. // Checks the load queue and signal the backgroud about pending
  102. // requests. It is normally called only by the Device.
  103. void check_load_queue();
  104. // Calls online() on loaded resources. Must be called only
  105. // in the main thread and generally only by Device.
  106. void bring_loaded_online();
  107. // Loads the resource by name and type and returns its ResourceId.
  108. ResourceId load(uint32_t name, uint32_t type);
  109. void background_load();
  110. void* load_by_type(ResourceId name);
  111. void unload_by_type(ResourceId name, void* resource);
  112. void online(ResourceId name, void* resource);
  113. private:
  114. static void* background_thread(void* thiz);
  115. private:
  116. // Archive whether to look for resources
  117. Bundle& m_resource_bundle;
  118. // Used to strore resource memory
  119. HeapAllocator m_resource_allocator;
  120. HeapAllocator m_allocator;
  121. // The master lookup table
  122. List<ResourceEntry> m_resources;
  123. // Resources waiting for loading
  124. Queue<ResourceId> m_loading_queue;
  125. // Resources already loaded, ready to bring online
  126. Queue<LoadedResource> m_loaded_queue;
  127. uint32_t m_seed;
  128. // Background loading thread
  129. bool m_background_thread_should_run;
  130. os::Thread m_thread;
  131. mutable os::Mutex m_loading_mutex;
  132. os::Cond m_loading_requests;
  133. os::Cond m_all_loaded;
  134. os::Mutex m_loaded_mutex;
  135. mutable os::Mutex m_resources_mutex;
  136. friend class Device;
  137. };
  138. } // namespace crown