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 <chrono>
20 #include <cinttypes>
21 #include <numeric>
22 #include <unordered_map>
23 #include <vector>
24
25 namespace android {
26 namespace scheduler {
27 using namespace std::chrono_literals;
28
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
33 // This number is used when we try to determine how long do we keep layer information around
34 // before we remove it. It is also used to determine how long the layer stays relevant.
35 // This time period captures infrequent updates when playing YouTube video with static image,
36 // or waiting idle in messaging app, when cursor is blinking.
37 static constexpr std::chrono::nanoseconds OBSOLETE_TIME_EPSILON_NS = 1200ms;
38
39 // Layer is considered low activity if the LOW_ACTIVITY_BUFFERS buffers come more than
40 // LOW_ACTIVITY_EPSILON_NS apart.
41 // This is helping SF to vote for lower refresh rates when there is not activity
42 // in screen.
43 static constexpr int LOW_ACTIVITY_BUFFERS = 2;
44 static constexpr std::chrono::nanoseconds LOW_ACTIVITY_EPSILON_NS = 250ms;
45
46 // Calculates the statistical mean (average) in the data structure (array, vector). The
47 // function does not modify the contents of the array.
48 template <typename T>
calculate_mean(const T & v)49 auto calculate_mean(const T& v) {
50 using V = typename T::value_type;
51 V sum = std::accumulate(v.begin(), v.end(), static_cast<V>(0));
52 return sum / static_cast<V>(v.size());
53 }
54
55 // Calculates the statistical median in the vector. Return 0 if the vector is empty. The
56 // function modifies the vector contents.
57 int64_t calculate_median(std::vector<int64_t>* v);
58
59 // Calculates the statistical mode in the vector. Return 0 if the vector is empty.
60 template <typename T>
calculate_mode(const T & v)61 auto calculate_mode(const T& v) {
62 if (v.empty()) {
63 return 0;
64 }
65
66 // Create a map with all the counts for the indivicual values in the vector.
67 std::unordered_map<int64_t, int> counts;
68 for (int64_t value : v) {
69 counts[value]++;
70 }
71
72 // Sort the map, and return the number with the highest count. If two numbers have
73 // the same count, first one is returned.
74 using ValueType = const decltype(counts)::value_type&;
75 const auto compareCounts = [](ValueType l, ValueType r) { return l.second <= r.second; };
76 return static_cast<int>(std::max_element(counts.begin(), counts.end(), compareCounts)->first);
77 }
78
79 } // namespace scheduler
80 } // namespace android