| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL; |
| | 4 | | using DCL.SettingsData; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// An implementation of an AutoQualityController for capped FPS |
| | 9 | | /// </summary> |
| | 10 | | public class AutoQualityCappedFPSController : IAutoQualityController |
| | 11 | | { |
| | 12 | | internal const int EVALUATIONS_SIZE = 5; |
| | 13 | | internal const float INCREASE_MARGIN = 0.9f; |
| | 14 | | internal const float STAY_MARGIN = 0.8f; |
| | 15 | |
|
| | 16 | | internal int targetFPS; |
| | 17 | | internal int currentQualityIndex; |
| | 18 | | internal readonly QualitySettingsData qualitySettings; |
| | 19 | |
|
| 39 | 20 | | internal readonly List<float> fpsEvaluations = new List<float>(); |
| | 21 | |
|
| 39 | 22 | | public AutoQualityCappedFPSController(int targetFPS, int startIndex, QualitySettingsData qualitySettings) |
| | 23 | | { |
| 39 | 24 | | this.targetFPS = targetFPS; |
| 39 | 25 | | currentQualityIndex = startIndex; |
| 39 | 26 | | this.qualitySettings = qualitySettings; |
| 39 | 27 | | } |
| | 28 | |
|
| | 29 | | public int EvaluateQuality(PerformanceMetricsData metrics) |
| | 30 | | { |
| 9 | 31 | | if (metrics == null) |
| 0 | 32 | | return currentQualityIndex; |
| | 33 | |
|
| 9 | 34 | | fpsEvaluations.Add(metrics.fpsCount); |
| 9 | 35 | | if (fpsEvaluations.Count <= EVALUATIONS_SIZE) |
| 5 | 36 | | return currentQualityIndex; |
| | 37 | |
|
| 4 | 38 | | fpsEvaluations.RemoveAt(0); |
| 4 | 39 | | float performance = fpsEvaluations.Average() / targetFPS; |
| | 40 | |
|
| 4 | 41 | | int newCurrentQualityIndex = currentQualityIndex; |
| 4 | 42 | | if (performance < STAY_MARGIN) |
| 2 | 43 | | newCurrentQualityIndex = Mathf.Max(0, currentQualityIndex - 1); |
| | 44 | |
|
| 4 | 45 | | if (performance >= INCREASE_MARGIN) |
| 1 | 46 | | newCurrentQualityIndex = Mathf.Min(qualitySettings.Length - 1, currentQualityIndex + 2); //We increase quali |
| | 47 | |
|
| 4 | 48 | | if (newCurrentQualityIndex != currentQualityIndex) |
| 3 | 49 | | ResetEvaluation(); |
| | 50 | |
|
| 4 | 51 | | currentQualityIndex = newCurrentQualityIndex; |
| 4 | 52 | | return currentQualityIndex; |
| | 53 | | } |
| | 54 | |
|
| 74 | 55 | | public void ResetEvaluation() { fpsEvaluations.Clear(); } |
| | 56 | | } |