blob: 76c1352ae5b2c7e85b7e2065f37da5e7c268ccda [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#pragma once
18
19#include <array>
20#include <cinttypes>
21#include <cstdint>
22#include <numeric>
23#include <string>
24#include <unordered_map>
25
26#include <utils/Timers.h>
27
28namespace android {
29
30/*
31 * This class represents a circular buffer in which we keep layer history for
32 * the past ARRAY_SIZE frames. Each time, a signal for new frame comes, the counter
33 * gets incremented and includes all the layers that are requested to draw in that
34 * frame.
35 *
36 * Once the buffer reaches the end of the array, it starts overriding the elements
37 * at the beginning of the array.
38 */
39class LayerHistory {
40public:
41 LayerHistory();
42 ~LayerHistory();
43
44 // Method for inserting layers and their requested present time into the ring buffer.
Ana Krulec3084c052018-11-21 20:27:17 +010045 // The elements are going to be inserted into an unordered_map at the position 'now'.
Ana Krulec61f86db2018-11-19 14:16:35 +010046 void insert(const std::string layerName, nsecs_t presentTime);
47 // Method for incrementing the current slot in the ring buffer. It also clears the
48 // unordered_map, if it was created previously.
49 void incrementCounter();
Ana Krulec3084c052018-11-21 20:27:17 +010050 // Returns unordered_map at the given at index. The index is decremented from 'now'. For
51 // example, 0 is now, 1 is previous frame.
Ana Krulec61f86db2018-11-19 14:16:35 +010052 const std::unordered_map<std::string, nsecs_t>& get(size_t index) const;
Ana Krulec3084c052018-11-21 20:27:17 +010053 // Returns the total size of the ring buffer. The value is always the same regardless
54 // of how many slots we filled in.
55 static constexpr size_t getSize() { return ARRAY_SIZE; }
Ana Krulec61f86db2018-11-19 14:16:35 +010056
57private:
58 size_t mCounter = 0;
59 static constexpr size_t ARRAY_SIZE = 30;
60 std::array<std::unordered_map<std::string, nsecs_t>, ARRAY_SIZE> mElements;
61};
62
63} // namespace android