blob: 47945d10f994a99c43e5e3d06ff9f5a2fb93daf4 [file] [log] [blame]
Ady Abraham8a82ba62020-01-17 12:43:17 -08001/*
2 * Copyright 2020 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 <utils/Timers.h>
20
21#include <chrono>
22#include <deque>
23
24#include "LayerHistory.h"
25#include "RefreshRateConfigs.h"
26#include "SchedulerUtils.h"
27
28namespace android {
29
30class Layer;
31
32namespace scheduler {
33
34using namespace std::chrono_literals;
35
36// Maximum period between presents for a layer to be considered active.
37constexpr std::chrono::nanoseconds MAX_ACTIVE_LAYER_PERIOD_NS = 1200ms;
38
39// Earliest present time for a layer to be considered active.
40constexpr nsecs_t getActiveLayerThreshold(nsecs_t now) {
41 return now - MAX_ACTIVE_LAYER_PERIOD_NS.count();
42}
43
44// Stores history of present times and refresh rates for a layer.
45class LayerInfoV2 {
46 // Layer is considered frequent if the earliest value in the window of most recent present times
47 // is within a threshold. If a layer is infrequent, its average refresh rate is disregarded in
48 // favor of a low refresh rate.
49 static constexpr size_t FREQUENT_LAYER_WINDOW_SIZE = 3;
Ady Abrahamf5bc7792020-05-27 20:04:20 +000050 static constexpr float MIN_FPS_FOR_FREQUENT_LAYER = 10.0f;
51 static constexpr auto MAX_FREQUENT_LAYER_PERIOD_NS =
52 std::chrono::nanoseconds(static_cast<nsecs_t>(1e9f / MIN_FPS_FOR_FREQUENT_LAYER)) + 1ms;
Ady Abraham8a82ba62020-01-17 12:43:17 -080053
54 friend class LayerHistoryTestV2;
55
56public:
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000057 LayerInfoV2(nsecs_t highRefreshRatePeriod, LayerHistory::LayerVoteType defaultVote);
Ady Abraham8a82ba62020-01-17 12:43:17 -080058
59 LayerInfoV2(const LayerInfo&) = delete;
60 LayerInfoV2& operator=(const LayerInfoV2&) = delete;
61
62 // Records the last requested present time. It also stores information about when
63 // the layer was last updated. If the present time is farther in the future than the
64 // updated time, the updated time is the present time.
Ady Abraham32efd542020-05-19 17:49:26 -070065 void setLastPresentTime(nsecs_t lastPresentTime, nsecs_t now, bool pendingConfigChange);
Ady Abraham8a82ba62020-01-17 12:43:17 -080066
Ady Abraham8a82ba62020-01-17 12:43:17 -080067 // Sets an explicit layer vote. This usually comes directly from the application via
68 // ANativeWindow_setFrameRate API
69 void setLayerVote(LayerHistory::LayerVoteType type, float fps) { mLayerVote = {type, fps}; }
70
71 // Sets the default layer vote. This will be the layer vote after calling to resetLayerVote().
72 // This is used for layers that called to setLayerVote() and then removed the vote, so that the
73 // layer can go back to whatever vote it had before the app voted for it.
74 void setDefaultLayerVote(LayerHistory::LayerVoteType type) { mDefaultVote = type; }
75
76 // Resets the layer vote to its default.
77 void resetLayerVote() { mLayerVote = {mDefaultVote, 0.0f}; }
78
79 std::pair<LayerHistory::LayerVoteType, float> getRefreshRate(nsecs_t now);
80
81 // Return the last updated time. If the present time is farther in the future than the
82 // updated time, the updated time is the present time.
83 nsecs_t getLastUpdatedTime() const { return mLastUpdatedTime; }
84
Ady Abrahama61edcb2020-01-30 18:32:03 -080085 void clearHistory() {
Ady Abrahamdfb63ba2020-05-27 20:05:05 +000086 // Mark mFrameTimeValidSince to now to ignore all previous frame times.
87 // We are not deleting the old frame to keep track of whether we should treat the first
88 // buffer as Max as we don't know anything about this layer or Min as this layer is
89 // posting infrequent updates.
90 mFrameTimeValidSince = std::chrono::steady_clock::now();
Ady Abrahamc9664832020-05-12 14:16:56 -070091 mLastReportedRefreshRate = 0.0f;
Ady Abrahama61edcb2020-01-30 18:32:03 -080092 }
Ady Abraham8a82ba62020-01-17 12:43:17 -080093
94private:
Ady Abrahama61edcb2020-01-30 18:32:03 -080095 // Used to store the layer timestamps
96 struct FrameTimeData {
97 nsecs_t presetTime; // desiredPresentTime, if provided
98 nsecs_t queueTime; // buffer queue time
Ady Abraham32efd542020-05-19 17:49:26 -070099 bool pendingConfigChange;
Ady Abrahama61edcb2020-01-30 18:32:03 -0800100 };
101
Ady Abrahamaf6d8a42020-05-27 19:56:15 +0000102 bool isFrequent(nsecs_t now) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103 bool hasEnoughDataForHeuristic() const;
104 std::optional<float> calculateRefreshRateIfPossible();
Ady Abraham32efd542020-05-19 17:49:26 -0700105 std::pair<nsecs_t, bool> calculateAverageFrameTime() const;
106 bool isRefreshRateStable(nsecs_t averageFrameTime, bool missingPresentTime) const;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000107 bool isFrameTimeValid(const FrameTimeData&) const;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800108
109 // Used for sanitizing the heuristic data
110 const nsecs_t mHighRefreshRatePeriod;
111 LayerHistory::LayerVoteType mDefaultVote;
112
113 nsecs_t mLastUpdatedTime = 0;
114
115 float mLastReportedRefreshRate = 0.0f;
116
117 // Holds information about the layer vote
118 struct {
119 LayerHistory::LayerVoteType type;
120 float fps;
121 } mLayerVote;
122
Ady Abraham8a82ba62020-01-17 12:43:17 -0800123 std::deque<FrameTimeData> mFrameTimes;
Ady Abrahamdfb63ba2020-05-27 20:05:05 +0000124 std::chrono::time_point<std::chrono::steady_clock> mFrameTimeValidSince =
125 std::chrono::steady_clock::now();
Ady Abraham8a82ba62020-01-17 12:43:17 -0800126 static constexpr size_t HISTORY_SIZE = 90;
127 static constexpr std::chrono::nanoseconds HISTORY_TIME = 1s;
128};
129
130} // namespace scheduler
131} // namespace android