ResourceLoader.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "Resource.h"
  26. #include "Thread.h"
  27. #include "Queue.h"
  28. #include "List.h"
  29. #include "Mutex.h"
  30. #include "Cond.h"
  31. #include "OS.h"
  32. namespace crown
  33. {
  34. class Bundle;
  35. class Allocator;
  36. #define MAX_LOAD_REQUESTS 1024
  37. typedef uint32_t LoadResourceId;
  38. enum LoadResourceStatus
  39. {
  40. LRS_NO_INFORMATION,
  41. LRS_QUEUED,
  42. LRS_LOADING,
  43. LRS_LOADED
  44. };
  45. struct LoadResource
  46. {
  47. LoadResourceId id;
  48. uint32_t type;
  49. ResourceId resource;
  50. };
  51. struct LoadResourceData
  52. {
  53. LoadResourceStatus status;
  54. void* data;
  55. };
  56. /// Loads resources in a background thread.
  57. class ResourceLoader : public Thread
  58. {
  59. public:
  60. /// Reads the resources data from the given @a bundle using
  61. /// @a resource_heap to allocate memory for them.
  62. ResourceLoader(Bundle& bundle, Allocator& resource_heap);
  63. /// Loads the @a resource in a background thread.
  64. LoadResourceId load_resource(uint32_t type, ResourceId resource);
  65. /// Returns the status of the given load request @a id.
  66. LoadResourceStatus load_resource_status(LoadResourceId id) const;
  67. /// Returns the data which has been loaded for the given request @a id.
  68. void* load_resource_data(LoadResourceId id) const;
  69. // Loads resources in the loading queue.
  70. int32_t run();
  71. private:
  72. // Whether to look for resources
  73. Bundle& m_bundle;
  74. // Used to strore resource memory
  75. Allocator& m_resource_heap;
  76. uint32_t m_num_requests;
  77. Queue<LoadResource> m_requests;
  78. LoadResourceData m_results[MAX_LOAD_REQUESTS];
  79. Mutex m_requests_mutex;
  80. Mutex m_results_mutex;
  81. Cond m_full;
  82. };
  83. } // namespace crown