blob: 15ac8caccb1f20737755fd30e5a701e3e8fa0afe [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;
31
Ady Abraham09bd3922019-04-08 10:44:56 -070032namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010033
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070034class LayerInfo;
35
36// Records per-layer history of scheduling-related information (primarily present time),
37// heuristically categorizes layers as active or inactive, and summarizes stats about
38// active layers (primarily maximum refresh rate). See go/content-fps-detection-in-scheduler.
Ana Krulec61f86db2018-11-19 14:16:35 +010039class LayerHistory {
40public:
41 LayerHistory();
42 ~LayerHistory();
43
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070044 // Layers are unregistered when the weak reference expires.
45 void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate);
Ady Abraham09bd3922019-04-08 10:44:56 -070046
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070047 // Marks the layer as active, and records the given state to its history.
48 void record(Layer*, nsecs_t presentTime, bool isHDR, nsecs_t now);
Ady Abrahama315ce72019-04-24 14:35:20 -070049
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070050 struct Summary {
51 float maxRefreshRate; // Maximum refresh rate among recently active layers.
52 bool isHDR; // True if any recently active layer has HDR content.
53 };
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;
Ady Abraham09bd3922019-04-08 10:44:56 -070062
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070063 using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>;
64 using LayerInfos = std::vector<LayerPair>;
Ady Abraham09bd3922019-04-08 10:44:56 -070065
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070066 struct ActiveLayers {
67 LayerInfos& infos;
68 const size_t index;
Ady Abraham09bd3922019-04-08 10:44:56 -070069
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070070 auto begin() { return infos.begin(); }
71 auto end() { return begin() + index; }
72 };
73
74 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; }
75
76 // Iterates over layers in a single pass, swapping pairs such that active layers precede
77 // inactive layers, and inactive layers precede expired layers. Removes expired layers by
78 // truncating after inactive layers.
79 void partitionLayers(nsecs_t now) REQUIRES(mLock);
80
81 mutable std::mutex mLock;
82
83 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active
84 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache.
85 LayerInfos mLayerInfos GUARDED_BY(mLock);
86 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0;
87
88 // Whether to emit systrace output and debug logs.
89 const bool mTraceEnabled;
Ana Krulec61f86db2018-11-19 14:16:35 +010090};
91
Ady Abraham09bd3922019-04-08 10:44:56 -070092} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070093} // namespace android