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 | #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 | |
| 28 | namespace 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 | */ |
| 39 | class LayerHistory { |
| 40 | public: |
| 41 | LayerHistory(); |
| 42 | ~LayerHistory(); |
| 43 | |
| 44 | // Method for inserting layers and their requested present time into the ring buffer. |
| 45 | // The elements are going to be inserted into an unordered_map at the position of |
| 46 | // mCounter. |
| 47 | void insert(const std::string layerName, nsecs_t presentTime); |
| 48 | // Method for incrementing the current slot in the ring buffer. It also clears the |
| 49 | // unordered_map, if it was created previously. |
| 50 | void incrementCounter(); |
| 51 | // Returns unordered_map at the given at index. |
| 52 | const std::unordered_map<std::string, nsecs_t>& get(size_t index) const; |
| 53 | |
| 54 | private: |
| 55 | size_t mCounter = 0; |
| 56 | static constexpr size_t ARRAY_SIZE = 30; |
| 57 | std::array<std::unordered_map<std::string, nsecs_t>, ARRAY_SIZE> mElements; |
| 58 | }; |
| 59 | |
| 60 | } // namespace android |