blob: d5ccbe186b3754fc5365e8bf36a97fbc62cc8c80 [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
Ana Krulec434c22d2018-11-28 13:48:36 +010029#include "SchedulerUtils.h"
30
Ana Krulec61f86db2018-11-19 14:16:35 +010031namespace android {
32
33LayerHistory::LayerHistory() {}
34
35LayerHistory::~LayerHistory() = default;
36
37void LayerHistory::insert(const std::string layerName, nsecs_t presentTime) {
38 mElements[mCounter].insert(std::make_pair(layerName, presentTime));
39}
40
41void LayerHistory::incrementCounter() {
42 mCounter++;
Ana Krulec434c22d2018-11-28 13:48:36 +010043 mCounter = mCounter % scheduler::ARRAY_SIZE;
Ana Krulec3084c052018-11-21 20:27:17 +010044 // Clear all the previous data from the history. This is a ring buffer, so we are
45 // reusing memory.
Ana Krulec61f86db2018-11-19 14:16:35 +010046 mElements[mCounter].clear();
47}
48
49const std::unordered_map<std::string, nsecs_t>& LayerHistory::get(size_t index) const {
Ana Krulec3084c052018-11-21 20:27:17 +010050 // 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 Krulec434c22d2018-11-28 13:48:36 +010052 return mElements.at((scheduler::ARRAY_SIZE + (mCounter - (index % scheduler::ARRAY_SIZE))) %
53 scheduler::ARRAY_SIZE);
Ana Krulec61f86db2018-11-19 14:16:35 +010054}
55
56} // namespace android