blob: 9aada114735a9134b8921b61b8a182a4a75a3331 [file] [log] [blame]
Ana Krulec61f86db2018-11-19 14:16:35 +01001/*
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
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070017#undef LOG_TAG
18#define LOG_TAG "LayerHistory"
Ady Abraham09bd3922019-04-08 10:44:56 -070019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
Ana Krulec61f86db2018-11-19 14:16:35 +010021#include "LayerHistory.h"
22
Ady Abraham09bd3922019-04-08 10:44:56 -070023#include <cutils/properties.h>
Ana Krulec61f86db2018-11-19 14:16:35 +010024#include <utils/Log.h>
25#include <utils/Timers.h>
26#include <utils/Trace.h>
27
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070028#include <algorithm>
29#include <cmath>
30#include <string>
31#include <utility>
32
33#include "../Layer.h"
34#include "LayerInfo.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010035#include "SchedulerUtils.h"
36
Ady Abrahame3ed2f92020-01-06 17:01:28 -080037namespace android::scheduler::impl {
Ana Krulec61f86db2018-11-19 14:16:35 +010038
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070039namespace {
Ady Abraham09bd3922019-04-08 10:44:56 -070040
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070041bool isLayerActive(const Layer& layer, const LayerInfo& info, nsecs_t threshold) {
Ady Abraham71c437d2020-01-31 15:56:57 -080042 if (layer.getFrameRate().rate > 0) {
Steven Thomas540730a2020-01-08 20:12:42 -080043 return layer.isVisible();
44 }
Ady Abraham2139f732019-11-13 18:56:40 -080045 return layer.isVisible() && info.getLastUpdatedTime() >= threshold;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070046}
47
48bool traceEnabled() {
Ady Abraham09bd3922019-04-08 10:44:56 -070049 char value[PROPERTY_VALUE_MAX];
50 property_get("debug.sf.layer_history_trace", value, "0");
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070051 return atoi(value);
Ady Abraham09bd3922019-04-08 10:44:56 -070052}
Ana Krulec61f86db2018-11-19 14:16:35 +010053
Ana Krulecc84d09b2019-11-02 23:10:29 +010054bool useFrameRatePriority() {
55 char value[PROPERTY_VALUE_MAX];
56 property_get("debug.sf.use_frame_rate_priority", value, "1");
57 return atoi(value);
58}
59
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070060void trace(const wp<Layer>& weak, int fps) {
61 const auto layer = weak.promote();
62 if (!layer) return;
63
64 const auto& name = layer->getName();
65 const auto tag = "LFPS " + name;
66 ATRACE_INT(tag.c_str(), fps);
67 ALOGD("%s: %s @ %d Hz", __FUNCTION__, name.c_str(), fps);
68}
69
70} // namespace
71
Ana Krulecc84d09b2019-11-02 23:10:29 +010072LayerHistory::LayerHistory()
73 : mTraceEnabled(traceEnabled()), mUseFrameRatePriority(useFrameRatePriority()) {}
Ana Krulec61f86db2018-11-19 14:16:35 +010074LayerHistory::~LayerHistory() = default;
75
Ady Abraham8a82ba62020-01-17 12:43:17 -080076void LayerHistory::registerLayer(Layer* layer, float lowRefreshRate, float highRefreshRate,
77 LayerVoteType /*type*/) {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070078 auto info = std::make_unique<LayerInfo>(lowRefreshRate, highRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -070079 std::lock_guard lock(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070080 mLayerInfos.emplace_back(layer, std::move(info));
Ana Krulec61f86db2018-11-19 14:16:35 +010081}
82
Ady Abraham2139f732019-11-13 18:56:40 -080083void LayerHistory::record(Layer* layer, nsecs_t presentTime, nsecs_t now) {
Ady Abraham09bd3922019-04-08 10:44:56 -070084 std::lock_guard lock(mLock);
Ady Abraham09bd3922019-04-08 10:44:56 -070085
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070086 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 Laskowskif7a09ed2019-10-07 13:54:18 -070092
93 // Activate layer if inactive.
94 if (const auto end = activeLayers().end(); it >= end) {
95 std::iter_swap(it, end);
96 mActiveLayersEnd++;
Ady Abraham09bd3922019-04-08 10:44:56 -070097 }
Ana Krulec61f86db2018-11-19 14:16:35 +010098}
99
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700100LayerHistory::Summary LayerHistory::summarize(nsecs_t now) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700101 std::lock_guard lock(mLock);
102
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700103 partitionLayers(now);
Ady Abraham09bd3922019-04-08 10:44:56 -0700104
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700105 // Find the maximum refresh rate among recently active layers.
Ana Krulecc84d09b2019-11-02 23:10:29 +0100106 for (const auto& [activeLayer, info] : activeLayers()) {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700107 const bool recent = info->isRecentlyActive(now);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100108
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700109 if (recent || CC_UNLIKELY(mTraceEnabled)) {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700110 const float refreshRate = info->getRefreshRate(now);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800111 if (recent && refreshRate > 0.0f) {
Ana Krulecc84d09b2019-11-02 23:10:29 +0100112 if (const auto layer = activeLayer.promote(); layer) {
113 const int32_t priority = layer->getFrameRateSelectionPriority();
114 // TODO(b/142507166): This is where the scoring algorithm should live.
115 // Layers should be organized by priority
116 ALOGD("Layer has priority: %d", priority);
117 }
Steven Thomas540730a2020-01-08 20:12:42 -0800118 }
119 }
120 }
121
Ady Abraham8a82ba62020-01-17 12:43:17 -0800122 LayerHistory::Summary summary;
Steven Thomas540730a2020-01-08 20:12:42 -0800123 for (const auto& [weakLayer, info] : activeLayers()) {
124 const bool recent = info->isRecentlyActive(now);
125 auto layer = weakLayer.promote();
126 // Only use the layer if the reference still exists.
127 if (layer || CC_UNLIKELY(mTraceEnabled)) {
Steven Thomas540730a2020-01-08 20:12:42 -0800128 // Check if frame rate was set on layer.
Ady Abraham71c437d2020-01-31 15:56:57 -0800129 const auto frameRate = layer->getFrameRate();
130 if (frameRate.rate > 0.f) {
131 const auto voteType = [&]() {
132 switch (frameRate.type) {
133 case Layer::FrameRateCompatibility::Default:
134 return LayerVoteType::ExplicitDefault;
135 case Layer::FrameRateCompatibility::ExactOrMultiple:
136 return LayerVoteType::ExplicitExactOrMultiple;
137 case Layer::FrameRateCompatibility::NoVote:
138 return LayerVoteType::NoVote;
139 }
140 }();
141 summary.push_back({layer->getName(), voteType, frameRate.rate, /* weight */ 1.0f});
Ady Abraham8a82ba62020-01-17 12:43:17 -0800142 } else if (recent) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800143 summary.push_back({layer->getName(), LayerVoteType::Heuristic,
144 info->getRefreshRate(now),
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 /* weight */ 1.0f});
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700146 }
147
148 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abraham71c437d2020-01-31 15:56:57 -0800149 trace(weakLayer, round<int>(frameRate.rate));
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700150 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700151 }
152 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700153
Ady Abraham8a82ba62020-01-17 12:43:17 -0800154 return summary;
Ady Abraham09bd3922019-04-08 10:44:56 -0700155}
156
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700157void LayerHistory::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 continue;
167 }
168
169 if (CC_UNLIKELY(mTraceEnabled)) {
170 trace(weak, 0);
171 }
172
173 info->clearHistory();
174 std::swap(mLayerInfos[i], mLayerInfos[--mActiveLayersEnd]);
175 }
176
177 // Collect expired layers after inactive layers.
178 size_t end = mLayerInfos.size();
179 while (i < end) {
180 if (mLayerInfos[i].first.promote()) {
181 i++;
Ady Abraham09bd3922019-04-08 10:44:56 -0700182 } else {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700183 std::swap(mLayerInfos[i], mLayerInfos[--end]);
Ady Abraham09bd3922019-04-08 10:44:56 -0700184 }
185 }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700186
Ady Abrahamdec1a412020-01-24 10:23:50 -0800187 mLayerInfos.erase(mLayerInfos.begin() + static_cast<long>(end), mLayerInfos.end());
Ady Abraham09bd3922019-04-08 10:44:56 -0700188}
189
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700190void LayerHistory::clear() {
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700191 std::lock_guard lock(mLock);
192
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700193 for (const auto& [layer, info] : activeLayers()) {
194 info->clearHistory();
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700195 }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700196
197 mActiveLayersEnd = 0;
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700198}
199
Ady Abrahame3ed2f92020-01-06 17:01:28 -0800200} // namespace android::scheduler::impl
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800201