ResourceManager.h 5.0 KB

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