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