blob: 2582c93d048671d2075550b9452e3d707915fb33 [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>
20#include <memory>
21
Ana Krulece588e312018-09-18 12:32:24 -070022#include <ui/DisplayStatInfo.h>
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080023#include <ui/GraphicTypes.h>
Ana Krulec98b5b242018-08-10 15:03:23 -070024
25#include "DispSync.h"
Ana Krulece588e312018-09-18 12:32:24 -070026#include "EventControlThread.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070027#include "EventThread.h"
Ana Krulecfb772822018-11-30 10:44:07 +010028#include "IdleTimer.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070029#include "InjectVSyncSource.h"
Ana Krulec3084c052018-11-21 20:27:17 +010030#include "LayerHistory.h"
Ana Krulec8d3e4f32019-03-05 10:40:33 -080031#include "RefreshRateConfigs.h"
Ana Krulec434c22d2018-11-28 13:48:36 +010032#include "SchedulerUtils.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070033
34namespace android {
35
Ana Krulece588e312018-09-18 12:32:24 -070036class EventControlThread;
37
Ana Krulec98b5b242018-08-10 15:03:23 -070038class Scheduler {
39public:
Ana Krulec8d3e4f32019-03-05 10:40:33 -080040 // Enum to keep track of whether we trigger event to notify choreographer of config changes.
41 enum class ConfigEvent { None, Changed };
42
43 // logical or operator with the semantics of at least one of the events is Changed
44 friend ConfigEvent operator|(const ConfigEvent& first, const ConfigEvent& second) {
45 if (first == ConfigEvent::Changed) return ConfigEvent::Changed;
46 if (second == ConfigEvent::Changed) return ConfigEvent::Changed;
47 return ConfigEvent::None;
48 }
49
50 using RefreshRateType = scheduler::RefreshRateConfigs::RefreshRateType;
51 using ChangeRefreshRateCallback = std::function<void(RefreshRateType, ConfigEvent)>;
Ana Krulecc2870422019-01-29 19:00:58 -080052 using GetVsyncPeriod = std::function<nsecs_t()>;
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
Ana Krulecc2870422019-01-29 19:00:58 -080082 // Stores per-display state about VSYNC.
83 struct VsyncState {
84 explicit VsyncState(Scheduler& scheduler) : scheduler(scheduler) {}
85
86 void resync(const GetVsyncPeriod&);
87
88 Scheduler& scheduler;
89 std::atomic<nsecs_t> lastResyncTime = 0;
90 };
91
Ana Krulece588e312018-09-18 12:32:24 -070092 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
93
Ana Krulec0c8cd522018-08-31 12:27:28 -070094 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070095
96 /** Creates an EventThread connection. */
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080097 sp<ConnectionHandle> createConnection(const char* connectionName, int64_t phaseOffsetNs,
Ady Abrahama1a49af2019-02-07 14:36:55 -080098 ResyncCallback,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080099 impl::EventThread::InterceptVSyncsCallback);
Ana Krulece588e312018-09-18 12:32:24 -0700100
Dominik Laskowskif654d572018-12-20 11:03:06 -0800101 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle,
Ady Abrahama1a49af2019-02-07 14:36:55 -0800102 ResyncCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -0700103
104 // Getter methods.
105 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700106
Ana Krulec85c39af2018-12-26 17:29:57 -0800107 sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle);
Ana Krulec98b5b242018-08-10 15:03:23 -0700108
109 // Should be called when receiving a hotplug event.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800110 void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
Ana Krulec98b5b242018-08-10 15:03:23 -0700111 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -0700112
Ana Krulec98b5b242018-08-10 15:03:23 -0700113 // Should be called after the screen is turned on.
114 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700115
Ana Krulec98b5b242018-08-10 15:03:23 -0700116 // Should be called before the screen is turned off.
117 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700118
Ady Abraham447052e2019-02-13 16:07:27 -0800119 // Should be called when display config changed
120 void onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
121 int32_t configId);
122
Ana Krulec98b5b242018-08-10 15:03:23 -0700123 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800124 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -0700125
Ana Krulec98b5b242018-08-10 15:03:23 -0700126 // Offers ability to modify phase offset in the event thread.
127 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
128
Ady Abrahamb838aed2019-02-12 15:30:16 -0800129 // pause/resume vsync callback generation to avoid sending vsync callbacks during config switch
130 void pauseVsyncCallback(const sp<ConnectionHandle>& handle, bool pause);
131
Ana Krulece588e312018-09-18 12:32:24 -0700132 void getDisplayStatInfo(DisplayStatInfo* stats);
133
134 void enableHardwareVsync();
135 void disableHardwareVsync(bool makeUnavailable);
Ana Krulecc2870422019-01-29 19:00:58 -0800136 void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
137 // Creates a callback for resyncing.
138 ResyncCallback makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod);
139 void setRefreshSkipCount(int count);
Ana Krulece588e312018-09-18 12:32:24 -0700140 void addResyncSample(const nsecs_t timestamp);
141 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
142 void setIgnorePresentFences(bool ignore);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800143 nsecs_t expectedPresentTime();
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800144 // apiId indicates the API (NATIVE_WINDOW_API_xxx) that queues the buffer.
145 // TODO(b/123956502): Remove this call with V1 go/content-fps-detection-in-scheduler.
146 void addNativeWindowApi(int apiId);
147 // Updates FPS based on the most occured request for Native Window API.
148 void updateFpsBasedOnNativeWindowApi();
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800149 // Callback that gets invoked when Scheduler wants to change the refresh rate.
150 void setChangeRefreshRateCallback(const ChangeRefreshRateCallback& changeRefreshRateCallback);
Ady Abraham97d04232019-03-05 19:48:12 -0800151
152 // Returns whether idle timer is enabled or not
153 bool isIdleTimerEnabled() { return mSetIdleTimerMs > 0; }
Ana Krulecb43429d2019-01-09 14:28:51 -0800154 // Returns relevant information about Scheduler for dumpsys purposes.
155 std::string doDump();
Ana Krulece588e312018-09-18 12:32:24 -0700156
Ady Abraham3aff9172019-02-07 19:10:26 -0800157 // calls DispSync::dump() on primary disp sync
158 void dumpPrimaryDispSync(std::string& result) const;
159
Ana Krulec0c8cd522018-08-31 12:27:28 -0700160protected:
161 virtual std::unique_ptr<EventThread> makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800162 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700163 impl::EventThread::InterceptVSyncsCallback interceptCallback);
164
Ana Krulec98b5b242018-08-10 15:03:23 -0700165private:
Ana Krulecafb45842019-02-13 13:33:03 -0800166 friend class TestableScheduler;
167
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800168 // In order to make sure that the features don't override themselves, we need a state machine
169 // to keep track which feature requested the config change.
170 enum class MediaFeatureState { MEDIA_PLAYING, MEDIA_OFF };
171 enum class IdleTimerState { EXPIRED, RESET };
172
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800173 // Creates a connection on the given EventThread and forwards the given callbacks.
Ady Abrahama1a49af2019-02-07 14:36:55 -0800174 sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800175
Ana Krulec7ab56032018-11-02 20:51:06 +0100176 nsecs_t calculateAverage() const;
177 void updateFrameSkipping(const int64_t skipCount);
Ana Krulecfb772822018-11-30 10:44:07 +0100178 // Function that resets the idle timer.
179 void resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800180 // Function that is called when the timer resets.
181 void resetTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100182 // Function that is called when the timer expires.
183 void expiredTimerCallback();
Ana Krulecc2870422019-01-29 19:00:58 -0800184 // Sets vsync period.
185 void setVsyncPeriod(const nsecs_t period);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800186 // Media feature's function to change the refresh rate.
187 void mediaChangeRefreshRate(MediaFeatureState mediaFeatureState);
188 // Idle timer feature's function to change the refresh rate.
189 void timerChangeRefreshRate(IdleTimerState idleTimerState);
190 // Acquires a lock and calls the ChangeRefreshRateCallback() with given parameters.
191 void changeRefreshRate(RefreshRateType refreshRateType, ConfigEvent configEvent);
Ana Krulec7ab56032018-11-02 20:51:06 +0100192
Ana Krulece588e312018-09-18 12:32:24 -0700193 // If fences from sync Framework are supported.
194 const bool mHasSyncFramework;
195
196 // The offset in nanoseconds to use, when DispSync timestamps present fence
197 // signaling time.
Sundong Ahnd5e08f62018-12-12 20:27:28 +0900198 nsecs_t mDispSyncPresentTimeOffset;
Ana Krulece588e312018-09-18 12:32:24 -0700199
200 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700201 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700202
203 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700204 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700205
206 std::mutex mHWVsyncLock;
207 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
208 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
Ana Krulecc2870422019-01-29 19:00:58 -0800209 const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)};
Ana Krulece588e312018-09-18 12:32:24 -0700210
211 std::unique_ptr<DispSync> mPrimaryDispSync;
212 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100213
214 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
215 // a proof of concept. We turn on frame skipping if the difference between the timestamps
216 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
217 nsecs_t mPreviousFrameTimestamp = 0;
218 // Keeping track of whether we are skipping the refresh count. If we want to
219 // simulate 30Hz rendering, we skip every other frame, and this variable is set
220 // to 1.
221 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100222 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100223 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100224
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800225 // The following few fields follow native window api bits that come with buffers. If there are
226 // more buffers with NATIVE_WINDOW_API_MEDIA we render at 60Hz, otherwise we render at 90Hz.
227 // There is not dependency on timestamp for V0.
228 // TODO(b/123956502): Remove this when more robust logic for content fps detection is developed.
229 std::mutex mWindowApiHistoryLock;
230 std::array<int, scheduler::ARRAY_SIZE> mWindowApiHistory GUARDED_BY(mWindowApiHistoryLock);
231 int64_t mApiHistoryCounter = 0;
Ana Krulecfb772822018-11-30 10:44:07 +0100232
233 // Timer that records time between requests for next vsync. If the time is higher than a given
234 // interval, a callback is fired. Set this variable to >0 to use this feature.
235 int64_t mSetIdleTimerMs = 0;
236 std::unique_ptr<scheduler::IdleTimer> mIdleTimer;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800237
238 std::mutex mCallbackLock;
Ana Krulec8d3e4f32019-03-05 10:40:33 -0800239 ChangeRefreshRateCallback mChangeRefreshRateCallback GUARDED_BY(mCallbackLock);
Ana Krulecfefd6ae2019-02-13 17:53:08 -0800240
241 // In order to make sure that the features don't override themselves, we need a state machine
242 // to keep track which feature requested the config change.
243 std::mutex mFeatureStateLock;
244 MediaFeatureState mCurrentMediaFeatureState GUARDED_BY(mFeatureStateLock) =
245 MediaFeatureState::MEDIA_OFF;
246 IdleTimerState mCurrentIdleTimerState GUARDED_BY(mFeatureStateLock) = IdleTimerState::RESET;
Ana Krulec98b5b242018-08-10 15:03:23 -0700247};
248
Ana Krulec7ab56032018-11-02 20:51:06 +0100249} // namespace android