blob: 4b21dadde95c6d51869dfcc4bb3c044fbea0ee57 [file] [log] [blame]
Ana Krulec98b5b242018-08-10 15:03:23 -07001/*
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 <cstdint>
Kevin DuBois413287f2019-02-25 08:46:47 -080020#include <functional>
Ana Krulec98b5b242018-08-10 15:03:23 -070021#include <memory>
22
Ana Krulece588e312018-09-18 12:32:24 -070023#include <ui/DisplayStatInfo.h>
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080024#include <ui/GraphicTypes.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070025
26#include "DispSync.h"
Ana Krulece588e312018-09-18 12:32:24 -070027#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070028#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010029#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070030#include "InjectVSyncSource.h"
Ana Krulec3084c052018-11-21 20:27:17 +010031#include "LayerHistory.h"
Ana Krulec8d3e4f32019-03-05 10:40:33 -080032#include "RefreshRateConfigs.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010033#include "SchedulerUtils.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070034
35namespace android {
36
Ana Krulece588e312018-09-18 12:32:24 -070037class EventControlThread;
38
Ana Krulec98b5b242018-08-10 15:03:23 -070039class Scheduler {
40public:
Ana Krulec8d3e4f32019-03-05 10:40:33 -080041 // Enum to keep track of whether we trigger event to notify choreographer of config changes.
42 enum class ConfigEvent { None, Changed };
43
44 // logical or operator with the semantics of at least one of the events is Changed
45 friend ConfigEvent operator|(const ConfigEvent& first, const ConfigEvent& second) {
46 if (first == ConfigEvent::Changed) return ConfigEvent::Changed;
47 if (second == ConfigEvent::Changed) return ConfigEvent::Changed;
48 return ConfigEvent::None;
49 }
50
51 using RefreshRateType = scheduler::RefreshRateConfigs::RefreshRateType;
52 using ChangeRefreshRateCallback = std::function<void(RefreshRateType, ConfigEvent)>;
Ana Krulec7d1d6832018-12-27 11:10:09 -080053
Ana Krulec7ecce8c2018-10-12 13:44:41 -070054 // Enum to indicate whether to start the transaction early, or at vsync time.
55 enum class TransactionStart { EARLY, NORMAL };
56
Ana Krulec98b5b242018-08-10 15:03:23 -070057 /* The scheduler handle is a BBinder object passed to the client from which we can extract
58 * an ID for subsequent operations.
59 */
60 class ConnectionHandle : public BBinder {
61 public:
62 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070063
Ana Krulec98b5b242018-08-10 15:03:23 -070064 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070065
Ana Krulec98b5b242018-08-10 15:03:23 -070066 const int64_t id;
67 };
68
69 class Connection {
70 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080071 Connection(sp<ConnectionHandle> handle, sp<EventThreadConnection> eventConnection,
Ana Krulec98b5b242018-08-10 15:03:23 -070072 std::unique_ptr<EventThread> eventThread)
73 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070074
Ana Krulec98b5b242018-08-10 15:03:23 -070075 ~Connection() = default;
76
77 sp<ConnectionHandle> handle;
Ana Krulec85c39af2018-12-26 17:29:57 -080078 sp<EventThreadConnection> eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070079 const std::unique_ptr<EventThread> thread;
80 };
81
Ady Abraham09bd3922019-04-08 10:44:56 -070082 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
83 const scheduler::RefreshRateConfigs& refreshRateConfig);
Ana Krulece588e312018-09-18 12:32:24 -070084
Ana Krulec0c8cd522018-08-31 12:27:28 -070085 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070086
87 /** Creates an EventThread connection. */
Ady Abraham45e4e362019-06-07 18:20:51 -070088 sp<ConnectionHandle> createConnection(const char* connectionName, nsecs_t phaseOffsetNs,
Steven Thomas7fb29b92019-08-28 16:08:35 -070089 nsecs_t offsetThresholdForNextVsync,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080090 impl::EventThread::InterceptVSyncsCallback);
Ana Krulece588e312018-09-18 12:32:24 -070091
Ady Abraham0f4a1b12019-06-04 16:04:04 -070092 sp<IDisplayEventConnection> createDisplayEventConnection(
Steven Thomas7fb29b92019-08-28 16:08:35 -070093 const sp<ConnectionHandle>& handle, ISurfaceComposer::ConfigChanged configChanged);
Ana Krulec98b5b242018-08-10 15:03:23 -070094
95 // Getter methods.
96 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070097
Kevin DuBois413287f2019-02-25 08:46:47 -080098 // Provides access to the DispSync object for the primary display.
99 void withPrimaryDispSync(std::function<void(DispSync&)> const& fn);
100
Ana Krulec85c39af2018-12-26 17:29:57 -0800101 sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle);
Ana Krulec98b5b242018-08-10 15:03:23 -0700102
103 // Should be called when receiving a hotplug event.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800104 void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
Ana Krulec98b5b242018-08-10 15:03:23 -0700105 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -0700106
Ana Krulec98b5b242018-08-10 15:03:23 -0700107 // Should be called after the screen is turned on.
108 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700109
Ana Krulec98b5b242018-08-10 15:03:23 -0700110 // Should be called before the screen is turned off.
111 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700112
Ady Abraham447052e2019-02-13 16:07:27 -0800113 // Should be called when display config changed
114 void onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
115 int32_t configId);
116
Ana Krulec98b5b242018-08-10 15:03:23 -0700117 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800118 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -0700119
Ana Krulec98b5b242018-08-10 15:03:23 -0700120 // Offers ability to modify phase offset in the event thread.
121 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
122
Ana Krulece588e312018-09-18 12:32:24 -0700123 void getDisplayStatInfo(DisplayStatInfo* stats);
124
125 void enableHardwareVsync();
126 void disableHardwareVsync(bool makeUnavailable);
Alec Mourif8e689c2019-05-20 18:32:22 -0700127 // Resyncs the scheduler to hardware vsync.
128 // If makeAvailable is true, then hardware vsync will be turned on.
129 // Otherwise, if hardware vsync is not already enabled then this method will
130 // no-op.
131 // The period is the vsync period from the current display configuration.
Ana Krulecc2870422019-01-29 19:00:58 -0800132 void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
Steven Thomas7fb29b92019-08-28 16:08:35 -0700133 void resync();
Ana Krulecc2870422019-01-29 19:00:58 -0800134 void setRefreshSkipCount(int count);
Alec Mourif8e689c2019-05-20 18:32:22 -0700135 // Passes a vsync sample to DispSync. periodFlushed will be true if
136 // DispSync detected that the vsync period changed, and false otherwise.
137 void addResyncSample(const nsecs_t timestamp, bool* periodFlushed);
Ana Krulece588e312018-09-18 12:32:24 -0700138 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
139 void setIgnorePresentFences(bool ignore);
Ady Abraham8fe11022019-06-12 17:11:12 -0700140 nsecs_t getDispSyncExpectedPresentTime();
Ady Abraham09bd3922019-04-08 10:44:56 -0700141 // Registers the layer in the scheduler, and returns the handle for future references.
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700142 std::unique_ptr<scheduler::LayerHistory::LayerHandle> registerLayer(std::string const& name,
143 int windowType);
144
Ady Abraham09bd3922019-04-08 10:44:56 -0700145 // Stores present time for a layer.
Ady Abrahama315ce72019-04-24 14:35:20 -0700146 void addLayerPresentTimeAndHDR(
Ady Abraham09bd3922019-04-08 10:44:56 -0700147 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
Ady Abrahama315ce72019-04-24 14:35:20 -0700148 nsecs_t presentTime, bool isHDR);
149 // Stores visibility for a layer.
150 void setLayerVisibility(
151 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle, bool visible);
Ady Abraham09bd3922019-04-08 10:44:56 -0700152 // Updates FPS based on the most content presented.
153 void updateFpsBasedOnContent();
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800154 // Callback that gets invoked when Scheduler wants to change the refresh rate.
Alec Mouri7f015182019-07-11 13:56:22 -0700155 void setChangeRefreshRateCallback(const ChangeRefreshRateCallback&& changeRefreshRateCallback);
Ady Abraham97d04232019-03-05 19:48:12 -0800156
157 // Returns whether idle timer is enabled or not
158 bool isIdleTimerEnabled() { return mSetIdleTimerMs > 0; }
Ady Abraham09bd3922019-04-08 10:44:56 -0700159
Ady Abraham8532d012019-05-08 14:50:56 -0700160 // Function that resets the idle timer.
161 void resetIdleTimer();
162
163 // Function that resets the touch timer.
164 void notifyTouchEvent();
165
Ady Abraham24363172019-07-12 12:37:57 -0700166 // Function that sets whether display power mode is normal or not.
167 void setDisplayPowerState(bool normal);
168
Ana Krulecb43429d2019-01-09 14:28:51 -0800169 // Returns relevant information about Scheduler for dumpsys purposes.
170 std::string doDump();
Ana Krulece588e312018-09-18 12:32:24 -0700171
Ady Abraham3aff9172019-02-07 19:10:26 -0800172 // calls DispSync::dump() on primary disp sync
173 void dumpPrimaryDispSync(std::string& result) const;
174
Daniel Solomond916d942019-08-19 19:31:09 -0700175 // Get the appropriate refresh type for current conditions.
176 RefreshRateType getPreferredRefreshRateType();
177
Ana Krulec0c8cd522018-08-31 12:27:28 -0700178protected:
179 virtual std::unique_ptr<EventThread> makeEventThread(
Ady Abraham45e4e362019-06-07 18:20:51 -0700180 const char* connectionName, DispSync* dispSync, nsecs_t phaseOffsetNs,
181 nsecs_t offsetThresholdForNextVsync,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700182 impl::EventThread::InterceptVSyncsCallback interceptCallback);
183
Ana Krulec98b5b242018-08-10 15:03:23 -0700184private:
Ana Krulecafb45842019-02-13 13:33:03 -0800185 friend class TestableScheduler;
186
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800187 // In order to make sure that the features don't override themselves, we need a state machine
188 // to keep track which feature requested the config change.
Ady Abraham09bd3922019-04-08 10:44:56 -0700189 enum class ContentFeatureState { CONTENT_DETECTION_ON, CONTENT_DETECTION_OFF };
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800190 enum class IdleTimerState { EXPIRED, RESET };
Ady Abraham8532d012019-05-08 14:50:56 -0700191 enum class TouchState { INACTIVE, ACTIVE };
Ady Abraham24363172019-07-12 12:37:57 -0700192 enum class DisplayPowerTimerState { EXPIRED, RESET };
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800193
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800194 // Creates a connection on the given EventThread and forwards the given callbacks.
Steven Thomas7fb29b92019-08-28 16:08:35 -0700195 sp<EventThreadConnection> createConnectionInternal(EventThread*,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700196 ISurfaceComposer::ConfigChanged);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800197
Ana Krulec7ab56032018-11-02 20:51:06 +0100198 nsecs_t calculateAverage() const;
199 void updateFrameSkipping(const int64_t skipCount);
Ady Abraham8532d012019-05-08 14:50:56 -0700200
Ady Abrahama1a49af2019-02-07 14:36:55 -0800201 // Function that is called when the timer resets.
202 void resetTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100203 // Function that is called when the timer expires.
204 void expiredTimerCallback();
Alec Mouridc28b372019-04-18 21:17:13 -0700205 // Function that is called when the timer resets when paired with a display
206 // driver timeout in the kernel. This enables hardware vsync when we move
207 // out from idle.
208 void resetKernelTimerCallback();
209 // Function that is called when the timer expires when paired with a display
210 // driver timeout in the kernel. This disables hardware vsync when we move
211 // into idle.
212 void expiredKernelTimerCallback();
Ady Abraham8532d012019-05-08 14:50:56 -0700213 // Function that is called when the touch timer resets.
214 void resetTouchTimerCallback();
215 // Function that is called when the touch timer expires.
216 void expiredTouchTimerCallback();
Ady Abraham24363172019-07-12 12:37:57 -0700217 // Function that is called when the display power timer resets.
218 void resetDisplayPowerTimerCallback();
219 // Function that is called when the display power timer expires.
220 void expiredDisplayPowerTimerCallback();
Ana Krulecc2870422019-01-29 19:00:58 -0800221 // Sets vsync period.
222 void setVsyncPeriod(const nsecs_t period);
Ady Abraham24363172019-07-12 12:37:57 -0700223 // handles various timer features to change the refresh rate.
224 template <class T>
225 void handleTimerStateChanged(T* currentState, T newState, bool eventOnContentDetection);
Ady Abraham09bd3922019-04-08 10:44:56 -0700226 // Calculate the new refresh rate type
227 RefreshRateType calculateRefreshRateType() REQUIRES(mFeatureStateLock);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800228 // Acquires a lock and calls the ChangeRefreshRateCallback() with given parameters.
229 void changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent);
Ana Krulec7ab56032018-11-02 20:51:06 +0100230
Ady Abraham09bd3922019-04-08 10:44:56 -0700231 // Helper function to calculate error frames
232 float getErrorFrames(float contentFps, float configFps);
233
Ana Krulece588e312018-09-18 12:32:24 -0700234 // If fences from sync Framework are supported.
235 const bool mHasSyncFramework;
236
237 // The offset in nanoseconds to use, when DispSync timestamps present fence
238 // signaling time.
Sundong Ahnd5e08f62018-12-12 20:27:28 +0900239 nsecs_t mDispSyncPresentTimeOffset;
Ana Krulece588e312018-09-18 12:32:24 -0700240
241 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700242 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700243
244 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700245 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700246
247 std::mutex mHWVsyncLock;
248 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
249 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
Steven Thomas7fb29b92019-08-28 16:08:35 -0700250
251 std::atomic<nsecs_t> mLastResyncTime = 0;
Ana Krulece588e312018-09-18 12:32:24 -0700252
253 std::unique_ptr<DispSync> mPrimaryDispSync;
254 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100255
256 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
257 // a proof of concept. We turn on frame skipping if the difference between the timestamps
258 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
259 nsecs_t mPreviousFrameTimestamp = 0;
260 // Keeping track of whether we are skipping the refresh count. If we want to
261 // simulate 30Hz rendering, we skip every other frame, and this variable is set
262 // to 1.
263 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100264 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100265 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100266
Ady Abraham09bd3922019-04-08 10:44:56 -0700267 // Historical information about individual layers. Used for predicting the refresh rate.
268 scheduler::LayerHistory mLayerHistory;
Ana Krulecfb772822018-11-30 10:44:07 +0100269
270 // Timer that records time between requests for next vsync. If the time is higher than a given
271 // interval, a callback is fired. Set this variable to >0 to use this feature.
272 int64_t mSetIdleTimerMs = 0;
273 std::unique_ptr<scheduler::IdleTimer> mIdleTimer;
Alec Mouridc28b372019-04-18 21:17:13 -0700274 // Enables whether to use idle timer callbacks that support the kernel
275 // timer.
276 bool mSupportKernelTimer;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800277
Ady Abraham8532d012019-05-08 14:50:56 -0700278 // Timer used to monitor touch events.
279 int64_t mSetTouchTimerMs = 0;
280 std::unique_ptr<scheduler::IdleTimer> mTouchTimer;
281
Ady Abraham24363172019-07-12 12:37:57 -0700282 // Timer used to monitor display power mode.
283 int64_t mSetDisplayPowerTimerMs = 0;
284 std::unique_ptr<scheduler::IdleTimer> mDisplayPowerTimer;
285
Ana Krulec7d1d6832018-12-27 11:10:09 -0800286 std::mutex mCallbackLock;
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800287 ChangeRefreshRateCallback mChangeRefreshRateCallback GUARDED_BY(mCallbackLock);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800288
289 // In order to make sure that the features don't override themselves, we need a state machine
290 // to keep track which feature requested the config change.
291 std::mutex mFeatureStateLock;
Ady Abraham09bd3922019-04-08 10:44:56 -0700292 ContentFeatureState mCurrentContentFeatureState GUARDED_BY(mFeatureStateLock) =
293 ContentFeatureState::CONTENT_DETECTION_OFF;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800294 IdleTimerState mCurrentIdleTimerState GUARDED_BY(mFeatureStateLock) = IdleTimerState::RESET;
Ady Abraham8532d012019-05-08 14:50:56 -0700295 TouchState mCurrentTouchState GUARDED_BY(mFeatureStateLock) = TouchState::INACTIVE;
Ady Abraham24363172019-07-12 12:37:57 -0700296 DisplayPowerTimerState mDisplayPowerTimerState GUARDED_BY(mFeatureStateLock) =
297 DisplayPowerTimerState::EXPIRED;
Ady Abraham6398a0a2019-04-18 19:30:44 -0700298 uint32_t mContentRefreshRate GUARDED_BY(mFeatureStateLock);
299 RefreshRateType mRefreshRateType GUARDED_BY(mFeatureStateLock);
Ady Abrahama315ce72019-04-24 14:35:20 -0700300 bool mIsHDRContent GUARDED_BY(mFeatureStateLock) = false;
Ady Abraham24363172019-07-12 12:37:57 -0700301 bool mIsDisplayPowerStateNormal GUARDED_BY(mFeatureStateLock) = true;
Ady Abraham09bd3922019-04-08 10:44:56 -0700302
303 const scheduler::RefreshRateConfigs& mRefreshRateConfigs;
Ady Abrahama315ce72019-04-24 14:35:20 -0700304
305 // Global config to force HDR content to work on DEFAULT refreshRate
Ady Abraham8444c362019-05-30 17:59:36 -0700306 static constexpr bool mForceHDRContentToDefaultRefreshRate = false;
Ana Krulec98b5b242018-08-10 15:03:23 -0700307};
308
Ana Krulec7ab56032018-11-02 20:51:06 +0100309} // namespace android