blob: c6fab07c87cfc5f5099ed694567b26832563670b [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
Ana Krulec434c22d2018-11-28 13:48:36 +010028#include "SchedulerUtils.h"
29
Ana Krulec61f86db2018-11-19 14:16:35 +010030namespace android {
31
32/*
33 * This class represents a circular buffer in which we keep layer history for
34 * the past ARRAY_SIZE frames. Each time, a signal for new frame comes, the counter
35 * gets incremented and includes all the layers that are requested to draw in that
36 * frame.
37 *
38 * Once the buffer reaches the end of the array, it starts overriding the elements
39 * at the beginning of the array.
40 */
41class LayerHistory {
42public:
43 LayerHistory();
44 ~LayerHistory();
45
46 // Method for inserting layers and their requested present time into the ring buffer.
Ana Krulec3084c052018-11-21 20:27:17 +010047 // The elements are going to be inserted into an unordered_map at the position 'now'.
Ana Krulec61f86db2018-11-19 14:16:35 +010048 void insert(const std::string layerName, nsecs_t presentTime);
49 // Method for incrementing the current slot in the ring buffer. It also clears the
50 // unordered_map, if it was created previously.
51 void incrementCounter();
Ana Krulec3084c052018-11-21 20:27:17 +010052 // Returns unordered_map at the given at index. The index is decremented from 'now'. For
53 // example, 0 is now, 1 is previous frame.
Ana Krulec61f86db2018-11-19 14:16:35 +010054 const std::unordered_map<std::string, nsecs_t>& get(size_t index) const;
Ana Krulec3084c052018-11-21 20:27:17 +010055 // Returns the total size of the ring buffer. The value is always the same regardless
56 // of how many slots we filled in.
Ana Krulec434c22d2018-11-28 13:48:36 +010057 static constexpr size_t getSize() { return scheduler::ARRAY_SIZE; }
Ana Krulec61f86db2018-11-19 14:16:35 +010058
59private:
60 size_t mCounter = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +010061 std::array<std::unordered_map<std::string, nsecs_t>, scheduler::ARRAY_SIZE> mElements;
Ana Krulec61f86db2018-11-19 14:16:35 +010062};
63
64} // namespace android