blob: 5dedb6a1e7d8686b45705a1df75dfbb2b8e9cbdd [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:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080077 return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}",
78 event.header.displayId,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080079 event.hotplug.connected ? "connected" : "disconnected");
80 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080081 return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
Ady Abraham5facfb12020-04-22 15:18:31 -070082 ", count=%u, expectedVSyncTimestamp=%" PRId64 "}",
83 event.header.displayId, event.vsync.count,
84 event.vsync.expectedVSyncTimestamp);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070085 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 Laskowskid9e4de62019-01-21 14:23:01 -080089 default:
90 return "Event{}";
91 }
92}
93
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080094DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
95 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080096 DisplayEventReceiver::Event event;
97 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
98 event.hotplug.connected = connected;
99 return event;
100}
101
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800102DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
Ady Abraham5facfb12020-04-22 15:18:31 -0700103 uint32_t count, nsecs_t expectedVSyncTimestamp) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800104 DisplayEventReceiver::Event event;
105 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
106 event.vsync.count = count;
Ady Abraham5facfb12020-04-22 15:18:31 -0700107 event.vsync.expectedVSyncTimestamp = expectedVSyncTimestamp;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800108 return event;
109}
110
Ady Abraham2139f732019-11-13 18:56:40 -0800111DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700112 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800113 DisplayEventReceiver::Event event;
114 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
Ady Abraham2139f732019-11-13 18:56:40 -0800115 event.config.configId = configId.value();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700116 event.config.vsyncPeriod = vsyncPeriod;
Ady Abraham447052e2019-02-13 16:07:27 -0800117 return event;
118}
119
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800120} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800121
Dominik Laskowskif654d572018-12-20 11:03:06 -0800122EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700123 ResyncCallback resyncCallback,
124 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800125 : resyncCallback(std::move(resyncCallback)),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700126 mConfigChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800127 mEventThread(eventThread),
128 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800129
130EventThreadConnection::~EventThreadConnection() {
131 // do nothing here -- clean-up will happen automatically
132 // when the main thread wakes up
133}
134
135void EventThreadConnection::onFirstRef() {
136 // NOTE: mEventThread doesn't hold a strong reference on us
137 mEventThread->registerDisplayEventConnection(this);
138}
139
140status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
141 outChannel->setReceiveFd(mChannel.moveReceiveFd());
142 return NO_ERROR;
143}
144
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800145status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
146 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800147 return NO_ERROR;
148}
149
150void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800151 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700152 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800153}
154
Alec Mouri60aee1c2019-10-28 16:18:59 -0700155void EventThreadConnection::toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) {
156 ATRACE_NAME("enableConfigEvents");
157 mConfigChanged = configChangeFlag;
158
159 // In principle it's possible for rapidly toggling config events to drop an
160 // event here, but it's unlikely in practice.
161 if (configChangeFlag == ISurfaceComposer::eConfigChangedDispatch) {
162 mForcedConfigChangeDispatch = true;
163 mEventThread->requestLatestConfig();
164 }
165}
166
Ana Krulec85c39af2018-12-26 17:29:57 -0800167status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
168 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
169 return size < 0 ? status_t(size) : status_t(NO_ERROR);
170}
171
172// ---------------------------------------------------------------------------
173
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800174EventThread::~EventThread() = default;
175
176namespace impl {
177
Dominik Laskowski6505f792019-09-18 11:10:05 -0700178EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
179 InterceptVSyncsCallback interceptVSyncsCallback)
180 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800181 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700182 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800183 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800184
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800185 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
186 std::unique_lock<std::mutex> lock(mMutex);
187 threadMain(lock);
188 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189
Dominik Laskowski6505f792019-09-18 11:10:05 -0700190 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800191
192 pid_t tid = pthread_gettid_np(mThread.native_handle());
193
194 // Use SCHED_FIFO to minimize jitter
195 constexpr int EVENT_THREAD_PRIORITY = 2;
196 struct sched_param param = {0};
197 param.sched_priority = EVENT_THREAD_PRIORITY;
198 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
199 ALOGE("Couldn't set SCHED_FIFO for EventThread");
200 }
201
202 set_sched_policy(tid, SP_FOREGROUND);
203}
204
205EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800206 mVSyncSource->setCallback(nullptr);
207
Lloyd Pique46a46b32018-01-31 19:01:18 -0800208 {
209 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800210 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211 mCondition.notify_all();
212 }
213 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700214}
215
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700216void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800217 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700218 mVSyncSource->setPhaseOffset(phaseOffset);
219}
220
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700221sp<EventThreadConnection> EventThread::createEventConnection(
222 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
223 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
224 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800225}
226
Ana Krulec85c39af2018-12-26 17:29:57 -0800227status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800228 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700229
230 // this should never happen
231 auto it = std::find(mDisplayEventConnections.cbegin(),
232 mDisplayEventConnections.cend(), connection);
233 if (it != mDisplayEventConnections.cend()) {
234 ALOGW("DisplayEventConnection %p already exists", connection.get());
235 mCondition.notify_all();
236 return ALREADY_EXISTS;
237 }
238
239 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800240 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800241 return NO_ERROR;
242}
243
Ana Krulec85c39af2018-12-26 17:29:57 -0800244void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700245 auto it = std::find(mDisplayEventConnections.cbegin(),
246 mDisplayEventConnections.cend(), connection);
247 if (it != mDisplayEventConnections.cend()) {
248 mDisplayEventConnections.erase(it);
249 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800250}
251
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800252void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
253 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
254 return;
255 }
256
257 std::lock_guard<std::mutex> lock(mMutex);
258
259 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
260 if (connection->vsyncRequest != request) {
261 connection->vsyncRequest = request;
262 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800263 }
264}
265
Ady Abraham8532d012019-05-08 14:50:56 -0700266void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800267 if (connection->resyncCallback) {
268 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800269 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000270
Ana Krulec7d1d6832018-12-27 11:10:09 -0800271 std::lock_guard<std::mutex> lock(mMutex);
272
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800273 if (connection->vsyncRequest == VSyncRequest::None) {
274 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800275 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800276 }
Mathias Agopian23748662011-12-05 14:33:34 -0800277}
278
Alec Mouri60aee1c2019-10-28 16:18:59 -0700279void EventThread::requestLatestConfig() {
280 std::lock_guard<std::mutex> lock(mMutex);
281 auto pendingConfigChange =
282 std::find_if(std::begin(mPendingEvents), std::end(mPendingEvents),
283 [&](const DisplayEventReceiver::Event& event) {
284 return event.header.type ==
285 DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED;
286 });
287
288 // If we didn't find a pending config change event, then push out the
289 // latest one we've ever seen.
290 if (pendingConfigChange == std::end(mPendingEvents)) {
291 mPendingEvents.push_back(mLastConfigChangeEvent);
292 }
293
294 mCondition.notify_all();
295}
296
Mathias Agopian22ffb112012-04-10 21:04:02 -0700297void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800298 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800299 if (!mVSyncState || mVSyncState->synthetic) {
300 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700301 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800302
303 mVSyncState->synthetic = true;
304 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700305}
306
307void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800308 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800309 if (!mVSyncState || !mVSyncState->synthetic) {
310 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700311 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800312
313 mVSyncState->synthetic = false;
314 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700315}
316
Ady Abraham5facfb12020-04-22 15:18:31 -0700317void EventThread::onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800318 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800319
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800320 LOG_FATAL_IF(!mVSyncState);
Ady Abraham5facfb12020-04-22 15:18:31 -0700321 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count,
322 expectedVSyncTimestamp));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800323 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700324}
325
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800326void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800327 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700328
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800329 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700330 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700331}
332
Alec Mouri60aee1c2019-10-28 16:18:59 -0700333void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
334 nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800335 std::lock_guard<std::mutex> lock(mMutex);
336
Alec Mouri60aee1c2019-10-28 16:18:59 -0700337 mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod));
Ady Abraham447052e2019-02-13 16:07:27 -0800338 mCondition.notify_all();
339}
340
Alec Mouri717bcb62020-02-10 17:07:19 -0800341size_t EventThread::getEventThreadConnectionCount() {
342 std::lock_guard<std::mutex> lock(mMutex);
343 return mDisplayEventConnections.size();
344}
345
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800346void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
347 DisplayEventConsumers consumers;
348
Dominik Laskowski029cc122019-01-23 19:52:06 -0800349 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800350 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700351
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800352 // Determine next event to dispatch.
353 if (!mPendingEvents.empty()) {
354 event = mPendingEvents.front();
355 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800356
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357 switch (event->header.type) {
358 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
359 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800360 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800361 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800362 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800363 mVSyncState.reset();
364 }
365 break;
366
367 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
368 if (mInterceptVSyncsCallback) {
369 mInterceptVSyncsCallback(event->header.timestamp);
370 }
371 break;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700372 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
373 mLastConfigChangeEvent = *event;
374 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800375 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700376 }
377
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800378 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700379
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800380 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700381 auto it = mDisplayEventConnections.begin();
382 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800383 if (const auto connection = it->promote()) {
384 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
385
386 if (event && shouldConsumeEvent(*event, connection)) {
387 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700388 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700389
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700390 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700391 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700392 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700393 }
394 }
395
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800396 if (!consumers.empty()) {
397 dispatchEvent(*event, consumers);
398 consumers.clear();
399 }
400
Dominik Laskowski029cc122019-01-23 19:52:06 -0800401 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800402 if (mVSyncState && vsyncRequested) {
403 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800404 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800405 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800406 nextState = State::Idle;
407 }
408
409 if (mState != nextState) {
410 if (mState == State::VSync) {
411 mVSyncSource->setVSyncEnabled(false);
412 } else if (nextState == State::VSync) {
413 mVSyncSource->setVSyncEnabled(true);
414 }
415
416 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700417 }
418
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800419 if (event) {
420 continue;
421 }
422
423 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800424 if (mState == State::Idle) {
425 mCondition.wait(lock);
426 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800427 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
428 // display is off, keep feeding clients at 60 Hz.
Ady Abraham5facfb12020-04-22 15:18:31 -0700429 const std::chrono::nanoseconds timeout =
430 mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800431 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700432 if (mState == State::VSync) {
433 ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName);
434 std::string debugInfo = "VsyncSource debug info:\n";
435 mVSyncSource->dump(debugInfo);
Ady Abraham75398722020-04-07 14:08:45 -0700436 // Log the debug info line-by-line to avoid logcat overflow
437 auto pos = debugInfo.find('\n');
438 while (pos != std::string::npos) {
439 ALOGW("%s", debugInfo.substr(0, pos).c_str());
440 debugInfo = debugInfo.substr(pos + 1);
441 pos = debugInfo.find('\n');
442 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700443 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800444
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800445 LOG_FATAL_IF(!mVSyncState);
Ady Abraham5facfb12020-04-22 15:18:31 -0700446 const auto now = systemTime(SYSTEM_TIME_MONOTONIC);
447 const auto expectedVSyncTime = now + timeout.count();
448 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now,
449 ++mVSyncState->count, expectedVSyncTime));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700450 }
451 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800452 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800453}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700454
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800455bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
456 const sp<EventThreadConnection>& connection) const {
457 switch (event.header.type) {
458 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
459 return true;
460
Alec Mouri60aee1c2019-10-28 16:18:59 -0700461 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: {
462 const bool forcedDispatch = connection->mForcedConfigChangeDispatch.exchange(false);
463 return forcedDispatch ||
464 connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch;
465 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700466
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800467 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
468 switch (connection->vsyncRequest) {
469 case VSyncRequest::None:
470 return false;
471 case VSyncRequest::Single:
472 connection->vsyncRequest = VSyncRequest::None;
473 return true;
474 case VSyncRequest::Periodic:
475 return true;
476 default:
477 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
478 }
479
480 default:
481 return false;
482 }
483}
484
485void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
486 const DisplayEventConsumers& consumers) {
487 for (const auto& consumer : consumers) {
488 switch (consumer->postEvent(event)) {
489 case NO_ERROR:
490 break;
491
492 case -EAGAIN:
493 // TODO: Try again if pipe is full.
494 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
495 toString(*consumer).c_str());
496 break;
497
498 default:
499 // Treat EPIPE and other errors as fatal.
500 removeDisplayEventConnectionLocked(consumer);
501 }
502 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700503}
504
Yiwei Zhang5434a782018-12-05 18:06:32 -0800505void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800506 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800507
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800508 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
509 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800510 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
511 mVSyncState->displayId, mVSyncState->count,
512 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800513 } else {
514 StringAppendF(&result, "none\n");
515 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800516
517 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
518 for (const auto& event : mPendingEvents) {
519 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700520 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800521
522 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
523 for (const auto& ptr : mDisplayEventConnections) {
524 if (const auto connection = ptr.promote()) {
525 StringAppendF(&result, " %s\n", toString(*connection).c_str());
526 }
527 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800528}
529
Dominik Laskowski029cc122019-01-23 19:52:06 -0800530const char* EventThread::toCString(State state) {
531 switch (state) {
532 case State::Idle:
533 return "Idle";
534 case State::Quit:
535 return "Quit";
536 case State::SyntheticVSync:
537 return "SyntheticVSync";
538 case State::VSync:
539 return "VSync";
540 }
541}
542
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800543} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800544} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800545
546// TODO(b/129481165): remove the #pragma below and fix conversion issues
547#pragma clang diagnostic pop // ignored "-Wconversion"