AddressInfo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Runtime.InteropServices;
  19. using DspAddress = System.UInt64;
  20. using CpuAddress = System.UInt64;
  21. namespace Ryujinx.Audio.Renderer.Server.MemoryPool
  22. {
  23. /// <summary>
  24. /// Represents the information of a region shared between the CPU and DSP.
  25. /// </summary>
  26. public struct AddressInfo
  27. {
  28. /// <summary>
  29. /// The target CPU address of the region.
  30. /// </summary>
  31. public CpuAddress CpuAddress;
  32. /// <summary>
  33. /// The size of the region.
  34. /// </summary>
  35. public ulong Size;
  36. private unsafe MemoryPoolState* _memoryPools;
  37. /// <summary>
  38. /// The forced DSP address of the region.
  39. /// </summary>
  40. public DspAddress ForceMappedDspAddress;
  41. private unsafe ref MemoryPoolState MemoryPoolState => ref *_memoryPools;
  42. public unsafe bool HasMemoryPoolState => (IntPtr)_memoryPools != IntPtr.Zero;
  43. /// <summary>
  44. /// Create an new empty <see cref="AddressInfo"/>.
  45. /// </summary>
  46. /// <returns>A new empty <see cref="AddressInfo"/>.</returns>
  47. public static AddressInfo Create()
  48. {
  49. return Create(0, 0);
  50. }
  51. /// <summary>
  52. /// Create a new <see cref="AddressInfo"/>.
  53. /// </summary>
  54. /// <param name="cpuAddress">The target <see cref="CpuAddress"/> of the region.</param>
  55. /// <param name="size">The target size of the region.</param>
  56. /// <returns>A new <see cref="AddressInfo"/>.</returns>
  57. public static AddressInfo Create(CpuAddress cpuAddress, ulong size)
  58. {
  59. unsafe
  60. {
  61. return new AddressInfo
  62. {
  63. CpuAddress = cpuAddress,
  64. _memoryPools = MemoryPoolState.Null,
  65. Size = size,
  66. ForceMappedDspAddress = 0
  67. };
  68. }
  69. }
  70. /// <summary>
  71. /// Setup the CPU address and size of the <see cref="AddressInfo"/>.
  72. /// </summary>
  73. /// <param name="cpuAddress">The target <see cref="CpuAddress"/> of the region.</param>
  74. /// <param name="size">The size of the region.</param>
  75. public void Setup(CpuAddress cpuAddress, ulong size)
  76. {
  77. CpuAddress = cpuAddress;
  78. Size = size;
  79. ForceMappedDspAddress = 0;
  80. unsafe
  81. {
  82. _memoryPools = MemoryPoolState.Null;
  83. }
  84. }
  85. /// <summary>
  86. /// Set the <see cref="MemoryPoolState"/> associated.
  87. /// </summary>
  88. /// <param name="memoryPoolState">The <see cref="MemoryPoolState"/> associated.</param>
  89. public void SetupMemoryPool(Span<MemoryPoolState> memoryPoolState)
  90. {
  91. unsafe
  92. {
  93. fixed (MemoryPoolState* ptr = &MemoryMarshal.GetReference(memoryPoolState))
  94. {
  95. SetupMemoryPool(ptr);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Set the <see cref="MemoryPoolState"/> associated.
  101. /// </summary>
  102. /// <param name="memoryPoolState">The <see cref="MemoryPoolState"/> associated.</param>
  103. public unsafe void SetupMemoryPool(MemoryPoolState* memoryPoolState)
  104. {
  105. _memoryPools = memoryPoolState;
  106. }
  107. /// <summary>
  108. /// Check if the <see cref="MemoryPoolState"/> is mapped.
  109. /// </summary>
  110. /// <returns>Returns true if the <see cref="MemoryPoolState"/> is mapped.</returns>
  111. public bool HasMappedMemoryPool()
  112. {
  113. return HasMemoryPoolState && MemoryPoolState.IsMapped();
  114. }
  115. /// <summary>
  116. /// Get the DSP address associated to the <see cref="AddressInfo"/>.
  117. /// </summary>
  118. /// <param name="markUsed">If true, mark the <see cref="MemoryPoolState"/> as used.</param>
  119. /// <returns>Returns the DSP address associated to the <see cref="AddressInfo"/>.</returns>
  120. public DspAddress GetReference(bool markUsed)
  121. {
  122. if (!HasMappedMemoryPool())
  123. {
  124. return ForceMappedDspAddress;
  125. }
  126. if (markUsed)
  127. {
  128. MemoryPoolState.IsUsed = true;
  129. }
  130. return MemoryPoolState.Translate(CpuAddress, Size);
  131. }
  132. }
  133. }