blob: 1f47949f6c6c0644658df9e50c0e7b9fa976532e [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
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
29namespace android {
30
31LayerHistory::LayerHistory() {}
32
33LayerHistory::~LayerHistory() = default;
34
35void LayerHistory::insert(const std::string layerName, nsecs_t presentTime) {
36 mElements[mCounter].insert(std::make_pair(layerName, presentTime));
37}
38
39void LayerHistory::incrementCounter() {
40 mCounter++;
41 mCounter = mCounter % ARRAY_SIZE;
Ana Krulec3084c052018-11-21 20:27:17 +010042 // Clear all the previous data from the history. This is a ring buffer, so we are
43 // reusing memory.
Ana Krulec61f86db2018-11-19 14:16:35 +010044 mElements[mCounter].clear();
45}
46
47const std::unordered_map<std::string, nsecs_t>& LayerHistory::get(size_t index) const {
Ana Krulec3084c052018-11-21 20:27:17 +010048 // 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 Krulec61f86db2018-11-19 14:16:35 +010051}
52
53} // namespace android