BsCoreObjectManager.h 4.5 KB

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