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