Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <utils/Timers.h> |
| 20 | |
| 21 | #include <chrono> |
| 22 | #include <deque> |
| 23 | |
| 24 | #include "LayerHistory.h" |
| 25 | #include "RefreshRateConfigs.h" |
| 26 | #include "SchedulerUtils.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | class Layer; |
| 31 | |
| 32 | namespace scheduler { |
| 33 | |
| 34 | using namespace std::chrono_literals; |
| 35 | |
| 36 | // Maximum period between presents for a layer to be considered active. |
| 37 | constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms; |
| 38 | |
| 39 | // Earliest present time for a layer to be considered active. |
| 40 | constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) { |
| 41 | return now - MAX_ACTIVE_LAYER_PERIOD_NS.count(); |
| 42 | } |
| 43 | |
| 44 | // Stores history of present times and refresh rates for a layer. |
| 45 | class LayerInfoV2 { |
| 46 | // Layer is considered frequent if the earliest value in the window of most recent present times |
| 47 | // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in |
| 48 | // favor of a low refresh rate. |
| 49 | static constexpr size_t FREQUENT_LAYER_WINDOW_SIZE = 3; |
Ady Abraham | f5bc779 | 2020-05-27 20:04:20 +0000 | [diff] [blame] | 50 | static constexpr float MIN_FPS_FOR_FREQUENT_LAYER = 10.0f; |
| 51 | static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS = |
| 52 | std::chrono::nanoseconds(static_cast<nsecs_t>(1e9f / MIN_FPS_FOR_FREQUENT_LAYER)) + 1ms; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 53 | |
| 54 | friend class LayerHistoryTestV2; |
| 55 | |
| 56 | public: |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 57 | LayerInfoV2(const std::string& name, nsecs_t highRefreshRatePeriod, |
| 58 | LayerHistory::LayerVoteType defaultVote); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 59 | |
| 60 | LayerInfoV2(const LayerInfo&) = delete; |
| 61 | LayerInfoV2& operator=(const LayerInfoV2&) = delete; |
| 62 | |
| 63 | // Records the last requested present time. It also stores information about when |
| 64 | // the layer was last updated. If the present time is farther in the future than the |
| 65 | // updated time, the updated time is the present time. |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 66 | void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, bool pendingConfigChange); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 67 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 68 | // Sets an explicit layer vote. This usually comes directly from the application via |
| 69 | // ANativeWindow_setFrameRate API |
| 70 | void setLayerVote(LayerHistory::LayerVoteType type, float fps) { mLayerVote = {type, fps}; } |
| 71 | |
| 72 | // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote(). |
| 73 | // This is used for layers that called to setLayerVote() and then removed the vote, so that the |
| 74 | // layer can go back to whatever vote it had before the app voted for it. |
| 75 | void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; } |
| 76 | |
| 77 | // Resets the layer vote to its default. |
| 78 | void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f}; } |
| 79 | |
| 80 | std::pair<LayerHistory::LayerVoteType, float> getRefreshRate(nsecs_t now); |
| 81 | |
| 82 | // Return the last updated time. If the present time is farther in the future than the |
| 83 | // updated time, the updated time is the present time. |
| 84 | nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; } |
| 85 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame^] | 86 | void onLayerInactive(nsecs_t now) { |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 87 | // Mark mFrameTimeValidSince to now to ignore all previous frame times. |
| 88 | // We are not deleting the old frame to keep track of whether we should treat the first |
| 89 | // buffer as Max as we don't know anything about this layer or Min as this layer is |
| 90 | // posting infrequent updates. |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame^] | 91 | const auto timePoint = std::chrono::nanoseconds(now); |
| 92 | mFrameTimeValidSince = std::chrono::time_point<std::chrono::steady_clock>(timePoint); |
Ady Abraham | c966483 | 2020-05-12 14:16:56 -0700 | [diff] [blame] | 93 | mLastReportedRefreshRate = 0.0f; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 94 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 95 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame^] | 96 | void clearHistory(nsecs_t now) { |
| 97 | onLayerInactive(now); |
| 98 | mFrameTimes.clear(); |
| 99 | } |
| 100 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 101 | private: |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 102 | // Used to store the layer timestamps |
| 103 | struct FrameTimeData { |
| 104 | nsecs_t presetTime; // desiredPresentTime, if provided |
| 105 | nsecs_t queueTime; // buffer queue time |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 106 | bool pendingConfigChange; |
Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
Ady Abraham | af6d8a4 | 2020-05-27 19:56:15 +0000 | [diff] [blame] | 109 | bool isFrequent(nsecs_t now) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 110 | bool hasEnoughDataForHeuristic() const; |
| 111 | std::optional<float> calculateRefreshRateIfPossible(); |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 112 | std::pair<nsecs_t, bool> calculateAverageFrameTime() const; |
| 113 | bool isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 114 | bool isFrameTimeValid(const FrameTimeData&) const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 115 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 116 | const std::string mName; |
| 117 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 118 | // Used for sanitizing the heuristic data |
| 119 | const nsecs_t mHighRefreshRatePeriod; |
| 120 | LayerHistory::LayerVoteType mDefaultVote; |
| 121 | |
| 122 | nsecs_t mLastUpdatedTime = 0; |
| 123 | |
| 124 | float mLastReportedRefreshRate = 0.0f; |
| 125 | |
| 126 | // Holds information about the layer vote |
| 127 | struct { |
| 128 | LayerHistory::LayerVoteType type; |
| 129 | float fps; |
| 130 | } mLayerVote; |
| 131 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 132 | std::deque<FrameTimeData> mFrameTimes; |
Ady Abraham | dfb63ba | 2020-05-27 20:05:05 +0000 | [diff] [blame] | 133 | std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince = |
| 134 | std::chrono::steady_clock::now(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 135 | static constexpr size_t HISTORY_SIZE = 90; |
| 136 | static constexpr std::chrono::nanoseconds HISTORY_TIME = 1s; |
| 137 | }; |
| 138 | |
| 139 | } // namespace scheduler |
| 140 | } // namespace android |