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