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