BsCoreObjectManager.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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), internalId(0)
  24. { }
  25. CoreStoredSyncObjData(const SPtr<CoreObjectCore> destObj, UINT64 internalId, const CoreSyncData& syncData)
  26. :destinationObj(destObj), syncData(syncData), internalId(internalId)
  27. { }
  28. SPtr<CoreObjectCore> destinationObj;
  29. CoreSyncData syncData;
  30. UINT64 internalId;
  31. };
  32. /**
  33. * @brief Stores dirty data that is to be transferred from sim
  34. * thread to core thread part of a CoreObject, for all dirty
  35. * objects in one frame.
  36. */
  37. struct CoreStoredSyncData
  38. {
  39. FrameAlloc* alloc = nullptr;
  40. Vector<CoreStoredSyncObjData> entries;
  41. };
  42. /**
  43. * @brief Contains information about a dirty CoreObject that requires syncing to
  44. * the core thread.
  45. */
  46. struct DirtyObjectData
  47. {
  48. CoreObject* object;
  49. INT32 syncDataId;
  50. };
  51. public:
  52. CoreObjectManager();
  53. ~CoreObjectManager();
  54. /**
  55. * @brief Registers a new CoreObject notifying the manager the object
  56. * is created.
  57. */
  58. UINT64 registerObject(CoreObject* object);
  59. /**
  60. * @brief Unregisters a CoreObject notifying the manager the object
  61. * is destroyed.
  62. */
  63. void unregisterObject(CoreObject* object);
  64. /**
  65. * @brief Notifies the system that a CoreObject is dirty and needs to be synced with the
  66. * core thread.
  67. */
  68. void notifyCoreDirty(CoreObject* object);
  69. /**
  70. * @brief Notifies the system that CoreObject dependencies are dirty and should be updated.
  71. */
  72. void notifyDependenciesDirty(CoreObject* object);
  73. /**
  74. * @brief Synchronizes all dirty CoreObjects with the core thread. Their dirty data will be
  75. * allocated using the global frame allocator and then queued for update using the provided
  76. * core thread accessor.
  77. *
  78. * @note Sim thread only.
  79. */
  80. void syncToCore(CoreAccessor& accessor);
  81. /**
  82. * @brief Synchronizes an individual dirty CoreObject with the core thread. Its dirty data will be
  83. * allocated using the global frame allocator and then queued for update using the provided
  84. * core thread accessor.
  85. *
  86. * @note Sim thread only.
  87. */
  88. void syncToCore(CoreObject* object, CoreAccessor& accessor);
  89. private:
  90. /**
  91. * @brief Stores all syncable data from dirty core objects into memory allocated
  92. * by the provided allocator. Additional meta-data is stored internally to be
  93. * used by call to syncUpload.
  94. *
  95. * @param allocator Allocator to use for allocating memory for stored data.
  96. *
  97. * @note Sim thread only.
  98. * Must be followed by a call to syncUpload with the same type.
  99. */
  100. void syncDownload(FrameAlloc* allocator);
  101. /**
  102. * @brief Copies all the data stored by previous call to "syncDownload"
  103. * into core thread versions of CoreObjects.
  104. *
  105. * @note Core thread only.
  106. * Must be preceded by a call to syncDownload.
  107. */
  108. void syncUpload();
  109. /**
  110. * @brief Updates the cached list of dependencies and dependants for the specified object.
  111. *
  112. * @param object Update to update dependencies for.
  113. * @param dependencies New set of dependencies, or null to clear all dependencies.
  114. */
  115. void updateDependencies(CoreObject* object, Vector<CoreObject*>* dependencies);
  116. UINT64 mNextAvailableID;
  117. Map<UINT64, CoreObject*> mObjects;
  118. Map<UINT64, DirtyObjectData> mDirtyObjects;
  119. Map<UINT64, Vector<CoreObject*>> mDependencies;
  120. Map<UINT64, Vector<CoreObject*>> mDependants;
  121. Vector<CoreStoredSyncObjData> mDestroyedSyncData;
  122. List<CoreStoredSyncData> mCoreSyncData;
  123. BS_MUTEX(mObjectsMutex);
  124. };
  125. }