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