blob: 128699b5a7c15a2e4098277d2f604f2a8f559b12 [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>
Dominik Laskowski983f2b52020-06-25 16:54:06 -070025#include <string>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070026#include <utility>
27#include <vector>
Ana Krulec434c22d2018-11-28 13:48:36 +010028
Ady Abraham8a82ba62020-01-17 12:43:17 -080029#include "RefreshRateConfigs.h"
30
Ana Krulec61f86db2018-11-19 14:16:35 +010031namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070032
33class Layer;
Dominik Laskowski49cea512019-11-12 14:13:23 -080034class TestableScheduler;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070035
Ady Abraham09bd3922019-04-08 10:44:56 -070036namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010037
Ady Abrahame3ed2f92020-01-06 17:01:28 -080038class LayerHistoryTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080039class LayerHistoryTestV2;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070040class LayerInfo;
Ady Abraham8a82ba62020-01-17 12:43:17 -080041class LayerInfoV2;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070042
Ana Krulec61f86db2018-11-19 14:16:35 +010043class LayerHistory {
44public:
Ady Abraham8a82ba62020-01-17 12:43:17 -080045 using LayerVoteType = RefreshRateConfigs::LayerVoteType;
46
Ady Abrahame3ed2f92020-01-06 17:01:28 -080047 virtual ~LayerHistory() = default;
Ana Krulec61f86db2018-11-19 14:16:35 +010048
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070049 // Layers are unregistered when the weak reference expires.
Ady Abraham8a82ba62020-01-17 12:43:17 -080050 virtual void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate,
51 LayerVoteType type) = 0;
52
53 // Sets the display size. Client is responsible for synchronization.
54 virtual void setDisplayArea(uint32_t displayArea) = 0;
Ady Abraham09bd3922019-04-08 10:44:56 -070055
Ady Abraham5def7332020-05-29 16:13:47 -070056 // Sets whether a config change is pending to be applied
Ady Abraham32efd542020-05-19 17:49:26 -070057 virtual void setConfigChangePending(bool pending) = 0;
58
Ady Abraham5def7332020-05-29 16:13:47 -070059 // Represents which layer activity is recorded
60 enum class LayerUpdateType {
61 Buffer, // a new buffer queued
62 AnimationTX, // a new transaction with eAnimation flag set
63 SetFrameRate, // setFrameRate API was called
64 };
65
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070066 // Marks the layer as active, and records the given state to its history.
Ady Abraham5def7332020-05-29 16:13:47 -070067 virtual void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType) = 0;
Ady Abrahama315ce72019-04-24 14:35:20 -070068
Ady Abraham8a82ba62020-01-17 12:43:17 -080069 using Summary = std::vector<RefreshRateConfigs::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070070
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070071 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Ady Abrahame3ed2f92020-01-06 17:01:28 -080072 virtual Summary summarize(nsecs_t now) = 0;
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070073
Ady Abrahame3ed2f92020-01-06 17:01:28 -080074 virtual void clear() = 0;
Dominik Laskowski983f2b52020-06-25 16:54:06 -070075 virtual std::string dump() const = 0;
Ady Abrahame3ed2f92020-01-06 17:01:28 -080076};
77
78namespace impl {
79// Records per-layer history of scheduling-related information (primarily present time),
80// heuristically categorizes layers as active or inactive, and summarizes stats about
81// active layers (primarily maximum refresh rate). See go/content-fps-detection-in-scheduler.
82class LayerHistory : public android::scheduler::LayerHistory {
83public:
84 LayerHistory();
85 virtual ~LayerHistory();
86
87 // Layers are unregistered when the weak reference expires.
Ady Abraham8a82ba62020-01-17 12:43:17 -080088 void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate,
89 LayerVoteType type) override;
90
91 void setDisplayArea(uint32_t /*displayArea*/) override {}
Ady Abrahame3ed2f92020-01-06 17:01:28 -080092
Ady Abraham32efd542020-05-19 17:49:26 -070093 void setConfigChangePending(bool /*pending*/) override {}
94
Ady Abrahame3ed2f92020-01-06 17:01:28 -080095 // Marks the layer as active, and records the given state to its history.
Ady Abraham5def7332020-05-29 16:13:47 -070096 void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType) override;
Ady Abrahame3ed2f92020-01-06 17:01:28 -080097
98 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
99 android::scheduler::LayerHistory::Summary summarize(nsecs_t now) override;
100
101 void clear() override;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700102 std::string dump() const override;
Ana Krulec61f86db2018-11-19 14:16:35 +0100103
104private:
Ady Abrahame3ed2f92020-01-06 17:01:28 -0800105 friend class android::scheduler::LayerHistoryTest;
Dominik Laskowski49cea512019-11-12 14:13:23 -0800106 friend TestableScheduler;
Ady Abraham09bd3922019-04-08 10:44:56 -0700107
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700108 using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>;
109 using LayerInfos = std::vector<LayerPair>;
Ady Abraham09bd3922019-04-08 10:44:56 -0700110
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700111 struct ActiveLayers {
112 LayerInfos& infos;
Ady Abrahamdec1a412020-01-24 10:23:50 -0800113 const size_t index;
Ady Abraham09bd3922019-04-08 10:44:56 -0700114
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700115 auto begin() { return infos.begin(); }
Ady Abrahamdec1a412020-01-24 10:23:50 -0800116 auto end() { return begin() + static_cast<long>(index); }
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700117 };
118
119 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; }
120
121 // Iterates over layers in a single pass, swapping pairs such that active layers precede
122 // inactive layers, and inactive layers precede expired layers. Removes expired layers by
123 // truncating after inactive layers.
124 void partitionLayers(nsecs_t now) REQUIRES(mLock);
125
126 mutable std::mutex mLock;
127
128 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active
129 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache.
130 LayerInfos mLayerInfos GUARDED_BY(mLock);
Ady Abrahamdec1a412020-01-24 10:23:50 -0800131 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800132
133 // Whether to emit systrace output and debug logs.
134 const bool mTraceEnabled;
135
136 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
137 const bool mUseFrameRatePriority;
138};
139
140class LayerHistoryV2 : public android::scheduler::LayerHistory {
141public:
Ady Abrahamb1b9d412020-06-01 19:53:52 -0700142 LayerHistoryV2(const scheduler::RefreshRateConfigs&);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800143 virtual ~LayerHistoryV2();
144
145 // Layers are unregistered when the weak reference expires.
146 void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate,
147 LayerVoteType type) override;
148
149 // Sets the display size. Client is responsible for synchronization.
150 void setDisplayArea(uint32_t displayArea) override { mDisplayArea = displayArea; }
151
Ady Abraham32efd542020-05-19 17:49:26 -0700152 void setConfigChangePending(bool pending) override { mConfigChangePending = pending; }
153
Ady Abraham8a82ba62020-01-17 12:43:17 -0800154 // Marks the layer as active, and records the given state to its history.
Ady Abraham5def7332020-05-29 16:13:47 -0700155 void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType) override;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800156
157 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
158 android::scheduler::LayerHistory::Summary summarize(nsecs_t /*now*/) override;
159
160 void clear() override;
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700161 std::string dump() const override;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800162
163private:
164 friend android::scheduler::LayerHistoryTestV2;
165 friend TestableScheduler;
166
167 using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfoV2>>;
168 using LayerInfos = std::vector<LayerPair>;
169
170 struct ActiveLayers {
171 LayerInfos& infos;
172 const size_t index;
173
174 auto begin() { return infos.begin(); }
175 auto end() { return begin() + static_cast<long>(index); }
176 };
177
178 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; }
179
180 // Iterates over layers in a single pass, swapping pairs such that active layers precede
181 // inactive layers, and inactive layers precede expired layers. Removes expired layers by
182 // truncating after inactive layers.
183 void partitionLayers(nsecs_t now) REQUIRES(mLock);
184
185 mutable std::mutex mLock;
186
187 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active
188 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache.
189 LayerInfos mLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700190 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0;
191
Ady Abraham8a82ba62020-01-17 12:43:17 -0800192 uint32_t mDisplayArea = 0;
193
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700194 // Whether to emit systrace output and debug logs.
195 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100196
197 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
198 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700199
200 // Whether a config change is in progress or not
201 std::atomic<bool> mConfigChangePending = false;
Ana Krulec61f86db2018-11-19 14:16:35 +0100202};
203
Ady Abrahame3ed2f92020-01-06 17:01:28 -0800204} // namespace impl
Ady Abraham09bd3922019-04-08 10:44:56 -0700205} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700206} // namespace android