| 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> | 
| Dominik Laskowski | 983f2b5 | 2020-06-25 16:54:06 -0700 | [diff] [blame] | 25 | #include <string> | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 26 | #include <utility> | 
 | 27 | #include <vector> | 
| Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 28 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 29 | #include "RefreshRateConfigs.h" | 
 | 30 |  | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 31 | namespace android { | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 32 |  | 
 | 33 | class Layer; | 
| Dominik Laskowski | 49cea51 | 2019-11-12 14:13:23 -0800 | [diff] [blame] | 34 | class TestableScheduler; | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 35 |  | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 36 | namespace scheduler { | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 37 |  | 
| Ady Abraham | e3ed2f9 | 2020-01-06 17:01:28 -0800 | [diff] [blame] | 38 | class LayerHistoryTest; | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 39 | class LayerInfo; | 
 | 40 |  | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 41 | class LayerHistory { | 
 | 42 | public: | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 43 |     using LayerVoteType = RefreshRateConfigs::LayerVoteType; | 
 | 44 |  | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 45 |     LayerHistory(const RefreshRateConfigs&); | 
 | 46 |     ~LayerHistory(); | 
| 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. | 
| Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame] | 49 |     void registerLayer(Layer*, Fps highRefreshRate, LayerVoteType type); | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 50 |  | 
 | 51 |     // Sets the display size. Client is responsible for synchronization. | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 52 |     void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; } | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 53 |  | 
| Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 54 |     // Sets whether a config change is pending to be applied | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 55 |     void setConfigChangePending(bool pending) { mConfigChangePending = pending; } | 
| Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 56 |  | 
| Ady Abraham | 5def733 | 2020-05-29 16:13:47 -0700 | [diff] [blame] | 57 |     // Represents which layer activity is recorded | 
 | 58 |     enum class LayerUpdateType { | 
 | 59 |         Buffer,       // a new buffer queued | 
 | 60 |         AnimationTX,  // a new transaction with eAnimation flag set | 
 | 61 |         SetFrameRate, // setFrameRate API was called | 
 | 62 |     }; | 
 | 63 |  | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 64 |     // Marks the layer as active, and records the given state to its history. | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 65 |     void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType); | 
| Ady Abraham | a315ce7 | 2019-04-24 14:35:20 -0700 | [diff] [blame] | 66 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 67 |     using Summary = std::vector<RefreshRateConfigs::LayerRequirement>; | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 68 |  | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 69 |     // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 70 |     Summary summarize(nsecs_t now); | 
| Ady Abraham | a9bf4ca | 2019-06-11 19:08:58 -0700 | [diff] [blame] | 71 |  | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 72 |     void clear(); | 
 | 73 |     std::string dump() const; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 74 |  | 
 | 75 | private: | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 76 |     friend LayerHistoryTest; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 77 |     friend TestableScheduler; | 
 | 78 |  | 
| Marin Shalamanov | 1bc43ee | 2020-11-20 16:56:52 +0100 | [diff] [blame] | 79 |     using LayerPair = std::pair<wp<Layer>, std::unique_ptr<LayerInfo>>; | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 80 |     using LayerInfos = std::vector<LayerPair>; | 
 | 81 |  | 
 | 82 |     struct ActiveLayers { | 
 | 83 |         LayerInfos& infos; | 
 | 84 |         const size_t index; | 
 | 85 |  | 
 | 86 |         auto begin() { return infos.begin(); } | 
 | 87 |         auto end() { return begin() + static_cast<long>(index); } | 
 | 88 |     }; | 
 | 89 |  | 
 | 90 |     ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; } | 
 | 91 |  | 
 | 92 |     // Iterates over layers in a single pass, swapping pairs such that active layers precede | 
 | 93 |     // inactive layers, and inactive layers precede expired layers. Removes expired layers by | 
 | 94 |     // truncating after inactive layers. | 
 | 95 |     void partitionLayers(nsecs_t now) REQUIRES(mLock); | 
 | 96 |  | 
 | 97 |     mutable std::mutex mLock; | 
 | 98 |  | 
 | 99 |     // Partitioned such that active layers precede inactive layers. For fast lookup, the few active | 
 | 100 |     // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache. | 
 | 101 |     LayerInfos mLayerInfos GUARDED_BY(mLock); | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 102 |     size_t mActiveLayersEnd GUARDED_BY(mLock) = 0; | 
 | 103 |  | 
| Ady Abraham | 8a82ba6 | 2020-01-17 12:43:17 -0800 | [diff] [blame] | 104 |     uint32_t mDisplayArea = 0; | 
 | 105 |  | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 106 |     // Whether to emit systrace output and debug logs. | 
 | 107 |     const bool mTraceEnabled; | 
| Ana Krulec | c84d09b | 2019-11-02 23:10:29 +0100 | [diff] [blame] | 108 |  | 
 | 109 |     // Whether to use priority sent from WindowManager to determine the relevancy of the layer. | 
 | 110 |     const bool mUseFrameRatePriority; | 
| Ady Abraham | 32efd54 | 2020-05-19 17:49:26 -0700 | [diff] [blame] | 111 |  | 
 | 112 |     // Whether a config change is in progress or not | 
 | 113 |     std::atomic<bool> mConfigChangePending = false; | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 114 | }; | 
 | 115 |  | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 116 | } // namespace scheduler | 
| Dominik Laskowski | f7a09ed | 2019-10-07 13:54:18 -0700 | [diff] [blame] | 117 | } // namespace android |