FileCacheManager.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <AzCore/IO/GenericStreams.h>
  11. #include <AzCore/std/smart_ptr/shared_ptr.h>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #include <AzCore/XML/rapidxml.h>
  14. #include <IAudioInterfacesCommonData.h>
  15. #include <ATLEntities.h>
  16. namespace AzFramework
  17. {
  18. class DebugDisplayRequests;
  19. }
  20. namespace Audio
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////////////////////
  23. class AudioFileCacheManagerNotifications
  24. : public AZ::EBusTraits
  25. {
  26. public:
  27. virtual ~AudioFileCacheManagerNotifications() = default;
  28. ///////////////////////////////////////////////////////////////////////////////////////////
  29. // EBusTraits - Single Bus Address, Single Handler, Mutex, Queued
  30. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
  31. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  32. static const bool EnableEventQueue = true;
  33. using MutexType = AZStd::recursive_mutex;
  34. ///////////////////////////////////////////////////////////////////////////////////////////
  35. virtual void FinishAsyncStreamRequest(AZ::IO::FileRequestHandle request) = 0;
  36. };
  37. using AudioFileCacheManagerNotficationBus = AZ::EBus<AudioFileCacheManagerNotifications>;
  38. ///////////////////////////////////////////////////////////////////////////////////////////////
  39. class CFileCacheManager
  40. : public AudioFileCacheManagerNotficationBus::Handler
  41. {
  42. public:
  43. explicit CFileCacheManager(TATLPreloadRequestLookup& preloadRequests);
  44. ~CFileCacheManager() override;
  45. CFileCacheManager(const CFileCacheManager&) = delete; // Copy protection
  46. CFileCacheManager& operator=(const CFileCacheManager&) = delete; // Copy protection
  47. // Public methods
  48. void Initialize();
  49. void Release();
  50. void Update();
  51. virtual TAudioFileEntryID TryAddFileCacheEntry(const AZ::rapidxml::xml_node<char>* fileXmlNode, EATLDataScope dataScope, bool autoLoad); // 'virtual' is needed for unit tests/mocking
  52. bool TryRemoveFileCacheEntry(const TAudioFileEntryID audioFileID, const EATLDataScope dataScope);
  53. void UpdateLocalizedFileCacheEntries();
  54. EAudioRequestStatus TryLoadRequest(const TAudioPreloadRequestID preloadRequestID, const bool loadSynchronously, const bool autoLoadOnly);
  55. EAudioRequestStatus TryUnloadRequest(const TAudioPreloadRequestID preloadRequestID);
  56. EAudioRequestStatus UnloadDataByScope(const EATLDataScope dataScope);
  57. #if !defined(AUDIO_RELEASE)
  58. void DrawDebugInfo(AzFramework::DebugDisplayRequests& debugDisplay, const float posX, const float posY);
  59. #endif // !AUDIO_RELEASE
  60. private:
  61. // Internal type definitions.
  62. using TAudioFileEntries = ATLMapLookupType<TAudioFileEntryID, CATLAudioFileEntry*>;
  63. // Internal methods
  64. void AllocateHeap(const size_t size, const char* const usage);
  65. bool UncacheFileCacheEntryInternal(CATLAudioFileEntry* const audioFileEntry, const bool now, const bool ignoreUsedCount = false);
  66. bool DoesRequestFitInternal(const size_t requestSize);
  67. void UpdatePreloadRequestsStatus();
  68. bool FinishCachingFileInternal(CATLAudioFileEntry* const audioFileEntry, AZ::IO::SizeType sizeBytes,
  69. AZ::IO::IStreamerTypes::RequestStatus requestState);
  70. ///////////////////////////////////////////////////////////////////////////////////////////
  71. // AudioFileCacheManagerNotficationBus
  72. void FinishAsyncStreamRequest(AZ::IO::FileRequestHandle request) override;
  73. ///////////////////////////////////////////////////////////////////////////////////////////
  74. bool AllocateMemoryBlockInternal(CATLAudioFileEntry* const audioFileEntry);
  75. void UncacheFile(CATLAudioFileEntry* const audioFileEntry);
  76. void TryToUncacheFiles();
  77. void UpdateLocalizedFileEntryData(CATLAudioFileEntry* const audioFileEntry);
  78. bool TryCacheFileCacheEntryInternal(
  79. CATLAudioFileEntry* const audioFileEntry,
  80. const TAudioFileEntryID fileID,
  81. const bool loadSynchronously,
  82. const bool overrideUseCount = false,
  83. const AZ::u32 useCount = 0);
  84. // Internal members
  85. TATLPreloadRequestLookup& m_preloadRequests;
  86. TAudioFileEntries m_audioFileEntries;
  87. size_t m_currentByteTotal;
  88. size_t m_maxByteTotal;
  89. };
  90. } // namespace Audio