blob: 5473db69740c4f8b4655e21ae2cedb2e27624d8a [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
Ady Abraham09bd3922019-04-08 10:44:56 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Ana Krulec61f86db2018-11-19 14:16:35 +010019#include "LayerHistory.h"
20
21#include <cinttypes>
22#include <cstdint>
Ady Abraham09bd3922019-04-08 10:44:56 -070023#include <limits>
Ana Krulec61f86db2018-11-19 14:16:35 +010024#include <numeric>
25#include <string>
26#include <unordered_map>
27
Ady Abraham09bd3922019-04-08 10:44:56 -070028#include <cutils/properties.h>
Ana Krulec61f86db2018-11-19 14:16:35 +010029#include <utils/Log.h>
30#include <utils/Timers.h>
31#include <utils/Trace.h>
32
Ana Krulec434c22d2018-11-28 13:48:36 +010033#include "SchedulerUtils.h"
34
Ana Krulec61f86db2018-11-19 14:16:35 +010035namespace android {
Ady Abraham09bd3922019-04-08 10:44:56 -070036namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010037
Ady Abraham09bd3922019-04-08 10:44:56 -070038std::atomic<int64_t> LayerHistory::sNextId = 0;
39
40LayerHistory::LayerHistory() {
41 char value[PROPERTY_VALUE_MAX];
42 property_get("debug.sf.layer_history_trace", value, "0");
Dominik Laskowskia7f850a2019-10-04 18:20:17 -070043 mTraceEnabled = static_cast<bool>(atoi(value));
Ady Abraham09bd3922019-04-08 10:44:56 -070044}
Ana Krulec61f86db2018-11-19 14:16:35 +010045
46LayerHistory::~LayerHistory() = default;
47
Ady Abraham09bd3922019-04-08 10:44:56 -070048std::unique_ptr<LayerHistory::LayerHandle> LayerHistory::createLayer(const std::string name,
Ana Krulecad083c42019-06-26 16:28:08 -070049 float minRefreshRate,
Ady Abraham09bd3922019-04-08 10:44:56 -070050 float maxRefreshRate) {
51 const int64_t id = sNextId++;
52
53 std::lock_guard lock(mLock);
Ana Krulecad083c42019-06-26 16:28:08 -070054 mInactiveLayerInfos.emplace(id,
55 std::make_shared<LayerInfo>(name, minRefreshRate, maxRefreshRate));
Ady Abraham09bd3922019-04-08 10:44:56 -070056 return std::make_unique<LayerHistory::LayerHandle>(*this, id);
Ana Krulec61f86db2018-11-19 14:16:35 +010057}
58
Ady Abraham09bd3922019-04-08 10:44:56 -070059void LayerHistory::destroyLayer(const int64_t id) {
60 std::lock_guard lock(mLock);
61 auto it = mActiveLayerInfos.find(id);
62 if (it != mActiveLayerInfos.end()) {
63 mActiveLayerInfos.erase(it);
64 }
65
66 it = mInactiveLayerInfos.find(id);
67 if (it != mInactiveLayerInfos.end()) {
68 mInactiveLayerInfos.erase(it);
69 }
Ana Krulec61f86db2018-11-19 14:16:35 +010070}
71
Ady Abrahama315ce72019-04-24 14:35:20 -070072void LayerHistory::insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime,
73 bool isHdr) {
Ady Abraham09bd3922019-04-08 10:44:56 -070074 std::shared_ptr<LayerInfo> layerInfo;
75 {
76 std::lock_guard lock(mLock);
77 auto layerInfoIterator = mInactiveLayerInfos.find(layerHandle->mId);
78 if (layerInfoIterator != mInactiveLayerInfos.end()) {
79 layerInfo = layerInfoIterator->second;
80 mInactiveLayerInfos.erase(layerInfoIterator);
81 mActiveLayerInfos.insert({layerHandle->mId, layerInfo});
82 } else {
83 layerInfoIterator = mActiveLayerInfos.find(layerHandle->mId);
84 if (layerInfoIterator != mActiveLayerInfos.end()) {
85 layerInfo = layerInfoIterator->second;
86 } else {
87 ALOGW("Inserting information about layer that is not registered: %" PRId64,
88 layerHandle->mId);
89 return;
90 }
91 }
92 }
93 layerInfo->setLastPresentTime(presentTime);
Ady Abrahama315ce72019-04-24 14:35:20 -070094 layerInfo->setHDRContent(isHdr);
Ana Krulec61f86db2018-11-19 14:16:35 +010095}
96
Ady Abrahama315ce72019-04-24 14:35:20 -070097void LayerHistory::setVisibility(const std::unique_ptr<LayerHandle>& layerHandle, bool visible) {
98 std::shared_ptr<LayerInfo> layerInfo;
99 {
100 std::lock_guard lock(mLock);
101 auto layerInfoIterator = mInactiveLayerInfos.find(layerHandle->mId);
102 if (layerInfoIterator != mInactiveLayerInfos.end()) {
103 layerInfo = layerInfoIterator->second;
104 if (visible) {
105 mInactiveLayerInfos.erase(layerInfoIterator);
106 mActiveLayerInfos.insert({layerHandle->mId, layerInfo});
107 }
108 } else {
109 layerInfoIterator = mActiveLayerInfos.find(layerHandle->mId);
110 if (layerInfoIterator != mActiveLayerInfos.end()) {
111 layerInfo = layerInfoIterator->second;
112 } else {
113 ALOGW("Inserting information about layer that is not registered: %" PRId64,
114 layerHandle->mId);
115 return;
116 }
117 }
118 }
119 layerInfo->setVisibility(visible);
120}
121
122std::pair<float, bool> LayerHistory::getDesiredRefreshRateAndHDR() {
123 bool isHDR = false;
Ady Abraham09bd3922019-04-08 10:44:56 -0700124 float newRefreshRate = 0.f;
125 std::lock_guard lock(mLock);
126
127 removeIrrelevantLayers();
128
129 // Iterate through all layers that have been recently updated, and find the max refresh rate.
130 for (const auto& [layerId, layerInfo] : mActiveLayerInfos) {
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700131 const bool recent = layerInfo->isRecentlyActive();
132 if (recent || CC_UNLIKELY(mTraceEnabled)) {
133 const float refreshRate = layerInfo->getDesiredRefreshRate();
134 if (recent && refreshRate > newRefreshRate) {
135 newRefreshRate = refreshRate;
136 }
137
138 if (CC_UNLIKELY(mTraceEnabled)) {
139 std::string name = "LFPS " + layerInfo->getName();
140 const float rate = std::round(refreshRate);
141 ATRACE_INT(name.c_str(), rate);
142 ALOGD("%s: %f", name.c_str(), rate);
143 }
Ady Abraham09bd3922019-04-08 10:44:56 -0700144 }
Ady Abrahama315ce72019-04-24 14:35:20 -0700145 isHDR |= layerInfo->getHDRContent();
Ady Abraham09bd3922019-04-08 10:44:56 -0700146 }
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700147 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700148 ALOGD("LayerHistory DesiredRefreshRate: %.2f", newRefreshRate);
149 }
150
Ady Abrahama315ce72019-04-24 14:35:20 -0700151 return {newRefreshRate, isHDR};
Ady Abraham09bd3922019-04-08 10:44:56 -0700152}
153
154void LayerHistory::removeIrrelevantLayers() {
155 const int64_t obsoleteEpsilon = systemTime() - scheduler::OBSOLETE_TIME_EPSILON_NS.count();
156 // Iterator pointing to first element in map
157 auto it = mActiveLayerInfos.begin();
158 while (it != mActiveLayerInfos.end()) {
159 // If last updated was before the obsolete time, remove it.
Ady Abrahama315ce72019-04-24 14:35:20 -0700160 // Keep HDR layer around as long as they are visible.
161 if (!it->second->isVisible() ||
162 (!it->second->getHDRContent() && it->second->getLastUpdatedTime() < obsoleteEpsilon)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700163 // erase() function returns the iterator of the next
164 // to last deleted element.
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700165 if (CC_UNLIKELY(mTraceEnabled)) {
Ady Abraham09bd3922019-04-08 10:44:56 -0700166 ALOGD("Layer %s obsolete", it->second->getName().c_str());
167 // Make sure to update systrace to indicate that the layer was erased.
168 std::string layerName = "LFPS " + it->second->getName();
169 ATRACE_INT(layerName.c_str(), 0);
170 }
171 auto id = it->first;
172 auto layerInfo = it->second;
173 layerInfo->clearHistory();
174 mInactiveLayerInfos.insert({id, layerInfo});
175 it = mActiveLayerInfos.erase(it);
176 } else {
177 ++it;
178 }
179 }
180}
181
Ady Abrahama9bf4ca2019-06-11 19:08:58 -0700182void LayerHistory::clearHistory() {
183 std::lock_guard lock(mLock);
184
185 auto it = mActiveLayerInfos.begin();
186 while (it != mActiveLayerInfos.end()) {
187 auto id = it->first;
188 auto layerInfo = it->second;
189 layerInfo->clearHistory();
190 mInactiveLayerInfos.insert({id, layerInfo});
191 it = mActiveLayerInfos.erase(it);
192 }
193}
194
Ady Abraham09bd3922019-04-08 10:44:56 -0700195} // namespace scheduler
Dominik Laskowskia7f850a2019-10-04 18:20:17 -0700196} // namespace android