DecompressionRegistrarImpl.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "DecompressionRegistrarImpl.h"
  9. #include <AzCore/std/functional.h>
  10. #include <AzCore/std/parallel/scoped_lock.h>
  11. #include <AzCore/std/ranges/ranges_algorithm.h>
  12. #include <Compression/CompressionTypeIds.h>
  13. namespace Compression
  14. {
  15. AZ_TYPE_INFO_WITH_NAME_IMPL(DecompressionRegistrarImpl, "DecompressionRegistrarImpl",
  16. DecompressionRegistrarImplTypeId);
  17. AZ_RTTI_NO_TYPE_INFO_IMPL(DecompressionRegistrarImpl, DecompressionRegistrarInterface);
  18. AZ_CLASS_ALLOCATOR_IMPL(DecompressionRegistrarImpl, AZ::SystemAllocator);
  19. DecompressionRegistrarImpl::DecompressionInterfaceDeleter::DecompressionInterfaceDeleter() = default;
  20. DecompressionRegistrarImpl::DecompressionInterfaceDeleter::DecompressionInterfaceDeleter(bool shouldDelete)
  21. : m_delete(shouldDelete)
  22. {}
  23. void DecompressionRegistrarImpl::DecompressionInterfaceDeleter::operator()(IDecompressionInterface* ptr) const
  24. {
  25. if (m_delete)
  26. {
  27. delete ptr;
  28. }
  29. }
  30. DecompressionRegistrarImpl::DecompressionRegistrarImpl() = default;
  31. DecompressionRegistrarImpl::~DecompressionRegistrarImpl() = default;
  32. void DecompressionRegistrarImpl::VisitDecompressionInterfaces(const VisitDecompressionInterfaceCallback& callback) const
  33. {
  34. auto VisitInterface = [&callback](const DecompressionIdIndexEntry& decompressionInterfaceEntry)
  35. {
  36. return decompressionInterfaceEntry.m_decompressionInterface != nullptr ? callback(*decompressionInterfaceEntry.m_decompressionInterface) : true;
  37. };
  38. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  39. AZStd::ranges::all_of(m_decompressionInterfaces, VisitInterface);
  40. }
  41. AZ::Outcome<void, AZStd::unique_ptr<IDecompressionInterface>> DecompressionRegistrarImpl::RegisterDecompressionInterface(
  42. CompressionAlgorithmId compressionAlgorithmId, AZStd::unique_ptr<IDecompressionInterface> decompressionInterface)
  43. {
  44. // Transfer ownership to a temporary DecompressionInterfacePtr which is supplied to the RegisterDecompressionInterfaceImpl
  45. // If registration fails, the compression interface pointer is returned in the failure outcome
  46. if (auto registerResult = RegisterDecompressionInterfaceImpl(compressionAlgorithmId, DecompressionInterfacePtr{ decompressionInterface.release() });
  47. !registerResult.IsSuccess())
  48. {
  49. return AZ::Failure(AZStd::unique_ptr<IDecompressionInterface>(registerResult.TakeError().release()));
  50. }
  51. // registration succeeded, return a void success outcome
  52. return AZ::Success();
  53. }
  54. bool DecompressionRegistrarImpl::RegisterDecompressionInterface(CompressionAlgorithmId compressionAlgorithmId,
  55. IDecompressionInterface& decompressionInterface)
  56. {
  57. // Create a temporary DecompressionInterfacePtr with a custom deleter that does NOT delete the decompressionInterface reference
  58. // On success, the the DecompressionInterfacePtr is stored in the decompression interface array
  59. // and will not delete the reference to registered interface
  60. return RegisterDecompressionInterfaceImpl(compressionAlgorithmId, DecompressionInterfacePtr{ &decompressionInterface, DecompressionInterfaceDeleter{false} }).IsSuccess();
  61. }
  62. auto DecompressionRegistrarImpl::RegisterDecompressionInterfaceImpl(CompressionAlgorithmId compressionAlgorithmId,
  63. DecompressionInterfacePtr decompressionInterfacePtr)
  64. -> AZ::Outcome<void, DecompressionInterfacePtr>
  65. {
  66. if (decompressionInterfacePtr == nullptr)
  67. {
  68. return AZ::Failure(AZStd::move(decompressionInterfacePtr));
  69. }
  70. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  71. auto decompressionIter = FindDecompressionInterfaceImpl(compressionAlgorithmId);
  72. if (decompressionIter != m_decompressionInterfaces.end())
  73. {
  74. // The compression interface has been found using the compression interface id,
  75. // so another registration cannot be done
  76. return AZ::Failure(AZStd::move(decompressionInterfacePtr));
  77. }
  78. // Use UpperBound to find the insertion slot for the new entry
  79. auto ProjectionToCompressionAlgorithmId = [](const DecompressionIdIndexEntry& entry) -> CompressionAlgorithmId
  80. {
  81. return entry.m_id;
  82. };
  83. m_decompressionInterfaces.emplace(AZStd::ranges::upper_bound(m_decompressionInterfaces, compressionAlgorithmId,
  84. AZStd::ranges::less{}, ProjectionToCompressionAlgorithmId),
  85. DecompressionIdIndexEntry{ compressionAlgorithmId, AZStd::move(decompressionInterfacePtr) });
  86. return AZ::Success();
  87. }
  88. bool DecompressionRegistrarImpl::UnregisterDecompressionInterface(CompressionAlgorithmId compressionAlgorithmId)
  89. {
  90. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  91. if (auto decompressionIter = FindDecompressionInterfaceImpl(compressionAlgorithmId);
  92. decompressionIter != m_decompressionInterfaces.end())
  93. {
  94. // Remove the decompressionInterface
  95. m_decompressionInterfaces.erase(decompressionIter);
  96. return true;
  97. }
  98. return false;
  99. }
  100. IDecompressionInterface* DecompressionRegistrarImpl::FindDecompressionInterface(CompressionAlgorithmId compressionAlgorithmId) const
  101. {
  102. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  103. auto decompressionIter = FindDecompressionInterfaceImpl(compressionAlgorithmId);
  104. return decompressionIter != m_decompressionInterfaces.end() ? decompressionIter->m_decompressionInterface.get() : nullptr;
  105. }
  106. IDecompressionInterface* DecompressionRegistrarImpl::FindDecompressionInterface(AZStd::string_view algorithmName) const
  107. {
  108. // Potentially the entire vector is iterated over
  109. IDecompressionInterface* resultInterface{};
  110. auto FindCompressionInterfaceByName = [algorithmName, &resultInterface](const IDecompressionInterface& decompressionInterface)
  111. {
  112. if (decompressionInterface.GetCompressionAlgorithmName() == algorithmName)
  113. {
  114. // const_cast is being used here to avoid making a second visitor overload
  115. // NOTE: this is function is internal to the implementation of the Decompression Registrar
  116. resultInterface = const_cast<IDecompressionInterface*>(&decompressionInterface);
  117. // The matching interface has been found, halt visitation.
  118. return false;
  119. }
  120. return true;
  121. };
  122. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  123. VisitDecompressionInterfaces(FindCompressionInterfaceByName);
  124. return resultInterface;
  125. }
  126. bool DecompressionRegistrarImpl::IsRegistered(CompressionAlgorithmId compressionAlgorithmId) const
  127. {
  128. AZStd::scoped_lock lock(m_decompressionInterfaceMutex);
  129. auto decompressionIter = FindDecompressionInterfaceImpl(compressionAlgorithmId);
  130. return decompressionIter != m_decompressionInterfaces.end();
  131. }
  132. // NOTE: The caller should lock the mutex
  133. // returns iterator to the compression inteface with the specified compression algorithm id
  134. // otherwise a sentinel iterator is returned
  135. auto DecompressionRegistrarImpl::FindDecompressionInterfaceImpl(CompressionAlgorithmId compressionAlgorithmId) const
  136. -> typename IdToDecompressionInterfaceMap::const_iterator
  137. {
  138. auto ProjectionToDecompressionAlgorithmId = [](const DecompressionIdIndexEntry& entry) -> CompressionAlgorithmId
  139. {
  140. return entry.m_id;
  141. };
  142. auto [firstFoundIter, lastFoundIter] = AZStd::ranges::equal_range(m_decompressionInterfaces,
  143. compressionAlgorithmId, AZStd::ranges::less{}, ProjectionToDecompressionAlgorithmId);
  144. return firstFoundIter != lastFoundIter ? firstFoundIter : m_decompressionInterfaces.end();
  145. }
  146. }// namespace Compression