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 <array>
20 #include <cinttypes>
21 #include <cstdint>
22 #include <numeric>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <utils/Timers.h>
27 
28 #include "LayerInfo.h"
29 #include "SchedulerUtils.h"
30 
31 namespace android {
32 namespace scheduler {
33 
34 /*
35  * This class represents information about layers that are considered current. We keep an
36  * unordered map between layer name and LayerInfo.
37  */
38 class LayerHistory {
39 public:
40     // Handle for each layer we keep track of.
41     class LayerHandle {
42     public:
LayerHandle(LayerHistory & lh,int64_t id)43         LayerHandle(LayerHistory& lh, int64_t id) : mId(id), mLayerHistory(lh) {}
~LayerHandle()44         ~LayerHandle() { mLayerHistory.destroyLayer(mId); }
45 
46         const int64_t mId;
47 
48     private:
49         LayerHistory& mLayerHistory;
50     };
51 
52     LayerHistory();
53     ~LayerHistory();
54 
55     // When the layer is first created, register it.
56     std::unique_ptr<LayerHandle> createLayer(const std::string name, float minRefreshRate,
57                                              float maxRefreshRate);
58 
59     // Method for inserting layers and their requested present time into the unordered map.
60     void insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime, bool isHdr);
61     // Method for setting layer visibility
62     void setVisibility(const std::unique_ptr<LayerHandle>& layerHandle, bool visible);
63 
64     // Returns the desired refresh rate, which is a max refresh rate of all the current
65     // layers. See go/content-fps-detection-in-scheduler for more information.
66     std::pair<float, bool> getDesiredRefreshRateAndHDR();
67 
68     // Clears all layer history.
69     void clearHistory();
70 
71     // Removes the handle and the object from the map.
72     void destroyLayer(const int64_t id);
73 
74 private:
75     // Removes the layers that have been idle for a given amount of time from mLayerInfos.
76     void removeIrrelevantLayers() REQUIRES(mLock);
77 
78     // Information about currently active layers.
79     std::mutex mLock;
80     std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mActiveLayerInfos GUARDED_BY(mLock);
81     std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mInactiveLayerInfos GUARDED_BY(mLock);
82 
83     // Each layer has it's own ID. This variable keeps track of the count.
84     static std::atomic<int64_t> sNextId;
85 
86     // Flag whether to log layer FPS in systrace
87     bool mTraceEnabled = false;
88 };
89 
90 } // namespace scheduler
91 } // namespace android