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 | |
| 17 | #include "LayerHistory.h" |
| 18 | |
| 19 | #include <cinttypes> |
| 20 | #include <cstdint> |
| 21 | #include <numeric> |
| 22 | #include <string> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/Timers.h> |
| 27 | #include <utils/Trace.h> |
| 28 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 29 | #include "SchedulerUtils.h" |
| 30 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | LayerHistory::LayerHistory() {} |
| 34 | |
| 35 | LayerHistory::~LayerHistory() = default; |
| 36 | |
| 37 | void LayerHistory::insert(const std::string layerName, nsecs_t presentTime) { |
| 38 | mElements[mCounter].insert(std::make_pair(layerName, presentTime)); |
| 39 | } |
| 40 | |
| 41 | void LayerHistory::incrementCounter() { |
| 42 | mCounter++; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 43 | mCounter = mCounter % scheduler::ARRAY_SIZE; |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 44 | // Clear all the previous data from the history. This is a ring buffer, so we are |
| 45 | // reusing memory. |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 46 | mElements[mCounter].clear(); |
| 47 | } |
| 48 | |
| 49 | const std::unordered_map<std::string, nsecs_t>& LayerHistory::get(size_t index) const { |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 50 | // For the purposes of the layer history, the index = 0 always needs to start at the |
| 51 | // current counter, and then decrement to access the layers in correct historical order. |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 52 | return mElements.at((scheduler::ARRAY_SIZE + (mCounter - (index % scheduler::ARRAY_SIZE))) % |
| 53 | scheduler::ARRAY_SIZE); |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace android |