blob: adc5ce5f64c96e4f01ff08270dbbdc955d5644f6 [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.
Ady Abrahama315ce72019-04-24 14:35:20 -070059 void insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime, bool isHdr);
60 // Method for setting layer visibility
61 void setVisibility(const std::unique_ptr<LayerHandle>& layerHandle, bool visible);
62
Ady Abraham09bd3922019-04-08 10:44:56 -070063 // Returns the desired refresh rate, which is a max refresh rate of all the current
64 // layers. See go/content-fps-detection-in-scheduler for more information.
Ady Abrahama315ce72019-04-24 14:35:20 -070065 std::pair<float, bool> getDesiredRefreshRateAndHDR();
Ady Abraham09bd3922019-04-08 10:44:56 -070066
67 // Removes the handle and the object from the map.
68 void destroyLayer(const int64_t id);
Ana Krulec61f86db2018-11-19 14:16:35 +010069
70private:
Ady Abraham09bd3922019-04-08 10:44:56 -070071 // Removes the layers that have been idle for a given amount of time from mLayerInfos.
72 void removeIrrelevantLayers() REQUIRES(mLock);
73
74 // Information about currently active layers.
75 std::mutex mLock;
76 std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mActiveLayerInfos GUARDED_BY(mLock);
77 std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mInactiveLayerInfos GUARDED_BY(mLock);
78
79 // Each layer has it's own ID. This variable keeps track of the count.
80 static std::atomic<int64_t> sNextId;
81
82 // Flag whether to log layer FPS in systrace
83 bool mTraceEnabled = false;
Ana Krulec61f86db2018-11-19 14:16:35 +010084};
85
Ady Abraham09bd3922019-04-08 10:44:56 -070086} // namespace scheduler
Ana Krulec61f86db2018-11-19 14:16:35 +010087} // namespace android