BsCoreObjectManager.h 4.4 KB

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