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 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 32 | #include "RefreshRateSelector.h" |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 33 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 34 | namespace android { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 35 | |
| 36 | class Layer; |
| 37 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 38 | namespace scheduler { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 39 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 40 | class LayerInfo; |
Vishnu Nair | ef68d6d | 2023-02-28 06:18:27 +0000 | [diff] [blame] | 41 | struct LayerProps; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 42 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 43 | class LayerHistory { |
| 44 | public: |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 45 | using LayerVoteType = RefreshRateSelector::LayerVoteType; |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame^] | 46 | static constexpr std::chrono::nanoseconds kMaxPeriodForHistory = 1s; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 47 | |
Ady Abraham | 3efa394 | 2021-06-24 19:01:25 -0700 | [diff] [blame] | 48 | LayerHistory(); |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 49 | ~LayerHistory(); |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 50 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 51 | // Layers are unregistered when the weak reference expires. |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 52 | void registerLayer(Layer*, bool contentDetectionEnabled); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 53 | |
| 54 | // Sets the display size. Client is responsible for synchronization. |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 55 | void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; } |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 56 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 57 | // Sets whether a mode change is pending to be applied |
| 58 | void setModeChangePending(bool pending) { mModeChangePending = pending; } |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 59 | |
Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 60 | // Represents which layer activity is recorded |
| 61 | enum class LayerUpdateType { |
| 62 | Buffer, // a new buffer queued |
| 63 | AnimationTX, // a new transaction with eAnimation flag set |
| 64 | SetFrameRate, // setFrameRate API was called |
| 65 | }; |
| 66 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 67 | // 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] | 68 | void record(int32_t id, const LayerProps& props, nsecs_t presentTime, nsecs_t now, |
| 69 | LayerUpdateType updateType); |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 70 | |
Andy Labrada | 096227e | 2022-06-15 16:58:11 +0000 | [diff] [blame] | 71 | // Updates the default frame rate compatibility which takes effect when the app |
| 72 | // does not set a preference for refresh rate. |
| 73 | void setDefaultFrameRateCompatibility(Layer*, bool contentDetectionEnabled); |
| 74 | |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 75 | using Summary = std::vector<RefreshRateSelector::LayerRequirement>; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 76 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 77 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
Dominik Laskowski | d82e0f0 | 2022-10-26 15:23:04 -0400 | [diff] [blame] | 78 | Summary summarize(const RefreshRateSelector&, nsecs_t now); |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 79 | |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 80 | void clear(); |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 81 | |
| 82 | void deregisterLayer(Layer*); |
Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 83 | std::string dump() const; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 84 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 85 | // return the frames per second of the layer with the given sequence id. |
| 86 | float getLayerFramerate(nsecs_t now, int32_t id) const; |
| 87 | |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame^] | 88 | bool isSmallDirtyArea(uint32_t dirtyArea) const; |
| 89 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 90 | private: |
Dominik Laskowski | 068173d | 2021-08-11 17:22:59 -0700 | [diff] [blame] | 91 | friend class LayerHistoryTest; |
| 92 | friend class TestableScheduler; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 93 | |
Arthur Hung | c70bee2 | 2023-06-02 01:35:52 +0000 | [diff] [blame^] | 94 | static constexpr float kSmallDirtyArea = 0.07f; |
| 95 | |
Ady Abraham | bdda8f0 | 2021-04-01 16:06:11 -0700 | [diff] [blame] | 96 | using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>; |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 97 | // keyed by id as returned from Layer::getSequence() |
| 98 | using LayerInfos = std::unordered_map<int32_t, LayerPair>; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 99 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 100 | // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive |
| 101 | // layers to mInactiveLayerInfos. |
| 102 | // worst case time complexity is O(2 * inactive + active) |
| 103 | void partitionLayers(nsecs_t now) REQUIRES(mLock); |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 104 | |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 105 | enum class LayerStatus { |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 106 | NotFound, |
| 107 | LayerInActiveMap, |
| 108 | LayerInInactiveMap, |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 111 | // looks up a layer by sequence id in both layerInfo maps. |
| 112 | // The first element indicates if and where the item was found |
Dominik Laskowski | 0c41ffa | 2021-12-24 16:45:12 -0800 | [diff] [blame] | 113 | std::pair<LayerStatus, LayerPair*> findLayer(int32_t id) REQUIRES(mLock); |
| 114 | |
| 115 | std::pair<LayerStatus, const LayerPair*> findLayer(int32_t id) const REQUIRES(mLock) { |
| 116 | return const_cast<LayerHistory*>(this)->findLayer(id); |
| 117 | } |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 118 | |
| 119 | mutable std::mutex mLock; |
| 120 | |
Nathaniel Nifong | 1303d91 | 2021-10-06 09:41:24 -0400 | [diff] [blame] | 121 | // Partitioned into two maps to facility two kinds of retrieval: |
| 122 | // 1. retrieval of a layer by id (attempt lookup in both maps) |
| 123 | // 2. retrieval of all active layers (iterate that map) |
| 124 | // The partitioning is allowed to become out of date but calling partitionLayers refreshes the |
| 125 | // validity of each map. |
| 126 | LayerInfos mActiveLayerInfos GUARDED_BY(mLock); |
| 127 | LayerInfos mInactiveLayerInfos GUARDED_BY(mLock); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 128 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 129 | uint32_t mDisplayArea = 0; |
| 130 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 131 | // Whether to emit systrace output and debug logs. |
| 132 | const bool mTraceEnabled; |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 133 | |
| 134 | // Whether to use priority sent from WindowManager to determine the relevancy of the layer. |
| 135 | const bool mUseFrameRatePriority; |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 136 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 137 | // Whether a mode change is in progress or not |
| 138 | std::atomic<bool> mModeChangePending = false; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 141 | } // namespace scheduler |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 142 | } // namespace android |