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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "LayerHistory" |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 23 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 24 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 25 | #include "LayerHistory.h" |
| 26 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 27 | #include <cutils/properties.h> |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 28 | #include <utils/Log.h> |
| 29 | #include <utils/Timers.h> |
| 30 | #include <utils/Trace.h> |
| 31 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | #include <cmath> |
| 34 | #include <string> |
| 35 | #include <utility> |
| 36 | |
| 37 | #include "../Layer.h" |
| 38 | #include "LayerInfo.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 39 | #include "SchedulerUtils.h" |
| 40 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 41 | namespace android::scheduler::impl { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 42 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 43 | namespace { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 44 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 45 | bool isLayerActive(const Layer& layer, const LayerInfo& info, nsecs_t threshold) { |
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 | } |
| 70 | |
| 71 | } // namespace |
| 72 | |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 73 | LayerHistory::LayerHistory() |
| 74 | : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {} |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 75 | LayerHistory::~LayerHistory() = default; |
| 76 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 77 | void LayerHistory::registerLayer(Layer* layer, float lowRefreshRate, float highRefreshRate) { |
| 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 | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 83 | void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now) { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 84 | std::lock_guard lock(mLock); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 85 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 86 | const auto it = std::find_if(mLayerInfos.begin(), mLayerInfos.end(), |
| 87 | [layer](const auto& pair) { return pair.first == layer; }); |
| 88 | LOG_FATAL_IF(it == mLayerInfos.end(), "%s: unknown layer %p", __FUNCTION__, layer); |
| 89 | |
| 90 | const auto& info = it->second; |
| 91 | info->setLastPresentTime(presentTime, now); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 92 | |
| 93 | // Activate layer if inactive. |
| 94 | if (const auto end = activeLayers().end(); it >= end) { |
| 95 | std::iter_swap(it, end); |
| 96 | mActiveLayersEnd++; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 97 | } |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 100 | LayerHistory::Summary LayerHistory::summarize(nsecs_t now) { |
| 101 | float maxRefreshRate = 0; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 102 | |
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 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 107 | // Find the maximum refresh rate among recently active layers. |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 108 | for (const auto& [activeLayer, info] : activeLayers()) { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 109 | const bool recent = info->isRecentlyActive(now); |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 110 | |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 111 | if (recent || CC_UNLIKELY(mTraceEnabled)) { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 112 | const float refreshRate = info->getRefreshRate(now); |
| 113 | if (recent && refreshRate > maxRefreshRate) { |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 114 | if (const auto layer = activeLayer.promote(); layer) { |
| 115 | const int32_t priority = layer->getFrameRateSelectionPriority(); |
| 116 | // TODO(b/142507166): This is where the scoring algorithm should live. |
| 117 | // Layers should be organized by priority |
| 118 | ALOGD("Layer has priority: %d", priority); |
| 119 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 120 | maxRefreshRate = refreshRate; |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (CC_UNLIKELY(mTraceEnabled)) { |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 124 | trace(activeLayer, std::round(refreshRate)); |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 125 | } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Dominik Laskowski | a7f850a | 2019-10-04 18:20:17 -0700 | [diff] [blame] | 128 | if (CC_UNLIKELY(mTraceEnabled)) { |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 129 | ALOGD("%s: maxRefreshRate=%.2f", __FUNCTION__, maxRefreshRate); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 132 | return {maxRefreshRate}; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 135 | void LayerHistory::partitionLayers(nsecs_t now) { |
| 136 | const nsecs_t threshold = getActiveLayerThreshold(now); |
| 137 | |
| 138 | // Collect expired and inactive layers after active layers. |
| 139 | size_t i = 0; |
| 140 | while (i < mActiveLayersEnd) { |
| 141 | auto& [weak, info] = mLayerInfos[i]; |
| 142 | if (const auto layer = weak.promote(); layer && isLayerActive(*layer, *info, threshold)) { |
| 143 | i++; |
| 144 | continue; |
| 145 | } |
| 146 | |
| 147 | if (CC_UNLIKELY(mTraceEnabled)) { |
| 148 | trace(weak, 0); |
| 149 | } |
| 150 | |
| 151 | info->clearHistory(); |
| 152 | std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]); |
| 153 | } |
| 154 | |
| 155 | // Collect expired layers after inactive layers. |
| 156 | size_t end = mLayerInfos.size(); |
| 157 | while (i < end) { |
| 158 | if (mLayerInfos[i].first.promote()) { |
| 159 | i++; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 160 | } else { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 161 | std::swap(mLayerInfos[i], mLayerInfos[--end]); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 164 | |
| 165 | mLayerInfos.erase(mLayerInfos.begin() + end, mLayerInfos.end()); |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 168 | void LayerHistory::clear() { |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 169 | std::lock_guard lock(mLock); |
| 170 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 171 | for (const auto& [layer, info] : activeLayers()) { |
| 172 | info->clearHistory(); |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 173 | } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 174 | |
| 175 | mActiveLayersEnd = 0; |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 178 | } // namespace android::scheduler::impl |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 179 | |
| 180 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 181 | #pragma clang diagnostic pop // ignored "-Wconversion" |