Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 22 | #include <ui/DisplayStatInfo.h> |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 23 | #include <ui/GraphicTypes.h> |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 24 | |
| 25 | #include "DispSync.h" |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 26 | #include "EventControlThread.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 27 | #include "EventThread.h" |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 28 | #include "IdleTimer.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 29 | #include "InjectVSyncSource.h" |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 30 | #include "LayerHistory.h" |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 31 | #include "SchedulerUtils.h" |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 35 | class EventControlThread; |
| 36 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 37 | class Scheduler { |
| 38 | public: |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 39 | using ExpiredIdleTimerCallback = std::function<void()>; |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 40 | using GetVsyncPeriod = std::function<nsecs_t()>; |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 41 | using ResetIdleTimerCallback = std::function<void()>; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 42 | |
Ana Krulec | 7ecce8c | 2018-10-12 13:44:41 -0700 | [diff] [blame] | 43 | // Enum to indicate whether to start the transaction early, or at vsync time. |
| 44 | enum class TransactionStart { EARLY, NORMAL }; |
| 45 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 46 | /* 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 Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 52 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 53 | ~ConnectionHandle() = default; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 54 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 55 | const int64_t id; |
| 56 | }; |
| 57 | |
| 58 | class Connection { |
| 59 | public: |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 60 | Connection(sp<ConnectionHandle> handle, sp<EventThreadConnection> eventConnection, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 61 | std::unique_ptr<EventThread> eventThread) |
| 62 | : handle(handle), eventConnection(eventConnection), thread(std::move(eventThread)) {} |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 63 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 64 | ~Connection() = default; |
| 65 | |
| 66 | sp<ConnectionHandle> handle; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 67 | sp<EventThreadConnection> eventConnection; |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 68 | const std::unique_ptr<EventThread> thread; |
| 69 | }; |
| 70 | |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 71 | // 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 Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 81 | explicit Scheduler(impl::EventControlThread::SetVSyncEnabledFunction function); |
| 82 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 83 | virtual ~Scheduler(); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 84 | |
| 85 | /** Creates an EventThread connection. */ |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 86 | sp<ConnectionHandle> createConnection(const char* connectionName, int64_t phaseOffsetNs, |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 87 | ResyncCallback, |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 88 | impl::EventThread::InterceptVSyncsCallback); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 89 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 90 | sp<IDisplayEventConnection> createDisplayEventConnection(const sp<ConnectionHandle>& handle, |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 91 | ResyncCallback); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 92 | |
| 93 | // Getter methods. |
| 94 | EventThread* getEventThread(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 95 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 96 | sp<EventThreadConnection> getEventConnection(const sp<ConnectionHandle>& handle); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 97 | |
| 98 | // Should be called when receiving a hotplug event. |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 99 | void hotplugReceived(const sp<ConnectionHandle>& handle, PhysicalDisplayId displayId, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 100 | bool connected); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 101 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 102 | // Should be called after the screen is turned on. |
| 103 | void onScreenAcquired(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 104 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 105 | // Should be called before the screen is turned off. |
| 106 | void onScreenReleased(const sp<ConnectionHandle>& handle); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 107 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 108 | // Should be called when dumpsys command is received. |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 109 | void dump(const sp<ConnectionHandle>& handle, std::string& result) const; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 110 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 111 | // Offers ability to modify phase offset in the event thread. |
| 112 | void setPhaseOffset(const sp<ConnectionHandle>& handle, nsecs_t phaseOffset); |
| 113 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 114 | void getDisplayStatInfo(DisplayStatInfo* stats); |
| 115 | |
| 116 | void enableHardwareVsync(); |
| 117 | void disableHardwareVsync(bool makeUnavailable); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 118 | void resyncToHardwareVsync(bool makeAvailable, nsecs_t period); |
| 119 | // Creates a callback for resyncing. |
| 120 | ResyncCallback makeResyncCallback(GetVsyncPeriod&& getVsyncPeriod); |
| 121 | void setRefreshSkipCount(int count); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 122 | void addResyncSample(const nsecs_t timestamp); |
| 123 | void addPresentFence(const std::shared_ptr<FenceTime>& fenceTime); |
| 124 | void setIgnorePresentFences(bool ignore); |
Ady Abraham | c3e2131 | 2019-02-07 14:30:23 -0800 | [diff] [blame] | 125 | nsecs_t expectedPresentTime(); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 126 | // Adds the present time for given layer to the history of present times. |
| 127 | void addFramePresentTimeForLayer(const nsecs_t framePresentTime, bool isAutoTimestamp, |
| 128 | const std::string layerName); |
| 129 | // Increments counter in the layer history to indicate that SF has started a new frame. |
| 130 | void incrementFrameCounter(); |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 131 | // Callback that gets invoked once the idle timer expires. |
| 132 | void setExpiredIdleTimerCallback(const ExpiredIdleTimerCallback& expiredTimerCallback); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 133 | // Callback that gets invoked once the idle timer is reset. |
| 134 | void setResetIdleTimerCallback(const ResetIdleTimerCallback& resetTimerCallback); |
Ana Krulec | b43429d | 2019-01-09 14:28:51 -0800 | [diff] [blame] | 135 | // Returns relevant information about Scheduler for dumpsys purposes. |
| 136 | std::string doDump(); |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 137 | |
Ady Abraham | 3aff917 | 2019-02-07 19:10:26 -0800 | [diff] [blame] | 138 | // calls DispSync::dump() on primary disp sync |
| 139 | void dumpPrimaryDispSync(std::string& result) const; |
| 140 | |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 141 | protected: |
| 142 | virtual std::unique_ptr<EventThread> makeEventThread( |
Dominik Laskowski | bd52c84 | 2019-01-28 18:11:23 -0800 | [diff] [blame] | 143 | const char* connectionName, DispSync* dispSync, int64_t phaseOffsetNs, |
Ana Krulec | 0c8cd52 | 2018-08-31 12:27:28 -0700 | [diff] [blame] | 144 | impl::EventThread::InterceptVSyncsCallback interceptCallback); |
| 145 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 146 | private: |
Ana Krulec | afb4584 | 2019-02-13 13:33:03 -0800 | [diff] [blame^] | 147 | friend class TestableScheduler; |
| 148 | |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 149 | // Creates a connection on the given EventThread and forwards the given callbacks. |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 150 | sp<EventThreadConnection> createConnectionInternal(EventThread*, ResyncCallback&&); |
Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 151 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 152 | nsecs_t calculateAverage() const; |
| 153 | void updateFrameSkipping(const int64_t skipCount); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 154 | // Collects the statistical mean (average) and median between timestamp |
| 155 | // intervals for each frame for each layer. |
| 156 | void determineLayerTimestampStats(const std::string layerName, const nsecs_t framePresentTime); |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 157 | // Collects the average difference between timestamps for each frame regardless |
| 158 | // of which layer the timestamp came from. |
| 159 | void determineTimestampAverage(bool isAutoTimestamp, const nsecs_t framePresentTime); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 160 | // Function that resets the idle timer. |
| 161 | void resetIdleTimer(); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 162 | // Function that is called when the timer resets. |
| 163 | void resetTimerCallback(); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 164 | // Function that is called when the timer expires. |
| 165 | void expiredTimerCallback(); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 166 | // Sets vsync period. |
| 167 | void setVsyncPeriod(const nsecs_t period); |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 168 | |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 169 | // If fences from sync Framework are supported. |
| 170 | const bool mHasSyncFramework; |
| 171 | |
| 172 | // The offset in nanoseconds to use, when DispSync timestamps present fence |
| 173 | // signaling time. |
Sundong Ahn | d5e08f6 | 2018-12-12 20:27:28 +0900 | [diff] [blame] | 174 | nsecs_t mDispSyncPresentTimeOffset; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 175 | |
| 176 | // Each connection has it's own ID. This variable keeps track of the count. |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 177 | static std::atomic<int64_t> sNextId; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 178 | |
| 179 | // Connections are stored in a map <connection ID, connection> for easy retrieval. |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 180 | std::unordered_map<int64_t, std::unique_ptr<Connection>> mConnections; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 181 | |
| 182 | std::mutex mHWVsyncLock; |
| 183 | bool mPrimaryHWVsyncEnabled GUARDED_BY(mHWVsyncLock); |
| 184 | bool mHWVsyncAvailable GUARDED_BY(mHWVsyncLock); |
Ana Krulec | c287042 | 2019-01-29 19:00:58 -0800 | [diff] [blame] | 185 | const std::shared_ptr<VsyncState> mPrimaryVsyncState{std::make_shared<VsyncState>(*this)}; |
Ana Krulec | e588e31 | 2018-09-18 12:32:24 -0700 | [diff] [blame] | 186 | |
| 187 | std::unique_ptr<DispSync> mPrimaryDispSync; |
| 188 | std::unique_ptr<EventControlThread> mEventControlThread; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 189 | |
| 190 | // TODO(b/113612090): The following set of variables needs to be revised. For now, this is |
| 191 | // a proof of concept. We turn on frame skipping if the difference between the timestamps |
| 192 | // is between 32 and 34ms. We expect this currently for 30fps videos, so we render them at 30Hz. |
| 193 | nsecs_t mPreviousFrameTimestamp = 0; |
| 194 | // Keeping track of whether we are skipping the refresh count. If we want to |
| 195 | // simulate 30Hz rendering, we skip every other frame, and this variable is set |
| 196 | // to 1. |
| 197 | int64_t mSkipCount = 0; |
Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 198 | std::array<int64_t, scheduler::ARRAY_SIZE> mTimeDifferences{}; |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 199 | size_t mCounter = 0; |
Ana Krulec | 3084c05 | 2018-11-21 20:27:17 +0100 | [diff] [blame] | 200 | |
| 201 | LayerHistory mLayerHistory; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 202 | |
| 203 | // Timer that records time between requests for next vsync. If the time is higher than a given |
| 204 | // interval, a callback is fired. Set this variable to >0 to use this feature. |
| 205 | int64_t mSetIdleTimerMs = 0; |
| 206 | std::unique_ptr<scheduler::IdleTimer> mIdleTimer; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 207 | |
| 208 | std::mutex mCallbackLock; |
| 209 | ExpiredIdleTimerCallback mExpiredTimerCallback GUARDED_BY(mCallbackLock); |
Ady Abraham | a1a49af | 2019-02-07 14:36:55 -0800 | [diff] [blame] | 210 | ExpiredIdleTimerCallback mResetTimerCallback GUARDED_BY(mCallbackLock); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Ana Krulec | 7ab5603 | 2018-11-02 20:51:06 +0100 | [diff] [blame] | 213 | } // namespace android |