blob: cc5570050394cd23d5f10bab90dd860ed86b233a [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
Nathaniel Nifong1303d912021-10-06 09:41:24 -040023#include <map>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070024#include <memory>
25#include <mutex>
Dominik Laskowski983f2b52020-06-25 16:54:06 -070026#include <string>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070027#include <utility>
28#include <vector>
Ana Krulec434c22d2018-11-28 13:48:36 +010029
Ady Abraham8a82ba62020-01-17 12:43:17 -080030#include "RefreshRateConfigs.h"
31
Ana Krulec61f86db2018-11-19 14:16:35 +010032namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070033
34class Layer;
35
Ady Abraham09bd3922019-04-08 10:44:56 -070036namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010037
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070038class LayerInfo;
39
Ana Krulec61f86db2018-11-19 14:16:35 +010040class LayerHistory {
41public:
Ady Abraham8a82ba62020-01-17 12:43:17 -080042 using LayerVoteType = RefreshRateConfigs::LayerVoteType;
43
Ady Abraham3efa3942021-06-24 19:01:25 -070044 LayerHistory();
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010045 ~LayerHistory();
Ana Krulec61f86db2018-11-19 14:16:35 +010046
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070047 // Layers are unregistered when the weak reference expires.
Marin Shalamanov4ad8b302020-12-11 15:50:08 +010048 void registerLayer(Layer*, LayerVoteType type);
Ady Abraham8a82ba62020-01-17 12:43:17 -080049
50 // Sets the display size. Client is responsible for synchronization.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010051 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
Ady Abraham09bd3922019-04-08 10:44:56 -070052
Marin Shalamanova7fe3042021-01-29 21:02:08 +010053 // Sets whether a mode change is pending to be applied
54 void setModeChangePending(bool pending) { mModeChangePending = pending; }
Ady Abraham32efd542020-05-19 17:49:26 -070055
Ady Abraham5def7332020-05-29 16:13:47 -070056 // Represents which layer activity is recorded
57 enum class LayerUpdateType {
58 Buffer, // a new buffer queued
59 AnimationTX, // a new transaction with eAnimation flag set
60 SetFrameRate, // setFrameRate API was called
61 };
62
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070063 // Marks the layer as active, and records the given state to its history.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010064 void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType);
Ady Abrahama315ce72019-04-24 14:35:20 -070065
Ady Abraham8a82ba62020-01-17 12:43:17 -080066 using Summary = std::vector<RefreshRateConfigs::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070067
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070068 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Ady Abraham3efa3942021-06-24 19:01:25 -070069 Summary summarize(const RefreshRateConfigs&, nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070070
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010071 void clear();
Ady Abrahambdda8f02021-04-01 16:06:11 -070072
73 void deregisterLayer(Layer*);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010074 std::string dump() const;
Ady Abraham8a82ba62020-01-17 12:43:17 -080075
Nathaniel Nifong1303d912021-10-06 09:41:24 -040076 // return the frames per second of the layer with the given sequence id.
77 float getLayerFramerate(nsecs_t now, int32_t id) const;
78
Ady Abraham8a82ba62020-01-17 12:43:17 -080079private:
Dominik Laskowski068173d2021-08-11 17:22:59 -070080 friend class LayerHistoryTest;
81 friend class TestableScheduler;
Ady Abraham8a82ba62020-01-17 12:43:17 -080082
Ady Abrahambdda8f02021-04-01 16:06:11 -070083 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
Nathaniel Nifong1303d912021-10-06 09:41:24 -040084 // keyed by id as returned from Layer::getSequence()
85 using LayerInfos = std::unordered_map<int32_t, LayerPair>;
Ady Abraham8a82ba62020-01-17 12:43:17 -080086
Nathaniel Nifong1303d912021-10-06 09:41:24 -040087 // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive
88 // layers to mInactiveLayerInfos.
89 // worst case time complexity is O(2 * inactive + active)
90 void partitionLayers(nsecs_t now) REQUIRES(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -080091
Nathaniel Nifong1303d912021-10-06 09:41:24 -040092 enum class layerStatus {
93 NotFound,
94 LayerInActiveMap,
95 LayerInInactiveMap,
Ady Abraham8a82ba62020-01-17 12:43:17 -080096 };
97
Nathaniel Nifong1303d912021-10-06 09:41:24 -040098 // looks up a layer by sequence id in both layerInfo maps.
99 // The first element indicates if and where the item was found
100 std::pair<layerStatus, LayerHistory::LayerPair*> findLayer(int32_t id) REQUIRES(mLock);
101 std::pair<layerStatus, const LayerHistory::LayerPair*> findLayer(int32_t id) const
102 REQUIRES(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800103
104 mutable std::mutex mLock;
105
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400106 // Partitioned into two maps to facility two kinds of retrieval:
107 // 1. retrieval of a layer by id (attempt lookup in both maps)
108 // 2. retrieval of all active layers (iterate that map)
109 // The partitioning is allowed to become out of date but calling partitionLayers refreshes the
110 // validity of each map.
111 LayerInfos mActiveLayerInfos GUARDED_BY(mLock);
112 LayerInfos mInactiveLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700113
Ady Abraham8a82ba62020-01-17 12:43:17 -0800114 uint32_t mDisplayArea = 0;
115
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700116 // Whether to emit systrace output and debug logs.
117 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100118
119 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
120 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700121
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100122 // Whether a mode change is in progress or not
123 std::atomic<bool> mModeChangePending = false;
Ana Krulec61f86db2018-11-19 14:16:35 +0100124};
125
Ady Abraham09bd3922019-04-08 10:44:56 -0700126} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700127} // namespace android