Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LayerHistory" |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 21 | #include "LayerHistory.h" |
| 22 | |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 24 | #include <cutils/properties.h> |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <utils/Timers.h> |
| 27 | #include <utils/Trace.h> |
| 28 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <cmath> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | #include "../Layer.h" |
| 35 | #include "LayerInfo.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 36 | #include "SchedulerUtils.h" |
| 37 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 38 | namespace android::scheduler::impl { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 39 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 40 | namespace { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 41 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 42 | bool isLayerActive(const Layer& layer, const LayerInfo& info, nsecs_t threshold) { |
Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 43 | if (layer.getFrameRateForLayerTree().rate > 0) { |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 44 | return layer.isVisible(); |
| 45 | } |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 46 | return layer.isVisible() && info.getLastUpdatedTime() >= threshold; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool traceEnabled() { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 50 | char value[PROPERTY_VALUE_MAX]; |
| 51 | property_get("debug.sf.layer_history_trace", value, "0"); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 52 | return atoi(value); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 53 | } |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 54 | |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 55 | bool useFrameRatePriority() { |
| 56 | char value[PROPERTY_VALUE_MAX]; |
| 57 | property_get("debug.sf.use_frame_rate_priority", value, "1"); |
| 58 | return atoi(value); |
| 59 | } |
| 60 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 61 | void trace(const wp<Layer>& weak, int fps) { |
| 62 | const auto layer = weak.promote(); |
| 63 | if (!layer) return; |
| 64 | |
| 65 | const auto& name = layer->getName(); |
| 66 | const auto tag = "LFPS " + name; |
| 67 | ATRACE_INT(tag.c_str(), fps); |
| 68 | ALOGD("%s: %s @ %d Hz", __FUNCTION__, name.c_str(), fps); |
| 69 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 70 | } // namespace |
| 71 | |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 72 | LayerHistory::LayerHistory() |
| 73 | : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {} |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 74 | LayerHistory::~LayerHistory() = default; |
| 75 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 76 | void LayerHistory::registerLayer(Layer* layer, float lowRefreshRate, float highRefreshRate, |
| 77 | LayerVoteType /*type*/) { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 78 | auto info = std::make_unique<LayerInfo>(lowRefreshRate, highRefreshRate); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 79 | std::lock_guard lock(mLock); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 80 | mLayerInfos.emplace_back(layer, std::move(info)); |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 83 | void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now, |
| 84 | LayerUpdateType /*updateType*/) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 85 | std::lock_guard lock(mLock); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 86 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 87 | const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(), |
| 88 | [layer](const auto& pair) { return pair.first == layer; }); |
| 89 | LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer); |
| 90 | |
| 91 | const auto& info = it->second; |
| 92 | info->setLastPresentTime(presentTime, now); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 93 | |
| 94 | // Activate layer if inactive. |
| 95 | if (const auto end = activeLayers().end(); it >= end) { |
| 96 | std::iter_swap(it, end); |
| 97 | mActiveLayersEnd++; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 98 | } |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 101 | LayerHistory::Summary LayerHistory::summarize(nsecs_t now) { |
Ady Abraham | 4ccdcb4 | 2020-02-11 17:34:34 -0800 | [diff] [blame] | 102 | ATRACE_CALL(); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 103 | std::lock_guard lock(mLock); |
| 104 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 105 | partitionLayers(now); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 106 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 107 | LayerHistory::Summary summary; |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 108 | for (const auto& [weakLayer, info] : activeLayers()) { |
| 109 | const bool recent = info->isRecentlyActive(now); |
| 110 | auto layer = weakLayer.promote(); |
| 111 | // Only use the layer if the reference still exists. |
| 112 | if (layer || CC_UNLIKELY(mTraceEnabled)) { |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 113 | const auto layerFocused = |
| 114 | Layer::isLayerFocusedBasedOnPriority(layer->getFrameRateSelectionPriority()); |
Steven Thomas | 540730a | 2020-01-08 20:12:42 -0800 | [diff] [blame] | 115 | // Check if frame rate was set on layer. |
Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 116 | const auto frameRate = layer->getFrameRateForLayerTree(); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 117 | if (frameRate.rate > 0.f) { |
| 118 | const auto voteType = [&]() { |
| 119 | switch (frameRate.type) { |
| 120 | case Layer::FrameRateCompatibility::Default: |
| 121 | return LayerVoteType::ExplicitDefault; |
| 122 | case Layer::FrameRateCompatibility::ExactOrMultiple: |
| 123 | return LayerVoteType::ExplicitExactOrMultiple; |
| 124 | case Layer::FrameRateCompatibility::NoVote: |
| 125 | return LayerVoteType::NoVote; |
| 126 | } |
| 127 | }(); |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 128 | summary.push_back({layer->getName(), voteType, frameRate.rate, /* weight */ 1.0f, |
| 129 | layerFocused}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 130 | } else if (recent) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 131 | summary.push_back({layer->getName(), LayerVoteType::Heuristic, |
| 132 | info->getRefreshRate(now), |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 133 | /* weight */ 1.0f, layerFocused}); |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (CC_UNLIKELY(mTraceEnabled)) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 137 | trace(weakLayer, round<int>(frameRate.rate)); |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 138 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 141 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 142 | return summary; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 145 | void LayerHistory::partitionLayers(nsecs_t now) { |
| 146 | const nsecs_t threshold = getActiveLayerThreshold(now); |
| 147 | |
| 148 | // Collect expired and inactive layers after active layers. |
| 149 | size_t i = 0; |
| 150 | while (i < mActiveLayersEnd) { |
| 151 | auto& [weak, info] = mLayerInfos[i]; |
| 152 | if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) { |
| 153 | i++; |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | if (CC_UNLIKELY(mTraceEnabled)) { |
| 158 | trace(weak, 0); |
| 159 | } |
| 160 | |
| 161 | info->clearHistory(); |
| 162 | std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]); |
| 163 | } |
| 164 | |
| 165 | // Collect expired layers after inactive layers. |
| 166 | size_t end = mLayerInfos.size(); |
| 167 | while (i < end) { |
| 168 | if (mLayerInfos[i].first.promote()) { |
| 169 | i++; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 170 | } else { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 171 | std::swap(mLayerInfos[i], mLayerInfos[--end]); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 174 | |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 175 | mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end()); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 178 | void LayerHistory::clear() { |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 179 | std::lock_guard lock(mLock); |
| 180 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 181 | for (const auto& [layer, info] : activeLayers()) { |
| 182 | info->clearHistory(); |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 183 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 184 | |
| 185 | mActiveLayersEnd = 0; |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 186 | } |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 187 | |
| 188 | std::string LayerHistory::dump() const { |
| 189 | std::lock_guard lock(mLock); |
| 190 | return base::StringPrintf("LayerHistory{size=%zu, active=%zu}", mLayerInfos.size(), |
| 191 | mActiveLayersEnd); |
| 192 | } |
| 193 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 194 | } // namespace android::scheduler::impl |