blob: 77b2f4273e7c6345023a54ba285c59a4ef51b5ed [file] [log] [blame]
Mathias Agopiand0566bc2011-11-17 17:49:17 -08001/*
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 Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopian841cde52012-03-01 15:44:37 -080021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique46a46b32018-01-31 19:01:18 -080023#include <pthread.h>
24#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <sys/types.h>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080026
Lloyd Pique46a46b32018-01-31 19:01:18 -080027#include <chrono>
28#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080029#include <optional>
30#include <type_traits>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031
Yiwei Zhang5434a782018-12-05 18:06:32 -080032#include <android-base/stringprintf.h>
33
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070034#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080035#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/DisplayEventReceiver.h>
38
39#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080040#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042#include "EventThread.h"
Ady Abraham2139f732019-11-13 18:56:40 -080043#include "HwcStrongTypes.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
Lloyd Pique46a46b32018-01-31 19:01:18 -080045using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046
Lloyd Pique46a46b32018-01-31 19:01:18 -080047namespace android {
48
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080049using base::StringAppendF;
50using base::StringPrintf;
51
52namespace {
53
54auto vsyncPeriod(VSyncRequest request) {
55 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
56}
57
58std::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
69std::string toString(const EventThreadConnection& connection) {
70 return StringPrintf("Connection{%p, %s}", &connection,
71 toString(connection.vsyncRequest).c_str());
72}
73
74std::string toString(const DisplayEventReceiver::Event& event) {
75 switch (event.header.type) {
76 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Marin Shalamanova524a092020-07-27 21:39:55 +020077 return StringPrintf("Hotplug{displayId=%s, %s}",
78 to_string(event.header.displayId).c_str(),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080079 event.hotplug.connected ? "connected" : "disconnected");
80 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Marin Shalamanova524a092020-07-27 21:39:55 +020081 return StringPrintf("VSync{displayId=%s, count=%u, expectedVSyncTimestamp=%" PRId64 "}",
82 to_string(event.header.displayId).c_str(), event.vsync.count,
Ady Abraham5facfb12020-04-22 15:18:31 -070083 event.vsync.expectedVSyncTimestamp);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070084 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
Marin Shalamanova524a092020-07-27 21:39:55 +020085 return StringPrintf("ConfigChanged{displayId=%s, configId=%u}",
86 to_string(event.header.displayId).c_str(), event.config.configId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080087 default:
88 return "Event{}";
89 }
90}
91
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080092DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
93 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080094 DisplayEventReceiver::Event event;
95 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
96 event.hotplug.connected = connected;
97 return event;
98}
99
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800100DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
Ady Abraham8cb21882020-08-26 18:22:05 -0700101 uint32_t count, nsecs_t expectedVSyncTimestamp,
Ady Abraham74e17562020-08-24 18:18:19 -0700102 nsecs_t deadlineTimestamp, int64_t vsyncId) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800103 DisplayEventReceiver::Event event;
104 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
105 event.vsync.count = count;
Ady Abraham5facfb12020-04-22 15:18:31 -0700106 event.vsync.expectedVSyncTimestamp = expectedVSyncTimestamp;
Ady Abraham8cb21882020-08-26 18:22:05 -0700107 event.vsync.deadlineTimestamp = deadlineTimestamp;
Ady Abraham74e17562020-08-24 18:18:19 -0700108 event.vsync.vsyncId = vsyncId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800109 return event;
110}
111
Ady Abraham2139f732019-11-13 18:56:40 -0800112DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700113 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800114 DisplayEventReceiver::Event event;
115 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
Ady Abraham2139f732019-11-13 18:56:40 -0800116 event.config.configId = configId.value();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700117 event.config.vsyncPeriod = vsyncPeriod;
Ady Abraham447052e2019-02-13 16:07:27 -0800118 return event;
119}
120
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800121} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800122
Dominik Laskowskif654d572018-12-20 11:03:06 -0800123EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700124 ResyncCallback resyncCallback,
125 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800126 : resyncCallback(std::move(resyncCallback)),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700127 mConfigChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800128 mEventThread(eventThread),
129 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800130
131EventThreadConnection::~EventThreadConnection() {
132 // do nothing here -- clean-up will happen automatically
133 // when the main thread wakes up
134}
135
136void EventThreadConnection::onFirstRef() {
137 // NOTE: mEventThread doesn't hold a strong reference on us
138 mEventThread->registerDisplayEventConnection(this);
139}
140
141status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
142 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Alec Mouri967d5d72020-08-05 12:50:03 -0700143 outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd())));
Ana Krulec85c39af2018-12-26 17:29:57 -0800144 return NO_ERROR;
145}
146
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800147status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
148 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800149 return NO_ERROR;
150}
151
152void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800153 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700154 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800155}
156
157status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
158 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
159 return size < 0 ? status_t(size) : status_t(NO_ERROR);
160}
161
162// ---------------------------------------------------------------------------
163
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800164EventThread::~EventThread() = default;
165
166namespace impl {
167
Dominik Laskowski6505f792019-09-18 11:10:05 -0700168EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
169 InterceptVSyncsCallback interceptVSyncsCallback)
170 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800171 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700172 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800173 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800174
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800175 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
176 std::unique_lock<std::mutex> lock(mMutex);
177 threadMain(lock);
178 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800179
Dominik Laskowski6505f792019-09-18 11:10:05 -0700180 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800181
182 pid_t tid = pthread_gettid_np(mThread.native_handle());
183
184 // Use SCHED_FIFO to minimize jitter
185 constexpr int EVENT_THREAD_PRIORITY = 2;
186 struct sched_param param = {0};
187 param.sched_priority = EVENT_THREAD_PRIORITY;
188 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
189 ALOGE("Couldn't set SCHED_FIFO for EventThread");
190 }
191
192 set_sched_policy(tid, SP_FOREGROUND);
193}
194
195EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800196 mVSyncSource->setCallback(nullptr);
197
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 {
199 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800200 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800201 mCondition.notify_all();
202 }
203 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700204}
205
Ady Abraham9c53ee72020-07-22 21:16:18 -0700206void EventThread::setDuration(std::chrono::nanoseconds workDuration,
207 std::chrono::nanoseconds readyDuration) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800208 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700209 mVSyncSource->setDuration(workDuration, readyDuration);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700210}
211
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700212sp<EventThreadConnection> EventThread::createEventConnection(
213 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
214 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
215 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800216}
217
Ana Krulec85c39af2018-12-26 17:29:57 -0800218status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800219 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700220
221 // this should never happen
222 auto it = std::find(mDisplayEventConnections.cbegin(),
223 mDisplayEventConnections.cend(), connection);
224 if (it != mDisplayEventConnections.cend()) {
225 ALOGW("DisplayEventConnection %p already exists", connection.get());
226 mCondition.notify_all();
227 return ALREADY_EXISTS;
228 }
229
230 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800231 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800232 return NO_ERROR;
233}
234
Ana Krulec85c39af2018-12-26 17:29:57 -0800235void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700236 auto it = std::find(mDisplayEventConnections.cbegin(),
237 mDisplayEventConnections.cend(), connection);
238 if (it != mDisplayEventConnections.cend()) {
239 mDisplayEventConnections.erase(it);
240 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800241}
242
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800243void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
244 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
245 return;
246 }
247
248 std::lock_guard<std::mutex> lock(mMutex);
249
250 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
251 if (connection->vsyncRequest != request) {
252 connection->vsyncRequest = request;
253 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800254 }
255}
256
Ady Abraham8532d012019-05-08 14:50:56 -0700257void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800258 if (connection->resyncCallback) {
259 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800260 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000261
Ana Krulec7d1d6832018-12-27 11:10:09 -0800262 std::lock_guard<std::mutex> lock(mMutex);
263
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800264 if (connection->vsyncRequest == VSyncRequest::None) {
265 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800266 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800267 }
Mathias Agopian23748662011-12-05 14:33:34 -0800268}
269
Mathias Agopian22ffb112012-04-10 21:04:02 -0700270void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800271 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800272 if (!mVSyncState || mVSyncState->synthetic) {
273 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700274 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800275
276 mVSyncState->synthetic = true;
277 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700278}
279
280void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800281 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800282 if (!mVSyncState || !mVSyncState->synthetic) {
283 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700284 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800285
286 mVSyncState->synthetic = false;
287 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700288}
289
Ady Abraham9c53ee72020-07-22 21:16:18 -0700290void EventThread::onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
Ady Abraham8cb21882020-08-26 18:22:05 -0700291 nsecs_t deadlineTimestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800292 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800293
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800294 LOG_FATAL_IF(!mVSyncState);
Ady Abraham74e17562020-08-24 18:18:19 -0700295 // TODO(b/162890590): use TokenManager to populate vsyncId
Ady Abraham5facfb12020-04-22 15:18:31 -0700296 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count,
Ady Abraham74e17562020-08-24 18:18:19 -0700297 expectedVSyncTimestamp, deadlineTimestamp, /*vsyncId=*/0));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800298 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700299}
300
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800301void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800302 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700303
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800304 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700305 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700306}
307
Alec Mouri60aee1c2019-10-28 16:18:59 -0700308void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
309 nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800310 std::lock_guard<std::mutex> lock(mMutex);
311
Alec Mouri60aee1c2019-10-28 16:18:59 -0700312 mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod));
Ady Abraham447052e2019-02-13 16:07:27 -0800313 mCondition.notify_all();
314}
315
Alec Mouri717bcb62020-02-10 17:07:19 -0800316size_t EventThread::getEventThreadConnectionCount() {
317 std::lock_guard<std::mutex> lock(mMutex);
318 return mDisplayEventConnections.size();
319}
320
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800321void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
322 DisplayEventConsumers consumers;
323
Dominik Laskowski029cc122019-01-23 19:52:06 -0800324 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800325 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700326
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800327 // Determine next event to dispatch.
328 if (!mPendingEvents.empty()) {
329 event = mPendingEvents.front();
330 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800331
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800332 switch (event->header.type) {
333 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
334 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800335 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800336 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800337 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800338 mVSyncState.reset();
339 }
340 break;
341
342 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
343 if (mInterceptVSyncsCallback) {
344 mInterceptVSyncsCallback(event->header.timestamp);
345 }
346 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800347 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700348 }
349
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800350 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700351
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800352 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700353 auto it = mDisplayEventConnections.begin();
354 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800355 if (const auto connection = it->promote()) {
356 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
357
358 if (event && shouldConsumeEvent(*event, connection)) {
359 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700360 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700361
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700362 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700363 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700364 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700365 }
366 }
367
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800368 if (!consumers.empty()) {
369 dispatchEvent(*event, consumers);
370 consumers.clear();
371 }
372
Dominik Laskowski029cc122019-01-23 19:52:06 -0800373 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800374 if (mVSyncState && vsyncRequested) {
375 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800376 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800377 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800378 nextState = State::Idle;
379 }
380
381 if (mState != nextState) {
382 if (mState == State::VSync) {
383 mVSyncSource->setVSyncEnabled(false);
384 } else if (nextState == State::VSync) {
385 mVSyncSource->setVSyncEnabled(true);
386 }
387
388 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700389 }
390
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800391 if (event) {
392 continue;
393 }
394
395 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800396 if (mState == State::Idle) {
397 mCondition.wait(lock);
398 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800399 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
400 // display is off, keep feeding clients at 60 Hz.
Ady Abraham5facfb12020-04-22 15:18:31 -0700401 const std::chrono::nanoseconds timeout =
402 mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800403 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700404 if (mState == State::VSync) {
405 ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName);
406 std::string debugInfo = "VsyncSource debug info:\n";
407 mVSyncSource->dump(debugInfo);
Ady Abraham75398722020-04-07 14:08:45 -0700408 // Log the debug info line-by-line to avoid logcat overflow
409 auto pos = debugInfo.find('\n');
410 while (pos != std::string::npos) {
411 ALOGW("%s", debugInfo.substr(0, pos).c_str());
412 debugInfo = debugInfo.substr(pos + 1);
413 pos = debugInfo.find('\n');
414 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700415 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800416
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800417 LOG_FATAL_IF(!mVSyncState);
Ady Abraham5facfb12020-04-22 15:18:31 -0700418 const auto now = systemTime(SYSTEM_TIME_MONOTONIC);
Ady Abraham8cb21882020-08-26 18:22:05 -0700419 const auto deadlineTimestamp = now + timeout.count();
420 const auto expectedVSyncTime = deadlineTimestamp + timeout.count();
Ady Abraham74e17562020-08-24 18:18:19 -0700421 // TODO(b/162890590): use TokenManager to populate vsyncId
Ady Abraham5facfb12020-04-22 15:18:31 -0700422 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now,
Ady Abraham8cb21882020-08-26 18:22:05 -0700423 ++mVSyncState->count, expectedVSyncTime,
Ady Abraham74e17562020-08-24 18:18:19 -0700424 deadlineTimestamp, /*vsyncId=*/0));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700425 }
426 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800427 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800428}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700429
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800430bool 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 Mouri60aee1c2019-10-28 16:18:59 -0700436 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: {
Alec Mouri967d5d72020-08-05 12:50:03 -0700437 return connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700438 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700439
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800440 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
441 switch (connection->vsyncRequest) {
442 case VSyncRequest::None:
443 return false;
444 case VSyncRequest::Single:
445 connection->vsyncRequest = VSyncRequest::None;
446 return true;
447 case VSyncRequest::Periodic:
448 return true;
449 default:
450 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
451 }
452
453 default:
454 return false;
455 }
456}
457
458void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
459 const DisplayEventConsumers& consumers) {
460 for (const auto& consumer : consumers) {
461 switch (consumer->postEvent(event)) {
462 case NO_ERROR:
463 break;
464
465 case -EAGAIN:
466 // TODO: Try again if pipe is full.
467 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
468 toString(*consumer).c_str());
469 break;
470
471 default:
472 // Treat EPIPE and other errors as fatal.
473 removeDisplayEventConnectionLocked(consumer);
474 }
475 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700476}
477
Yiwei Zhang5434a782018-12-05 18:06:32 -0800478void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800479 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800480
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800481 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
482 if (mVSyncState) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200483 StringAppendF(&result, "{displayId=%s, count=%u%s}\n",
484 to_string(mVSyncState->displayId).c_str(), mVSyncState->count,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800485 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800486 } else {
487 StringAppendF(&result, "none\n");
488 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800489
490 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
491 for (const auto& event : mPendingEvents) {
492 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700493 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800494
495 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
496 for (const auto& ptr : mDisplayEventConnections) {
497 if (const auto connection = ptr.promote()) {
498 StringAppendF(&result, " %s\n", toString(*connection).c_str());
499 }
500 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800501}
502
Dominik Laskowski029cc122019-01-23 19:52:06 -0800503const char* EventThread::toCString(State state) {
504 switch (state) {
505 case State::Idle:
506 return "Idle";
507 case State::Quit:
508 return "Quit";
509 case State::SyntheticVSync:
510 return "SyntheticVSync";
511 case State::VSync:
512 return "VSync";
513 }
514}
515
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800516} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800517} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800518
519// TODO(b/129481165): remove the #pragma below and fix conversion issues
520#pragma clang diagnostic pop // ignored "-Wconversion"