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