blob: 5a9445bcaf3e0547ae27adde4abee30bad44a809 [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
Vishnu Nair80e8cfe2023-09-29 17:03:45 -070032#include "FrameRateCompatibility.h"
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040033#include "RefreshRateSelector.h"
Ady Abraham8a82ba62020-01-17 12:43:17 -080034
Ana Krulec61f86db2018-11-19 14:16:35 +010035namespace android {
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070036
37class Layer;
38
Ady Abraham09bd3922019-04-08 10:44:56 -070039namespace scheduler {
Ana Krulec61f86db2018-11-19 14:16:35 +010040
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070041class LayerInfo;
Vishnu Nairef68d6d2023-02-28 06:18:27 +000042struct LayerProps;
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070043
Ana Krulec61f86db2018-11-19 14:16:35 +010044class LayerHistory {
45public:
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040046 using LayerVoteType = RefreshRateSelector::LayerVoteType;
Arthur Hungc70bee22023-06-02 01:35:52 +000047 static constexpr std::chrono::nanoseconds kMaxPeriodForHistory = 1s;
Ady Abraham8a82ba62020-01-17 12:43:17 -080048
Ady Abraham3efa3942021-06-24 19:01:25 -070049 LayerHistory();
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010050 ~LayerHistory();
Ana Krulec61f86db2018-11-19 14:16:35 +010051
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070052 // Layers are unregistered when the weak reference expires.
Andy Labrada096227e2022-06-15 16:58:11 +000053 void registerLayer(Layer*, bool contentDetectionEnabled);
Ady Abraham8a82ba62020-01-17 12:43:17 -080054
55 // Sets the display size. Client is responsible for synchronization.
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010056 void setDisplayArea(uint32_t displayArea) { mDisplayArea = displayArea; }
Ady Abraham09bd3922019-04-08 10:44:56 -070057
Marin Shalamanova7fe3042021-01-29 21:02:08 +010058 // Sets whether a mode change is pending to be applied
59 void setModeChangePending(bool pending) { mModeChangePending = pending; }
Ady Abraham32efd542020-05-19 17:49:26 -070060
Ady Abraham5def7332020-05-29 16:13:47 -070061 // Represents which layer activity is recorded
62 enum class LayerUpdateType {
63 Buffer, // a new buffer queued
64 AnimationTX, // a new transaction with eAnimation flag set
65 SetFrameRate, // setFrameRate API was called
66 };
67
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070068 // Marks the layer as active, and records the given state to its history.
Vishnu Nairef68d6d2023-02-28 06:18:27 +000069 void record(int32_t id, const LayerProps& props, nsecs_t presentTime, nsecs_t now,
70 LayerUpdateType updateType);
Ady Abrahama315ce72019-04-24 14:35:20 -070071
Andy Labrada096227e2022-06-15 16:58:11 +000072 // Updates the default frame rate compatibility which takes effect when the app
73 // does not set a preference for refresh rate.
Vishnu Nair80e8cfe2023-09-29 17:03:45 -070074 void setDefaultFrameRateCompatibility(int32_t id, FrameRateCompatibility frameRateCompatibility,
75 bool contentDetectionEnabled);
Vishnu Nair41376b62023-11-08 05:08:58 -080076 void setLayerProperties(int32_t id, const LayerProps&);
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040077 using Summary = std::vector<RefreshRateSelector::LayerRequirement>;
Ady Abraham09bd3922019-04-08 10:44:56 -070078
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -070079 // Rebuilds sets of active/inactive layers, and accumulates stats for active layers.
Dominik Laskowskid82e0f02022-10-26 15:23:04 -040080 Summary summarize(const RefreshRateSelector&, nsecs_t now);
Ady Abrahama9bf4ca2019-06-11 19:08:58 -070081
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010082 void clear();
Ady Abrahambdda8f02021-04-01 16:06:11 -070083
84 void deregisterLayer(Layer*);
Marin Shalamanov1bc43ee2020-11-20 16:56:52 +010085 std::string dump() const;
Ady Abraham8a82ba62020-01-17 12:43:17 -080086
Nathaniel Nifong1303d912021-10-06 09:41:24 -040087 // return the frames per second of the layer with the given sequence id.
88 float getLayerFramerate(nsecs_t now, int32_t id) const;
89
Tony Huang9ac5e6e2023-08-24 09:01:44 +000090 bool isSmallDirtyArea(uint32_t dirtyArea, float threshold) const;
Arthur Hungc70bee22023-06-02 01:35:52 +000091
Ady Abraham8a82ba62020-01-17 12:43:17 -080092private:
Dominik Laskowski068173d2021-08-11 17:22:59 -070093 friend class LayerHistoryTest;
Vishnu Nair47b7bb42023-09-29 16:27:33 -070094 friend class LayerHistoryIntegrationTest;
Dominik Laskowski068173d2021-08-11 17:22:59 -070095 friend class TestableScheduler;
Ady Abraham8a82ba62020-01-17 12:43:17 -080096
Ady Abrahambdda8f02021-04-01 16:06:11 -070097 using LayerPair = std::pair<Layer*, std::unique_ptr<LayerInfo>>;
Nathaniel Nifong1303d912021-10-06 09:41:24 -040098 // keyed by id as returned from Layer::getSequence()
99 using LayerInfos = std::unordered_map<int32_t, LayerPair>;
Ady Abraham8a82ba62020-01-17 12:43:17 -0800100
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400101 // Iterates over layers maps moving all active layers to mActiveLayerInfos and all inactive
102 // layers to mInactiveLayerInfos.
103 // worst case time complexity is O(2 * inactive + active)
104 void partitionLayers(nsecs_t now) REQUIRES(mLock);
Ady Abraham8a82ba62020-01-17 12:43:17 -0800105
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800106 enum class LayerStatus {
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400107 NotFound,
108 LayerInActiveMap,
109 LayerInInactiveMap,
Ady Abraham8a82ba62020-01-17 12:43:17 -0800110 };
111
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400112 // looks up a layer by sequence id in both layerInfo maps.
113 // The first element indicates if and where the item was found
Dominik Laskowski0c41ffa2021-12-24 16:45:12 -0800114 std::pair<LayerStatus, LayerPair*> findLayer(int32_t id) REQUIRES(mLock);
115
116 std::pair<LayerStatus, const LayerPair*> findLayer(int32_t id) const REQUIRES(mLock) {
117 return const_cast<LayerHistory*>(this)->findLayer(id);
118 }
Ady Abraham8a82ba62020-01-17 12:43:17 -0800119
120 mutable std::mutex mLock;
121
Nathaniel Nifong1303d912021-10-06 09:41:24 -0400122 // Partitioned into two maps to facility two kinds of retrieval:
123 // 1. retrieval of a layer by id (attempt lookup in both maps)
124 // 2. retrieval of all active layers (iterate that map)
125 // The partitioning is allowed to become out of date but calling partitionLayers refreshes the
126 // validity of each map.
127 LayerInfos mActiveLayerInfos GUARDED_BY(mLock);
128 LayerInfos mInactiveLayerInfos GUARDED_BY(mLock);
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700129
Ady Abraham8a82ba62020-01-17 12:43:17 -0800130 uint32_t mDisplayArea = 0;
131
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700132 // Whether to emit systrace output and debug logs.
133 const bool mTraceEnabled;
Ana Krulecc84d09b2019-11-02 23:10:29 +0100134
135 // Whether to use priority sent from WindowManager to determine the relevancy of the layer.
136 const bool mUseFrameRatePriority;
Ady Abraham32efd542020-05-19 17:49:26 -0700137
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100138 // Whether a mode change is in progress or not
139 std::atomic<bool> mModeChangePending = false;
Ana Krulec61f86db2018-11-19 14:16:35 +0100140};
141
Ady Abraham09bd3922019-04-08 10:44:56 -0700142} // namespace scheduler
Dominik Laskowskif7a09ed2019-10-07 13:54:18 -0700143} // namespace android