暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdaptivePerformanceSubsystem.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. namespace UnityEngine.AdaptivePerformance.Provider
  3. {
  4. /// <summary>
  5. /// Feature flags
  6. /// See <see cref="PerformanceDataRecord.ChangeFlags"/> and <seealso cref="AdaptivePerformanceSubsystem.Capabilities"/>.
  7. /// </summary>
  8. [Flags]
  9. public enum Feature
  10. {
  11. /// <summary>
  12. /// No features
  13. /// </summary>
  14. None = 0,
  15. /// <summary>
  16. /// See <see cref="PerformanceDataRecord.WarningLevel"/>
  17. /// </summary>
  18. WarningLevel = 0x1,
  19. /// <summary>
  20. /// See <see cref="PerformanceDataRecord.TemperatureLevel"/>
  21. /// </summary>
  22. TemperatureLevel = 0x2,
  23. /// <summary>
  24. /// See <see cref="PerformanceDataRecord.TemperatureTrend"/>
  25. /// </summary>
  26. TemperatureTrend = 0x4,
  27. /// <summary>
  28. /// See <see cref="PerformanceDataRecord.CpuPerformanceLevel"/> and <seealso cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/>
  29. /// </summary>
  30. CpuPerformanceLevel = 0x8,
  31. /// <summary>
  32. /// See <see cref="PerformanceDataRecord.GpuPerformanceLevel"/> and <seealso cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/>
  33. /// </summary>
  34. GpuPerformanceLevel = 0x10,
  35. /// <summary>
  36. /// See <see cref="PerformanceDataRecord.PerformanceLevelControlAvailable"/> and <seealso cref="AdaptivePerformanceSubsystem.PerformanceLevelControl"/>
  37. /// </summary>
  38. PerformanceLevelControl = 0x20,
  39. /// <summary>
  40. /// See <see cref="PerformanceDataRecord.GpuFrameTime"/>
  41. /// </summary>
  42. GpuFrameTime = 0x40,
  43. /// <summary>
  44. /// See <see cref="PerformanceDataRecord.CpuFrameTime"/>
  45. /// </summary>
  46. CpuFrameTime = 0x80,
  47. /// <summary>
  48. /// See <see cref="PerformanceDataRecord.OverallFrameTime"/>
  49. /// </summary>
  50. OverallFrameTime = 0x100,
  51. /// <summary>
  52. /// See <see cref="PerformanceDataRecord.CpuPerformanceBoost"/> and <seealso cref="IDevicePerformanceLevelControl.EnableCpuBoost"/>
  53. /// </summary>
  54. CpuPerformanceBoost = 0x200,
  55. /// <summary>
  56. /// See <see cref="PerformanceDataRecord.GpuPerformanceBoost"/> and <seealso cref="IDevicePerformanceLevelControl.EnableGpuBoost"/>
  57. /// </summary>
  58. GpuPerformanceBoost = 0x400,
  59. /// <summary>
  60. /// See <see cref="PerformanceDataRecord.ClusterInfo"/>
  61. /// </summary>
  62. ClusterInfo = 0x800
  63. }
  64. /// <summary>
  65. /// The performance data record stores all information about the thermal and performance status and delivers it from the provider subsystem to Adaptive Performance for further processing.
  66. /// </summary>
  67. public struct PerformanceDataRecord
  68. {
  69. /// <summary>
  70. /// A bitset of features which indicate if their value changed in the last frame or at startup.
  71. /// Unsupported features will never change.
  72. /// Fields not changing always have valid data as long as its capability is supported.
  73. /// </summary>
  74. /// <value>Bitset</value>
  75. public Feature ChangeFlags { get; set; }
  76. /// <summary>
  77. /// The current normalized temperature level in the range of [0.0, 1.0], or -1.0 when not supported or not available right now.
  78. /// A level of 1.0 means that the device is thermal throttling.
  79. /// The temperature level has changed when the <see cref="Feature.TemperatureLevel"/> bit is set in <see cref="ChangeFlags"/>.
  80. /// </summary>
  81. /// <value>Temperature level in the range of [0.0, 1.0] or -1.0</value>
  82. public float TemperatureLevel { get; set; }
  83. /// <summary>
  84. /// The current temperature trend in the range of [-1.0, 1.0] that is a metric of temperature change over time.
  85. /// The temperature trend is constant at 0.0 in case the feature is not supported.
  86. /// The temperature trend has changed when <see cref="Feature.TemperatureTrend"/> bit is set in <see cref="ChangeFlags"/>.
  87. /// </summary>
  88. /// <value>Temperature trend in the range of [-1.0, 1.0]</value>
  89. public float TemperatureTrend { get; set; }
  90. /// <summary>
  91. /// The current warning level as documented in <see cref="Feature.WarningLevel"/>.
  92. /// The warning level has changed when <see cref="Feature.WarningLevel"/> bit is set in <see cref="ChangeFlags"/>.
  93. /// </summary>
  94. /// <value>The current warning level</value>
  95. public WarningLevel WarningLevel { get; set; }
  96. /// <summary>
  97. /// The currently active CPU performance level. This is typically the value previously set with <see cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/> once the levels are successfully applied.
  98. /// Adaptive Performance might also change this level on its own. This typically happens when the device is thermal throttling or when <see cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/> failed.
  99. /// CPU performance level has a value in the range of [<see cref="Constants.MinCpuPerformanceLevel"/>, <see cref="IDevicePerformanceLevelControl.MaxCpuPerformanceLevel"/>], or <seealso cref="Constants.UnknownPerformanceLevel"/>.
  100. /// A value of <see cref="Constants.UnknownPerformanceLevel"/> means that Adaptive Performance took control of performance levels.
  101. /// CPU performance level has changed when <see cref="Feature.CpuPerformanceLevel"/> bit is set in <see cref="ChangeFlags"/>.
  102. /// </summary>
  103. /// <value></value>
  104. public int CpuPerformanceLevel { get; set; }
  105. /// <summary>
  106. /// The currently active GPU performance level. This is typically the value previously set with <see cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/> once the levels are successfully applied.
  107. /// Adaptive Performance might also change this level on its own. This typically happens when the device is thermal throttling or when <see cref="IDevicePerformanceLevelControl.SetPerformanceLevel"/> failed.
  108. /// GPU performance level has a value in the range of [<see cref="Constants.MinCpuPerformanceLevel"/>, <see cref="IDevicePerformanceLevelControl.MaxGpuPerformanceLevel"/>], or <seealso cref="Constants.UnknownPerformanceLevel"/>.
  109. /// A value of <see cref="Constants.UnknownPerformanceLevel"/> means that Adaptive Performance took control of performance levels.
  110. /// GPU performance level has changed when <see cref="Feature.GpuPerformanceLevel"/> bit is set in <see cref="ChangeFlags"/>.
  111. /// </summary>
  112. public int GpuPerformanceLevel { get; set; }
  113. /// <summary>
  114. /// True if =performance levels can currently be controlled manually and aren't controlled by Adaptive Performance or the operating system.
  115. /// Has changed when <see cref="Feature.PerformanceLevelControl"/> bit is set in <see cref="ChangeFlags"/>.
  116. /// </summary>
  117. public bool PerformanceLevelControlAvailable { get; set; }
  118. /// <summary>
  119. /// The time in seconds spent by the CPU for rendering the last complete frame.
  120. /// Has changed when <see cref="Feature.CpuFrameTime"/> bit is set in <see cref="ChangeFlags"/>.
  121. /// </summary>
  122. public float CpuFrameTime { get; set; }
  123. /// <summary>
  124. /// The time in seconds spent by the GPU for rendering the last complete frame.
  125. /// Has changed when <see cref="Feature.GpuFrameTime"/> bit is set in <see cref="ChangeFlags"/>.
  126. /// </summary>
  127. public float GpuFrameTime { get; set; }
  128. /// <summary>
  129. /// The total time in seconds spent for the frame.
  130. /// Has changed when <see cref="Feature.OverallFrameTime"/> bit is set in <see cref="ChangeFlags"/>.
  131. /// </summary>
  132. public float OverallFrameTime { get; set; }
  133. /// <summary>
  134. /// The currently active CPU boost state. This is typically true if previously enabled with <see cref="IDevicePerformanceLevelControl.EnableCpuBoost"/> once the boost is successfully applied.
  135. /// Adaptive Performance might also change this level on its own. This typically happens when the device is thermal throttling or when <see cref="IDevicePerformanceLevelControl.EnableCpuBoost"/> fails.
  136. /// Once the CPU boost is enabled it is active until you receive a callback that it is disabled.
  137. /// CPU boost level has changed when <see cref="Feature.CpuPerformanceBoost"/> bit is set in <see cref="ChangeFlags"/>.
  138. /// </summary>
  139. public bool CpuPerformanceBoost { get; set; }
  140. /// <summary>
  141. /// The currently active GPU boost state. This is typically true if previously enabled with <see cref="IDevicePerformanceLevelControl.EnableGpuBoost"/> once the boost is successfully applied.
  142. /// Adaptive Performance might also change this level on its own. This typically happens when the device is thermal throttling or when <see cref="IDevicePerformanceLevelControl.EnableGpuBoost"/> fails.
  143. /// Once the GPU boost is enabled it is active until you receive a callback that it is disabled.
  144. /// GPU boost level has changed when <see cref="Feature.GpuPerformanceBoost"/> bit is set in <see cref="ChangeFlags"/>.
  145. /// </summary>
  146. public bool GpuPerformanceBoost { get; set; }
  147. /// <summary>
  148. /// Current CPU cluster information information. Includes number of big, medium and small cores use at the application startup.
  149. /// </summary>
  150. public ClusterInfo ClusterInfo { get; set; }
  151. }
  152. /// <summary>
  153. /// This interface describes how the Adaptive Performance provider lifecycle behaves.
  154. /// </summary>
  155. public interface IApplicationLifecycle
  156. {
  157. /// <summary>
  158. /// Called before an application pauses.
  159. /// To be called from `MonoBehaviour.OnApplicationPause`.
  160. /// </summary>
  161. void ApplicationPause();
  162. /// <summary>
  163. /// Called after an application resumes.
  164. /// To be called from `MonoBehaviour.OnApplicationPause`.
  165. /// </summary>
  166. void ApplicationResume();
  167. }
  168. /// <summary>
  169. /// The device performance level control lets you change CPU and GPU levels and informs you about the current levels.
  170. /// </summary>
  171. public interface IDevicePerformanceLevelControl
  172. {
  173. /// <summary>
  174. /// Maximum supported CPU performance level. This should not change after startup.
  175. /// <see cref="Constants.UnknownPerformanceLevel"/> in case performance levels are not supported.
  176. /// Value in the range of [<see cref="Constants.MinCpuPerformanceLevel"/>, 10].
  177. /// </summary>
  178. /// <value>Value in the range of [<see cref="Constants.MinCpuPerformanceLevel"/>, 10]</value>
  179. int MaxCpuPerformanceLevel { get; }
  180. /// <summary>
  181. /// Maximum supported GPU performance level. This should not change after startup.
  182. /// <see cref="Constants.UnknownPerformanceLevel"/> in case performance levels are not supported.
  183. /// Value in the range of [<see cref="Constants.MinGpuPerformanceLevel"/>, 10].
  184. /// </summary>
  185. /// <value>Value in the range of [<see cref="Constants.MinGpuPerformanceLevel"/>, 10]</value>
  186. int MaxGpuPerformanceLevel { get; }
  187. /// <summary>
  188. /// Request a performance level change.
  189. /// If <see cref="Constants.UnknownPerformanceLevel"/> is passed, the subsystem picks the level to be used.
  190. /// </summary>
  191. /// <param name="cpu">
  192. /// The new performance level. Can be <see cref="Constants.UnknownPerformanceLevel"/> or range of [<see cref="Constants.MinCpuPerformanceLevel"/>, <see cref="IDevicePerformanceLevelControl.MaxCpuPerformanceLevel"/>].
  193. /// If <see cref="Feature.CpuPerformanceLevel"/> is not supported (see <see cref="AdaptivePerformanceSubsystem.Capabilities"/>), this parameter is ignored.
  194. /// </param>
  195. /// <param name="gpu">
  196. /// The new performance level. Can be <see cref="Constants.UnknownPerformanceLevel"/> or range of [<see cref="Constants.MinCpuPerformanceLevel"/>, <see cref="IDevicePerformanceLevelControl.MaxGpuPerformanceLevel"/>].
  197. /// If <see cref="Feature.GpuPerformanceLevel"/> is not supported (see <see cref="AdaptivePerformanceSubsystem.Capabilities"/>), this parameter is ignored.
  198. /// </param>
  199. /// <returns>Returns true on success. When this fails, it means that the system took control of the active performance levels.</returns>
  200. bool SetPerformanceLevel(ref int cpu, ref int gpu);
  201. /// <summary>
  202. /// Request a CPU performance boost.
  203. /// </summary>
  204. /// If <see cref="Feature.CpuPerformanceBoost"/> is not supported (see <see cref="AdaptivePerformanceSubsystem.Capabilities"/>), this function is ignored.
  205. /// <returns>Returns true on success. When this fails, it means that the system took control and does not allow boosts.</returns>
  206. bool EnableCpuBoost();
  207. /// <summary>
  208. /// Request a GPU performance boost.
  209. /// </summary>
  210. /// If <see cref="Feature.GpuPerformanceBoost"/> is not supported (see <see cref="AdaptivePerformanceSubsystem.Capabilities"/>), this function is ignored.
  211. /// <returns>Returns true on success. When this fails, it means that the system took control and does not allow boosts.</returns>
  212. bool EnableGpuBoost();
  213. }
  214. /// <summary>
  215. /// Use the Adaptive Performance Subsystem class to create your custom provider subsystem to deliver data from your provider to Adaptive Performance.
  216. /// </summary>
  217. public abstract class AdaptivePerformanceSubsystem : AdaptivePerformanceSubsystemBase
  218. {
  219. /// <summary>
  220. /// Main constructor, not used in the subsystem specifically.
  221. /// </summary>
  222. protected AdaptivePerformanceSubsystem()
  223. {
  224. }
  225. /// <summary>
  226. /// Bitset of supported features.
  227. /// Does not change after startup.
  228. /// </summary>
  229. /// <value>Bitset</value>
  230. public Feature Capabilities { get; protected set; }
  231. /// <summary>
  232. /// To be called once per frame.
  233. /// The returned data structure's fields are populated with the latest available data, according to the supported <see cref="Capabilities"/>.
  234. /// </summary>
  235. /// <returns>Data structure with the most recent performance data.</returns>
  236. public abstract PerformanceDataRecord Update();
  237. /// <summary>
  238. /// Application lifecycle events to be consumed by subsystem.
  239. /// Can be null if the subsystem does not need special handling on life-cycle events.
  240. /// The returned reference does not change after startup.
  241. /// </summary>
  242. /// <value>Application lifecycle object</value>
  243. public abstract IApplicationLifecycle ApplicationLifecycle { get; }
  244. /// <summary>
  245. /// Control CPU or GPU performance levels of the device.
  246. /// Can be null if the subsystem does not support controlling CPU/GPU performance levels.
  247. /// Is null when the <see cref="Feature.PerformanceLevelControl"/> bit is not set in <see cref="Capabilities"/>.
  248. /// The returned reference does not change after startup.
  249. /// </summary>
  250. /// <value>Performance level control object</value>
  251. public abstract IDevicePerformanceLevelControl PerformanceLevelControl { get; }
  252. /// <summary>
  253. /// Returns the version of the subsystem implementation.
  254. /// Can be used together with SubsystemDescriptor to identify a subsystem.
  255. /// </summary>
  256. /// <value>Version number</value>
  257. public abstract Version Version { get; }
  258. /// <summary>
  259. /// Generates a human readable string of subsystem internal stats.
  260. /// Optional and only used for development.
  261. /// </summary>
  262. /// <value>String with subsystem specific statistics</value>
  263. public virtual string Stats { get { return ""; } }
  264. }
  265. /// <summary>
  266. /// This is the base class for <see cref="AdaptivePerformanceSubsystem"/> and acts as a stub for backwards compability.
  267. /// </summary>
  268. #pragma warning disable CS0618
  269. public abstract class AdaptivePerformanceSubsystemBase : UnityEngine.Subsystem<AdaptivePerformanceSubsystemDescriptor>
  270. {
  271. /// <summary>
  272. /// Returns if the provider subsystem is currently running.
  273. /// </summary>
  274. override public bool running { get { return initialized; } }
  275. /// <summary>
  276. /// Returns if the provider subsystem was initialized successfully.
  277. /// </summary>
  278. public bool initialized { get; protected set; }
  279. }
  280. #pragma warning restore CS0618
  281. }