BsCoreObjectManager.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, UINT64 internalId, const CoreSyncData& syncData)
  26. :destinationObj(destObj), syncData(syncData), internalId(internalId)
  27. { }
  28. 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. public:
  43. CoreObjectManager();
  44. ~CoreObjectManager();
  45. /**
  46. * @brief Registers a new CoreObject notifying the manager the object
  47. * is created.
  48. */
  49. UINT64 registerObject(CoreObject* object);
  50. /**
  51. * @brief Unregisters a CoreObject notifying the manager the object
  52. * is destroyed.
  53. */
  54. void unregisterObject(CoreObject* object);
  55. /**
  56. * @brief Synchronizes all dirty CoreObjects with the core thread. Their dirty data will be
  57. * allocated using the provided allocator and then queued for update using the provided
  58. * core thread accessor.
  59. *
  60. * @note Sim thread only.
  61. */
  62. void syncToCore(CoreAccessor& accessor);
  63. private:
  64. /**
  65. * @brief Stores all syncable data from dirty core objects into memory allocated
  66. * by the provided allocator. Additional meta-data is stored internally to be
  67. * used by call to syncUpload.
  68. *
  69. * @param allocator Allocator to use for allocating memory for stored data.
  70. *
  71. * @note Sim thread only.
  72. * Must be followed by a call to syncUpload with the same type.
  73. */
  74. void syncDownload(FrameAlloc* allocator);
  75. /**
  76. * @brief Copies all the data stored by previous call to "syncDownload"
  77. * into core thread versions of CoreObjects.
  78. *
  79. * @note Core thread only.
  80. * Must be preceded by a call to syncDownload.
  81. */
  82. void syncUpload();
  83. UINT64 mNextAvailableID;
  84. Map<UINT64, CoreObject*> mObjects;
  85. List<CoreStoredSyncData> mCoreSyncData;
  86. BS_MUTEX(mObjectsMutex);
  87. };
  88. }