CircularBufferSink.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Ryujinx.Audio.Renderer.Common;
  18. using Ryujinx.Audio.Renderer.Parameter;
  19. using Ryujinx.Audio.Renderer.Parameter.Sink;
  20. using Ryujinx.Audio.Renderer.Server.MemoryPool;
  21. using System.Diagnostics;
  22. using System.Runtime.InteropServices;
  23. namespace Ryujinx.Audio.Renderer.Server.Sink
  24. {
  25. /// <summary>
  26. /// Server information for a circular buffer sink.
  27. /// </summary>
  28. public class CircularBufferSink : BaseSink
  29. {
  30. /// <summary>
  31. /// The circular buffer parameter.
  32. /// </summary>
  33. public CircularBufferParameter Parameter;
  34. /// <summary>
  35. /// The last written data offset on the circular buffer.
  36. /// </summary>
  37. private uint _lastWrittenOffset;
  38. /// <summary>
  39. /// THe previous written offset of the circular buffer.
  40. /// </summary>
  41. private uint _oldWrittenOffset;
  42. /// <summary>
  43. /// The current offset to write data on the circular buffer.
  44. /// </summary>
  45. public uint CurrentWriteOffset { get; private set; }
  46. /// <summary>
  47. /// The <see cref="AddressInfo"/> of the circular buffer.
  48. /// </summary>
  49. public AddressInfo CircularBufferAddressInfo;
  50. public CircularBufferSink()
  51. {
  52. CircularBufferAddressInfo = AddressInfo.Create();
  53. }
  54. public override SinkType TargetSinkType => SinkType.CircularBuffer;
  55. public override void Update(out BehaviourParameter.ErrorInfo errorInfo, ref SinkInParameter parameter, ref SinkOutStatus outStatus, PoolMapper mapper)
  56. {
  57. errorInfo = new BehaviourParameter.ErrorInfo();
  58. outStatus = new SinkOutStatus();
  59. Debug.Assert(IsTypeValid(ref parameter));
  60. ref CircularBufferParameter inputDeviceParameter = ref MemoryMarshal.Cast<byte, CircularBufferParameter>(parameter.SpecificData)[0];
  61. if (parameter.IsUsed != IsUsed || ShouldSkip)
  62. {
  63. UpdateStandardParameter(ref parameter);
  64. if (parameter.IsUsed)
  65. {
  66. Debug.Assert(CircularBufferAddressInfo.CpuAddress == 0);
  67. Debug.Assert(CircularBufferAddressInfo.GetReference(false) == 0);
  68. ShouldSkip = !mapper.TryAttachBuffer(out errorInfo, ref CircularBufferAddressInfo, inputDeviceParameter.BufferAddress, inputDeviceParameter.BufferSize);
  69. }
  70. else
  71. {
  72. Debug.Assert(CircularBufferAddressInfo.CpuAddress != 0);
  73. Debug.Assert(CircularBufferAddressInfo.GetReference(false) != 0);
  74. }
  75. Parameter = inputDeviceParameter;
  76. }
  77. outStatus.LastWrittenOffset = _lastWrittenOffset;
  78. }
  79. public override void UpdateForCommandGeneration()
  80. {
  81. Debug.Assert(Type == TargetSinkType);
  82. if (IsUsed)
  83. {
  84. uint frameSize = Constants.TargetSampleSize * Parameter.SampleCount * Parameter.InputCount;
  85. _lastWrittenOffset = _oldWrittenOffset;
  86. _oldWrittenOffset = CurrentWriteOffset;
  87. CurrentWriteOffset += frameSize;
  88. if (Parameter.BufferSize > 0)
  89. {
  90. CurrentWriteOffset %= Parameter.BufferSize;
  91. }
  92. }
  93. }
  94. public override void CleanUp()
  95. {
  96. CircularBufferAddressInfo = AddressInfo.Create();
  97. _lastWrittenOffset = 0;
  98. _oldWrittenOffset = 0;
  99. CurrentWriteOffset = 0;
  100. base.CleanUp();
  101. }
  102. }
  103. }