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