| 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 |  | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
 | 18 | #pragma clang diagnostic push | 
 | 19 | #pragma clang diagnostic ignored "-Wconversion" | 
 | 20 |  | 
| Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
 | 22 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 23 | #include <pthread.h> | 
 | 24 | #include <sched.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 25 | #include <sys/types.h> | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 26 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 27 | #include <chrono> | 
 | 28 | #include <cstdint> | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 29 | #include <optional> | 
 | 30 | #include <type_traits> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 31 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> | 
 | 33 |  | 
| Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 34 | #include <cutils/compiler.h> | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 35 | #include <cutils/sched_policy.h> | 
| Mathias Agopian | a4cb35a | 2012-08-20 20:07:34 -0700 | [diff] [blame] | 36 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | #include <gui/DisplayEventReceiver.h> | 
 | 38 |  | 
 | 39 | #include <utils/Errors.h> | 
| Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 41 |  | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 42 | #include "EventThread.h" | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 43 | #include "HwcStrongTypes.h" | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 44 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 45 | using namespace std::chrono_literals; | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 46 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 47 | namespace android { | 
 | 48 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 49 | using base::StringAppendF; | 
 | 50 | using base::StringPrintf; | 
 | 51 |  | 
 | 52 | namespace { | 
 | 53 |  | 
 | 54 | auto vsyncPeriod(VSyncRequest request) { | 
 | 55 |     return static_cast<std::underlying_type_t<VSyncRequest>>(request); | 
 | 56 | } | 
 | 57 |  | 
 | 58 | std::string toString(VSyncRequest request) { | 
 | 59 |     switch (request) { | 
 | 60 |         case VSyncRequest::None: | 
 | 61 |             return "VSyncRequest::None"; | 
 | 62 |         case VSyncRequest::Single: | 
 | 63 |             return "VSyncRequest::Single"; | 
 | 64 |         default: | 
 | 65 |             return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request)); | 
 | 66 |     } | 
 | 67 | } | 
 | 68 |  | 
 | 69 | std::string toString(const EventThreadConnection& connection) { | 
 | 70 |     return StringPrintf("Connection{%p, %s}", &connection, | 
 | 71 |                         toString(connection.vsyncRequest).c_str()); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | std::string toString(const DisplayEventReceiver::Event& event) { | 
 | 75 |     switch (event.header.type) { | 
 | 76 |         case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 77 |             return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}", | 
 | 78 |                                 event.header.displayId, | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 79 |                                 event.hotplug.connected ? "connected" : "disconnected"); | 
 | 80 |         case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 81 |             return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 82 |                                 ", count=%u, expectedVSyncTimestamp=%" PRId64 "}", | 
 | 83 |                                 event.header.displayId, event.vsync.count, | 
 | 84 |                                 event.vsync.expectedVSyncTimestamp); | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 85 |         case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: | 
 | 86 |             return StringPrintf("ConfigChanged{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT | 
 | 87 |                                 ", configId=%u}", | 
 | 88 |                                 event.header.displayId, event.config.configId); | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 89 |         default: | 
 | 90 |             return "Event{}"; | 
 | 91 |     } | 
 | 92 | } | 
 | 93 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 94 | DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp, | 
 | 95 |                                         bool connected) { | 
| Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 96 |     DisplayEventReceiver::Event event; | 
 | 97 |     event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp}; | 
 | 98 |     event.hotplug.connected = connected; | 
 | 99 |     return event; | 
 | 100 | } | 
 | 101 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 102 | DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp, | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 103 |                                       uint32_t count, nsecs_t expectedVSyncTimestamp) { | 
| Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 104 |     DisplayEventReceiver::Event event; | 
 | 105 |     event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp}; | 
 | 106 |     event.vsync.count = count; | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 107 |     event.vsync.expectedVSyncTimestamp = expectedVSyncTimestamp; | 
| Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 108 |     return event; | 
 | 109 | } | 
 | 110 |  | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 111 | DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId, | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 112 |                                               HwcConfigIndexType configId, nsecs_t vsyncPeriod) { | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 113 |     DisplayEventReceiver::Event event; | 
 | 114 |     event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()}; | 
| Ady Abraham | 2139f73 | 2019-11-13 18:56:40 -0800 | [diff] [blame] | 115 |     event.config.configId = configId.value(); | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 116 |     event.config.vsyncPeriod = vsyncPeriod; | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 117 |     return event; | 
 | 118 | } | 
 | 119 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 120 | } // namespace | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 121 |  | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 122 | EventThreadConnection::EventThreadConnection(EventThread* eventThread, | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 123 |                                              ResyncCallback resyncCallback, | 
 | 124 |                                              ISurfaceComposer::ConfigChanged configChanged) | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 125 |       : resyncCallback(std::move(resyncCallback)), | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 126 |         mConfigChanged(configChanged), | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 127 |         mEventThread(eventThread), | 
 | 128 |         mChannel(gui::BitTube::DefaultSize) {} | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 129 |  | 
 | 130 | EventThreadConnection::~EventThreadConnection() { | 
 | 131 |     // do nothing here -- clean-up will happen automatically | 
 | 132 |     // when the main thread wakes up | 
 | 133 | } | 
 | 134 |  | 
 | 135 | void EventThreadConnection::onFirstRef() { | 
 | 136 |     // NOTE: mEventThread doesn't hold a strong reference on us | 
 | 137 |     mEventThread->registerDisplayEventConnection(this); | 
 | 138 | } | 
 | 139 |  | 
 | 140 | status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) { | 
 | 141 |     outChannel->setReceiveFd(mChannel.moveReceiveFd()); | 
| Alec Mouri | c080030 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 142 |     outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd()))); | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 143 |     return NO_ERROR; | 
 | 144 | } | 
 | 145 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 146 | status_t EventThreadConnection::setVsyncRate(uint32_t rate) { | 
 | 147 |     mEventThread->setVsyncRate(rate, this); | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 148 |     return NO_ERROR; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | void EventThreadConnection::requestNextVsync() { | 
| Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 152 |     ATRACE_NAME("requestNextVsync"); | 
| Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 153 |     mEventThread->requestNextVsync(this); | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 154 | } | 
 | 155 |  | 
 | 156 | status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) { | 
 | 157 |     ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1); | 
 | 158 |     return size < 0 ? status_t(size) : status_t(NO_ERROR); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | // --------------------------------------------------------------------------- | 
 | 162 |  | 
| Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 163 | EventThread::~EventThread() = default; | 
 | 164 |  | 
 | 165 | namespace impl { | 
 | 166 |  | 
| Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 167 | EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource, | 
 | 168 |                          InterceptVSyncsCallback interceptVSyncsCallback) | 
 | 169 |       : mVSyncSource(std::move(vsyncSource)), | 
| Dominik Laskowski | ccf37d7 | 2019-02-01 16:47:58 -0800 | [diff] [blame] | 170 |         mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)), | 
| Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 171 |         mThreadName(mVSyncSource->getName()) { | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 172 |     mVSyncSource->setCallback(this); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 173 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 174 |     mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS { | 
 | 175 |         std::unique_lock<std::mutex> lock(mMutex); | 
 | 176 |         threadMain(lock); | 
 | 177 |     }); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 178 |  | 
| Dominik Laskowski | 6505f79 | 2019-09-18 11:10:05 -0700 | [diff] [blame] | 179 |     pthread_setname_np(mThread.native_handle(), mThreadName); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 180 |  | 
 | 181 |     pid_t tid = pthread_gettid_np(mThread.native_handle()); | 
 | 182 |  | 
 | 183 |     // Use SCHED_FIFO to minimize jitter | 
 | 184 |     constexpr int EVENT_THREAD_PRIORITY = 2; | 
 | 185 |     struct sched_param param = {0}; | 
 | 186 |     param.sched_priority = EVENT_THREAD_PRIORITY; | 
 | 187 |     if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, ¶m) != 0) { | 
 | 188 |         ALOGE("Couldn't set SCHED_FIFO for EventThread"); | 
 | 189 |     } | 
 | 190 |  | 
 | 191 |     set_sched_policy(tid, SP_FOREGROUND); | 
 | 192 | } | 
 | 193 |  | 
 | 194 | EventThread::~EventThread() { | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 195 |     mVSyncSource->setCallback(nullptr); | 
 | 196 |  | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 197 |     { | 
 | 198 |         std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 199 |         mState = State::Quit; | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 200 |         mCondition.notify_all(); | 
 | 201 |     } | 
 | 202 |     mThread.join(); | 
| Ruchi Kandoi | ef472ec | 2014-04-02 12:50:06 -0700 | [diff] [blame] | 203 | } | 
 | 204 |  | 
| Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 205 | void EventThread::setPhaseOffset(nsecs_t phaseOffset) { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 206 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dan Stoza | db4ac3c | 2015-04-14 11:34:01 -0700 | [diff] [blame] | 207 |     mVSyncSource->setPhaseOffset(phaseOffset); | 
 | 208 | } | 
 | 209 |  | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 210 | sp<EventThreadConnection> EventThread::createEventConnection( | 
 | 211 |         ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const { | 
 | 212 |     return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback), | 
 | 213 |                                      configChanged); | 
| Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 214 | } | 
 | 215 |  | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 216 | status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 217 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 218 |  | 
 | 219 |     // this should never happen | 
 | 220 |     auto it = std::find(mDisplayEventConnections.cbegin(), | 
 | 221 |             mDisplayEventConnections.cend(), connection); | 
 | 222 |     if (it != mDisplayEventConnections.cend()) { | 
 | 223 |         ALOGW("DisplayEventConnection %p already exists", connection.get()); | 
 | 224 |         mCondition.notify_all(); | 
 | 225 |         return ALREADY_EXISTS; | 
 | 226 |     } | 
 | 227 |  | 
 | 228 |     mDisplayEventConnections.push_back(connection); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 229 |     mCondition.notify_all(); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 230 |     return NO_ERROR; | 
 | 231 | } | 
 | 232 |  | 
| Ana Krulec | 85c39af | 2018-12-26 17:29:57 -0800 | [diff] [blame] | 233 | void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) { | 
| Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 234 |     auto it = std::find(mDisplayEventConnections.cbegin(), | 
 | 235 |             mDisplayEventConnections.cend(), connection); | 
 | 236 |     if (it != mDisplayEventConnections.cend()) { | 
 | 237 |         mDisplayEventConnections.erase(it); | 
 | 238 |     } | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 239 | } | 
 | 240 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 241 | void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) { | 
 | 242 |     if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) { | 
 | 243 |         return; | 
 | 244 |     } | 
 | 245 |  | 
 | 246 |     std::lock_guard<std::mutex> lock(mMutex); | 
 | 247 |  | 
 | 248 |     const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate); | 
 | 249 |     if (connection->vsyncRequest != request) { | 
 | 250 |         connection->vsyncRequest = request; | 
 | 251 |         mCondition.notify_all(); | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 252 |     } | 
 | 253 | } | 
 | 254 |  | 
| Ady Abraham | 8532d01 | 2019-05-08 14:50:56 -0700 | [diff] [blame] | 255 | void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) { | 
| Dominik Laskowski | f654d57 | 2018-12-20 11:03:06 -0800 | [diff] [blame] | 256 |     if (connection->resyncCallback) { | 
 | 257 |         connection->resyncCallback(); | 
| Lloyd Pique | 24b0a48 | 2018-03-09 18:52:26 -0800 | [diff] [blame] | 258 |     } | 
| Tim Murray | 4a4e4a2 | 2016-04-19 16:29:23 +0000 | [diff] [blame] | 259 |  | 
| Ana Krulec | 7d1d683 | 2018-12-27 11:10:09 -0800 | [diff] [blame] | 260 |     std::lock_guard<std::mutex> lock(mMutex); | 
 | 261 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 262 |     if (connection->vsyncRequest == VSyncRequest::None) { | 
 | 263 |         connection->vsyncRequest = VSyncRequest::Single; | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 264 |         mCondition.notify_all(); | 
| Mathias Agopian | 478ae5e | 2011-12-06 17:22:19 -0800 | [diff] [blame] | 265 |     } | 
| Mathias Agopian | 2374866 | 2011-12-05 14:33:34 -0800 | [diff] [blame] | 266 | } | 
 | 267 |  | 
| Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 268 | void EventThread::onScreenReleased() { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 269 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 270 |     if (!mVSyncState || mVSyncState->synthetic) { | 
 | 271 |         return; | 
| Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 272 |     } | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 273 |  | 
 | 274 |     mVSyncState->synthetic = true; | 
 | 275 |     mCondition.notify_all(); | 
| Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 276 | } | 
 | 277 |  | 
 | 278 | void EventThread::onScreenAcquired() { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 279 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 280 |     if (!mVSyncState || !mVSyncState->synthetic) { | 
 | 281 |         return; | 
| Mathias Agopian | 7d88647 | 2012-06-14 23:39:35 -0700 | [diff] [blame] | 282 |     } | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 283 |  | 
 | 284 |     mVSyncState->synthetic = false; | 
 | 285 |     mCondition.notify_all(); | 
| Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 286 | } | 
 | 287 |  | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 288 | void EventThread::onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp) { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 289 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 290 |  | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 291 |     LOG_FATAL_IF(!mVSyncState); | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 292 |     mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count, | 
 | 293 |                                        expectedVSyncTimestamp)); | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 294 |     mCondition.notify_all(); | 
| Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 295 | } | 
 | 296 |  | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 297 | void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 298 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 299 |  | 
| Dominik Laskowski | 23b867a | 2019-01-22 12:44:53 -0800 | [diff] [blame] | 300 |     mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected)); | 
| Dominik Laskowski | 00a6fa2 | 2018-06-06 16:42:02 -0700 | [diff] [blame] | 301 |     mCondition.notify_all(); | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 302 | } | 
 | 303 |  | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 304 | void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId, | 
 | 305 |                                   nsecs_t vsyncPeriod) { | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 306 |     std::lock_guard<std::mutex> lock(mMutex); | 
 | 307 |  | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 308 |     mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod)); | 
| Ady Abraham | 447052e | 2019-02-13 16:07:27 -0800 | [diff] [blame] | 309 |     mCondition.notify_all(); | 
 | 310 | } | 
 | 311 |  | 
| Alec Mouri | 717bcb6 | 2020-02-10 17:07:19 -0800 | [diff] [blame] | 312 | size_t EventThread::getEventThreadConnectionCount() { | 
 | 313 |     std::lock_guard<std::mutex> lock(mMutex); | 
 | 314 |     return mDisplayEventConnections.size(); | 
 | 315 | } | 
 | 316 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 317 | void EventThread::threadMain(std::unique_lock<std::mutex>& lock) { | 
 | 318 |     DisplayEventConsumers consumers; | 
 | 319 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 320 |     while (mState != State::Quit) { | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 321 |         std::optional<DisplayEventReceiver::Event> event; | 
| Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 322 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 323 |         // Determine next event to dispatch. | 
 | 324 |         if (!mPendingEvents.empty()) { | 
 | 325 |             event = mPendingEvents.front(); | 
 | 326 |             mPendingEvents.pop_front(); | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 327 |  | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 328 |             switch (event->header.type) { | 
 | 329 |                 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: | 
 | 330 |                     if (event->hotplug.connected && !mVSyncState) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 331 |                         mVSyncState.emplace(event->header.displayId); | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 332 |                     } else if (!event->hotplug.connected && mVSyncState && | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 333 |                                mVSyncState->displayId == event->header.displayId) { | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 334 |                         mVSyncState.reset(); | 
 | 335 |                     } | 
 | 336 |                     break; | 
 | 337 |  | 
 | 338 |                 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: | 
 | 339 |                     if (mInterceptVSyncsCallback) { | 
 | 340 |                         mInterceptVSyncsCallback(event->header.timestamp); | 
 | 341 |                     } | 
 | 342 |                     break; | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 343 |             } | 
| Mathias Agopian | ff28e20 | 2012-09-20 23:24:19 -0700 | [diff] [blame] | 344 |         } | 
 | 345 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 346 |         bool vsyncRequested = false; | 
| Mathias Agopian | 148994e | 2012-09-19 17:31:36 -0700 | [diff] [blame] | 347 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 348 |         // Find connections that should consume this event. | 
| Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 349 |         auto it = mDisplayEventConnections.begin(); | 
 | 350 |         while (it != mDisplayEventConnections.end()) { | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 351 |             if (const auto connection = it->promote()) { | 
 | 352 |                 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None; | 
 | 353 |  | 
 | 354 |                 if (event && shouldConsumeEvent(*event, connection)) { | 
 | 355 |                     consumers.push_back(connection); | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 356 |                 } | 
| Mathias Agopian | b4d18ed | 2012-09-20 23:14:05 -0700 | [diff] [blame] | 357 |  | 
| Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 358 |                 ++it; | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 359 |             } else { | 
| Chia-I Wu | 98cd38f | 2018-09-14 11:53:25 -0700 | [diff] [blame] | 360 |                 it = mDisplayEventConnections.erase(it); | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 361 |             } | 
 | 362 |         } | 
 | 363 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 364 |         if (!consumers.empty()) { | 
 | 365 |             dispatchEvent(*event, consumers); | 
 | 366 |             consumers.clear(); | 
 | 367 |         } | 
 | 368 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 369 |         State nextState; | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 370 |         if (mVSyncState && vsyncRequested) { | 
 | 371 |             nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync; | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 372 |         } else { | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 373 |             ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected"); | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 374 |             nextState = State::Idle; | 
 | 375 |         } | 
 | 376 |  | 
 | 377 |         if (mState != nextState) { | 
 | 378 |             if (mState == State::VSync) { | 
 | 379 |                 mVSyncSource->setVSyncEnabled(false); | 
 | 380 |             } else if (nextState == State::VSync) { | 
 | 381 |                 mVSyncSource->setVSyncEnabled(true); | 
 | 382 |             } | 
 | 383 |  | 
 | 384 |             mState = nextState; | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 385 |         } | 
 | 386 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 387 |         if (event) { | 
 | 388 |             continue; | 
 | 389 |         } | 
 | 390 |  | 
 | 391 |         // Wait for event or client registration/request. | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 392 |         if (mState == State::Idle) { | 
 | 393 |             mCondition.wait(lock); | 
 | 394 |         } else { | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 395 |             // Generate a fake VSYNC after a long timeout in case the driver stalls. When the | 
 | 396 |             // display is off, keep feeding clients at 60 Hz. | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 397 |             const std::chrono::nanoseconds timeout = | 
 | 398 |                     mState == State::SyntheticVSync ? 16ms : 1000ms; | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 399 |             if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) { | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 400 |                 if (mState == State::VSync) { | 
 | 401 |                     ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName); | 
 | 402 |                     std::string debugInfo = "VsyncSource debug info:\n"; | 
 | 403 |                     mVSyncSource->dump(debugInfo); | 
| Ady Abraham | 7539872 | 2020-04-07 14:08:45 -0700 | [diff] [blame] | 404 |                     // Log the debug info line-by-line to avoid logcat overflow | 
 | 405 |                     auto pos = debugInfo.find('\n'); | 
 | 406 |                     while (pos != std::string::npos) { | 
 | 407 |                         ALOGW("%s", debugInfo.substr(0, pos).c_str()); | 
 | 408 |                         debugInfo = debugInfo.substr(pos + 1); | 
 | 409 |                         pos = debugInfo.find('\n'); | 
 | 410 |                     } | 
| Ady Abraham | 5e7371c | 2020-03-24 14:47:24 -0700 | [diff] [blame] | 411 |                 } | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 412 |  | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 413 |                 LOG_FATAL_IF(!mVSyncState); | 
| Ady Abraham | 5facfb1 | 2020-04-22 15:18:31 -0700 | [diff] [blame] | 414 |                 const auto now = systemTime(SYSTEM_TIME_MONOTONIC); | 
 | 415 |                 const auto expectedVSyncTime = now + timeout.count(); | 
 | 416 |                 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now, | 
 | 417 |                                                    ++mVSyncState->count, expectedVSyncTime)); | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 418 |             } | 
 | 419 |         } | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 420 |     } | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 421 | } | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 422 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 423 | bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event, | 
 | 424 |                                      const sp<EventThreadConnection>& connection) const { | 
 | 425 |     switch (event.header.type) { | 
 | 426 |         case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG: | 
 | 427 |             return true; | 
 | 428 |  | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 429 |         case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: { | 
| Alec Mouri | c080030 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 430 |             return connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch; | 
| Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 431 |         } | 
| Ady Abraham | 0f4a1b1 | 2019-06-04 16:04:04 -0700 | [diff] [blame] | 432 |  | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 433 |         case DisplayEventReceiver::DISPLAY_EVENT_VSYNC: | 
 | 434 |             switch (connection->vsyncRequest) { | 
 | 435 |                 case VSyncRequest::None: | 
 | 436 |                     return false; | 
 | 437 |                 case VSyncRequest::Single: | 
 | 438 |                     connection->vsyncRequest = VSyncRequest::None; | 
 | 439 |                     return true; | 
 | 440 |                 case VSyncRequest::Periodic: | 
 | 441 |                     return true; | 
 | 442 |                 default: | 
 | 443 |                     return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0; | 
 | 444 |             } | 
 | 445 |  | 
 | 446 |         default: | 
 | 447 |             return false; | 
 | 448 |     } | 
 | 449 | } | 
 | 450 |  | 
 | 451 | void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event, | 
 | 452 |                                 const DisplayEventConsumers& consumers) { | 
 | 453 |     for (const auto& consumer : consumers) { | 
 | 454 |         switch (consumer->postEvent(event)) { | 
 | 455 |             case NO_ERROR: | 
 | 456 |                 break; | 
 | 457 |  | 
 | 458 |             case -EAGAIN: | 
 | 459 |                 // TODO: Try again if pipe is full. | 
 | 460 |                 ALOGW("Failed dispatching %s for %s", toString(event).c_str(), | 
 | 461 |                       toString(*consumer).c_str()); | 
 | 462 |                 break; | 
 | 463 |  | 
 | 464 |             default: | 
 | 465 |                 // Treat EPIPE and other errors as fatal. | 
 | 466 |                 removeDisplayEventConnectionLocked(consumer); | 
 | 467 |         } | 
 | 468 |     } | 
| Mathias Agopian | f6bbd44 | 2012-08-21 15:47:28 -0700 | [diff] [blame] | 469 | } | 
 | 470 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 471 | void EventThread::dump(std::string& result) const { | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 472 |     std::lock_guard<std::mutex> lock(mMutex); | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 473 |  | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 474 |     StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState)); | 
 | 475 |     if (mVSyncState) { | 
| Dominik Laskowski | dcb38bb | 2019-01-25 02:35:50 -0800 | [diff] [blame] | 476 |         StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n", | 
 | 477 |                       mVSyncState->displayId, mVSyncState->count, | 
 | 478 |                       mVSyncState->synthetic ? ", synthetic" : ""); | 
| Dominik Laskowski | 1eba020 | 2019-01-24 09:14:40 -0800 | [diff] [blame] | 479 |     } else { | 
 | 480 |         StringAppendF(&result, "none\n"); | 
 | 481 |     } | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 482 |  | 
 | 483 |     StringAppendF(&result, "  pending events (count=%zu):\n", mPendingEvents.size()); | 
 | 484 |     for (const auto& event : mPendingEvents) { | 
 | 485 |         StringAppendF(&result, "    %s\n", toString(event).c_str()); | 
| Mathias Agopian | e2c4f4e | 2012-04-10 18:25:31 -0700 | [diff] [blame] | 486 |     } | 
| Dominik Laskowski | d9e4de6 | 2019-01-21 14:23:01 -0800 | [diff] [blame] | 487 |  | 
 | 488 |     StringAppendF(&result, "  connections (count=%zu):\n", mDisplayEventConnections.size()); | 
 | 489 |     for (const auto& ptr : mDisplayEventConnections) { | 
 | 490 |         if (const auto connection = ptr.promote()) { | 
 | 491 |             StringAppendF(&result, "    %s\n", toString(*connection).c_str()); | 
 | 492 |         } | 
 | 493 |     } | 
| Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 494 | } | 
 | 495 |  | 
| Dominik Laskowski | 029cc12 | 2019-01-23 19:52:06 -0800 | [diff] [blame] | 496 | const char* EventThread::toCString(State state) { | 
 | 497 |     switch (state) { | 
 | 498 |         case State::Idle: | 
 | 499 |             return "Idle"; | 
 | 500 |         case State::Quit: | 
 | 501 |             return "Quit"; | 
 | 502 |         case State::SyntheticVSync: | 
 | 503 |             return "SyntheticVSync"; | 
 | 504 |         case State::VSync: | 
 | 505 |             return "VSync"; | 
 | 506 |     } | 
 | 507 | } | 
 | 508 |  | 
| Lloyd Pique | 0fcde1b | 2017-12-20 16:50:21 -0800 | [diff] [blame] | 509 | } // namespace impl | 
| Lloyd Pique | 46a46b3 | 2018-01-31 19:01:18 -0800 | [diff] [blame] | 510 | } // namespace android | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 511 |  | 
 | 512 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
 | 513 | #pragma clang diagnostic pop // ignored "-Wconversion" |