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 19 #include <android-base/thread_annotations.h> 20 #include <utils/RefBase.h> 21 #include <utils/Timers.h> 22 23 #include <memory> 24 #include <mutex> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 #include "RefreshRateConfigs.h" 30 31 namespace android { 32 33 class Layer; 34 class TestableScheduler; 35 36 namespace scheduler { 37 38 class LayerHistoryTest; 39 class LayerInfo; 40 41 class LayerHistory { 42 public: 43 using LayerVoteType = RefreshRateConfigs::LayerVoteType; 44 45 LayerHistory(const RefreshRateConfigs&); 46 ~LayerHistory(); 47 48 // Layers are unregistered when the weak reference expires. 49 void registerLayer(Layer*, LayerVoteType type); 50 51 // Sets the display size. Client is responsible for synchronization. setDisplayArea(uint32_t displayArea)52 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; } 53 54 // Sets whether a mode change is pending to be applied setModeChangePending(bool pending)55 void setModeChangePending(bool pending) { mModeChangePending = pending; } 56 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 64 // Marks the layer as active, and records the given state to its history. 65 void record(Layer*, nsecs_t presentTime, nsecs_t now, LayerUpdateType updateType); 66 67 using Summary = std::vector<RefreshRateConfigs::LayerRequirement>; 68 69 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers. 70 Summary summarize(nsecs_t now); 71 72 void clear(); 73 74 void deregisterLayer(Layer*); 75 std::string dump() const; 76 77 private: 78 friend LayerHistoryTest; 79 friend TestableScheduler; 80 81 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>; 82 using LayerInfos = std::vector<LayerPair>; 83 84 struct ActiveLayers { 85 LayerInfos& infos; 86 const size_t index; 87 beginActiveLayers88 auto begin() { return infos.begin(); } endActiveLayers89 auto end() { return begin() + static_cast<long>(index); } 90 }; 91 activeLayers()92 ActiveLayers activeLayers() REQUIRES(mLock) { return {mLayerInfos, mActiveLayersEnd}; } 93 94 // Iterates over layers in a single pass, swapping pairs such that active layers precede 95 // inactive layers, and inactive layers precede expired layers. Removes expired layers by 96 // truncating after inactive layers. 97 void partitionLayers(nsecs_t now) REQUIRES(mLock); 98 99 mutable std::mutex mLock; 100 101 // Partitioned such that active layers precede inactive layers. For fast lookup, the few active 102 // layers are at the front, and weak pointers are stored in contiguous memory to hit the cache. 103 LayerInfos mLayerInfos GUARDED_BY(mLock); 104 size_t mActiveLayersEnd GUARDED_BY(mLock) = 0; 105 106 uint32_t mDisplayArea = 0; 107 108 // Whether to emit systrace output and debug logs. 109 const bool mTraceEnabled; 110 111 // Whether to use priority sent from WindowManager to determine the relevancy of the layer. 112 const bool mUseFrameRatePriority; 113 114 // Whether a mode change is in progress or not 115 std::atomic<bool> mModeChangePending = false; 116 }; 117 118 } // namespace scheduler 119 } // namespace android 120