blob: 92236f560a57eaea7cfa3f27bf4cc105821ed3d9 [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;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070039class LayerInfo;
40
Ana Krulec61f86db2018-11-19 14:16:35 +010041class LayerHistory {
42public:
Ady Abraham8a82ba62020-01-17 12:43:17 -080043 using LayerVoteType = RefreshRateConfigs::LayerVoteType;
44
Ady Abraham3efa3942021-06-24 19:01:25 -070045 LayerHistory();
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010046 ~LayerHistory();
Ana Krulec61f86db2018-11-19 14:16:35 +010047
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070048 // Layers are unregistered when the weak reference expires.
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010049 void registerLayer(Layer*, LayerVoteType type);
Ady Abraham8a82ba62020-01-17 12:43:17 -080050
51 // Sets the display size. Client is responsible for synchronization.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010052 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
Ady Abraham09bd3922019-04-08 10:44:56 -070053
Marin Shalamanova7fe3042021-01-29 21:02:08 +010054 // Sets whether a mode change is pending to be applied
55 void setModeChangePending(bool pending) { mModeChangePending = pending; }
Ady Abraham32efd542020-05-19 17:49:26 -070056
Ady Abraham5def7332020-05-29 16:13:47 -070057 // Represents which layer activity is recorded
58 enum class LayerUpdateType {
59 Buffer, // a new buffer queued
60 AnimationTX, // a new transaction with eAnimation flag set
61 SetFrameRate, // setFrameRate API was called
62 };
63
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070064 // Marks the layer as active, and records the given state to its history.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010065 void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType);
Ady Abrahama315ce72019-04-24 14:35:20 -070066
Ady Abraham8a82ba62020-01-17 12:43:17 -080067 using Summary = std::vector<RefreshRateConfigs::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070068
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070069 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Ady Abraham3efa3942021-06-24 19:01:25 -070070 Summary summarize(const RefreshRateConfigs&, nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070071
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010072 void clear();
Ady Abrahambdda8f02021-04-01 16:06:11 -070073
74 void deregisterLayer(Layer*);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010075 std::string dump() const;
Ady Abraham8a82ba62020-01-17 12:43:17 -080076
77private:
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010078 friend LayerHistoryTest;
Ady Abraham8a82ba62020-01-17 12:43:17 -080079 friend TestableScheduler;
80
Ady Abrahambdda8f02021-04-01 16:06:11 -070081 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
Ady Abraham8a82ba62020-01-17 12:43:17 -080082 using LayerInfos = std::vector<LayerPair>;
83
84 struct ActiveLayers {
85 LayerInfos& infos;
86 const size_t index;
87
88 auto begin() { return infos.begin(); }
89 auto end() { return begin() + static_cast<long>(index); }
90 };
91
92 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; }
93
94 // Iterates over layers in a single pass, swapping pairs such that active layers precede
95 // inactive layers, and inactive layers precede expired layers. Removes expired layers by
96 // truncating after inactive layers.
97 void partitionLayers(nsecs_t now) REQUIRES(mLock);
98
99 mutable std::mutex mLock;
100
101 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active
102 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache.
103 LayerInfos mLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700104 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0;
105
Ady Abraham8a82ba62020-01-17 12:43:17 -0800106 uint32_t mDisplayArea = 0;
107
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700108 // Whether to emit systrace output and debug logs.
109 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100110
111 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
112 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700113
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100114 // Whether a mode change is in progress or not
115 std::atomic<bool> mModeChangePending = false;
Ana Krulec61f86db2018-11-19 14:16:35 +0100116};
117
Ady Abraham09bd3922019-04-08 10:44:56 -0700118} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700119} // namespace android