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> |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 25 | #include <gui/TraceUtils.h> |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | #include <utils/Timers.h> |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 28 | |
| 29 | #include <algorithm> |
| 30 | #include <cmath> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | #include "../Layer.h" |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 35 | #include "EventThread.h" |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 36 | #include "FlagManager.h" |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 37 | #include "LayerInfo.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 38 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 39 | namespace android::scheduler { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 43 | bool isLayerActive(const LayerInfo& info, nsecs_t threshold) { |
Ady Abraham | d6d8016 | 2023-10-23 12:57:41 -0700 | [diff] [blame] | 44 | if (FlagManager::getInstance().misc1() && !info.isVisible()) { |
Rachel Lee | 414f408 | 2023-10-23 14:48:44 -0700 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
| 48 | // Layers with an explicit frame rate or frame rate category are kept active, |
Rachel Lee | d0694bc | 2023-09-12 14:57:58 -0700 | [diff] [blame] | 49 | // but ignore NoVote. |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 50 | if (info.getSetFrameRateVote().isValid() && !info.getSetFrameRateVote().isNoVote()) { |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 51 | return true; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 52 | } |
Ady Abraham | 89089c9 | 2020-04-15 18:24:41 -0700 | [diff] [blame] | 53 | |
Alec Mouri | 89f5d4e | 2023-10-20 17:12:49 +0000 | [diff] [blame] | 54 | // Make all front buffered layers active |
| 55 | if (FlagManager::getInstance().vrr_config() && info.isFrontBuffered() && info.isVisible()) { |
| 56 | return true; |
| 57 | } |
| 58 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 59 | return info.isVisible() && info.getLastUpdatedTime() >= threshold; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool traceEnabled() { |
| 63 | return property_get_bool("debug.sf.layer_history_trace", false); |
| 64 | } |
| 65 | |
| 66 | bool useFrameRatePriority() { |
| 67 | char value[PROPERTY_VALUE_MAX]; |
| 68 | property_get("debug.sf.use_frame_rate_priority", value, "1"); |
| 69 | return atoi(value); |
| 70 | } |
| 71 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 72 | void trace(const LayerInfo& info, LayerHistory::LayerVoteType type, int fps) { |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 73 | const auto traceType = [&](LayerHistory::LayerVoteType checkedType, int value) { |
| 74 | ATRACE_INT(info.getTraceTag(checkedType), type == checkedType ? value : 0); |
Ady Abraham | a6b676e | 2020-05-27 14:29:09 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 77 | traceType(LayerHistory::LayerVoteType::NoVote, 1); |
| 78 | traceType(LayerHistory::LayerVoteType::Heuristic, fps); |
| 79 | traceType(LayerHistory::LayerVoteType::ExplicitDefault, fps); |
| 80 | traceType(LayerHistory::LayerVoteType::ExplicitExactOrMultiple, fps); |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 81 | traceType(LayerHistory::LayerVoteType::ExplicitExact, fps); |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 82 | traceType(LayerHistory::LayerVoteType::Min, 1); |
| 83 | traceType(LayerHistory::LayerVoteType::Max, 1); |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 84 | traceType(LayerHistory::LayerVoteType::ExplicitCategory, 1); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 85 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 86 | ALOGD("%s: %s @ %d Hz", __FUNCTION__, info.getName().c_str(), fps); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | } |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 88 | |
Vishnu Nair | 3fbe326 | 2023-09-29 17:07:00 -0700 | [diff] [blame] | 89 | LayerHistory::LayerVoteType getVoteType(FrameRateCompatibility compatibility, |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 90 | bool contentDetectionEnabled) { |
| 91 | LayerHistory::LayerVoteType voteType; |
Vishnu Nair | 3fbe326 | 2023-09-29 17:07:00 -0700 | [diff] [blame] | 92 | if (!contentDetectionEnabled || compatibility == FrameRateCompatibility::NoVote) { |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 93 | voteType = LayerHistory::LayerVoteType::NoVote; |
Vishnu Nair | 3fbe326 | 2023-09-29 17:07:00 -0700 | [diff] [blame] | 94 | } else if (compatibility == FrameRateCompatibility::Min) { |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 95 | voteType = LayerHistory::LayerVoteType::Min; |
| 96 | } else { |
| 97 | voteType = LayerHistory::LayerVoteType::Heuristic; |
| 98 | } |
| 99 | return voteType; |
| 100 | } |
| 101 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 102 | } // namespace |
| 103 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 104 | LayerHistory::LayerHistory() |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 105 | : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) { |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 106 | LayerInfo::setTraceEnabled(mTraceEnabled); |
Ady Abraham | b1b9d41 | 2020-06-01 19:53:52 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 109 | LayerHistory::~LayerHistory() = default; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 110 | |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 111 | void LayerHistory::registerLayer(Layer* layer, bool contentDetectionEnabled) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 112 | std::lock_guard lock(mLock); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 113 | LOG_ALWAYS_FATAL_IF(findLayer(layer->getSequence()).first != LayerStatus::NotFound, |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 114 | "%s already registered", layer->getName().c_str()); |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 115 | LayerVoteType type = |
| 116 | getVoteType(layer->getDefaultFrameRateCompatibility(), contentDetectionEnabled); |
Ady Abraham | be09aad | 2021-05-03 18:59:38 -0700 | [diff] [blame] | 117 | auto info = std::make_unique<LayerInfo>(layer->getName(), layer->getOwnerUid(), type); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 118 | |
| 119 | // The layer can be placed on either map, it is assumed that partitionLayers() will be called |
| 120 | // to correct them. |
| 121 | mInactiveLayerInfos.insert({layer->getSequence(), std::make_pair(layer, std::move(info))}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 124 | void LayerHistory::deregisterLayer(Layer* layer) { |
| 125 | std::lock_guard lock(mLock); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 126 | if (!mActiveLayerInfos.erase(layer->getSequence())) { |
| 127 | if (!mInactiveLayerInfos.erase(layer->getSequence())) { |
| 128 | LOG_ALWAYS_FATAL("%s: unknown layer %p", __FUNCTION__, layer); |
| 129 | } |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 130 | } |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 133 | void LayerHistory::record(int32_t id, const LayerProps& layerProps, nsecs_t presentTime, |
| 134 | nsecs_t now, LayerUpdateType updateType) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 135 | std::lock_guard lock(mLock); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 136 | auto [found, layerPair] = findLayer(id); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 137 | if (found == LayerStatus::NotFound) { |
Ady Abraham | be09aad | 2021-05-03 18:59:38 -0700 | [diff] [blame] | 138 | // Offscreen layer |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 139 | ALOGV("%s: %d not registered", __func__, id); |
Ady Abraham | be09aad | 2021-05-03 18:59:38 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 142 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 143 | const auto& info = layerPair->second; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 144 | info->setLastPresentTime(presentTime, now, updateType, mModeChangePending, layerProps); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 145 | |
| 146 | // Activate layer if inactive. |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 147 | if (found == LayerStatus::LayerInInactiveMap) { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 148 | mActiveLayerInfos.insert( |
| 149 | {id, std::make_pair(layerPair->first, std::move(layerPair->second))}); |
| 150 | mInactiveLayerInfos.erase(id); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 154 | void LayerHistory::setDefaultFrameRateCompatibility(int32_t id, |
| 155 | FrameRateCompatibility frameRateCompatibility, |
| 156 | bool contentDetectionEnabled) { |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 157 | std::lock_guard lock(mLock); |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 158 | |
| 159 | auto [found, layerPair] = findLayer(id); |
| 160 | if (found == LayerStatus::NotFound) { |
| 161 | // Offscreen layer |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 162 | ALOGV("%s: %d not registered", __func__, id); |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | const auto& info = layerPair->second; |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 167 | info->setDefaultLayerVote(getVoteType(frameRateCompatibility, contentDetectionEnabled)); |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame^] | 170 | void LayerHistory::setLayerProperties(int32_t id, const LayerProps& properties) { |
| 171 | std::lock_guard lock(mLock); |
| 172 | |
| 173 | auto [found, layerPair] = findLayer(id); |
| 174 | if (found == LayerStatus::NotFound) { |
| 175 | // Offscreen layer |
| 176 | ALOGV("%s: %d not registered", __func__, id); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | const auto& info = layerPair->second; |
| 181 | info->setProperties(properties); |
| 182 | |
| 183 | // Activate layer if inactive and visible. |
| 184 | if (found == LayerStatus::LayerInInactiveMap && info->isVisible()) { |
| 185 | mActiveLayerInfos.insert( |
| 186 | {id, std::make_pair(layerPair->first, std::move(layerPair->second))}); |
| 187 | mInactiveLayerInfos.erase(id); |
| 188 | } |
| 189 | } |
| 190 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 191 | auto LayerHistory::summarize(const RefreshRateSelector& selector, nsecs_t now) -> Summary { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 192 | ATRACE_CALL(); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 193 | Summary summary; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 194 | |
| 195 | std::lock_guard lock(mLock); |
| 196 | |
| 197 | partitionLayers(now); |
| 198 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 199 | for (const auto& [key, value] : mActiveLayerInfos) { |
| 200 | auto& info = value.second; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 201 | const auto frameRateSelectionPriority = info->getFrameRateSelectionPriority(); |
Ady Abraham | aae5ed5 | 2020-06-26 09:32:43 -0700 | [diff] [blame] | 202 | const auto layerFocused = Layer::isLayerFocusedBasedOnPriority(frameRateSelectionPriority); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 203 | ALOGV("%s has priority: %d %s focused", info->getName().c_str(), frameRateSelectionPriority, |
| 204 | layerFocused ? "" : "not"); |
Ana Krulec | 3803b8d | 2020-02-03 16:35:46 -0800 | [diff] [blame] | 205 | |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 206 | ATRACE_FORMAT("%s", info->getName().c_str()); |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 207 | const auto votes = info->getRefreshRateVote(selector, now); |
| 208 | for (LayerInfo::LayerVote vote : votes) { |
| 209 | if (vote.isNoVote()) { |
| 210 | continue; |
| 211 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 212 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 213 | // Compute the layer's position on the screen |
| 214 | const Rect bounds = Rect(info->getBounds()); |
| 215 | const ui::Transform transform = info->getTransform(); |
| 216 | constexpr bool roundOutwards = true; |
| 217 | Rect transformed = transform.transform(bounds, roundOutwards); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 218 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 219 | const float layerArea = transformed.getWidth() * transformed.getHeight(); |
| 220 | float weight = mDisplayArea ? layerArea / mDisplayArea : 0.0f; |
| 221 | const std::string categoryString = vote.category == FrameRateCategory::Default |
Rachel Lee | 94ff799 | 2023-08-31 15:15:22 -0700 | [diff] [blame] | 222 | ? "" |
| 223 | : base::StringPrintf("category=%s", ftl::enum_string(vote.category).c_str()); |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 224 | ATRACE_FORMAT_INSTANT("%s %s %s (%d%)", ftl::enum_string(vote.type).c_str(), |
| 225 | to_string(vote.fps).c_str(), categoryString.c_str(), |
| 226 | weight * 100); |
| 227 | summary.push_back({info->getName(), info->getOwnerUid(), vote.type, vote.fps, |
Rachel Lee | 67afbea | 2023-09-28 15:35:07 -0700 | [diff] [blame] | 228 | vote.seamlessness, vote.category, vote.categorySmoothSwitchOnly, |
| 229 | weight, layerFocused}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 230 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 231 | if (CC_UNLIKELY(mTraceEnabled)) { |
| 232 | trace(*info, vote.type, vote.fps.getIntValue()); |
| 233 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | return summary; |
| 238 | } |
| 239 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 240 | void LayerHistory::partitionLayers(nsecs_t now) { |
Ady Abraham | 73c3df5 | 2023-01-12 18:09:31 -0800 | [diff] [blame] | 241 | ATRACE_CALL(); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 242 | const nsecs_t threshold = getActiveLayerThreshold(now); |
| 243 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 244 | // iterate over inactive map |
| 245 | LayerInfos::iterator it = mInactiveLayerInfos.begin(); |
| 246 | while (it != mInactiveLayerInfos.end()) { |
| 247 | auto& [layerUnsafe, info] = it->second; |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 248 | if (isLayerActive(*info, threshold)) { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 249 | // move this to the active map |
| 250 | |
| 251 | mActiveLayerInfos.insert({it->first, std::move(it->second)}); |
| 252 | it = mInactiveLayerInfos.erase(it); |
| 253 | } else { |
| 254 | if (CC_UNLIKELY(mTraceEnabled)) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 255 | trace(*info, LayerVoteType::NoVote, 0); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 256 | } |
| 257 | info->onLayerInactive(now); |
| 258 | it++; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // iterate over active map |
| 263 | it = mActiveLayerInfos.begin(); |
| 264 | while (it != mActiveLayerInfos.end()) { |
| 265 | auto& [layerUnsafe, info] = it->second; |
| 266 | if (isLayerActive(*info, threshold)) { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 267 | // Set layer vote if set |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 268 | const auto frameRate = info->getSetFrameRateVote(); |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 269 | const auto voteType = [&]() { |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 270 | switch (frameRate.vote.type) { |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 271 | case Layer::FrameRateCompatibility::Default: |
| 272 | return LayerVoteType::ExplicitDefault; |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 273 | case Layer::FrameRateCompatibility::Min: |
| 274 | return LayerVoteType::Min; |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 275 | case Layer::FrameRateCompatibility::ExactOrMultiple: |
| 276 | return LayerVoteType::ExplicitExactOrMultiple; |
| 277 | case Layer::FrameRateCompatibility::NoVote: |
| 278 | return LayerVoteType::NoVote; |
Ady Abraham | dd5bfa9 | 2021-01-07 17:56:08 -0800 | [diff] [blame] | 279 | case Layer::FrameRateCompatibility::Exact: |
| 280 | return LayerVoteType::ExplicitExact; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 281 | } |
Ady Abraham | 71c437d | 2020-01-31 15:56:57 -0800 | [diff] [blame] | 282 | }(); |
Ady Abraham | 5dde597 | 2020-05-13 10:50:54 -0700 | [diff] [blame] | 283 | |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 284 | if (frameRate.isValid()) { |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 285 | const auto type = info->isVisible() ? voteType : LayerVoteType::NoVote; |
Rachel Lee | ce6e004 | 2023-06-27 11:22:54 -0700 | [diff] [blame] | 286 | info->setLayerVote({type, frameRate.vote.rate, frameRate.vote.seamlessness, |
| 287 | frameRate.category}); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 288 | } else { |
| 289 | info->resetLayerVote(); |
| 290 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 291 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 292 | it++; |
| 293 | } else { |
| 294 | if (CC_UNLIKELY(mTraceEnabled)) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 295 | trace(*info, LayerVoteType::NoVote, 0); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 296 | } |
| 297 | info->onLayerInactive(now); |
| 298 | // move this to the inactive map |
| 299 | mInactiveLayerInfos.insert({it->first, std::move(it->second)}); |
| 300 | it = mActiveLayerInfos.erase(it); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 301 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 302 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 305 | void LayerHistory::clear() { |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 306 | std::lock_guard lock(mLock); |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 307 | for (const auto& [key, value] : mActiveLayerInfos) { |
| 308 | value.second->clearHistory(systemTime()); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 309 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 310 | } |
Ady Abraham | 0ccd79b | 2020-06-10 10:11:17 -0700 | [diff] [blame] | 311 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 312 | std::string LayerHistory::dump() const { |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 313 | std::lock_guard lock(mLock); |
Dominik Laskowski | 03cfce8 | 2022-11-02 12:13:29 -0400 | [diff] [blame] | 314 | return base::StringPrintf("{size=%zu, active=%zu}", |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 315 | mActiveLayerInfos.size() + mInactiveLayerInfos.size(), |
| 316 | mActiveLayerInfos.size()); |
| 317 | } |
| 318 | |
| 319 | float LayerHistory::getLayerFramerate(nsecs_t now, int32_t id) const { |
| 320 | std::lock_guard lock(mLock); |
| 321 | auto [found, layerPair] = findLayer(id); |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 322 | if (found != LayerStatus::NotFound) { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 323 | return layerPair->second->getFps(now).getValue(); |
| 324 | } |
| 325 | return 0.f; |
| 326 | } |
| 327 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 328 | auto LayerHistory::findLayer(int32_t id) -> std::pair<LayerStatus, LayerPair*> { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 329 | // the layer could be in either the active or inactive map, try both |
| 330 | auto it = mActiveLayerInfos.find(id); |
| 331 | if (it != mActiveLayerInfos.end()) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 332 | return {LayerStatus::LayerInActiveMap, &(it->second)}; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 333 | } |
| 334 | it = mInactiveLayerInfos.find(id); |
| 335 | if (it != mInactiveLayerInfos.end()) { |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 336 | return {LayerStatus::LayerInInactiveMap, &(it->second)}; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 337 | } |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 338 | return {LayerStatus::NotFound, nullptr}; |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 341 | bool LayerHistory::isSmallDirtyArea(uint32_t dirtyArea, float threshold) const { |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 342 | const float ratio = (float)dirtyArea / mDisplayArea; |
Tony Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 343 | const bool isSmallDirty = ratio <= threshold; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 344 | ATRACE_FORMAT_INSTANT("small dirty=%s, ratio=%.3f", isSmallDirty ? "true" : "false", ratio); |
| 345 | return isSmallDirty; |
| 346 | } |
| 347 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 348 | } // namespace android::scheduler |