BsCoreObjectManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObjectCore.h"
  4. #include "BsModule.h"
  5. namespace BansheeEngine
  6. {
  7. // TODO Low priority - Add debug option that would remember a call stack for each resource initialization,
  8. // so when we fail to release one we know which one it is.
  9. /**
  10. * @brief Manager that keeps track of all active CoreObjects.
  11. *
  12. * @note Internal class. Thread safe unless specified otherwise.
  13. */
  14. class BS_CORE_EXPORT CoreObjectManager : public Module<CoreObjectManager>
  15. {
  16. /**
  17. * @brief Stores dirty data that is to be transferred from sim
  18. * thread to core thread part of a CoreObject, for a single object.
  19. */
  20. struct CoreStoredSyncObjData
  21. {
  22. CoreStoredSyncObjData()
  23. :destinationObj(nullptr)
  24. { }
  25. CoreStoredSyncObjData(CoreObjectCore* destObj, const CoreSyncData& syncData)
  26. :destinationObj(destObj), syncData(syncData)
  27. { }
  28. CoreObjectCore* destinationObj;
  29. CoreSyncData syncData;
  30. };
  31. /**
  32. * @brief Stores dirty data that is to be transferred from sim
  33. * thread to core thread part of a CoreObject, for all dirty
  34. * objects in one frame.
  35. */
  36. struct CoreStoredSyncData
  37. {
  38. FrameAlloc* alloc = nullptr;
  39. Map<UINT64, CoreStoredSyncObjData> entries;
  40. };
  41. public:
  42. CoreObjectManager();
  43. ~CoreObjectManager();
  44. /**
  45. * @brief Registers a new CoreObject notifying the manager the object
  46. * is created.
  47. */
  48. UINT64 registerObject(CoreObject* object);
  49. /**
  50. * @brief Unregisters a CoreObject notifying the manager the object
  51. * is destroyed.
  52. */
  53. void unregisterObject(CoreObject* object);
  54. /**
  55. * @brief Synchronizes all dirty CoreObjects with the core thread. Their dirty data will be
  56. * allocated using the provided allocator and then queued for update using the provided
  57. * core thread accessor.
  58. *
  59. * @note Sim thread only.
  60. */
  61. void syncToCore(CoreAccessor& accessor);
  62. private:
  63. /**
  64. * @brief Stores all syncable data from dirty core objects into memory allocated
  65. * by the provided allocator. Additional meta-data is stored internally to be
  66. * used by call to syncUpload.
  67. *
  68. * @param allocator Allocator to use for allocating memory for stored data.
  69. *
  70. * @note Sim thread only.
  71. * Must be followed by a call to syncUpload with the same type.
  72. */
  73. void syncDownload(FrameAlloc* allocator);
  74. /**
  75. * @brief Copies all the data stored by previous call to "syncDownload"
  76. * into core thread versions of CoreObjects.
  77. *
  78. * @note Core thread only.
  79. * Must be preceded by a call to syncDownload.
  80. */
  81. void syncUpload();
  82. UINT64 mNextAvailableID;
  83. Map<UINT64, CoreObject*> mObjects;
  84. List<CoreStoredSyncData> mCoreSyncData;
  85. BS_MUTEX(mObjectsMutex);
  86. };
  87. }