blob: c09f148a9bcdca020c26a1cea762277f8afae0af [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
Rachel Lee2248f522023-01-27 16:45:23 -080030#include "EventThread.h"
31
Vishnu Nair80e8cfe2023-09-29 17:03:45 -070032#include "FrameRateCompatibility.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040033#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080034
Ana Krulec61f86db2018-11-19 14:16:35 +010035namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070036
37class Layer;
38
Ady Abraham09bd3922019-04-08 10:44:56 -070039namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010040
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070041class LayerInfo;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000042struct LayerProps;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070043
Ana Krulec61f86db2018-11-19 14:16:35 +010044class LayerHistory {
45public:
Andy Yu8c2703d2023-11-03 11:22:46 -070046 using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040047 using LayerVoteType = RefreshRateSelector::LayerVoteType;
Arthur Hungc70bee22023-06-02 01:35:52 +000048 static constexpr std::chrono::nanoseconds kMaxPeriodForHistory = 1s;
Ady Abraham8a82ba62020-01-17 12:43:17 -080049
Ady Abraham3efa3942021-06-24 19:01:25 -070050 LayerHistory();
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010051 ~LayerHistory();
Ana Krulec61f86db2018-11-19 14:16:35 +010052
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070053 // Layers are unregistered when the weak reference expires.
Andy Labrada096227e2022-06-15 16:58:11 +000054 void registerLayer(Layer*, bool contentDetectionEnabled);
Ady Abraham8a82ba62020-01-17 12:43:17 -080055
56 // Sets the display size. Client is responsible for synchronization.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010057 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
Ady Abraham09bd3922019-04-08 10:44:56 -070058
Marin Shalamanova7fe3042021-01-29 21:02:08 +010059 // Sets whether a mode change is pending to be applied
60 void setModeChangePending(bool pending) { mModeChangePending = pending; }
Ady Abraham32efd542020-05-19 17:49:26 -070061
Ady Abraham5def7332020-05-29 16:13:47 -070062 // Represents which layer activity is recorded
63 enum class LayerUpdateType {
64 Buffer, // a new buffer queued
65 AnimationTX, // a new transaction with eAnimation flag set
66 SetFrameRate, // setFrameRate API was called
67 };
68
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070069 // Marks the layer as active, and records the given state to its history.
Vishnu Nairef68d6d2023-02-28 06:18:27 +000070 void record(int32_t id, const LayerProps& props, nsecs_t presentTime, nsecs_t now,
71 LayerUpdateType updateType);
Ady Abrahama315ce72019-04-24 14:35:20 -070072
Andy Labrada096227e2022-06-15 16:58:11 +000073 // Updates the default frame rate compatibility which takes effect when the app
74 // does not set a preference for refresh rate.
Vishnu Nair80e8cfe2023-09-29 17:03:45 -070075 void setDefaultFrameRateCompatibility(int32_t id, FrameRateCompatibility frameRateCompatibility,
76 bool contentDetectionEnabled);
Vishnu Nair41376b62023-11-08 05:08:58 -080077 void setLayerProperties(int32_t id, const LayerProps&);
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040078 using Summary = std::vector<RefreshRateSelector::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070079
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070080 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040081 Summary summarize(const RefreshRateSelector&, nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070082
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010083 void clear();
Ady Abrahambdda8f02021-04-01 16:06:11 -070084
85 void deregisterLayer(Layer*);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010086 std::string dump() const;
Ady Abraham8a82ba62020-01-17 12:43:17 -080087
Nathaniel Nifong1303d912021-10-06 09:41:24 -040088 // return the frames per second of the layer with the given sequence id.
89 float getLayerFramerate(nsecs_t now, int32_t id) const;
90
Tony Huang9ac5e6e2023-08-24 09:01:44 +000091 bool isSmallDirtyArea(uint32_t dirtyArea, float threshold) const;
Arthur Hungc70bee22023-06-02 01:35:52 +000092
Andy Yu8c2703d2023-11-03 11:22:46 -070093 // Updates the frame rate override set by game mode intervention
94 void updateGameModeFrameRateOverride(FrameRateOverride frameRateOverride) EXCLUDES(mLock);
95
96 // Updates the frame rate override set by game default frame rate
97 void updateGameDefaultFrameRateOverride(FrameRateOverride frameRateOverride) EXCLUDES(mLock);
98
99 std::pair<Fps, Fps> getGameFrameRateOverride(uid_t uid) const EXCLUDES(mLock);
100 std::pair<Fps, Fps> getGameFrameRateOverrideLocked(uid_t uid) const REQUIRES(mLock);
101
Ady Abraham8a82ba62020-01-17 12:43:17 -0800102private:
Dominik Laskowski068173d2021-08-11 17:22:59 -0700103 friend class LayerHistoryTest;
Vishnu Nair47b7bb42023-09-29 16:27:33 -0700104 friend class LayerHistoryIntegrationTest;
Dominik Laskowski068173d2021-08-11 17:22:59 -0700105 friend class TestableScheduler;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800106
Ady Abrahambdda8f02021-04-01 16:06:11 -0700107 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400108 // keyed by id as returned from Layer::getSequence()
109 using LayerInfos = std::unordered_map<int32_t, LayerPair>;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110
Andy Yu0dca4862024-02-26 15:01:05 -0800111 std::string dumpGameFrameRateOverridesLocked() const REQUIRES(mLock);
112
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400113 // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive
Rachel Lee45681982024-03-14 18:40:15 -0700114 // layers to mInactiveLayerInfos. Layer's active state is determined by multiple factors
115 // such as update activity, visibility, and frame rate vote.
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400116 // worst case time complexity is O(2 * inactive + active)
Rachel Lee45681982024-03-14 18:40:15 -0700117 // now: the current time (system time) when calling the method
118 // isVrrDevice: true if the device has DisplayMode with VrrConfig specified.
119 void partitionLayers(nsecs_t now, bool isVrrDevice) REQUIRES(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800120
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800121 enum class LayerStatus {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400122 NotFound,
123 LayerInActiveMap,
124 LayerInInactiveMap,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800125 };
126
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400127 // looks up a layer by sequence id in both layerInfo maps.
128 // The first element indicates if and where the item was found
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800129 std::pair<LayerStatus, LayerPair*> findLayer(int32_t id) REQUIRES(mLock);
130
131 std::pair<LayerStatus, const LayerPair*> findLayer(int32_t id) const REQUIRES(mLock) {
132 return const_cast<LayerHistory*>(this)->findLayer(id);
133 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800134
135 mutable std::mutex mLock;
136
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400137 // Partitioned into two maps to facility two kinds of retrieval:
138 // 1. retrieval of a layer by id (attempt lookup in both maps)
139 // 2. retrieval of all active layers (iterate that map)
140 // The partitioning is allowed to become out of date but calling partitionLayers refreshes the
141 // validity of each map.
142 LayerInfos mActiveLayerInfos GUARDED_BY(mLock);
143 LayerInfos mInactiveLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700144
Ady Abraham8a82ba62020-01-17 12:43:17 -0800145 uint32_t mDisplayArea = 0;
146
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700147 // Whether to emit systrace output and debug logs.
148 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100149
150 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
151 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700152
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100153 // Whether a mode change is in progress or not
154 std::atomic<bool> mModeChangePending = false;
Andy Yu8c2703d2023-11-03 11:22:46 -0700155
156 // A list to look up the game frame rate overrides
157 // Each entry includes:
158 // 1. the uid of the app
159 // 2. a pair of game mode intervention frame frame and game default frame rate override
160 // set to 0.0 if there is no such override
161 std::map<uid_t, std::pair<Fps, Fps>> mGameFrameRateOverride GUARDED_BY(mLock);
Ana Krulec61f86db2018-11-19 14:16:35 +0100162};
163
Ady Abraham09bd3922019-04-08 10:44:56 -0700164} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700165} // namespace android