blob: 7f113e717bf54667bc3ae8cf8f84382501593f26 [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 Krulec434c22d2018-11-28 13:48:36 +010031#include "SchedulerUtils.h"
Ana Krulec98b5b242018-08-10 15:03:23 -070032
33namespace android {
34
Ana Krulece588e312018-09-18 12:32:24 -070035class EventControlThread;
36
Ana Krulec98b5b242018-08-10 15:03:23 -070037class Scheduler {
38public:
Ana Krulec7d1d6832018-12-27 11:10:09 -080039 using ExpiredIdleTimerCallback = std::function<void()>;
Ana Krulecc2870422019-01-29 19:00:58 -080040 using GetVsyncPeriod = std::function<nsecs_t()>;
Ady Abrahama1a49af2019-02-07 14:36:55 -080041 using ResetIdleTimerCallback = std::function<void()>;
Ana Krulec7d1d6832018-12-27 11:10:09 -080042
Ana Krulec7ecce8c2018-10-12 13:44:41 -070043 // Enum to indicate whether to start the transaction early, or at vsync time.
44 enum class TransactionStart { EARLY, NORMAL };
45
Ana Krulec98b5b242018-08-10 15:03:23 -070046 /* The scheduler handle is a BBinder object passed to the client from which we can extract
47 * an ID for subsequent operations.
48 */
49 class ConnectionHandle : public BBinder {
50 public:
51 ConnectionHandle(int64_t id) : id(id) {}
Ana Krulece588e312018-09-18 12:32:24 -070052
Ana Krulec98b5b242018-08-10 15:03:23 -070053 ~ConnectionHandle() = default;
Ana Krulece588e312018-09-18 12:32:24 -070054
Ana Krulec98b5b242018-08-10 15:03:23 -070055 const int64_t id;
56 };
57
58 class Connection {
59 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080060 Connection(sp<ConnectionHandle> handle, sp<EventThreadConnection> eventConnection,
Ana Krulec98b5b242018-08-10 15:03:23 -070061 std::unique_ptr<EventThread> eventThread)
62 : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {}
Ana Krulece588e312018-09-18 12:32:24 -070063
Ana Krulec98b5b242018-08-10 15:03:23 -070064 ~Connection() = default;
65
66 sp<ConnectionHandle> handle;
Ana Krulec85c39af2018-12-26 17:29:57 -080067 sp<EventThreadConnection> eventConnection;
Ana Krulec98b5b242018-08-10 15:03:23 -070068 const std::unique_ptr<EventThread> thread;
69 };
70
Ana Krulecc2870422019-01-29 19:00:58 -080071 // Stores per-display state about VSYNC.
72 struct VsyncState {
73 explicit VsyncState(Scheduler& scheduler) : scheduler(scheduler) {}
74
75 void resync(const GetVsyncPeriod&);
76
77 Scheduler& scheduler;
78 std::atomic<nsecs_t> lastResyncTime = 0;
79 };
80
Ana Krulece588e312018-09-18 12:32:24 -070081 explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function);
82
Ana Krulec0c8cd522018-08-31 12:27:28 -070083 virtual ~Scheduler();
Ana Krulec98b5b242018-08-10 15:03:23 -070084
85 /** Creates an EventThread connection. */
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080086 sp<ConnectionHandle> createConnection(const char* connectionName, int64_t phaseOffsetNs,
Ady Abrahama1a49af2019-02-07 14:36:55 -080087 ResyncCallback,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -080088 impl::EventThread::InterceptVSyncsCallback);
Ana Krulece588e312018-09-18 12:32:24 -070089
Dominik Laskowskif654d572018-12-20 11:03:06 -080090 sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle,
Ady Abrahama1a49af2019-02-07 14:36:55 -080091 ResyncCallback);
Ana Krulec98b5b242018-08-10 15:03:23 -070092
93 // Getter methods.
94 EventThread* getEventThread(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -070095
Ana Krulec85c39af2018-12-26 17:29:57 -080096 sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle);
Ana Krulec98b5b242018-08-10 15:03:23 -070097
98 // Should be called when receiving a hotplug event.
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080099 void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
Ana Krulec98b5b242018-08-10 15:03:23 -0700100 bool connected);
Ana Krulece588e312018-09-18 12:32:24 -0700101
Ana Krulec98b5b242018-08-10 15:03:23 -0700102 // Should be called after the screen is turned on.
103 void onScreenAcquired(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700104
Ana Krulec98b5b242018-08-10 15:03:23 -0700105 // Should be called before the screen is turned off.
106 void onScreenReleased(const sp<ConnectionHandle>& handle);
Ana Krulece588e312018-09-18 12:32:24 -0700107
Ady Abraham447052e2019-02-13 16:07:27 -0800108 // Should be called when display config changed
109 void onConfigChanged(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId,
110 int32_t configId);
111
Ana Krulec98b5b242018-08-10 15:03:23 -0700112 // Should be called when dumpsys command is received.
Yiwei Zhang5434a782018-12-05 18:06:32 -0800113 void dump(const sp<ConnectionHandle>& handle, std::string& result) const;
Ana Krulece588e312018-09-18 12:32:24 -0700114
Ana Krulec98b5b242018-08-10 15:03:23 -0700115 // Offers ability to modify phase offset in the event thread.
116 void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset);
117
Ady Abrahamb838aed2019-02-12 15:30:16 -0800118 // pause/resume vsync callback generation to avoid sending vsync callbacks during config switch
119 void pauseVsyncCallback(const sp<ConnectionHandle>& handle, bool pause);
120
Ana Krulece588e312018-09-18 12:32:24 -0700121 void getDisplayStatInfo(DisplayStatInfo* stats);
122
123 void enableHardwareVsync();
124 void disableHardwareVsync(bool makeUnavailable);
Ana Krulecc2870422019-01-29 19:00:58 -0800125 void resyncToHardwareVsync(bool makeAvailable, nsecs_t period);
126 // Creates a callback for resyncing.
127 ResyncCallback makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod);
128 void setRefreshSkipCount(int count);
Ana Krulece588e312018-09-18 12:32:24 -0700129 void addResyncSample(const nsecs_t timestamp);
130 void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime);
131 void setIgnorePresentFences(bool ignore);
Ady Abrahamc3e21312019-02-07 14:30:23 -0800132 nsecs_t expectedPresentTime();
Ana Krulec3084c052018-11-21 20:27:17 +0100133 // Adds the present time for given layer to the history of present times.
134 void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp,
135 const std::string layerName);
136 // Increments counter in the layer history to indicate that SF has started a new frame.
137 void incrementFrameCounter();
Ana Krulec7d1d6832018-12-27 11:10:09 -0800138 // Callback that gets invoked once the idle timer expires.
139 void setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback);
Ady Abrahama1a49af2019-02-07 14:36:55 -0800140 // Callback that gets invoked once the idle timer is reset.
141 void setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback);
Ana Krulecb43429d2019-01-09 14:28:51 -0800142 // Returns relevant information about Scheduler for dumpsys purposes.
143 std::string doDump();
Ana Krulece588e312018-09-18 12:32:24 -0700144
Ady Abraham3aff9172019-02-07 19:10:26 -0800145 // calls DispSync::dump() on primary disp sync
146 void dumpPrimaryDispSync(std::string& result) const;
147
Ana Krulec0c8cd522018-08-31 12:27:28 -0700148protected:
149 virtual std::unique_ptr<EventThread> makeEventThread(
Dominik Laskowskibd52c842019-01-28 18:11:23 -0800150 const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs,
Ana Krulec0c8cd522018-08-31 12:27:28 -0700151 impl::EventThread::InterceptVSyncsCallback interceptCallback);
152
Ana Krulec98b5b242018-08-10 15:03:23 -0700153private:
Ana Krulecafb45842019-02-13 13:33:03 -0800154 friend class TestableScheduler;
155
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800156 // Creates a connection on the given EventThread and forwards the given callbacks.
Ady Abrahama1a49af2019-02-07 14:36:55 -0800157 sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&);
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800158
Ana Krulec7ab56032018-11-02 20:51:06 +0100159 nsecs_t calculateAverage() const;
160 void updateFrameSkipping(const int64_t skipCount);
Ana Krulec3084c052018-11-21 20:27:17 +0100161 // Collects the statistical mean (average) and median between timestamp
162 // intervals for each frame for each layer.
163 void determineLayerTimestampStats(const std::string layerName, const nsecs_t framePresentTime);
Ana Krulec3084c052018-11-21 20:27:17 +0100164 // Collects the average difference between timestamps for each frame regardless
165 // of which layer the timestamp came from.
166 void determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime);
Ana Krulecfb772822018-11-30 10:44:07 +0100167 // Function that resets the idle timer.
168 void resetIdleTimer();
Ady Abrahama1a49af2019-02-07 14:36:55 -0800169 // Function that is called when the timer resets.
170 void resetTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100171 // Function that is called when the timer expires.
172 void expiredTimerCallback();
Ana Krulecc2870422019-01-29 19:00:58 -0800173 // Sets vsync period.
174 void setVsyncPeriod(const nsecs_t period);
Ana Krulec7ab56032018-11-02 20:51:06 +0100175
Ana Krulece588e312018-09-18 12:32:24 -0700176 // If fences from sync Framework are supported.
177 const bool mHasSyncFramework;
178
179 // The offset in nanoseconds to use, when DispSync timestamps present fence
180 // signaling time.
Sundong Ahnd5e08f62018-12-12 20:27:28 +0900181 nsecs_t mDispSyncPresentTimeOffset;
Ana Krulece588e312018-09-18 12:32:24 -0700182
183 // Each connection has it's own ID. This variable keeps track of the count.
Ana Krulec98b5b242018-08-10 15:03:23 -0700184 static std::atomic<int64_t> sNextId;
Ana Krulece588e312018-09-18 12:32:24 -0700185
186 // Connections are stored in a map <connection ID, connection> for easy retrieval.
Ana Krulec98b5b242018-08-10 15:03:23 -0700187 std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections;
Ana Krulece588e312018-09-18 12:32:24 -0700188
189 std::mutex mHWVsyncLock;
190 bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock);
191 bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock);
Ana Krulecc2870422019-01-29 19:00:58 -0800192 const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)};
Ana Krulece588e312018-09-18 12:32:24 -0700193
194 std::unique_ptr<DispSync> mPrimaryDispSync;
195 std::unique_ptr<EventControlThread> mEventControlThread;
Ana Krulec7ab56032018-11-02 20:51:06 +0100196
197 // TODO(b/113612090): The following set of variables needs to be revised. For now, this is
198 // a proof of concept. We turn on frame skipping if the difference between the timestamps
199 // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz.
200 nsecs_t mPreviousFrameTimestamp = 0;
201 // Keeping track of whether we are skipping the refresh count. If we want to
202 // simulate 30Hz rendering, we skip every other frame, and this variable is set
203 // to 1.
204 int64_t mSkipCount = 0;
Ana Krulec434c22d2018-11-28 13:48:36 +0100205 std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{};
Ana Krulec7ab56032018-11-02 20:51:06 +0100206 size_t mCounter = 0;
Ana Krulec3084c052018-11-21 20:27:17 +0100207
Ana Krulec6da0e492019-02-19 14:46:01 -0800208 // DetermineLayerTimestampStats is called from BufferQueueLayer::onFrameAvailable which
209 // can run on any thread, and cause failure.
210 std::mutex mLayerHistoryLock;
211 LayerHistory mLayerHistory GUARDED_BY(mLayerHistoryLock);
Ana Krulecfb772822018-11-30 10:44:07 +0100212
213 // Timer that records time between requests for next vsync. If the time is higher than a given
214 // interval, a callback is fired. Set this variable to >0 to use this feature.
215 int64_t mSetIdleTimerMs = 0;
216 std::unique_ptr<scheduler::IdleTimer> mIdleTimer;
Ana Krulec7d1d6832018-12-27 11:10:09 -0800217
218 std::mutex mCallbackLock;
219 ExpiredIdleTimerCallback mExpiredTimerCallback GUARDED_BY(mCallbackLock);
Ady Abrahama1a49af2019-02-07 14:36:55 -0800220 ExpiredIdleTimerCallback mResetTimerCallback GUARDED_BY(mCallbackLock);
Ana Krulec98b5b242018-08-10 15:03:23 -0700221};
222
Ana Krulec7ab56032018-11-02 20:51:06 +0100223} // namespace android