Ana Krulec | 434c22d | 2018-11-28 13:48:36 +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 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 19 | #include <chrono> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 20 | #include <cinttypes> |
| 21 | #include <numeric> |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 22 | #include <unordered_map> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| 25 | namespace android { |
| 26 | namespace scheduler { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 27 | using namespace std::chrono_literals; |
| 28 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 29 | // This number is used to set the size of the arrays in scheduler that hold information |
| 30 | // about layers. |
| 31 | static constexpr size_t ARRAY_SIZE = 30; |
| 32 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 33 | // This number is used to have a place holder for when the screen is not NORMAL/ON. Currently |
| 34 | // the config is not visible to SF, and is completely maintained by HWC. However, we would |
| 35 | // still like to keep track of time when the device is in this config. |
| 36 | static constexpr int SCREEN_OFF_CONFIG_ID = -1; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 37 | static constexpr uint32_t HWC2_SCREEN_OFF_CONFIG_ID = 0xffffffff; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 38 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 39 | // This number is used when we try to determine how long does a given layer stay relevant. |
Ady Abraham | 616b832 | 2019-06-12 13:30:07 -0700 | [diff] [blame] | 40 | // The value is set based on testing different scenarios. |
| 41 | static constexpr std::chrono::nanoseconds TIME_EPSILON_NS = 200ms; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 42 | |
| 43 | // This number is used when we try to determine how long do we keep layer information around |
Ady Abraham | 616b832 | 2019-06-12 13:30:07 -0700 | [diff] [blame] | 44 | // before we remove it. |
| 45 | static constexpr std::chrono::nanoseconds OBSOLETE_TIME_EPSILON_NS = 200ms; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 46 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 47 | // Calculates the statistical mean (average) in the data structure (array, vector). The |
| 48 | // function does not modify the contents of the array. |
| 49 | template <typename T> |
| 50 | auto calculate_mean(const T& v) { |
| 51 | using V = typename T::value_type; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 52 | V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0)); |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 53 | return sum / static_cast<V>(v.size()); |
| 54 | } |
| 55 | |
| 56 | // Calculates the statistical median in the vector. Return 0 if the vector is empty. The |
| 57 | // function modifies the vector contents. |
| 58 | int64_t calculate_median(std::vector<int64_t>* v); |
| 59 | |
| 60 | // Calculates the statistical mode in the vector. Return 0 if the vector is empty. |
Ana Krulec | fefd6ae | 2019-02-13 17:53:08 -0800 | [diff] [blame] | 61 | template <typename T> |
| 62 | auto calculate_mode(const T& v) { |
| 63 | if (v.empty()) { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | // Create a map with all the counts for the indivicual values in the vector. |
| 68 | std::unordered_map<int64_t, int> counts; |
| 69 | for (int64_t value : v) { |
| 70 | counts[value]++; |
| 71 | } |
| 72 | |
| 73 | // Sort the map, and return the number with the highest count. If two numbers have |
| 74 | // the same count, first one is returned. |
| 75 | using ValueType = const decltype(counts)::value_type&; |
| 76 | const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; }; |
| 77 | return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first); |
| 78 | } |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 79 | |
| 80 | } // namespace scheduler |
| 81 | } // namespace android |