| 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 | // #define LOG_NDEBUG 0 | 
|  | 18 |  | 
|  | 19 | #include "LayerInfoV2.h" | 
|  | 20 |  | 
|  | 21 | #include <algorithm> | 
|  | 22 | #include <utility> | 
|  | 23 |  | 
|  | 24 | #undef LOG_TAG | 
|  | 25 | #define LOG_TAG "LayerInfoV2" | 
|  | 26 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 27 |  | 
|  | 28 | namespace android::scheduler { | 
|  | 29 |  | 
|  | 30 | LayerInfoV2::LayerInfoV2(nsecs_t highRefreshRatePeriod, LayerHistory::LayerVoteType defaultVote) | 
|  | 31 | : mHighRefreshRatePeriod(highRefreshRatePeriod), | 
|  | 32 | mDefaultVote(defaultVote), | 
|  | 33 | mLayerVote({defaultVote, 0.0f}) {} | 
|  | 34 |  | 
|  | 35 | void LayerInfoV2::setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now) { | 
|  | 36 | lastPresentTime = std::max(lastPresentTime, static_cast<nsecs_t>(0)); | 
|  | 37 |  | 
|  | 38 | mLastUpdatedTime = std::max(lastPresentTime, now); | 
|  | 39 |  | 
|  | 40 | FrameTimeData frameTime = {.presetTime = lastPresentTime, .queueTime = mLastUpdatedTime}; | 
|  | 41 |  | 
|  | 42 | mFrameTimes.push_back(frameTime); | 
|  | 43 | if (mFrameTimes.size() > HISTORY_SIZE) { | 
|  | 44 | mFrameTimes.pop_front(); | 
|  | 45 | } | 
|  | 46 | } | 
|  | 47 |  | 
| Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 48 | bool LayerInfoV2::isFrameTimeValid(const FrameTimeData& frameTime) const { | 
|  | 49 | return frameTime.queueTime >= std::chrono::duration_cast<std::chrono::nanoseconds>( | 
|  | 50 | mFrameTimeValidSince.time_since_epoch()) | 
|  | 51 | .count(); | 
|  | 52 | } | 
|  | 53 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 54 | bool LayerInfoV2::isFrequent(nsecs_t now) const { | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 55 | // Find the first valid frame time | 
|  | 56 | auto it = mFrameTimes.begin(); | 
|  | 57 | for (; it != mFrameTimes.end(); ++it) { | 
|  | 58 | if (isFrameTimeValid(*it)) { | 
|  | 59 | break; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
| Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 63 | // If we know nothing about this layer we consider it as frequent as it might be the start | 
|  | 64 | // of an animation. | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 65 | if (std::distance(it, mFrameTimes.end()) < FREQUENT_LAYER_WINDOW_SIZE) { | 
| Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 66 | return true; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 69 | // Find the first active frame | 
|  | 70 | for (; it != mFrameTimes.end(); ++it) { | 
|  | 71 | if (it->queueTime >= getActiveLayerThreshold(now)) { | 
|  | 72 | break; | 
|  | 73 | } | 
| Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Ady Abraham | 0d73666 | 2020-02-28 18:22:30 -0800 | [diff] [blame] | 76 | const auto numFrames = std::distance(it, mFrameTimes.end()); | 
|  | 77 | if (numFrames < FREQUENT_LAYER_WINDOW_SIZE) { | 
| Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 78 | return false; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | // Layer is considered frequent if the average frame rate is higher than the threshold | 
|  | 82 | const auto totalTime = mFrameTimes.back().queueTime - it->queueTime; | 
| Ady Abraham | 0d73666 | 2020-02-28 18:22:30 -0800 | [diff] [blame] | 83 | return (1e9f * (numFrames - 1)) / totalTime >= MIN_FPS_FOR_FREQUENT_LAYER; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | bool LayerInfoV2::hasEnoughDataForHeuristic() const { | 
|  | 87 | // The layer had to publish at least HISTORY_SIZE or HISTORY_TIME of updates | 
| Ady Abraham | a61edcb | 2020-01-30 18:32:03 -0800 | [diff] [blame] | 88 | if (mFrameTimes.size() < 2) { | 
|  | 89 | return false; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | if (!isFrameTimeValid(mFrameTimes.front())) { | 
|  | 93 | return false; | 
|  | 94 | } | 
|  | 95 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 96 | if (mFrameTimes.size() < HISTORY_SIZE && | 
|  | 97 | mFrameTimes.back().queueTime - mFrameTimes.front().queueTime < HISTORY_TIME.count()) { | 
|  | 98 | return false; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | return true; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | std::optional<float> LayerInfoV2::calculateRefreshRateIfPossible() { | 
|  | 105 | static constexpr float MARGIN = 1.0f; // 1Hz | 
|  | 106 |  | 
|  | 107 | if (!hasEnoughDataForHeuristic()) { | 
|  | 108 | ALOGV("Not enough data"); | 
|  | 109 | return std::nullopt; | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | // Calculate the refresh rate by finding the average delta between frames | 
|  | 113 | nsecs_t totalPresentTimeDeltas = 0; | 
|  | 114 | for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) { | 
|  | 115 | // If there are no presentation timestamp provided we can't calculate the refresh rate | 
|  | 116 | if (it->presetTime == 0 || (it + 1)->presetTime == 0) { | 
|  | 117 | return std::nullopt; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | totalPresentTimeDeltas += | 
|  | 121 | std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod); | 
|  | 122 | } | 
|  | 123 | const float averageFrameTime = | 
|  | 124 | static_cast<float>(totalPresentTimeDeltas) / (mFrameTimes.size() - 1); | 
|  | 125 |  | 
|  | 126 | // Now once we calculated the refresh rate we need to make sure that all the frames we captured | 
| Ady Abraham | f6b7707 | 2020-01-30 14:22:54 -0800 | [diff] [blame] | 127 | // are evenly distributed and we don't calculate the average across some burst of frames. | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 128 | for (auto it = mFrameTimes.begin(); it != mFrameTimes.end() - 1; ++it) { | 
|  | 129 | const nsecs_t presentTimeDeltas = | 
|  | 130 | std::max(((it + 1)->presetTime - it->presetTime), mHighRefreshRatePeriod); | 
|  | 131 | if (std::abs(presentTimeDeltas - averageFrameTime) > 2 * averageFrameTime) { | 
|  | 132 | return std::nullopt; | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | const auto refreshRate = 1e9f / averageFrameTime; | 
|  | 137 | if (std::abs(refreshRate - mLastReportedRefreshRate) > MARGIN) { | 
|  | 138 | mLastReportedRefreshRate = refreshRate; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | ALOGV("Refresh rate: %.2f", mLastReportedRefreshRate); | 
|  | 142 | return mLastReportedRefreshRate; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | std::pair<LayerHistory::LayerVoteType, float> LayerInfoV2::getRefreshRate(nsecs_t now) { | 
|  | 146 | if (mLayerVote.type != LayerHistory::LayerVoteType::Heuristic) { | 
|  | 147 | return {mLayerVote.type, mLayerVote.fps}; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | if (!isFrequent(now)) { | 
|  | 151 | return {LayerHistory::LayerVoteType::Min, 0}; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | auto refreshRate = calculateRefreshRateIfPossible(); | 
|  | 155 | if (refreshRate.has_value()) { | 
|  | 156 | return {LayerHistory::LayerVoteType::Heuristic, refreshRate.value()}; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | return {LayerHistory::LayerVoteType::Max, 0}; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | } // namespace android::scheduler |