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