blob: 8e36ae9dc08c64d405a4f491825c26468e0fb7a6 [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");
43 mTraceEnabled = bool(atoi(value));
44}
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,
49 float maxRefreshRate) {
50 const int64_t id = sNextId++;
51
52 std::lock_guard lock(mLock);
53 mInactiveLayerInfos.emplace(id, std::make_shared<LayerInfo>(name, maxRefreshRate));
54 return std::make_unique<LayerHistory::LayerHandle>(*this, id);
Ana Krulec61f86db2018-11-19 14:16:35 +010055}
56
Ady Abraham09bd3922019-04-08 10:44:56 -070057void LayerHistory::destroyLayer(const int64_t id) {
58 std::lock_guard lock(mLock);
59 auto it = mActiveLayerInfos.find(id);
60 if (it != mActiveLayerInfos.end()) {
61 mActiveLayerInfos.erase(it);
62 }
63
64 it = mInactiveLayerInfos.find(id);
65 if (it != mInactiveLayerInfos.end()) {
66 mInactiveLayerInfos.erase(it);
67 }
Ana Krulec61f86db2018-11-19 14:16:35 +010068}
69
Ady Abraham09bd3922019-04-08 10:44:56 -070070void LayerHistory::insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime) {
71 std::shared_ptr<LayerInfo> layerInfo;
72 {
73 std::lock_guard lock(mLock);
74 auto layerInfoIterator = mInactiveLayerInfos.find(layerHandle->mId);
75 if (layerInfoIterator != mInactiveLayerInfos.end()) {
76 layerInfo = layerInfoIterator->second;
77 mInactiveLayerInfos.erase(layerInfoIterator);
78 mActiveLayerInfos.insert({layerHandle->mId, layerInfo});
79 } else {
80 layerInfoIterator = mActiveLayerInfos.find(layerHandle->mId);
81 if (layerInfoIterator != mActiveLayerInfos.end()) {
82 layerInfo = layerInfoIterator->second;
83 } else {
84 ALOGW("Inserting information about layer that is not registered: %" PRId64,
85 layerHandle->mId);
86 return;
87 }
88 }
89 }
90 layerInfo->setLastPresentTime(presentTime);
Ana Krulec61f86db2018-11-19 14:16:35 +010091}
92
Ady Abraham09bd3922019-04-08 10:44:56 -070093float LayerHistory::getDesiredRefreshRate() {
94 float newRefreshRate = 0.f;
95 std::lock_guard lock(mLock);
96
97 removeIrrelevantLayers();
98
99 // Iterate through all layers that have been recently updated, and find the max refresh rate.
100 for (const auto& [layerId, layerInfo] : mActiveLayerInfos) {
101 const float layerRefreshRate = layerInfo->getDesiredRefreshRate();
102 if (mTraceEnabled) {
103 // Store the refresh rate in traces for easy debugging.
104 std::string layerName = "LFPS " + layerInfo->getName();
105 ATRACE_INT(layerName.c_str(), std::round(layerRefreshRate));
106 ALOGD("%s: %f", layerName.c_str(), std::round(layerRefreshRate));
107 }
108 if (layerInfo->isRecentlyActive() && layerRefreshRate > newRefreshRate) {
109 newRefreshRate = layerRefreshRate;
110 }
111 }
112 if (mTraceEnabled) {
113 ALOGD("LayerHistory DesiredRefreshRate: %.2f", newRefreshRate);
114 }
115
116 return newRefreshRate;
117}
118
119void LayerHistory::removeIrrelevantLayers() {
120 const int64_t obsoleteEpsilon = systemTime() - scheduler::OBSOLETE_TIME_EPSILON_NS.count();
121 // Iterator pointing to first element in map
122 auto it = mActiveLayerInfos.begin();
123 while (it != mActiveLayerInfos.end()) {
124 // If last updated was before the obsolete time, remove it.
125 if (it->second->getLastUpdatedTime() < obsoleteEpsilon) {
126 // erase() function returns the iterator of the next
127 // to last deleted element.
128 if (mTraceEnabled) {
129 ALOGD("Layer %s obsolete", it->second->getName().c_str());
130 // Make sure to update systrace to indicate that the layer was erased.
131 std::string layerName = "LFPS " + it->second->getName();
132 ATRACE_INT(layerName.c_str(), 0);
133 }
134 auto id = it->first;
135 auto layerInfo = it->second;
136 layerInfo->clearHistory();
137 mInactiveLayerInfos.insert({id, layerInfo});
138 it = mActiveLayerInfos.erase(it);
139 } else {
140 ++it;
141 }
142 }
143}
144
145} // namespace scheduler
Ana Krulec61f86db2018-11-19 14:16:35 +0100146} // namespace android