blob: 3b7567ce8b84c173e2787f074c72233cdbb52403 [file] [log] [blame]
Ana Krulec434c22d2018-11-28 13:48:36 +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
Ady Abraham09bd3922019-04-08 10:44:56 -070019#include <chrono>
Ana Krulec434c22d2018-11-28 13:48:36 +010020#include <cinttypes>
21#include <numeric>
Ana Krulecfefd6ae2019-02-13 17:53:08 -080022#include <unordered_map>
Ana Krulec434c22d2018-11-28 13:48:36 +010023#include <vector>
24
Dominik Laskowski98041832019-08-01 18:35:59 -070025namespace android::scheduler {
Ady Abraham09bd3922019-04-08 10:44:56 -070026
Dominik Laskowski98041832019-08-01 18:35:59 -070027// Opaque handle to scheduler connection.
28struct 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
37inline bool operator==(ConnectionHandle lhs, ConnectionHandle rhs) {
38 return lhs.id == rhs.id;
39}
40
41using namespace std::chrono_literals;
Ana Krulec434c22d2018-11-28 13:48:36 +010042
Ady Abraham09bd3922019-04-08 10:44:56 -070043// This number is used when we try to determine how long do we keep layer information around
Ana Krulecad083c42019-06-26 16:28:08 -070044// before we remove it. It is also used to determine how long the layer stays relevant.
45// This time period captures infrequent updates when playing YouTube video with static image,
46// or waiting idle in messaging app, when cursor is blinking.
47static constexpr std::chrono::nanoseconds OBSOLETE_TIME_EPSILON_NS = 1200ms;
48
Ady Abraham0d772e22019-09-18 16:44:02 -070049// Layer is considered low activity if the LOW_ACTIVITY_BUFFERS buffers come more than
50// LOW_ACTIVITY_EPSILON_NS apart.
51// This is helping SF to vote for lower refresh rates when there is not activity
Ana Krulecad083c42019-06-26 16:28:08 -070052// in screen.
Ady Abraham0d772e22019-09-18 16:44:02 -070053static constexpr int LOW_ACTIVITY_BUFFERS = 2;
Ana Krulecad083c42019-06-26 16:28:08 -070054static constexpr std::chrono::nanoseconds LOW_ACTIVITY_EPSILON_NS = 250ms;
Ady Abraham09bd3922019-04-08 10:44:56 -070055
Ana Krulec434c22d2018-11-28 13:48:36 +010056// Calculates the statistical mean (average) in the data structure (array, vector). The
57// function does not modify the contents of the array.
58template <typename T>
59auto calculate_mean(const T& v) {
60 using V = typename T::value_type;
Ady Abraham09bd3922019-04-08 10:44:56 -070061 V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0));
Ana Krulec434c22d2018-11-28 13:48:36 +010062 return sum / static_cast<V>(v.size());
63}
64
65// Calculates the statistical median in the vector. Return 0 if the vector is empty. The
66// function modifies the vector contents.
67int64_t calculate_median(std::vector<int64_t>* v);
68
69// Calculates the statistical mode in the vector. Return 0 if the vector is empty.
Ana Krulecfefd6ae2019-02-13 17:53:08 -080070template <typename T>
71auto calculate_mode(const T& v) {
72 if (v.empty()) {
73 return 0;
74 }
75
76 // Create a map with all the counts for the indivicual values in the vector.
77 std::unordered_map<int64_t, int> counts;
78 for (int64_t value : v) {
79 counts[value]++;
80 }
81
82 // Sort the map, and return the number with the highest count. If two numbers have
83 // the same count, first one is returned.
84 using ValueType = const decltype(counts)::value_type&;
85 const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; };
86 return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first);
87}
Ana Krulec434c22d2018-11-28 13:48:36 +010088
Dominik Laskowski98041832019-08-01 18:35:59 -070089} // namespace android::scheduler
90
91namespace std {
92
93template <>
94struct hash<android::scheduler::ConnectionHandle> {
95 size_t operator()(android::scheduler::ConnectionHandle handle) const {
96 return hash<android::scheduler::ConnectionHandle::Id>()(handle.id);
97 }
98};
99
100} // namespace std