blob: 562bb4cace7d78b3ec316cde4a12bb810c058ccf [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
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070019#include <android-base/thread_annotations.h>
20#include <utils/RefBase.h>
Ana Krulec61f86db2018-11-19 14:16:35 +010021#include <utils/Timers.h>
22
Nathaniel Nifong1303d912021-10-06 09:41:24 -040023#include <map>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070024#include <memory>
25#include <mutex>
Dominik Laskowski983f2b52020-06-25 16:54:06 -070026#include <string>
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070027#include <utility>
28#include <vector>
Ana Krulec434c22d2018-11-28 13:48:36 +010029
Rachel Lee2248f522023-01-27 16:45:23 -080030#include "EventThread.h"
31
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040032#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080033
Ana Krulec61f86db2018-11-19 14:16:35 +010034namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070035
36class Layer;
37
Ady Abraham09bd3922019-04-08 10:44:56 -070038namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010039
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070040class LayerInfo;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000041struct LayerProps;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070042
Ana Krulec61f86db2018-11-19 14:16:35 +010043class LayerHistory {
44public:
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040045 using LayerVoteType = RefreshRateSelector::LayerVoteType;
Arthur Hungc70bee22023-06-02 01:35:52 +000046 static constexpr std::chrono::nanoseconds kMaxPeriodForHistory = 1s;
Ady Abraham8a82ba62020-01-17 12:43:17 -080047
Ady Abraham3efa3942021-06-24 19:01:25 -070048 LayerHistory();
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010049 ~LayerHistory();
Ana Krulec61f86db2018-11-19 14:16:35 +010050
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070051 // Layers are unregistered when the weak reference expires.
Andy Labrada096227e2022-06-15 16:58:11 +000052 void registerLayer(Layer*, bool contentDetectionEnabled);
Ady Abraham8a82ba62020-01-17 12:43:17 -080053
54 // Sets the display size. Client is responsible for synchronization.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010055 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
Ady Abraham09bd3922019-04-08 10:44:56 -070056
Marin Shalamanova7fe3042021-01-29 21:02:08 +010057 // Sets whether a mode change is pending to be applied
58 void setModeChangePending(bool pending) { mModeChangePending = pending; }
Ady Abraham32efd542020-05-19 17:49:26 -070059
Ady Abraham5def7332020-05-29 16:13:47 -070060 // Represents which layer activity is recorded
61 enum class LayerUpdateType {
62 Buffer, // a new buffer queued
63 AnimationTX, // a new transaction with eAnimation flag set
64 SetFrameRate, // setFrameRate API was called
65 };
66
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070067 // Marks the layer as active, and records the given state to its history.
Vishnu Nairef68d6d2023-02-28 06:18:27 +000068 void record(int32_t id, const LayerProps& props, nsecs_t presentTime, nsecs_t now,
69 LayerUpdateType updateType);
Ady Abrahama315ce72019-04-24 14:35:20 -070070
Andy Labrada096227e2022-06-15 16:58:11 +000071 // Updates the default frame rate compatibility which takes effect when the app
72 // does not set a preference for refresh rate.
73 void setDefaultFrameRateCompatibility(Layer*, bool contentDetectionEnabled);
74
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040075 using Summary = std::vector<RefreshRateSelector::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070076
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070077 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040078 Summary summarize(const RefreshRateSelector&, nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070079
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010080 void clear();
Ady Abrahambdda8f02021-04-01 16:06:11 -070081
82 void deregisterLayer(Layer*);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010083 std::string dump() const;
Ady Abraham8a82ba62020-01-17 12:43:17 -080084
Nathaniel Nifong1303d912021-10-06 09:41:24 -040085 // return the frames per second of the layer with the given sequence id.
86 float getLayerFramerate(nsecs_t now, int32_t id) const;
87
Arthur Hungc70bee22023-06-02 01:35:52 +000088 bool isSmallDirtyArea(uint32_t dirtyArea) const;
89
Ady Abraham8a82ba62020-01-17 12:43:17 -080090private:
Dominik Laskowski068173d2021-08-11 17:22:59 -070091 friend class LayerHistoryTest;
92 friend class TestableScheduler;
Ady Abraham8a82ba62020-01-17 12:43:17 -080093
Arthur Hungc70bee22023-06-02 01:35:52 +000094 static constexpr float kSmallDirtyArea = 0.07f;
95
Ady Abrahambdda8f02021-04-01 16:06:11 -070096 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
Nathaniel Nifong1303d912021-10-06 09:41:24 -040097 // keyed by id as returned from Layer::getSequence()
98 using LayerInfos = std::unordered_map<int32_t, LayerPair>;
Ady Abraham8a82ba62020-01-17 12:43:17 -080099
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400100 // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive
101 // layers to mInactiveLayerInfos.
102 // worst case time complexity is O(2 * inactive + active)
103 void partitionLayers(nsecs_t now) REQUIRES(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800104
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800105 enum class LayerStatus {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400106 NotFound,
107 LayerInActiveMap,
108 LayerInInactiveMap,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800109 };
110
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400111 // looks up a layer by sequence id in both layerInfo maps.
112 // The first element indicates if and where the item was found
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800113 std::pair<LayerStatus, LayerPair*> findLayer(int32_t id) REQUIRES(mLock);
114
115 std::pair<LayerStatus, const LayerPair*> findLayer(int32_t id) const REQUIRES(mLock) {
116 return const_cast<LayerHistory*>(this)->findLayer(id);
117 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800118
119 mutable std::mutex mLock;
120
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400121 // Partitioned into two maps to facility two kinds of retrieval:
122 // 1. retrieval of a layer by id (attempt lookup in both maps)
123 // 2. retrieval of all active layers (iterate that map)
124 // The partitioning is allowed to become out of date but calling partitionLayers refreshes the
125 // validity of each map.
126 LayerInfos mActiveLayerInfos GUARDED_BY(mLock);
127 LayerInfos mInactiveLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700128
Ady Abraham8a82ba62020-01-17 12:43:17 -0800129 uint32_t mDisplayArea = 0;
130
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700131 // Whether to emit systrace output and debug logs.
132 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100133
134 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
135 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700136
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100137 // Whether a mode change is in progress or not
138 std::atomic<bool> mModeChangePending = false;
Ana Krulec61f86db2018-11-19 14:16:35 +0100139};
140
Ady Abraham09bd3922019-04-08 10:44:56 -0700141} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700142} // namespace android