blob: 78bf7c5c491fdb809f7648a9878b366220cbe1ba [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
Mathias Agopian841cde52012-03-01 15:44:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Lloyd Pique46a46b32018-01-31 19:01:18 -080019#include <pthread.h>
20#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080021#include <sys/types.h>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080022
Lloyd Pique46a46b32018-01-31 19:01:18 -080023#include <chrono>
24#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080025#include <optional>
26#include <type_traits>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080027
Yiwei Zhang5434a782018-12-05 18:06:32 -080028#include <android-base/stringprintf.h>
29
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070030#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080031#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070032
Mathias Agopiand0566bc2011-11-17 17:49:17 -080033#include <gui/DisplayEventReceiver.h>
34
35#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080036#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037
Mathias Agopiand0566bc2011-11-17 17:49:17 -080038#include "EventThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039
Lloyd Pique46a46b32018-01-31 19:01:18 -080040using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
Lloyd Pique46a46b32018-01-31 19:01:18 -080042namespace android {
43
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080044using base::StringAppendF;
45using base::StringPrintf;
46
47namespace {
48
49auto vsyncPeriod(VSyncRequest request) {
50 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
51}
52
53std::string toString(VSyncRequest request) {
54 switch (request) {
55 case VSyncRequest::None:
56 return "VSyncRequest::None";
57 case VSyncRequest::Single:
58 return "VSyncRequest::Single";
59 default:
60 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
61 }
62}
63
64std::string toString(const EventThreadConnection& connection) {
65 return StringPrintf("Connection{%p, %s}", &connection,
66 toString(connection.vsyncRequest).c_str());
67}
68
69std::string toString(const DisplayEventReceiver::Event& event) {
70 switch (event.header.type) {
71 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080072 return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}",
73 event.header.displayId,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080074 event.hotplug.connected ? "connected" : "disconnected");
75 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080076 return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
77 ", count=%u}",
78 event.header.displayId, event.vsync.count);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080079 default:
80 return "Event{}";
81 }
82}
83
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080084DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
85 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080086 DisplayEventReceiver::Event event;
87 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
88 event.hotplug.connected = connected;
89 return event;
90}
91
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080092DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
93 uint32_t count) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080094 DisplayEventReceiver::Event event;
95 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
96 event.vsync.count = count;
97 return event;
98}
99
Ady Abraham447052e2019-02-13 16:07:27 -0800100DisplayEventReceiver::Event makeConfigChanged(uint32_t displayId, int32_t configId) {
101 DisplayEventReceiver::Event event;
102 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
103 event.config.configId = configId;
104 return event;
105}
106
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800107} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800108
Dominik Laskowskif654d572018-12-20 11:03:06 -0800109EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800110 ResyncCallback resyncCallback,
111 ResetIdleTimerCallback resetIdleTimerCallback)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800112 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800113 resetIdleTimerCallback(std::move(resetIdleTimerCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800114 mEventThread(eventThread),
115 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800116
117EventThreadConnection::~EventThreadConnection() {
118 // do nothing here -- clean-up will happen automatically
119 // when the main thread wakes up
120}
121
122void EventThreadConnection::onFirstRef() {
123 // NOTE: mEventThread doesn't hold a strong reference on us
124 mEventThread->registerDisplayEventConnection(this);
125}
126
127status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
128 outChannel->setReceiveFd(mChannel.moveReceiveFd());
129 return NO_ERROR;
130}
131
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800132status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
133 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800134 return NO_ERROR;
135}
136
137void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800138 ATRACE_NAME("requestNextVsync");
139 mEventThread->requestNextVsync(this, true);
140}
141
142void EventThreadConnection::requestNextVsyncForHWC() {
143 ATRACE_NAME("requestNextVsyncForHWC");
144 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -0800145}
146
147status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
148 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
149 return size < 0 ? status_t(size) : status_t(NO_ERROR);
150}
151
152// ---------------------------------------------------------------------------
153
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800154EventThread::~EventThread() = default;
155
156namespace impl {
157
Ana Krulec98b5b242018-08-10 15:03:23 -0700158EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800159 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
160 : EventThread(nullptr, std::move(src), std::move(interceptVSyncsCallback), threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700161
Dominik Laskowskif654d572018-12-20 11:03:06 -0800162EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
163 const char* threadName)
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800164 : EventThread(src, nullptr, std::move(interceptVSyncsCallback), threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700165
166EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700167 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800168 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700169 mVSyncSourceUnique(std::move(uniqueSrc)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800170 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800171 mThreadName(threadName) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700172 if (src == nullptr) {
173 mVSyncSource = mVSyncSourceUnique.get();
174 }
Dominik Laskowski029cc122019-01-23 19:52:06 -0800175 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800176
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800177 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
178 std::unique_lock<std::mutex> lock(mMutex);
179 threadMain(lock);
180 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800181
182 pthread_setname_np(mThread.native_handle(), threadName);
183
184 pid_t tid = pthread_gettid_np(mThread.native_handle());
185
186 // Use SCHED_FIFO to minimize jitter
187 constexpr int EVENT_THREAD_PRIORITY = 2;
188 struct sched_param param = {0};
189 param.sched_priority = EVENT_THREAD_PRIORITY;
190 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
191 ALOGE("Couldn't set SCHED_FIFO for EventThread");
192 }
193
194 set_sched_policy(tid, SP_FOREGROUND);
195}
196
197EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800198 mVSyncSource->setCallback(nullptr);
199
Lloyd Pique46a46b32018-01-31 19:01:18 -0800200 {
201 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800202 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800203 mCondition.notify_all();
204 }
205 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700206}
207
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700208void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800209 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700210 mVSyncSource->setPhaseOffset(phaseOffset);
211}
212
Ady Abrahamb838aed2019-02-12 15:30:16 -0800213void EventThread::pauseVsyncCallback(bool pause) {
214 std::lock_guard<std::mutex> lock(mMutex);
215 ATRACE_INT("vsyncPaused", pause);
216 mVSyncSource->pauseVsyncCallback(pause);
217}
218
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800219sp<EventThreadConnection> EventThread::createEventConnection(
220 ResyncCallback resyncCallback, ResetIdleTimerCallback resetIdleTimerCallback) const {
221 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
222 std::move(resetIdleTimerCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800223}
224
Ana Krulec85c39af2018-12-26 17:29:57 -0800225status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800226 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700227
228 // this should never happen
229 auto it = std::find(mDisplayEventConnections.cbegin(),
230 mDisplayEventConnections.cend(), connection);
231 if (it != mDisplayEventConnections.cend()) {
232 ALOGW("DisplayEventConnection %p already exists", connection.get());
233 mCondition.notify_all();
234 return ALREADY_EXISTS;
235 }
236
237 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800238 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800239 return NO_ERROR;
240}
241
Ana Krulec85c39af2018-12-26 17:29:57 -0800242void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700243 auto it = std::find(mDisplayEventConnections.cbegin(),
244 mDisplayEventConnections.cend(), connection);
245 if (it != mDisplayEventConnections.cend()) {
246 mDisplayEventConnections.erase(it);
247 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800248}
249
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800250void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
251 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
252 return;
253 }
254
255 std::lock_guard<std::mutex> lock(mMutex);
256
257 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
258 if (connection->vsyncRequest != request) {
259 connection->vsyncRequest = request;
260 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800261 }
262}
263
Ana Krulec7d1d6832018-12-27 11:10:09 -0800264void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800265 if (connection->resetIdleTimerCallback && reset) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800266 ATRACE_NAME("resetIdleTimer");
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800267 connection->resetIdleTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100268 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800269
270 if (connection->resyncCallback) {
271 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800272 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000273
Ana Krulec7d1d6832018-12-27 11:10:09 -0800274 std::lock_guard<std::mutex> lock(mMutex);
275
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800276 if (connection->vsyncRequest == VSyncRequest::None) {
277 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800278 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800279 }
Mathias Agopian23748662011-12-05 14:33:34 -0800280}
281
Mathias Agopian22ffb112012-04-10 21:04:02 -0700282void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800283 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800284 if (!mVSyncState || mVSyncState->synthetic) {
285 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700286 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800287
288 mVSyncState->synthetic = true;
289 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700290}
291
292void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800293 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800294 if (!mVSyncState || !mVSyncState->synthetic) {
295 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700296 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800297
298 mVSyncState->synthetic = false;
299 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700300}
301
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700302void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800303 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800304
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800305 LOG_FATAL_IF(!mVSyncState);
306 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800307 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700308}
309
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800310void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800311 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700312
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800313 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700314 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700315}
316
Ady Abraham447052e2019-02-13 16:07:27 -0800317void EventThread::onConfigChanged(PhysicalDisplayId displayId, int32_t configId) {
318 std::lock_guard<std::mutex> lock(mMutex);
319
320 mPendingEvents.push_back(makeConfigChanged(displayId, configId));
321 mCondition.notify_all();
322}
323
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800324void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
325 DisplayEventConsumers consumers;
326
Dominik Laskowski029cc122019-01-23 19:52:06 -0800327 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800328 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700329
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800330 // Determine next event to dispatch.
331 if (!mPendingEvents.empty()) {
332 event = mPendingEvents.front();
333 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800334
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800335 switch (event->header.type) {
336 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
337 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800338 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800339 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800340 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800341 mVSyncState.reset();
342 }
343 break;
344
345 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
346 if (mInterceptVSyncsCallback) {
347 mInterceptVSyncsCallback(event->header.timestamp);
348 }
349 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800350 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700351 }
352
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800353 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700354
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800355 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700356 auto it = mDisplayEventConnections.begin();
357 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800358 if (const auto connection = it->promote()) {
359 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
360
361 if (event && shouldConsumeEvent(*event, connection)) {
362 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700363 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700364
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700365 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700366 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700367 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700368 }
369 }
370
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800371 if (!consumers.empty()) {
372 dispatchEvent(*event, consumers);
373 consumers.clear();
374 }
375
Dominik Laskowski029cc122019-01-23 19:52:06 -0800376 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800377 if (mVSyncState && vsyncRequested) {
378 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800379 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800380 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800381 nextState = State::Idle;
382 }
383
384 if (mState != nextState) {
385 if (mState == State::VSync) {
386 mVSyncSource->setVSyncEnabled(false);
387 } else if (nextState == State::VSync) {
388 mVSyncSource->setVSyncEnabled(true);
389 }
390
391 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700392 }
393
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800394 if (event) {
395 continue;
396 }
397
398 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800399 if (mState == State::Idle) {
400 mCondition.wait(lock);
401 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800402 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
403 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800404 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800405 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800406 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800407
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800408 LOG_FATAL_IF(!mVSyncState);
409 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800410 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800411 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700412 }
413 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800414 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800415}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700416
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800417bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
418 const sp<EventThreadConnection>& connection) const {
419 switch (event.header.type) {
420 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Ady Abraham447052e2019-02-13 16:07:27 -0800421 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800422 return true;
423
424 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
425 switch (connection->vsyncRequest) {
426 case VSyncRequest::None:
427 return false;
428 case VSyncRequest::Single:
429 connection->vsyncRequest = VSyncRequest::None;
430 return true;
431 case VSyncRequest::Periodic:
432 return true;
433 default:
434 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
435 }
436
437 default:
438 return false;
439 }
440}
441
442void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
443 const DisplayEventConsumers& consumers) {
444 for (const auto& consumer : consumers) {
445 switch (consumer->postEvent(event)) {
446 case NO_ERROR:
447 break;
448
449 case -EAGAIN:
450 // TODO: Try again if pipe is full.
451 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
452 toString(*consumer).c_str());
453 break;
454
455 default:
456 // Treat EPIPE and other errors as fatal.
457 removeDisplayEventConnectionLocked(consumer);
458 }
459 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700460}
461
Yiwei Zhang5434a782018-12-05 18:06:32 -0800462void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800463 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800464
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800465 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
466 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800467 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
468 mVSyncState->displayId, mVSyncState->count,
469 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800470 } else {
471 StringAppendF(&result, "none\n");
472 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800473
474 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
475 for (const auto& event : mPendingEvents) {
476 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700477 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800478
479 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
480 for (const auto& ptr : mDisplayEventConnections) {
481 if (const auto connection = ptr.promote()) {
482 StringAppendF(&result, " %s\n", toString(*connection).c_str());
483 }
484 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800485}
486
Dominik Laskowski029cc122019-01-23 19:52:06 -0800487const char* EventThread::toCString(State state) {
488 switch (state) {
489 case State::Idle:
490 return "Idle";
491 case State::Quit:
492 return "Quit";
493 case State::SyntheticVSync:
494 return "SyntheticVSync";
495 case State::VSync:
496 return "VSync";
497 }
498}
499
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800500} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800501} // namespace android