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