Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 17 | #pragma once |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 18 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 20 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 21 | #include <condition_variable> |
Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 22 | #include <cstdint> |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 23 | #include <deque> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 24 | #include <mutex> |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 25 | #include <optional> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 26 | #include <thread> |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 27 | #include <vector> |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 28 | |
| 29 | #include <android-base/thread_annotations.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | cb9732a | 2012-04-03 17:48:03 -0700 | [diff] [blame] | 31 | #include <gui/DisplayEventReceiver.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 32 | #include <gui/IDisplayEventConnection.h> |
Lloyd Pique | 78ce418 | 2018-01-31 16:39:51 -0800 | [diff] [blame] | 33 | #include <private/gui/BitTube.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 34 | |
| 35 | #include <utils/Errors.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 36 | |
| 37 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 38 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 39 | // --------------------------------------------------------------------------- |
| 40 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 41 | class EventThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 42 | class EventThreadTest; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 43 | class SurfaceFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 47 | using ResyncCallback = std::function<void()>; |
| 48 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 49 | enum class VSyncRequest { |
| 50 | None = -1, |
| 51 | Single = 0, |
| 52 | Periodic = 1, |
| 53 | // Subsequent values are periods. |
| 54 | }; |
| 55 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 56 | class VSyncSource { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 57 | public: |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 58 | class Callback { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 59 | public: |
| 60 | virtual ~Callback() {} |
| 61 | virtual void onVSyncEvent(nsecs_t when) = 0; |
| 62 | }; |
| 63 | |
| 64 | virtual ~VSyncSource() {} |
| 65 | virtual void setVSyncEnabled(bool enable) = 0; |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 66 | virtual void setCallback(Callback* callback) = 0; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 67 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 70 | class EventThreadConnection : public BnDisplayEventConnection { |
| 71 | public: |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 72 | EventThreadConnection(EventThread* eventThread, ResyncCallback resyncCallback); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 73 | virtual ~EventThreadConnection(); |
| 74 | |
| 75 | virtual status_t postEvent(const DisplayEventReceiver::Event& event); |
| 76 | |
| 77 | status_t stealReceiveChannel(gui::BitTube* outChannel) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 78 | status_t setVsyncRate(uint32_t rate) override; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 79 | void requestNextVsync() override; // asynchronous |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 80 | // Requesting Vsync for HWC does not reset the idle timer, since HWC requires a refresh |
| 81 | // in order to update the configs. |
| 82 | void requestNextVsyncForHWC(); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 83 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 84 | // Called in response to requestNextVsync. |
| 85 | const ResyncCallback resyncCallback; |
| 86 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 87 | VSyncRequest vsyncRequest = VSyncRequest::None; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | virtual void onFirstRef(); |
| 91 | EventThread* const mEventThread; |
| 92 | gui::BitTube mChannel; |
| 93 | }; |
| 94 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 95 | class EventThread { |
| 96 | public: |
| 97 | virtual ~EventThread(); |
| 98 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 99 | virtual sp<EventThreadConnection> createEventConnection( |
| 100 | ResyncCallback resyncCallback) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 101 | |
| 102 | // called before the screen is turned off from main thread |
| 103 | virtual void onScreenReleased() = 0; |
| 104 | |
| 105 | // called after the screen is turned on from main thread |
| 106 | virtual void onScreenAcquired() = 0; |
| 107 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame^] | 108 | virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 109 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 110 | virtual void dump(std::string& result) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 111 | |
| 112 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 113 | |
| 114 | virtual status_t registerDisplayEventConnection( |
| 115 | const sp<EventThreadConnection>& connection) = 0; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 116 | virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 117 | // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer. |
| 118 | virtual void requestNextVsync(const sp<EventThreadConnection>& connection, |
| 119 | bool resetIdleTimer) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | namespace impl { |
| 123 | |
| 124 | class EventThread : public android::EventThread, private VSyncSource::Callback { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 125 | public: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 126 | using InterceptVSyncsCallback = std::function<void(nsecs_t)>; |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 127 | using ResetIdleTimerCallback = std::function<void()>; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 128 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 129 | // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete. |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 130 | EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback, |
| 131 | const char* threadName); |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 132 | EventThread(std::unique_ptr<VSyncSource> src, |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 133 | const InterceptVSyncsCallback& interceptVSyncsCallback, |
| 134 | const ResetIdleTimerCallback& resetIdleTimerCallback, const char* threadName); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 135 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 136 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 137 | sp<EventThreadConnection> createEventConnection(ResyncCallback resyncCallback) const override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 138 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 139 | status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 140 | void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 141 | void requestNextVsync(const sp<EventThreadConnection>& connection, |
| 142 | bool resetIdleTimer) override; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 143 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 144 | // called before the screen is turned off from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 145 | void onScreenReleased() override; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 146 | |
| 147 | // called after the screen is turned on from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 148 | void onScreenAcquired() override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 149 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame^] | 150 | void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 151 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 152 | void dump(std::string& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 153 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 154 | void setPhaseOffset(nsecs_t phaseOffset) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 155 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 156 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 157 | friend EventThreadTest; |
| 158 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 159 | using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>; |
| 160 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 161 | // TODO(b/113612090): Once the Scheduler is complete this constructor will become obsolete. |
| 162 | EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc, |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 163 | InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName); |
| 164 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 165 | void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 166 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 167 | bool shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 168 | const sp<EventThreadConnection>& connection) const REQUIRES(mMutex); |
| 169 | void dispatchEvent(const DisplayEventReceiver::Event& event, |
| 170 | const DisplayEventConsumers& consumers) REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 171 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 172 | void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) |
| 173 | REQUIRES(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 174 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 175 | // Implements VSyncSource::Callback |
| 176 | void onVSyncEvent(nsecs_t timestamp) override; |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 177 | |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 178 | // Acquires mutex and requests next vsync. |
| 179 | void requestNextVsyncInternal(const sp<EventThreadConnection>& connection) EXCLUDES(mMutex); |
| 180 | |
Ana Krulec | 98b5b24 | 2018-08-10 15:03:23 -0700 | [diff] [blame] | 181 | // TODO(b/113612090): Once the Scheduler is complete this pointer will become obsolete. |
| 182 | VSyncSource* mVSyncSource GUARDED_BY(mMutex) = nullptr; |
| 183 | std::unique_ptr<VSyncSource> mVSyncSourceUnique GUARDED_BY(mMutex) = nullptr; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 184 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 185 | const InterceptVSyncsCallback mInterceptVSyncsCallback; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 186 | const char* const mThreadName; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 187 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 188 | std::thread mThread; |
| 189 | mutable std::mutex mMutex; |
| 190 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 191 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 192 | std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 193 | std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 194 | |
| 195 | // VSYNC state of connected display. |
| 196 | struct VSyncState { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame^] | 197 | explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {} |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 198 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame^] | 199 | const PhysicalDisplayId displayId; |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 200 | |
| 201 | // Number of VSYNC events since display was connected. |
| 202 | uint32_t count = 0; |
| 203 | |
| 204 | // True if VSYNC should be faked, e.g. when display is off. |
| 205 | bool synthetic = false; |
| 206 | }; |
| 207 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 208 | // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals, |
| 209 | // and support headless mode by injecting a fake display with synthetic VSYNC. |
| 210 | std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 211 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 212 | // State machine for event loop. |
| 213 | enum class State { |
| 214 | Idle, |
| 215 | Quit, |
| 216 | SyntheticVSync, |
| 217 | VSync, |
| 218 | }; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 219 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 220 | State mState GUARDED_BY(mMutex) = State::Idle; |
| 221 | |
| 222 | static const char* toCString(State); |
Ana Krulec | fb77282 | 2018-11-30 10:44:07 +0100 | [diff] [blame] | 223 | |
| 224 | // Callback that resets the idle timer when the next vsync is received. |
| 225 | ResetIdleTimerCallback mResetIdleTimer; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | // --------------------------------------------------------------------------- |
| 229 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 230 | } // namespace impl |
| 231 | } // namespace android |