blob: 745c4c16ee0257381523f1dcb1930c62527f2c05 [file] [log] [blame]
Ana Krulec61f86db2018-11-19 14:16:35 +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
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070019#include <android-base/thread_annotations.h>
20#include <utils/RefBase.h>
Ana Krulec61f86db2018-11-19 14:16:35 +010021#include <utils/Timers.h>
22
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070023#include <memory>
24#include <mutex>
25#include <utility>
26#include <vector>
Ana Krulec434c22d2018-11-28 13:48:36 +010027
Ana Krulec61f86db2018-11-19 14:16:35 +010028namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070029
30class Layer;
Dominik Laskowski49cea512019-11-12 14:13:23 -080031class TestableScheduler;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070032
Ady Abraham09bd3922019-04-08 10:44:56 -070033namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010034
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070035class LayerInfo;
36
37// Records per-layer history of scheduling-related information (primarily present time),
38// heuristically categorizes layers as active or inactive, and summarizes stats about
39// active layers (primarily maximum refresh rate). See go/content-fps-detection-in-scheduler.
Ana Krulec61f86db2018-11-19 14:16:35 +010040class LayerHistory {
41public:
42 LayerHistory();
43 ~LayerHistory();
44
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070045 // Layers are unregistered when the weak reference expires.
46 void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -070047
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070048 // Marks the layer as active, and records the given state to its history.
Ady Abraham2139f732019-11-13 18:56:40 -080049 void record(Layer*, nsecs_t presentTime, nsecs_t now);
Ady Abrahama315ce72019-04-24 14:35:20 -070050
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070051 struct Summary {
52 float maxRefreshRate; // Maximum refresh rate among recently active layers.
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070053 };
Ady Abraham09bd3922019-04-08 10:44:56 -070054
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070055 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
56 Summary summarize(nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070057
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070058 void clear();
Ana Krulec61f86db2018-11-19 14:16:35 +010059
60private:
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070061 friend class LayerHistoryTest;
Dominik Laskowski49cea512019-11-12 14:13:23 -080062 friend TestableScheduler;
Ady Abraham09bd3922019-04-08 10:44:56 -070063
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070064 using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>;
65 using LayerInfos = std::vector<LayerPair>;
Ady Abraham09bd3922019-04-08 10:44:56 -070066
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070067 struct ActiveLayers {
68 LayerInfos& infos;
69 const size_t index;
Ady Abraham09bd3922019-04-08 10:44:56 -070070
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070071 auto begin() { return infos.begin(); }
72 auto end() { return begin() + index; }
73 };
74
75 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; }
76
77 // Iterates over layers in a single pass, swapping pairs such that active layers precede
78 // inactive layers, and inactive layers precede expired layers. Removes expired layers by
79 // truncating after inactive layers.
80 void partitionLayers(nsecs_t now) REQUIRES(mLock);
81
82 mutable std::mutex mLock;
83
84 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active
85 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache.
86 LayerInfos mLayerInfos GUARDED_BY(mLock);
87 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0;
88
89 // Whether to emit systrace output and debug logs.
90 const bool mTraceEnabled;
Ana Krulec61f86db2018-11-19 14:16:35 +010091};
92
Ady Abraham09bd3922019-04-08 10:44:56 -070093} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070094} // namespace android