| 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 |  | 
| Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 61 | void trace(const wp<Layer>& weak, const LayerInfoV2& info, LayerHistory::LayerVoteType type, | 
|  | 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); | 
|  | 74 | traceType(LayerHistory::LayerVoteType::Min, 1); | 
|  | 75 | traceType(LayerHistory::LayerVoteType::Max, 1); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 76 |  | 
| Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 77 | ALOGD("%s: %s @ %d Hz", __FUNCTION__, layer->getName().c_str(), fps); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 78 | } | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 79 | } // namespace | 
|  | 80 |  | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 81 | LayerHistoryV2::LayerHistoryV2(const scheduler::RefreshRateConfigs& refreshRateConfigs) | 
|  | 82 | : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) { | 
| Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 83 | LayerInfoV2::setTraceEnabled(mTraceEnabled); | 
| Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 84 | LayerInfoV2::setRefreshRateConfigs(refreshRateConfigs); | 
|  | 85 | } | 
|  | 86 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | LayerHistoryV2::~LayerHistoryV2() = default; | 
|  | 88 |  | 
|  | 89 | void LayerHistoryV2::registerLayer(Layer* layer, float /*lowRefreshRate*/, float highRefreshRate, | 
|  | 90 | LayerVoteType type) { | 
|  | 91 | const nsecs_t highRefreshRatePeriod = static_cast<nsecs_t>(1e9f / highRefreshRate); | 
| Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 92 | auto info = std::make_unique<LayerInfoV2>(layer->getName(), highRefreshRatePeriod, type); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 93 | std::lock_guard lock(mLock); | 
|  | 94 | mLayerInfos.emplace_back(layer, std::move(info)); | 
|  | 95 | } | 
|  | 96 |  | 
| Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 97 | void LayerHistoryV2::record(Layer* layer, nsecs_t presentTime, nsecs_t now, | 
|  | 98 | LayerUpdateType updateType) { | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 99 | std::lock_guard lock(mLock); | 
|  | 100 |  | 
|  | 101 | const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(), | 
|  | 102 | [layer](const auto& pair) { return pair.first == layer; }); | 
|  | 103 | LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer); | 
|  | 104 |  | 
|  | 105 | const auto& info = it->second; | 
| Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 106 | info->setLastPresentTime(presentTime, now, updateType, mConfigChangePending); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 107 |  | 
|  | 108 | // Activate layer if inactive. | 
|  | 109 | if (const auto end = activeLayers().end(); it >= end) { | 
|  | 110 | std::iter_swap(it, end); | 
|  | 111 | mActiveLayersEnd++; | 
|  | 112 | } | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | LayerHistoryV2::Summary LayerHistoryV2::summarize(nsecs_t now) { | 
|  | 116 | LayerHistory::Summary summary; | 
|  | 117 |  | 
|  | 118 | std::lock_guard lock(mLock); | 
|  | 119 |  | 
|  | 120 | partitionLayers(now); | 
|  | 121 |  | 
|  | 122 | for (const auto& [layer, info] : activeLayers()) { | 
|  | 123 | const auto strong = layer.promote(); | 
|  | 124 | if (!strong) { | 
|  | 125 | continue; | 
|  | 126 | } | 
|  | 127 |  | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 128 | const auto frameRateSelectionPriority = strong->getFrameRateSelectionPriority(); | 
|  | 129 | const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority); | 
|  | 130 | ALOGV("%s has priority: %d %s focused", strong->getName().c_str(), | 
|  | 131 | frameRateSelectionPriority, layerFocused ? "" : "not"); | 
| Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 132 |  | 
| Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 133 | const auto [type, refreshRate] = info->getRefreshRate(now); | 
|  | 134 | // Skip NoVote layer as those don't have any requirements | 
|  | 135 | if (type == LayerHistory::LayerVoteType::NoVote) { | 
|  | 136 | continue; | 
|  | 137 | } | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 138 |  | 
| Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 139 | // Compute the layer's position on the screen | 
|  | 140 | const Rect bounds = Rect(strong->getBounds()); | 
|  | 141 | const ui::Transform transform = strong->getTransform(); | 
|  | 142 | constexpr bool roundOutwards = true; | 
|  | 143 | Rect transformed = transform.transform(bounds, roundOutwards); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 144 |  | 
| Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 145 | const float layerArea = transformed.getWidth() * transformed.getHeight(); | 
|  | 146 | float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f; | 
| Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 147 | summary.push_back({strong->getName(), type, refreshRate, 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)) { | 
| Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 150 | trace(layer, *info, type, static_cast<int>(std::round(refreshRate))); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 151 | } | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | return summary; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | void LayerHistoryV2::partitionLayers(nsecs_t now) { | 
|  | 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 | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 176 | } | 
| Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 177 | }(); | 
| Ady Abraham | 5dde597 | 2020-05-13 10:50:54 -0700 | [diff] [blame] | 178 |  | 
|  | 179 | if (frameRate.rate > 0 || voteType == LayerVoteType::NoVote) { | 
|  | 180 | const auto type = layer->isVisible() ? voteType : LayerVoteType::NoVote; | 
|  | 181 | info->setLayerVote(type, frameRate.rate); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 182 | } else { | 
|  | 183 | info->resetLayerVote(); | 
|  | 184 | } | 
|  | 185 | continue; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | if (CC_UNLIKELY(mTraceEnabled)) { | 
| Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 189 | trace(weak, *info, LayerHistory::LayerVoteType::NoVote, 0); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 192 | info->onLayerInactive(now); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 193 | std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]); | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | // Collect expired layers after inactive layers. | 
|  | 197 | size_t end = mLayerInfos.size(); | 
|  | 198 | while (i < end) { | 
|  | 199 | if (mLayerInfos[i].first.promote()) { | 
|  | 200 | i++; | 
|  | 201 | } else { | 
|  | 202 | std::swap(mLayerInfos[i], mLayerInfos[--end]); | 
|  | 203 | } | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end()); | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | void LayerHistoryV2::clear() { | 
|  | 210 | std::lock_guard lock(mLock); | 
|  | 211 |  | 
|  | 212 | for (const auto& [layer, info] : activeLayers()) { | 
| Ady Abraham | 983e568 | 2020-05-28 16:49:18 -0700 | [diff] [blame] | 213 | info->clearHistory(systemTime()); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 214 | } | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 215 | } | 
| Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 216 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 217 | } // namespace android::scheduler::impl |