BsCoreObjectManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObjectCore.h"
  4. #include "BsModule.h"
  5. namespace BansheeEngine
  6. {
  7. /** @cond INTERNAL */
  8. /** @addtogroup CoreThread
  9. * @{
  10. */
  11. // TODO Low priority - Add debug option that would remember a call stack for each resource initialization,
  12. // so when we fail to release one we know which one it is.
  13. /**
  14. * Manager that keeps track of all active CoreObject%s.
  15. *
  16. * @note Internal class.
  17. * @note Thread safe unless specified otherwise.
  18. */
  19. class BS_CORE_EXPORT CoreObjectManager : public Module<CoreObjectManager>
  20. {
  21. /**
  22. * Stores dirty data that is to be transferred from sim thread to core thread part of a CoreObject, for a single
  23. * object.
  24. */
  25. struct CoreStoredSyncObjData
  26. {
  27. CoreStoredSyncObjData()
  28. :internalId(0)
  29. { }
  30. CoreStoredSyncObjData(const SPtr<CoreObjectCore> destObj, UINT64 internalId, const CoreSyncData& syncData)
  31. :destinationObj(destObj), syncData(syncData), internalId(internalId)
  32. { }
  33. std::weak_ptr<CoreObjectCore> destinationObj;
  34. CoreSyncData syncData;
  35. UINT64 internalId;
  36. };
  37. /**
  38. * Stores dirty data that is to be transferred from sim thread to core thread part of a CoreObject, for all dirty
  39. * objects in one frame.
  40. */
  41. struct CoreStoredSyncData
  42. {
  43. FrameAlloc* alloc = nullptr;
  44. Vector<CoreStoredSyncObjData> entries;
  45. };
  46. /** Contains information about a dirty CoreObject that requires syncing to the core thread. */
  47. struct DirtyObjectData
  48. {
  49. CoreObject* object;
  50. INT32 syncDataId;
  51. };
  52. public:
  53. CoreObjectManager();
  54. ~CoreObjectManager();
  55. /** Registers a new CoreObject notifying the manager the object is created. */
  56. UINT64 registerObject(CoreObject* object);
  57. /** Unregisters a CoreObject notifying the manager the object is destroyed. */
  58. void unregisterObject(CoreObject* object);
  59. /** Notifies the system that a CoreObject is dirty and needs to be synced with the core thread. */
  60. void notifyCoreDirty(CoreObject* object);
  61. /** Notifies the system that CoreObject dependencies are dirty and should be updated. */
  62. void notifyDependenciesDirty(CoreObject* object);
  63. /**
  64. * Synchronizes all dirty CoreObjects with the core thread. Their dirty data will be allocated using the global
  65. * frame allocator and then queued for update using the provided core thread accessor.
  66. *
  67. * @note Sim thread only.
  68. */
  69. void syncToCore(CoreAccessor& accessor);
  70. /**
  71. * Synchronizes an individual dirty CoreObject with the core thread. Its dirty data will be allocated using the
  72. * global frame allocator and then queued for update using the provided core thread accessor.
  73. *
  74. * @note Sim thread only.
  75. */
  76. void syncToCore(CoreObject* object, CoreAccessor& accessor);
  77. /**
  78. * Clears any objects that are dirty and queued for sync on the core thread. Normally you only want to call this
  79. * during shutdown when you no longer care about any of that data.
  80. */
  81. void clearDirty();
  82. private:
  83. /**
  84. * Stores all syncable data from dirty core objects into memory allocated by the provided allocator. Additional
  85. * meta-data is stored internally to be used by call to syncUpload().
  86. *
  87. * @param[in] allocator Allocator to use for allocating memory for stored data.
  88. *
  89. * @note Sim thread only.
  90. * @note Must be followed by a call to syncUpload() with the same type.
  91. */
  92. void syncDownload(FrameAlloc* allocator);
  93. /**
  94. * Copies all the data stored by previous call to syncDownload() into core thread versions of CoreObjects.
  95. *
  96. * @note Core thread only.
  97. * @note Must be preceded by a call to syncDownload().
  98. */
  99. void syncUpload();
  100. /**
  101. * Updates the cached list of dependencies and dependants for the specified object.
  102. *
  103. * @param[in] object Update to update dependencies for.
  104. * @param[in] dependencies New set of dependencies, or null to clear all dependencies.
  105. */
  106. void updateDependencies(CoreObject* object, Vector<CoreObject*>* dependencies);
  107. UINT64 mNextAvailableID;
  108. Map<UINT64, CoreObject*> mObjects;
  109. Map<UINT64, DirtyObjectData> mDirtyObjects;
  110. Map<UINT64, Vector<CoreObject*>> mDependencies;
  111. Map<UINT64, Vector<CoreObject*>> mDependants;
  112. Vector<CoreStoredSyncObjData> mDestroyedSyncData;
  113. List<CoreStoredSyncData> mCoreSyncData;
  114. BS_MUTEX(mObjectsMutex);
  115. };
  116. /** @} */
  117. /** @endcond */
  118. }