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