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 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 25 | namespace android::scheduler { |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 26 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 27 | // Opaque handle to scheduler connection. |
| 28 | struct ConnectionHandle { |
| 29 | using Id = std::uintptr_t; |
| 30 | static constexpr Id INVALID_ID = static_cast<Id>(-1); |
| 31 | |
| 32 | Id id = INVALID_ID; |
| 33 | |
| 34 | explicit operator bool() const { return id != INVALID_ID; } |
| 35 | }; |
| 36 | |
| 37 | inline bool operator==(ConnectionHandle lhs, ConnectionHandle rhs) { |
| 38 | return lhs.id == rhs.id; |
| 39 | } |
| 40 | |
| 41 | using namespace std::chrono_literals; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 42 | |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 43 | // This number is used to have a place holder for when the screen is not NORMAL/ON. Currently |
| 44 | // the config is not visible to SF, and is completely maintained by HWC. However, we would |
| 45 | // still like to keep track of time when the device is in this config. |
| 46 | static constexpr int SCREEN_OFF_CONFIG_ID = -1; |
Ady Abraham | 796beb0 | 2019-04-11 15:23:07 -0700 | [diff] [blame] | 47 | static constexpr uint32_t HWC2_SCREEN_OFF_CONFIG_ID = 0xffffffff; |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 48 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 49 | // This number is used when we try to determine how long do we keep layer information around |
Ana Krulec | ad083c4 | 2019-06-26 16:28:08 -0700 | [diff] [blame] | 50 | // before we remove it. It is also used to determine how long the layer stays relevant. |
| 51 | // This time period captures infrequent updates when playing YouTube video with static image, |
| 52 | // or waiting idle in messaging app, when cursor is blinking. |
| 53 | static constexpr std::chrono::nanoseconds OBSOLETE_TIME_EPSILON_NS = 1200ms; |
| 54 | |
| 55 | // Layer is considered low activity if the buffers come more than LOW_ACTIVITY_EPSILON_NS |
| 56 | // apart. This is helping SF to vote for lower refresh rates when there is not activity |
| 57 | // in screen. |
| 58 | static constexpr std::chrono::nanoseconds LOW_ACTIVITY_EPSILON_NS = 250ms; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 59 | |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 60 | // Calculates the statistical mean (average) in the data structure (array, vector). The |
| 61 | // function does not modify the contents of the array. |
| 62 | template <typename T> |
| 63 | auto calculate_mean(const T& v) { |
| 64 | using V = typename T::value_type; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 65 | V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0)); |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 66 | return sum / static_cast<V>(v.size()); |
| 67 | } |
| 68 | |
| 69 | // Calculates the statistical median in the vector. Return 0 if the vector is empty. The |
| 70 | // function modifies the vector contents. |
| 71 | int64_t calculate_median(std::vector<int64_t>* v); |
| 72 | |
| 73 | // 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] | 74 | template <typename T> |
| 75 | auto calculate_mode(const T& v) { |
| 76 | if (v.empty()) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | // Create a map with all the counts for the indivicual values in the vector. |
| 81 | std::unordered_map<int64_t, int> counts; |
| 82 | for (int64_t value : v) { |
| 83 | counts[value]++; |
| 84 | } |
| 85 | |
| 86 | // Sort the map, and return the number with the highest count. If two numbers have |
| 87 | // the same count, first one is returned. |
| 88 | using ValueType = const decltype(counts)::value_type&; |
| 89 | const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; }; |
| 90 | return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first); |
| 91 | } |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 92 | |
Dominik Laskowski | 9804183 | 2019-08-01 18:35:59 -0700 | [diff] [blame] | 93 | } // namespace android::scheduler |
| 94 | |
| 95 | namespace std { |
| 96 | |
| 97 | template <> |
| 98 | struct hash<android::scheduler::ConnectionHandle> { |
| 99 | size_t operator()(android::scheduler::ConnectionHandle handle) const { |
| 100 | return hash<android::scheduler::ConnectionHandle::Id>()(handle.id); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // namespace std |