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 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 28 | namespace android { |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 29 | |
| 30 | class Layer; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 31 | class TestableScheduler; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 32 | |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 33 | namespace scheduler { |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 34 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 35 | class LayerHistoryTest; |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 36 | class LayerInfo; |
| 37 | |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 38 | class LayerHistory { |
| 39 | public: |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 40 | virtual ~LayerHistory() = default; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 41 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 42 | // Layers are unregistered when the weak reference expires. |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 43 | virtual void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate) = 0; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 44 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 45 | // 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] | 46 | virtual void record(Layer*, nsecs_t presentTime, nsecs_t now) = 0; |
Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 47 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 48 | struct Summary { |
| 49 | float maxRefreshRate; // Maximum refresh rate among recently active layers. |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 50 | }; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 51 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 52 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 53 | virtual Summary summarize(nsecs_t now) = 0; |
Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 54 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 55 | virtual void clear() = 0; |
| 56 | }; |
| 57 | |
| 58 | namespace impl { |
| 59 | // Records per-layer history of scheduling-related information (primarily present time), |
| 60 | // heuristically categorizes layers as active or inactive, and summarizes stats about |
| 61 | // active layers (primarily maximum refresh rate). See go/content-fps-detection-in-scheduler. |
| 62 | class LayerHistory : public android::scheduler::LayerHistory { |
| 63 | public: |
| 64 | LayerHistory(); |
| 65 | virtual ~LayerHistory(); |
| 66 | |
| 67 | // Layers are unregistered when the weak reference expires. |
| 68 | void registerLayer(Layer*, float lowRefreshRate, float highRefreshRate) override; |
| 69 | |
| 70 | // Marks the layer as active, and records the given state to its history. |
| 71 | void record(Layer*, nsecs_t presentTime, nsecs_t now) override; |
| 72 | |
| 73 | // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. |
| 74 | android::scheduler::LayerHistory::Summary summarize(nsecs_t now) override; |
| 75 | |
| 76 | void clear() override; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 77 | |
| 78 | private: |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 79 | friend class android::scheduler::LayerHistoryTest; |
Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 80 | friend TestableScheduler; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 81 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 82 | using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>; |
| 83 | using LayerInfos = std::vector<LayerPair>; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 84 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 85 | struct ActiveLayers { |
| 86 | LayerInfos& infos; |
| 87 | const size_t index; |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 88 | |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 89 | auto begin() { return infos.begin(); } |
| 90 | auto end() { return begin() + index; } |
| 91 | }; |
| 92 | |
| 93 | ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; } |
| 94 | |
| 95 | // Iterates over layers in a single pass, swapping pairs such that active layers precede |
| 96 | // inactive layers, and inactive layers precede expired layers. Removes expired layers by |
| 97 | // truncating after inactive layers. |
| 98 | void partitionLayers(nsecs_t now) REQUIRES(mLock); |
| 99 | |
| 100 | mutable std::mutex mLock; |
| 101 | |
| 102 | // Partitioned such that active layers precede inactive layers. For fast lookup, the few active |
| 103 | // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache. |
| 104 | LayerInfos mLayerInfos GUARDED_BY(mLock); |
| 105 | size_t mActiveLayersEnd GUARDED_BY(mLock) = 0; |
| 106 | |
| 107 | // Whether to emit systrace output and debug logs. |
| 108 | const bool mTraceEnabled; |
Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame^] | 109 | |
| 110 | // Whether to use priority sent from WindowManager to determine the relevancy of the layer. |
| 111 | const bool mUseFrameRatePriority; |
Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 114 | } // namespace impl |
Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 115 | } // namespace scheduler |
Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 116 | } // namespace android |