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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LayerHistoryV2" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
| 21 | #include "LayerHistory.h" |
| 22 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/Timers.h> |
| 26 | #include <utils/Trace.h> |
| 27 | |
| 28 | #include <algorithm> |
| 29 | #include <cmath> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | |
| 33 | #include "../Layer.h" |
| 34 | #include "SchedulerUtils.h" |
| 35 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 36 | #include "LayerInfoV2.h" |
| 37 | |
| 38 | namespace android::scheduler::impl { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | bool isLayerActive(const Layer& layer, const LayerInfoV2& info, nsecs_t threshold) { |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 43 | // Layers with an explicit vote are always kept active |
Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 44 | if (layer.getFrameRateForLayerTree().rate > 0) { |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 45 | return true; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 46 | } |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 47 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 48 | return layer.isVisible() && info.getLastUpdatedTime() >= threshold; |
| 49 | } |
| 50 | |
| 51 | bool traceEnabled() { |
| 52 | return property_get_bool("debug.sf.layer_history_trace", false); |
| 53 | } |
| 54 | |
| 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 | |
| 61 | void trace(const wp<Layer>& weak, LayerHistory::LayerVoteType type, int fps) { |
| 62 | const auto layer = weak.promote(); |
| 63 | if (!layer) return; |
| 64 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 65 | const auto makeTag = [layer](LayerHistory::LayerVoteType vote) { |
| 66 | return "LFPS " + RefreshRateConfigs::layerVoteTypeString(vote) + " " + layer->getName(); |
| 67 | }; |
| 68 | |
| 69 | const auto noVoteTag = makeTag(LayerHistory::LayerVoteType::NoVote); |
| 70 | const auto heuristicVoteTag = makeTag(LayerHistory::LayerVoteType::Heuristic); |
| 71 | const auto explicitDefaultVoteTag = makeTag(LayerHistory::LayerVoteType::ExplicitDefault); |
| 72 | const auto explicitExactOrMultipleVoteTag = |
| 73 | makeTag(LayerHistory::LayerVoteType::ExplicitExactOrMultiple); |
| 74 | const auto minVoteTag = makeTag(LayerHistory::LayerVoteType::Min); |
| 75 | const auto maxVoteTag = makeTag(LayerHistory::LayerVoteType::Max); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 76 | |
| 77 | ATRACE_INT(noVoteTag.c_str(), type == LayerHistory::LayerVoteType::NoVote ? 1 : 0); |
| 78 | ATRACE_INT(heuristicVoteTag.c_str(), type == LayerHistory::LayerVoteType::Heuristic ? fps : 0); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 79 | ATRACE_INT(explicitDefaultVoteTag.c_str(), |
| 80 | type == LayerHistory::LayerVoteType::ExplicitDefault ? fps : 0); |
| 81 | ATRACE_INT(explicitExactOrMultipleVoteTag.c_str(), |
| 82 | type == LayerHistory::LayerVoteType::ExplicitExactOrMultiple ? fps : 0); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 83 | ATRACE_INT(minVoteTag.c_str(), type == LayerHistory::LayerVoteType::Min ? 1 : 0); |
| 84 | ATRACE_INT(maxVoteTag.c_str(), type == LayerHistory::LayerVoteType::Max ? 1 : 0); |
| 85 | |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 86 | ALOGD("%s: %s @ %d Hz", __FUNCTION__, layer->getName().c_str(), fps); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 88 | } // namespace |
| 89 | |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame^] | 90 | LayerHistoryV2::LayerHistoryV2(const scheduler::RefreshRateConfigs& refreshRateConfigs) |
| 91 | : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) { |
| 92 | LayerInfoV2::setRefreshRateConfigs(refreshRateConfigs); |
| 93 | } |
| 94 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 95 | LayerHistoryV2::~LayerHistoryV2() = default; |
| 96 | |
| 97 | void LayerHistoryV2::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate, |
| 98 | LayerVoteType type) { |
| 99 | const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate); |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 100 | auto info = std::make_unique<LayerInfoV2>(layer->getName(), highRefreshRatePeriod, type); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 101 | std::lock_guard lock(mLock); |
| 102 | mLayerInfos.emplace_back(layer, std::move(info)); |
| 103 | } |
| 104 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 105 | void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now, |
| 106 | LayerUpdateType updateType) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 107 | std::lock_guard lock(mLock); |
| 108 | |
| 109 | const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(), |
| 110 | [layer](const auto& pair) { return pair.first == layer; }); |
| 111 | LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer); |
| 112 | |
| 113 | const auto& info = it->second; |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 114 | info->setLastPresentTime(presentTime, now, updateType, mConfigChangePending); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 115 | |
| 116 | // Activate layer if inactive. |
| 117 | if (const auto end = activeLayers().end(); it >= end) { |
| 118 | std::iter_swap(it, end); |
| 119 | mActiveLayersEnd++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) { |
| 124 | LayerHistory::Summary summary; |
| 125 | |
| 126 | std::lock_guard lock(mLock); |
| 127 | |
| 128 | partitionLayers(now); |
| 129 | |
| 130 | for (const auto& [layer, info] : activeLayers()) { |
| 131 | const auto strong = layer.promote(); |
| 132 | if (!strong) { |
| 133 | continue; |
| 134 | } |
| 135 | |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 136 | // TODO(b/144307188): This needs to be plugged into layer summary as |
| 137 | // an additional parameter. |
| 138 | ALOGV("Layer has priority: %d", strong->getFrameRateSelectionPriority()); |
| 139 | |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 140 | const auto [type, refreshRate] = info->getRefreshRate(now); |
| 141 | // Skip NoVote layer as those don't have any requirements |
| 142 | if (type == LayerHistory::LayerVoteType::NoVote) { |
| 143 | continue; |
| 144 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 145 | |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 146 | // Compute the layer's position on the screen |
| 147 | const Rect bounds = Rect(strong->getBounds()); |
| 148 | const ui::Transform transform = strong->getTransform(); |
| 149 | constexpr bool roundOutwards = true; |
| 150 | Rect transformed = transform.transform(bounds, roundOutwards); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 151 | |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 152 | const float layerArea = transformed.getWidth() * transformed.getHeight(); |
| 153 | float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f; |
| 154 | summary.push_back({strong->getName(), type, refreshRate, weight}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 155 | |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 156 | if (CC_UNLIKELY(mTraceEnabled)) { |
| 157 | trace(layer, type, static_cast<int>(std::round(refreshRate))); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | return summary; |
| 162 | } |
| 163 | |
| 164 | void LayerHistoryV2::partitionLayers(nsecs_t now) { |
| 165 | const nsecs_t threshold = getActiveLayerThreshold(now); |
| 166 | |
| 167 | // Collect expired and inactive layers after active layers. |
| 168 | size_t i = 0; |
| 169 | while (i < mActiveLayersEnd) { |
| 170 | auto& [weak, info] = mLayerInfos[i]; |
| 171 | if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) { |
| 172 | i++; |
| 173 | // Set layer vote if set |
Ady Abraham | 60e42ea | 2020-03-09 19:17:31 -0700 | [diff] [blame] | 174 | const auto frameRate = layer->getFrameRateForLayerTree(); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 175 | const auto voteType = [&]() { |
| 176 | switch (frameRate.type) { |
| 177 | case Layer::FrameRateCompatibility::Default: |
| 178 | return LayerVoteType::ExplicitDefault; |
| 179 | case Layer::FrameRateCompatibility::ExactOrMultiple: |
| 180 | return LayerVoteType::ExplicitExactOrMultiple; |
| 181 | case Layer::FrameRateCompatibility::NoVote: |
| 182 | return LayerVoteType::NoVote; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 183 | } |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 184 | }(); |
Ady Abraham | 5dde597 | 2020-05-13 10:50:54 -0700 | [diff] [blame] | 185 | |
| 186 | if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) { |
| 187 | const auto type = layer->isVisible() ? voteType : LayerVoteType::NoVote; |
| 188 | info->setLayerVote(type, frameRate.rate); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 189 | } else { |
| 190 | info->resetLayerVote(); |
| 191 | } |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | if (CC_UNLIKELY(mTraceEnabled)) { |
| 196 | trace(weak, LayerHistory::LayerVoteType::NoVote, 0); |
| 197 | } |
| 198 | |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 199 | info->onLayerInactive(now); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 200 | std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]); |
| 201 | } |
| 202 | |
| 203 | // Collect expired layers after inactive layers. |
| 204 | size_t end = mLayerInfos.size(); |
| 205 | while (i < end) { |
| 206 | if (mLayerInfos[i].first.promote()) { |
| 207 | i++; |
| 208 | } else { |
| 209 | std::swap(mLayerInfos[i], mLayerInfos[--end]); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end()); |
| 214 | } |
| 215 | |
| 216 | void LayerHistoryV2::clear() { |
| 217 | std::lock_guard lock(mLock); |
| 218 | |
| 219 | for (const auto& [layer, info] : activeLayers()) { |
Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 220 | info->clearHistory(systemTime()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 221 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 222 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 223 | } // namespace android::scheduler::impl |