| 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 |  | 
| Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 28 | #include "SchedulerUtils.h" | 
 | 29 |  | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 30 | namespace 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 |  */ | 
 | 41 | class LayerHistory { | 
 | 42 | public: | 
 | 43 |     LayerHistory(); | 
 | 44 |     ~LayerHistory(); | 
 | 45 |  | 
 | 46 |     // Method for inserting layers and their requested present time into the ring buffer. | 
| Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 47 |     // The elements are going to be inserted into an unordered_map at the position 'now'. | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 48 |     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 Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 52 |     // 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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 54 |     const std::unordered_map<std::string, nsecs_t>& get(size_t index) const; | 
| Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 55 |     // Returns the total size of the ring buffer. The value is always the same regardless | 
 | 56 |     // of how many slots we filled in. | 
| Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 57 |     static constexpr size_t getSize() { return scheduler::ARRAY_SIZE; } | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 58 |  | 
 | 59 | private: | 
 | 60 |     size_t mCounter = 0; | 
| Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 61 |     std::array<std::unordered_map<std::string, nsecs_t>, scheduler::ARRAY_SIZE> mElements; | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 62 | }; | 
 | 63 |  | 
 | 64 | } // namespace android |