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> |
Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 36 | #include "HwcStrongTypes.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | |
| 38 | // --------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 39 | namespace android { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 40 | // --------------------------------------------------------------------------- |
| 41 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 42 | class EventThread; |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 43 | class EventThreadTest; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 | class SurfaceFlinger; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 45 | |
| 46 | // --------------------------------------------------------------------------- |
| 47 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 48 | using ResyncCallback = std::function<void()>; |
| 49 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 50 | enum class VSyncRequest { |
| 51 | None = -1, |
| 52 | Single = 0, |
| 53 | Periodic = 1, |
| 54 | // Subsequent values are periods. |
| 55 | }; |
| 56 | |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 57 | class VSyncSource { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 58 | public: |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 59 | class Callback { |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 60 | public: |
| 61 | virtual ~Callback() {} |
| 62 | virtual void onVSyncEvent(nsecs_t when) = 0; |
| 63 | }; |
| 64 | |
| 65 | virtual ~VSyncSource() {} |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 66 | |
| 67 | virtual const char* getName() const = 0; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 68 | virtual void setVSyncEnabled(bool enable) = 0; |
Lloyd Pique | e83f931 | 2018-02-01 12:53:17 -0800 | [diff] [blame] | 69 | virtual void setCallback(Callback* callback) = 0; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 70 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 73 | class EventThreadConnection : public BnDisplayEventConnection { |
| 74 | public: |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 75 | EventThreadConnection(EventThread*, ResyncCallback, |
| 76 | ISurfaceComposer::ConfigChanged configChanged); |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 77 | virtual ~EventThreadConnection(); |
| 78 | |
| 79 | virtual status_t postEvent(const DisplayEventReceiver::Event& event); |
| 80 | |
| 81 | status_t stealReceiveChannel(gui::BitTube* outChannel) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 82 | status_t setVsyncRate(uint32_t rate) override; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 83 | void requestNextVsync() override; // asynchronous |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 84 | void toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) override; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 85 | |
Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 86 | // Called in response to requestNextVsync. |
| 87 | const ResyncCallback resyncCallback; |
| 88 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 89 | VSyncRequest vsyncRequest = VSyncRequest::None; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 90 | std::atomic<ISurfaceComposer::ConfigChanged> mConfigChanged = |
| 91 | ISurfaceComposer::ConfigChanged::eConfigChangedSuppress; |
| 92 | // Store whether we need to force dispatching a config change separately - |
| 93 | // if mConfigChanged ever changes before the config change is dispatched |
| 94 | // then we still need to propagate an initial config to the app if we |
| 95 | // haven't already. |
| 96 | std::atomic<bool> mForcedConfigChangeDispatch = false; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 97 | |
| 98 | private: |
| 99 | virtual void onFirstRef(); |
| 100 | EventThread* const mEventThread; |
| 101 | gui::BitTube mChannel; |
| 102 | }; |
| 103 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 104 | class EventThread { |
| 105 | public: |
| 106 | virtual ~EventThread(); |
| 107 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 108 | virtual sp<EventThreadConnection> createEventConnection( |
| 109 | ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 110 | |
| 111 | // called before the screen is turned off from main thread |
| 112 | virtual void onScreenReleased() = 0; |
| 113 | |
| 114 | // called after the screen is turned on from main thread |
| 115 | virtual void onScreenAcquired() = 0; |
| 116 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 117 | virtual void onHotplugReceived(PhysicalDisplayId displayId, bool connected) = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 118 | |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 119 | // called when SF changes the active config and apps needs to be notified about the change |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 120 | virtual void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId, |
| 121 | nsecs_t vsyncPeriod) = 0; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 122 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 123 | virtual void dump(std::string& result) const = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 124 | |
| 125 | virtual void setPhaseOffset(nsecs_t phaseOffset) = 0; |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 126 | |
| 127 | virtual status_t registerDisplayEventConnection( |
| 128 | const sp<EventThreadConnection>& connection) = 0; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 129 | virtual void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) = 0; |
Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 130 | // Requests the next vsync. If resetIdleTimer is set to true, it resets the idle timer. |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 131 | virtual void requestNextVsync(const sp<EventThreadConnection>& connection) = 0; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 132 | |
| 133 | // Dispatches the most recent configuration |
| 134 | // Usage of this method assumes that only the primary internal display |
| 135 | // supports multiple display configurations. |
| 136 | virtual void requestLatestConfig() = 0; |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | namespace impl { |
| 140 | |
| 141 | class EventThread : public android::EventThread, private VSyncSource::Callback { |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 142 | public: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 143 | using InterceptVSyncsCallback = std::function<void(nsecs_t)>; |
| 144 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 145 | EventThread(std::unique_ptr<VSyncSource>, InterceptVSyncsCallback); |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 146 | ~EventThread(); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 147 | |
Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 148 | sp<EventThreadConnection> createEventConnection( |
| 149 | ResyncCallback, ISurfaceComposer::ConfigChanged configChanged) const override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 150 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 151 | status_t registerDisplayEventConnection(const sp<EventThreadConnection>& connection) override; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 152 | void setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) override; |
Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 153 | void requestNextVsync(const sp<EventThreadConnection>& connection) override; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 154 | void requestLatestConfig() override; |
Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 155 | |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 156 | // called before the screen is turned off from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 157 | void onScreenReleased() override; |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 158 | |
| 159 | // called after the screen is turned on from main thread |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 160 | void onScreenAcquired() override; |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 161 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 162 | void onHotplugReceived(PhysicalDisplayId displayId, bool connected) override; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 163 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 164 | void onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId, |
| 165 | nsecs_t vsyncPeriod) override; |
Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 166 | |
Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 167 | void dump(std::string& result) const override; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 168 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 169 | void setPhaseOffset(nsecs_t phaseOffset) override; |
Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 170 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 171 | private: |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 172 | friend EventThreadTest; |
| 173 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 174 | using DisplayEventConsumers = std::vector<sp<EventThreadConnection>>; |
| 175 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 176 | void threadMain(std::unique_lock<std::mutex>& lock) REQUIRES(mMutex); |
Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 177 | |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 178 | bool shouldConsumeEvent(const DisplayEventReceiver::Event& event, |
| 179 | const sp<EventThreadConnection>& connection) const REQUIRES(mMutex); |
| 180 | void dispatchEvent(const DisplayEventReceiver::Event& event, |
| 181 | const DisplayEventConsumers& consumers) REQUIRES(mMutex); |
Jamie Gennis | faf77cc | 2013-07-30 15:10:32 -0700 | [diff] [blame] | 182 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 183 | void removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) |
| 184 | REQUIRES(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 185 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 186 | // Implements VSyncSource::Callback |
| 187 | void onVSyncEvent(nsecs_t timestamp) override; |
Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 188 | |
Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 189 | const std::unique_ptr<VSyncSource> mVSyncSource GUARDED_BY(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 190 | |
Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 191 | const InterceptVSyncsCallback mInterceptVSyncsCallback; |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 192 | const char* const mThreadName; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 193 | |
Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 194 | std::thread mThread; |
| 195 | mutable std::mutex mMutex; |
| 196 | mutable std::condition_variable mCondition; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 197 | |
Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 198 | std::vector<wp<EventThreadConnection>> mDisplayEventConnections GUARDED_BY(mMutex); |
Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 199 | std::deque<DisplayEventReceiver::Event> mPendingEvents GUARDED_BY(mMutex); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame^] | 200 | DisplayEventReceiver::Event mLastConfigChangeEvent GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 201 | |
| 202 | // VSYNC state of connected display. |
| 203 | struct VSyncState { |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 204 | explicit VSyncState(PhysicalDisplayId displayId) : displayId(displayId) {} |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 205 | |
Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 206 | const PhysicalDisplayId displayId; |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 207 | |
| 208 | // Number of VSYNC events since display was connected. |
| 209 | uint32_t count = 0; |
| 210 | |
| 211 | // True if VSYNC should be faked, e.g. when display is off. |
| 212 | bool synthetic = false; |
| 213 | }; |
| 214 | |
Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 215 | // TODO(b/74619554): Create per-display threads waiting on respective VSYNC signals, |
| 216 | // and support headless mode by injecting a fake display with synthetic VSYNC. |
| 217 | std::optional<VSyncState> mVSyncState GUARDED_BY(mMutex); |
Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 218 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 219 | // State machine for event loop. |
| 220 | enum class State { |
| 221 | Idle, |
| 222 | Quit, |
| 223 | SyntheticVSync, |
| 224 | VSync, |
| 225 | }; |
Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 226 | |
Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 227 | State mState GUARDED_BY(mMutex) = State::Idle; |
| 228 | |
| 229 | static const char* toCString(State); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | // --------------------------------------------------------------------------- |
| 233 | |
Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 234 | } // namespace impl |
| 235 | } // namespace android |