PerformanceManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 System;
  20. namespace Ryujinx.Audio.Renderer.Server.Performance
  21. {
  22. public abstract class PerformanceManager
  23. {
  24. /// <summary>
  25. /// Get the required size for a single performance frame.
  26. /// </summary>
  27. /// <param name="parameter">The audio renderer configuration.</param>
  28. /// <param name="behaviourContext">The behaviour context.</param>
  29. /// <returns>The required size for a single performance frame.</returns>
  30. public static ulong GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref AudioRendererConfiguration parameter, ref BehaviourContext behaviourContext)
  31. {
  32. uint version = behaviourContext.GetPerformanceMetricsDataFormat();
  33. if (version == 2)
  34. {
  35. return (ulong)PerformanceManagerGeneric<PerformanceFrameHeaderVersion2,
  36. PerformanceEntryVersion2,
  37. PerformanceDetailVersion2>.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter);
  38. }
  39. else if (version == 1)
  40. {
  41. return (ulong)PerformanceManagerGeneric<PerformanceFrameHeaderVersion1,
  42. PerformanceEntryVersion1,
  43. PerformanceDetailVersion1>.GetRequiredBufferSizeForPerformanceMetricsPerFrame(ref parameter);
  44. }
  45. throw new NotImplementedException($"Unknown Performance metrics data format version {version}");
  46. }
  47. /// <summary>
  48. /// Copy the performance frame history to the supplied user buffer and returns the size copied.
  49. /// </summary>
  50. /// <param name="performanceOutput">The supplied user buffer to store the performance frame into.</param>
  51. /// <returns>The size copied to the supplied buffer.</returns>
  52. public abstract uint CopyHistories(Span<byte> performanceOutput);
  53. /// <summary>
  54. /// Set the target node id to profile.
  55. /// </summary>
  56. /// <param name="target">The target node id to profile.</param>
  57. public abstract void SetTargetNodeId(int target);
  58. /// <summary>
  59. /// Check if the given target node id is profiled.
  60. /// </summary>
  61. /// <param name="target">The target node id to check.</param>
  62. /// <returns>Return true, if the given target node id is profiled.</returns>
  63. public abstract bool IsTargetNodeId(int target);
  64. /// <summary>
  65. /// Get the next buffer to store a performance entry.
  66. /// </summary>
  67. /// <param name="performanceEntry">The output <see cref="PerformanceEntryAddresses"/>.</param>
  68. /// <param name="entryType">The <see cref="PerformanceEntryType"/> info.</param>
  69. /// <param name="nodeId">The node id of the entry.</param>
  70. /// <returns>Return true, if a valid <see cref="PerformanceEntryAddresses"/> was returned.</returns>
  71. public abstract bool GetNextEntry(out PerformanceEntryAddresses performanceEntry, PerformanceEntryType entryType, int nodeId);
  72. /// <summary>
  73. /// Get the next buffer to store a performance detailed entry.
  74. /// </summary>
  75. /// <param name="performanceEntry">The output <see cref="PerformanceEntryAddresses"/>.</param>
  76. /// <param name="detailType">The <see cref="PerformanceDetailType"/> info.</param>
  77. /// <param name="entryType">The <see cref="PerformanceEntryType"/> info.</param>
  78. /// <param name="nodeId">The node id of the entry.</param>
  79. /// <returns>Return true, if a valid <see cref="PerformanceEntryAddresses"/> was returned.</returns>
  80. public abstract bool GetNextEntry(out PerformanceEntryAddresses performanceEntry, PerformanceDetailType detailType, PerformanceEntryType entryType, int nodeId);
  81. /// <summary>
  82. /// Finalize the current performance frame.
  83. /// </summary>
  84. /// <param name="dspRunningBehind">Indicate if the DSP is running behind.</param>
  85. /// <param name="voiceDropCount">The count of voices that were dropped.</param>
  86. /// <param name="startRenderingTicks">The start ticks of the audio rendering.</param>
  87. public abstract void TapFrame(bool dspRunningBehind, uint voiceDropCount, ulong startRenderingTicks);
  88. /// <summary>
  89. /// Create a new <see cref="PerformanceManager"/>.
  90. /// </summary>
  91. /// <param name="performanceBuffer">The backing memory available for use by the manager.</param>
  92. /// <param name="parameter">The audio renderer configuration.</param>
  93. /// <param name="behaviourContext">The behaviour context;</param>
  94. /// <returns>A new <see cref="PerformanceManager"/>.</returns>
  95. public static PerformanceManager Create(Memory<byte> performanceBuffer, ref AudioRendererConfiguration parameter, BehaviourContext behaviourContext)
  96. {
  97. uint version = behaviourContext.GetPerformanceMetricsDataFormat();
  98. switch (version)
  99. {
  100. case 1:
  101. return new PerformanceManagerGeneric<PerformanceFrameHeaderVersion1, PerformanceEntryVersion1, PerformanceDetailVersion1>(performanceBuffer,
  102. ref parameter);
  103. case 2:
  104. return new PerformanceManagerGeneric<PerformanceFrameHeaderVersion2, PerformanceEntryVersion2, PerformanceDetailVersion2>(performanceBuffer,
  105. ref parameter);
  106. default:
  107. throw new NotImplementedException($"Unknown Performance metrics data format version {version}");
  108. }
  109. }
  110. }
  111. }