blob: 39061e7fdfe3af1a6739940c3e958f88310ba3cc [file] [log] [blame]
Ana Krulec61f86db2018-11-19 14:16:35 +01001/*
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
Ady Abraham09bd3922019-04-08 10:44:56 -070028#include "LayerInfo.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010029#include "SchedulerUtils.h"
30
Ana Krulec61f86db2018-11-19 14:16:35 +010031namespace android {
Ady Abraham09bd3922019-04-08 10:44:56 -070032namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010033
34/*
Ady Abraham09bd3922019-04-08 10:44:56 -070035 * This class represents information about layers that are considered current. We keep an
36 * unordered map between layer name and LayerInfo.
Ana Krulec61f86db2018-11-19 14:16:35 +010037 */
38class LayerHistory {
39public:
Ady Abraham09bd3922019-04-08 10:44:56 -070040 // Handle for each layer we keep track of.
41 class LayerHandle {
42 public:
43 LayerHandle(LayerHistory& lh, int64_t id) : mId(id), mLayerHistory(lh) {}
44 ~LayerHandle() { mLayerHistory.destroyLayer(mId); }
45
46 const int64_t mId;
47
48 private:
49 LayerHistory& mLayerHistory;
50 };
51
Ana Krulec61f86db2018-11-19 14:16:35 +010052 LayerHistory();
53 ~LayerHistory();
54
Ady Abraham09bd3922019-04-08 10:44:56 -070055 // When the layer is first created, register it.
56 std::unique_ptr<LayerHandle> createLayer(const std::string name, float maxRefreshRate);
57
58 // Method for inserting layers and their requested present time into the unordered map.
59 void insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime);
60 // Returns the desired refresh rate, which is a max refresh rate of all the current
61 // layers. See go/content-fps-detection-in-scheduler for more information.
62 float getDesiredRefreshRate();
63
64 // Removes the handle and the object from the map.
65 void destroyLayer(const int64_t id);
Ana Krulec61f86db2018-11-19 14:16:35 +010066
67private:
Ady Abraham09bd3922019-04-08 10:44:56 -070068 // Removes the layers that have been idle for a given amount of time from mLayerInfos.
69 void removeIrrelevantLayers() REQUIRES(mLock);
70
71 // Information about currently active layers.
72 std::mutex mLock;
73 std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mActiveLayerInfos GUARDED_BY(mLock);
74 std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mInactiveLayerInfos GUARDED_BY(mLock);
75
76 // Each layer has it's own ID. This variable keeps track of the count.
77 static std::atomic<int64_t> sNextId;
78
79 // Flag whether to log layer FPS in systrace
80 bool mTraceEnabled = false;
Ana Krulec61f86db2018-11-19 14:16:35 +010081};
82
Ady Abraham09bd3922019-04-08 10:44:56 -070083} // namespace scheduler
Ana Krulec61f86db2018-11-19 14:16:35 +010084} // namespace android