blob: 1318fbb9c8b3fe5acadc11720a61022fe35fd252 [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 Krulecc2870422019-01-29 19:00:58 -080053 using GetVsyncPeriod = std::function<nsecs_t()>;
Ana Krulec7d1d6832018-12-27 11:10:09 -080054
Ana Krulec7ecce8c2018-10-12 13:44:41 -070055 // Enum to indicate whether to start the transaction early, or at vsync time.
56 enum class TransactionStart { EARLY, NORMAL };
57
Ana Krulec98b5b242018-08-10 15:03:23 -070058 /* The scheduler handle is a BBinder object passed to the client from which we can extract
59 * an ID for subsequent operations.
60 */
61 class ConnectionHandle : public BBinder {
62 public:
63 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070064
Ana Krulec98b5b242018-08-10 15:03:23 -070065 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070066
Ana Krulec98b5b242018-08-10 15:03:23 -070067 const int64_t id;
68 };
69
70 class Connection {
71 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080072 Connection(sp<ConnectionHandle> handle, sp<EventThreadConnection> eventConnection,
Ana Krulec98b5b242018-08-10 15:03:23 -070073 std::unique_ptr<EventThread> eventThread)
74 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070075
Ana Krulec98b5b242018-08-10 15:03:23 -070076 ~Connection() = default;
77
78 sp<ConnectionHandle> handle;
Ana Krulec85c39af2018-12-26 17:29:57 -080079 sp<EventThreadConnection> eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070080 const std::unique_ptr<EventThread> thread;
81 };
82
Ana Krulecc2870422019-01-29 19:00:58 -080083 // Stores per-display state about VSYNC.
84 struct VsyncState {
85 explicit VsyncState(Scheduler& scheduler) : scheduler(scheduler) {}
86
87 void resync(const GetVsyncPeriod&);
88
89 Scheduler& scheduler;
90 std::atomic<nsecs_t> lastResyncTime = 0;
91 };
92
Ady Abraham09bd3922019-04-08 10:44:56 -070093 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function,
94 const scheduler::RefreshRateConfigs& refreshRateConfig);
Ana Krulece588e312018-09-18 12:32:24 -070095
Ana Krulec0c8cd522018-08-31 12:27:28 -070096 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070097
98 /** Creates an EventThread connection. */
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080099 sp<ConnectionHandle> createConnection(const char* connectionName, int64_t phaseOffsetNs,
Ady Abrahama1a49af2019-02-07 14:36:55 -0800100 ResyncCallback,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800101 impl::EventThread::InterceptVSyncsCallback);
Ana Krulece588e312018-09-18 12:32:24 -0700102
Dominik Laskowskif654d572018-12-20 11:03:06 -0800103 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle,
Ady Abrahama1a49af2019-02-07 14:36:55 -0800104 ResyncCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -0700105
106 // Getter methods.
107 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700108
Kevin DuBois413287f2019-02-25 08:46:47 -0800109 // Provides access to the DispSync object for the primary display.
110 void withPrimaryDispSync(std::function<void(DispSync&)> const& fn);
111
Ana Krulec85c39af2018-12-26 17:29:57 -0800112 sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle);
Ana Krulec98b5b242018-08-10 15:03:23 -0700113
114 // Should be called when receiving a hotplug event.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800115 void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
Ana Krulec98b5b242018-08-10 15:03:23 -0700116 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -0700117
Ana Krulec98b5b242018-08-10 15:03:23 -0700118 // Should be called after the screen is turned on.
119 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700120
Ana Krulec98b5b242018-08-10 15:03:23 -0700121 // Should be called before the screen is turned off.
122 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700123
Ady Abraham447052e2019-02-13 16:07:27 -0800124 // Should be called when display config changed
125 void onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
126 int32_t configId);
127
Ana Krulec98b5b242018-08-10 15:03:23 -0700128 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800129 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -0700130
Ana Krulec98b5b242018-08-10 15:03:23 -0700131 // Offers ability to modify phase offset in the event thread.
132 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
133
Ady Abrahamb838aed2019-02-12 15:30:16 -0800134 // pause/resume vsync callback generation to avoid sending vsync callbacks during config switch
135 void pauseVsyncCallback(const sp<ConnectionHandle>& handle, bool pause);
136
Ana Krulece588e312018-09-18 12:32:24 -0700137 void getDisplayStatInfo(DisplayStatInfo* stats);
138
139 void enableHardwareVsync();
140 void disableHardwareVsync(bool makeUnavailable);
Ana Krulecc2870422019-01-29 19:00:58 -0800141 void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
142 // Creates a callback for resyncing.
143 ResyncCallback makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod);
144 void setRefreshSkipCount(int count);
Ana Krulece588e312018-09-18 12:32:24 -0700145 void addResyncSample(const nsecs_t timestamp);
146 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
147 void setIgnorePresentFences(bool ignore);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800148 nsecs_t expectedPresentTime();
Ady Abraham09bd3922019-04-08 10:44:56 -0700149 // Registers the layer in the scheduler, and returns the handle for future references.
Ady Abraham8f1ee7f2019-04-05 10:32:50 -0700150 std::unique_ptr<scheduler::LayerHistory::LayerHandle> registerLayer(std::string const& name,
151 int windowType);
152
Ady Abraham09bd3922019-04-08 10:44:56 -0700153 // Stores present time for a layer.
154 void addLayerPresentTime(
155 const std::unique_ptr<scheduler::LayerHistory::LayerHandle>& layerHandle,
156 nsecs_t presentTime);
157 // Updates FPS based on the most content presented.
158 void updateFpsBasedOnContent();
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800159 // Callback that gets invoked when Scheduler wants to change the refresh rate.
160 void setChangeRefreshRateCallback(const ChangeRefreshRateCallback& changeRefreshRateCallback);
Ady Abraham97d04232019-03-05 19:48:12 -0800161
162 // Returns whether idle timer is enabled or not
163 bool isIdleTimerEnabled() { return mSetIdleTimerMs > 0; }
Ady Abraham09bd3922019-04-08 10:44:56 -0700164
Ana Krulecb43429d2019-01-09 14:28:51 -0800165 // Returns relevant information about Scheduler for dumpsys purposes.
166 std::string doDump();
Ana Krulece588e312018-09-18 12:32:24 -0700167
Ady Abraham3aff9172019-02-07 19:10:26 -0800168 // calls DispSync::dump() on primary disp sync
169 void dumpPrimaryDispSync(std::string& result) const;
170
Ana Krulec0c8cd522018-08-31 12:27:28 -0700171protected:
172 virtual std::unique_ptr<EventThread> makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800173 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700174 impl::EventThread::InterceptVSyncsCallback interceptCallback);
175
Ana Krulec98b5b242018-08-10 15:03:23 -0700176private:
Ana Krulecafb45842019-02-13 13:33:03 -0800177 friend class TestableScheduler;
178
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800179 // In order to make sure that the features don't override themselves, we need a state machine
180 // to keep track which feature requested the config change.
Ady Abraham09bd3922019-04-08 10:44:56 -0700181 enum class ContentFeatureState { CONTENT_DETECTION_ON, CONTENT_DETECTION_OFF };
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800182 enum class IdleTimerState { EXPIRED, RESET };
183
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800184 // Creates a connection on the given EventThread and forwards the given callbacks.
Ady Abrahama1a49af2019-02-07 14:36:55 -0800185 sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800186
Ana Krulec7ab56032018-11-02 20:51:06 +0100187 nsecs_t calculateAverage() const;
188 void updateFrameSkipping(const int64_t skipCount);
Ana Krulecfb772822018-11-30 10:44:07 +0100189 // Function that resets the idle timer.
190 void resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800191 // Function that is called when the timer resets.
192 void resetTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100193 // Function that is called when the timer expires.
194 void expiredTimerCallback();
Ana Krulecc2870422019-01-29 19:00:58 -0800195 // Sets vsync period.
196 void setVsyncPeriod(const nsecs_t period);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800197 // Media feature's function to change the refresh rate.
Ady Abraham09bd3922019-04-08 10:44:56 -0700198 void contentChangeRefreshRate(ContentFeatureState contentFeatureState, uint32_t refreshRate);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800199 // Idle timer feature's function to change the refresh rate.
200 void timerChangeRefreshRate(IdleTimerState idleTimerState);
Ady Abraham09bd3922019-04-08 10:44:56 -0700201 // Calculate the new refresh rate type
202 RefreshRateType calculateRefreshRateType() REQUIRES(mFeatureStateLock);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800203 // Acquires a lock and calls the ChangeRefreshRateCallback() with given parameters.
204 void changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent);
Ana Krulec7ab56032018-11-02 20:51:06 +0100205
Ady Abraham09bd3922019-04-08 10:44:56 -0700206 // Helper function to calculate error frames
207 float getErrorFrames(float contentFps, float configFps);
208
Ana Krulece588e312018-09-18 12:32:24 -0700209 // If fences from sync Framework are supported.
210 const bool mHasSyncFramework;
211
212 // The offset in nanoseconds to use, when DispSync timestamps present fence
213 // signaling time.
Sundong Ahnd5e08f62018-12-12 20:27:28 +0900214 nsecs_t mDispSyncPresentTimeOffset;
Ana Krulece588e312018-09-18 12:32:24 -0700215
216 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700217 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700218
219 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700220 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700221
222 std::mutex mHWVsyncLock;
223 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
224 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
Ana Krulecc2870422019-01-29 19:00:58 -0800225 const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)};
Ana Krulece588e312018-09-18 12:32:24 -0700226
227 std::unique_ptr<DispSync> mPrimaryDispSync;
228 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100229
230 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
231 // a proof of concept. We turn on frame skipping if the difference between the timestamps
232 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
233 nsecs_t mPreviousFrameTimestamp = 0;
234 // Keeping track of whether we are skipping the refresh count. If we want to
235 // simulate 30Hz rendering, we skip every other frame, and this variable is set
236 // to 1.
237 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100238 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100239 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100240
Ady Abraham09bd3922019-04-08 10:44:56 -0700241 // Historical information about individual layers. Used for predicting the refresh rate.
242 scheduler::LayerHistory mLayerHistory;
Ana Krulecfb772822018-11-30 10:44:07 +0100243
244 // Timer that records time between requests for next vsync. If the time is higher than a given
245 // interval, a callback is fired. Set this variable to >0 to use this feature.
246 int64_t mSetIdleTimerMs = 0;
247 std::unique_ptr<scheduler::IdleTimer> mIdleTimer;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800248
249 std::mutex mCallbackLock;
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800250 ChangeRefreshRateCallback mChangeRefreshRateCallback GUARDED_BY(mCallbackLock);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800251
252 // In order to make sure that the features don't override themselves, we need a state machine
253 // to keep track which feature requested the config change.
254 std::mutex mFeatureStateLock;
Ady Abraham09bd3922019-04-08 10:44:56 -0700255 ContentFeatureState mCurrentContentFeatureState GUARDED_BY(mFeatureStateLock) =
256 ContentFeatureState::CONTENT_DETECTION_OFF;
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800257 IdleTimerState mCurrentIdleTimerState GUARDED_BY(mFeatureStateLock) = IdleTimerState::RESET;
Ady Abraham09bd3922019-04-08 10:44:56 -0700258 uint32_t mContentRefreshRate;
259
260 const scheduler::RefreshRateConfigs& mRefreshRateConfigs;
Ana Krulec98b5b242018-08-10 15:03:23 -0700261};
262
Ana Krulec7ab56032018-11-02 20:51:06 +0100263} // namespace android