Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 1 | /* |
| 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 Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 19 | #include <android-base/thread_annotations.h> |
| 20 | #include <utils/RefBase.h> |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 21 | #include <utils/Timers.h> |
| 22 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 23 | #include <map> |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <mutex> |
Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 26 | #include <string> |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 27 | #include <utility> |
| 28 | #include <vector> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 29 | |
Rachel Lee | 2248f52 | 2023-01-27 16:45:23 -0800 | [diff] [blame] | 30 | #include "EventThread.h" |
| 31 | |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 32 | #include "FrameRateCompatibility.h" |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 33 | #include "RefreshRateSelector.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 34 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 35 | namespace android { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 36 | |
| 37 | class Layer; |
| 38 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 39 | namespace scheduler { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 40 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 41 | class LayerInfo; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 42 | struct LayerProps; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 43 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 44 | class LayerHistory { |
| 45 | public: |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 46 | using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride; |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 47 | using LayerVoteType = RefreshRateSelector::LayerVoteType; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 48 | static constexpr std::chrono::nanoseconds kMaxPeriodForHistory = 1s; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 49 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 50 | LayerHistory(); |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 51 | ~LayerHistory(); |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 52 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 53 | // Layers are unregistered when the weak reference expires. |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 54 | void registerLayer(Layer*, bool contentDetectionEnabled); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 55 | |
| 56 | // Sets the display size. Client is responsible for synchronization. |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 57 | void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 58 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 59 | // Sets whether a mode change is pending to be applied |
| 60 | void setModeChangePending(bool pending) { mModeChangePending = pending; } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 61 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 62 | // 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 Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 69 | // Marks the layer as active, and records the given state to its history. |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 70 | void record(int32_t id, const LayerProps& props, nsecs_t presentTime, nsecs_t now, |
| 71 | LayerUpdateType updateType); |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 72 | |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 73 | // Updates the default frame rate compatibility which takes effect when the app |
| 74 | // does not set a preference for refresh rate. |
Vishnu Nair | 80e8cfe | 2023-09-29 17:03:45 -0700 | [diff] [blame] | 75 | void setDefaultFrameRateCompatibility(int32_t id, FrameRateCompatibility frameRateCompatibility, |
| 76 | bool contentDetectionEnabled); |
Vishnu Nair | 41376b6 | 2023-11-08 05:08:58 -0800 | [diff] [blame] | 77 | void setLayerProperties(int32_t id, const LayerProps&); |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 78 | using Summary = std::vector<RefreshRateSelector::LayerRequirement>; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 79 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 80 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 81 | Summary summarize(const RefreshRateSelector&, nsecs_t now); |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 82 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 83 | void clear(); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 84 | |
| 85 | void deregisterLayer(Layer*); |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 86 | std::string dump() const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 87 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 88 | // 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 Huang | 9ac5e6e | 2023-08-24 09:01:44 +0000 | [diff] [blame] | 91 | bool isSmallDirtyArea(uint32_t dirtyArea, float threshold) const; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame] | 92 | |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 93 | // 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 Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 102 | private: |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 103 | friend class LayerHistoryTest; |
Vishnu Nair | 47b7bb4 | 2023-09-29 16:27:33 -0700 | [diff] [blame] | 104 | friend class LayerHistoryIntegrationTest; |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 105 | friend class TestableScheduler; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 106 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 107 | using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 108 | // keyed by id as returned from Layer::getSequence() |
| 109 | using LayerInfos = std::unordered_map<int32_t, LayerPair>; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 110 | |
Andy Yu | 0dca486 | 2024-02-26 15:01:05 -0800 | [diff] [blame] | 111 | std::string dumpGameFrameRateOverridesLocked() const REQUIRES(mLock); |
| 112 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 113 | // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive |
Rachel Lee | 4568198 | 2024-03-14 18:40:15 -0700 | [diff] [blame] | 114 | // layers to mInactiveLayerInfos. Layer's active state is determined by multiple factors |
| 115 | // such as update activity, visibility, and frame rate vote. |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 116 | // worst case time complexity is O(2 * inactive + active) |
Rachel Lee | 4568198 | 2024-03-14 18:40:15 -0700 | [diff] [blame] | 117 | // 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 Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 120 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 121 | enum class LayerStatus { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 122 | NotFound, |
| 123 | LayerInActiveMap, |
| 124 | LayerInInactiveMap, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 127 | // looks up a layer by sequence id in both layerInfo maps. |
| 128 | // The first element indicates if and where the item was found |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 129 | 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 Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 134 | |
| 135 | mutable std::mutex mLock; |
| 136 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 137 | // 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 Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 144 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 145 | uint32_t mDisplayArea = 0; |
| 146 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 147 | // Whether to emit systrace output and debug logs. |
| 148 | const bool mTraceEnabled; |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 149 | |
| 150 | // Whether to use priority sent from WindowManager to determine the relevancy of the layer. |
| 151 | const bool mUseFrameRatePriority; |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 152 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 153 | // Whether a mode change is in progress or not |
| 154 | std::atomic<bool> mModeChangePending = false; |
Andy Yu | 8c2703d | 2023-11-03 11:22:46 -0700 | [diff] [blame] | 155 | |
| 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 Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 164 | } // namespace scheduler |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 165 | } // namespace android |