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 | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 19 | #include "LayerHistory.h" |
| 20 | |
| 21 | #include <cinttypes> |
| 22 | #include <cstdint> |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 23 | #include <limits> |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 24 | #include <numeric> |
| 25 | #include <string> |
| 26 | #include <unordered_map> |
| 27 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <utils/Timers.h> |
| 31 | #include <utils/Trace.h> |
| 32 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 33 | #include "SchedulerUtils.h" |
| 34 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 35 | namespace android { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 36 | namespace scheduler { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 37 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 38 | std::atomic<int64_t> LayerHistory::sNextId = 0; |
| 39 | |
| 40 | LayerHistory::LayerHistory() { |
| 41 | char value[PROPERTY_VALUE_MAX]; |
| 42 | property_get("debug.sf.layer_history_trace", value, "0"); |
| 43 | mTraceEnabled = bool(atoi(value)); |
| 44 | } |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 45 | |
| 46 | LayerHistory::~LayerHistory() = default; |
| 47 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 48 | std::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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 57 | void 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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 70 | void 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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 93 | float 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 | |
| 119 | void 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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 146 | } // namespace android |