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 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <mutex> |
| 25 | #include <utility> |
| 26 | #include <vector> |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 27 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 28 | #include "RefreshRateConfigs.h" |
| 29 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 30 | namespace android { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 31 | |
| 32 | class Layer; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 33 | class TestableScheduler; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 34 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 35 | namespace scheduler { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 36 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 37 | class LayerHistoryTest; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 38 | class LayerHistoryTestV2; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 39 | class LayerInfo; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 40 | class LayerInfoV2; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 41 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 42 | class LayerHistory { |
| 43 | public: |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 44 | using LayerVoteType = RefreshRateConfigs::LayerVoteType; |
| 45 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 46 | virtual ~LayerHistory() = default; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 47 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 48 | // Layers are unregistered when the weak reference expires. |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 49 | virtual void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate, |
| 50 | LayerVoteType type) = 0; |
| 51 | |
| 52 | // Sets the display size. Client is responsible for synchronization. |
| 53 | virtual void setDisplayArea(uint32_t displayArea) = 0; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 54 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 55 | virtual void setConfigChangePending(bool pending) = 0; |
| 56 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 57 | // Marks the layer as active, and records the given state to its history. |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 58 | virtual void record(Layer*, nsecs_t presentTime, nsecs_t now) = 0; |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 59 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 60 | using Summary = std::vector<RefreshRateConfigs::LayerRequirement>; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 61 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 62 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 63 | virtual Summary summarize(nsecs_t now) = 0; |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 64 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 65 | virtual void clear() = 0; |
| 66 | }; |
| 67 | |
| 68 | namespace impl { |
| 69 | // Records per-layer history of scheduling-related information (primarily present time), |
| 70 | // heuristically categorizes layers as active or inactive, and summarizes stats about |
| 71 | // active layers (primarily maximum refresh rate). See go/content-fps-detection-in-scheduler. |
| 72 | class LayerHistory : public android::scheduler::LayerHistory { |
| 73 | public: |
| 74 | LayerHistory(); |
| 75 | virtual ~LayerHistory(); |
| 76 | |
| 77 | // Layers are unregistered when the weak reference expires. |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 78 | void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate, |
| 79 | LayerVoteType type) override; |
| 80 | |
| 81 | void setDisplayArea(uint32_t /*displayArea*/) override {} |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 82 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 83 | void setConfigChangePending(bool /*pending*/) override {} |
| 84 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 85 | // Marks the layer as active, and records the given state to its history. |
| 86 | void record(Layer*, nsecs_t presentTime, nsecs_t now) override; |
| 87 | |
| 88 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
| 89 | android::scheduler::LayerHistory::Summary summarize(nsecs_t now) override; |
| 90 | |
| 91 | void clear() override; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 92 | |
| 93 | private: |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 94 | friend class android::scheduler::LayerHistoryTest; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 95 | friend TestableScheduler; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 96 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 97 | using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>; |
| 98 | using LayerInfos = std::vector<LayerPair>; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 99 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 100 | struct ActiveLayers { |
| 101 | LayerInfos& infos; |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 102 | const size_t index; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 103 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 104 | auto begin() { return infos.begin(); } |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 105 | auto end() { return begin() + static_cast<long>(index); } |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; } |
| 109 | |
| 110 | // Iterates over layers in a single pass, swapping pairs such that active layers precede |
| 111 | // inactive layers, and inactive layers precede expired layers. Removes expired layers by |
| 112 | // truncating after inactive layers. |
| 113 | void partitionLayers(nsecs_t now) REQUIRES(mLock); |
| 114 | |
| 115 | mutable std::mutex mLock; |
| 116 | |
| 117 | // Partitioned such that active layers precede inactive layers. For fast lookup, the few active |
| 118 | // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache. |
| 119 | LayerInfos mLayerInfos GUARDED_BY(mLock); |
Ady Abraham | dec1a41 | 2020-01-24 10:23:50 -0800 | [diff] [blame] | 120 | size_t mActiveLayersEnd GUARDED_BY(mLock) = 0; |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 121 | |
| 122 | // Whether to emit systrace output and debug logs. |
| 123 | const bool mTraceEnabled; |
| 124 | |
| 125 | // Whether to use priority sent from WindowManager to determine the relevancy of the layer. |
| 126 | const bool mUseFrameRatePriority; |
| 127 | }; |
| 128 | |
| 129 | class LayerHistoryV2 : public android::scheduler::LayerHistory { |
| 130 | public: |
| 131 | LayerHistoryV2(); |
| 132 | virtual ~LayerHistoryV2(); |
| 133 | |
| 134 | // Layers are unregistered when the weak reference expires. |
| 135 | void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate, |
| 136 | LayerVoteType type) override; |
| 137 | |
| 138 | // Sets the display size. Client is responsible for synchronization. |
| 139 | void setDisplayArea(uint32_t displayArea) override { mDisplayArea = displayArea; } |
| 140 | |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 141 | void setConfigChangePending(bool pending) override { mConfigChangePending = pending; } |
| 142 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 143 | // Marks the layer as active, and records the given state to its history. |
| 144 | void record(Layer*, nsecs_t presentTime, nsecs_t now) override; |
| 145 | |
| 146 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
| 147 | android::scheduler::LayerHistory::Summary summarize(nsecs_t /*now*/) override; |
| 148 | |
| 149 | void clear() override; |
| 150 | |
| 151 | private: |
| 152 | friend android::scheduler::LayerHistoryTestV2; |
| 153 | friend TestableScheduler; |
| 154 | |
| 155 | using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfoV2>>; |
| 156 | using LayerInfos = std::vector<LayerPair>; |
| 157 | |
| 158 | struct ActiveLayers { |
| 159 | LayerInfos& infos; |
| 160 | const size_t index; |
| 161 | |
| 162 | auto begin() { return infos.begin(); } |
| 163 | auto end() { return begin() + static_cast<long>(index); } |
| 164 | }; |
| 165 | |
| 166 | ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; } |
| 167 | |
| 168 | // Iterates over layers in a single pass, swapping pairs such that active layers precede |
| 169 | // inactive layers, and inactive layers precede expired layers. Removes expired layers by |
| 170 | // truncating after inactive layers. |
| 171 | void partitionLayers(nsecs_t now) REQUIRES(mLock); |
| 172 | |
| 173 | mutable std::mutex mLock; |
| 174 | |
| 175 | // Partitioned such that active layers precede inactive layers. For fast lookup, the few active |
| 176 | // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache. |
| 177 | LayerInfos mLayerInfos GUARDED_BY(mLock); |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 178 | size_t mActiveLayersEnd GUARDED_BY(mLock) = 0; |
| 179 | |
Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 180 | uint32_t mDisplayArea = 0; |
| 181 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 182 | // Whether to emit systrace output and debug logs. |
| 183 | const bool mTraceEnabled; |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 184 | |
| 185 | // Whether to use priority sent from WindowManager to determine the relevancy of the layer. |
| 186 | const bool mUseFrameRatePriority; |
Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame^] | 187 | |
| 188 | // Whether a config change is in progress or not |
| 189 | std::atomic<bool> mConfigChangePending = false; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 192 | } // namespace impl |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 193 | } // namespace scheduler |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 194 | } // namespace android |