blob: 4b500f11805f8cbe64c833fccf777f9b631228a9 [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
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800100} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800101
Dominik Laskowskif654d572018-12-20 11:03:06 -0800102EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800103 ResyncCallback resyncCallback,
104 ResetIdleTimerCallback resetIdleTimerCallback)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800105 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800106 resetIdleTimerCallback(std::move(resetIdleTimerCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800107 mEventThread(eventThread),
108 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800109
110EventThreadConnection::~EventThreadConnection() {
111 // do nothing here -- clean-up will happen automatically
112 // when the main thread wakes up
113}
114
115void EventThreadConnection::onFirstRef() {
116 // NOTE: mEventThread doesn't hold a strong reference on us
117 mEventThread->registerDisplayEventConnection(this);
118}
119
120status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
121 outChannel->setReceiveFd(mChannel.moveReceiveFd());
122 return NO_ERROR;
123}
124
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800125status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
126 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800127 return NO_ERROR;
128}
129
130void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800131 ATRACE_NAME("requestNextVsync");
132 mEventThread->requestNextVsync(this, true);
133}
134
135void EventThreadConnection::requestNextVsyncForHWC() {
136 ATRACE_NAME("requestNextVsyncForHWC");
137 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -0800138}
139
140status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
141 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
142 return size < 0 ? status_t(size) : status_t(NO_ERROR);
143}
144
145// ---------------------------------------------------------------------------
146
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800147EventThread::~EventThread() = default;
148
149namespace impl {
150
Ana Krulec98b5b242018-08-10 15:03:23 -0700151EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800152 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
153 : EventThread(nullptr, std::move(src), std::move(interceptVSyncsCallback), threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700154
Dominik Laskowskif654d572018-12-20 11:03:06 -0800155EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
156 const char* threadName)
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800157 : EventThread(src, nullptr, std::move(interceptVSyncsCallback), threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700158
159EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700160 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800161 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700162 mVSyncSourceUnique(std::move(uniqueSrc)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800163 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800164 mThreadName(threadName) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700165 if (src == nullptr) {
166 mVSyncSource = mVSyncSourceUnique.get();
167 }
Dominik Laskowski029cc122019-01-23 19:52:06 -0800168 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800169
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800170 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
171 std::unique_lock<std::mutex> lock(mMutex);
172 threadMain(lock);
173 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800174
175 pthread_setname_np(mThread.native_handle(), threadName);
176
177 pid_t tid = pthread_gettid_np(mThread.native_handle());
178
179 // Use SCHED_FIFO to minimize jitter
180 constexpr int EVENT_THREAD_PRIORITY = 2;
181 struct sched_param param = {0};
182 param.sched_priority = EVENT_THREAD_PRIORITY;
183 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
184 ALOGE("Couldn't set SCHED_FIFO for EventThread");
185 }
186
187 set_sched_policy(tid, SP_FOREGROUND);
188}
189
190EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800191 mVSyncSource->setCallback(nullptr);
192
Lloyd Pique46a46b32018-01-31 19:01:18 -0800193 {
194 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800195 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800196 mCondition.notify_all();
197 }
198 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700199}
200
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700201void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800202 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700203 mVSyncSource->setPhaseOffset(phaseOffset);
204}
205
Ady Abrahamb838aed2019-02-12 15:30:16 -0800206void EventThread::pauseVsyncCallback(bool pause) {
207 std::lock_guard<std::mutex> lock(mMutex);
208 ATRACE_INT("vsyncPaused", pause);
209 mVSyncSource->pauseVsyncCallback(pause);
210}
211
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800212sp<EventThreadConnection> EventThread::createEventConnection(
213 ResyncCallback resyncCallback, ResetIdleTimerCallback resetIdleTimerCallback) const {
214 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
215 std::move(resetIdleTimerCallback));
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
Ana Krulec7d1d6832018-12-27 11:10:09 -0800257void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800258 if (connection->resetIdleTimerCallback && reset) {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800259 ATRACE_NAME("resetIdleTimer");
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800260 connection->resetIdleTimerCallback();
Ana Krulecfb772822018-11-30 10:44:07 +0100261 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800262
263 if (connection->resyncCallback) {
264 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800265 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000266
Ana Krulec7d1d6832018-12-27 11:10:09 -0800267 std::lock_guard<std::mutex> lock(mMutex);
268
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800269 if (connection->vsyncRequest == VSyncRequest::None) {
270 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800271 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800272 }
Mathias Agopian23748662011-12-05 14:33:34 -0800273}
274
Mathias Agopian22ffb112012-04-10 21:04:02 -0700275void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800276 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800277 if (!mVSyncState || mVSyncState->synthetic) {
278 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700279 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800280
281 mVSyncState->synthetic = true;
282 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700283}
284
285void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800286 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800287 if (!mVSyncState || !mVSyncState->synthetic) {
288 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700289 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800290
291 mVSyncState->synthetic = false;
292 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700293}
294
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700295void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800296 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800297
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800298 LOG_FATAL_IF(!mVSyncState);
299 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800300 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700301}
302
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800303void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800304 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700305
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800306 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700307 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700308}
309
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800310void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
311 DisplayEventConsumers consumers;
312
Dominik Laskowski029cc122019-01-23 19:52:06 -0800313 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800314 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700315
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800316 // Determine next event to dispatch.
317 if (!mPendingEvents.empty()) {
318 event = mPendingEvents.front();
319 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800320
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800321 switch (event->header.type) {
322 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
323 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800324 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800325 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800326 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800327 mVSyncState.reset();
328 }
329 break;
330
331 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
332 if (mInterceptVSyncsCallback) {
333 mInterceptVSyncsCallback(event->header.timestamp);
334 }
335 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800336 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700337 }
338
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800339 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700340
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800341 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700342 auto it = mDisplayEventConnections.begin();
343 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800344 if (const auto connection = it->promote()) {
345 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
346
347 if (event && shouldConsumeEvent(*event, connection)) {
348 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700349 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700350
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700351 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700352 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700353 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700354 }
355 }
356
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800357 if (!consumers.empty()) {
358 dispatchEvent(*event, consumers);
359 consumers.clear();
360 }
361
Dominik Laskowski029cc122019-01-23 19:52:06 -0800362 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800363 if (mVSyncState && vsyncRequested) {
364 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800365 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800366 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800367 nextState = State::Idle;
368 }
369
370 if (mState != nextState) {
371 if (mState == State::VSync) {
372 mVSyncSource->setVSyncEnabled(false);
373 } else if (nextState == State::VSync) {
374 mVSyncSource->setVSyncEnabled(true);
375 }
376
377 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700378 }
379
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800380 if (event) {
381 continue;
382 }
383
384 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800385 if (mState == State::Idle) {
386 mCondition.wait(lock);
387 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800388 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
389 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800390 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800391 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800392 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800393
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800394 LOG_FATAL_IF(!mVSyncState);
395 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800396 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800397 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700398 }
399 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800400 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800401}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700402
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800403bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
404 const sp<EventThreadConnection>& connection) const {
405 switch (event.header.type) {
406 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
407 return true;
408
409 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
410 switch (connection->vsyncRequest) {
411 case VSyncRequest::None:
412 return false;
413 case VSyncRequest::Single:
414 connection->vsyncRequest = VSyncRequest::None;
415 return true;
416 case VSyncRequest::Periodic:
417 return true;
418 default:
419 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
420 }
421
422 default:
423 return false;
424 }
425}
426
427void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
428 const DisplayEventConsumers& consumers) {
429 for (const auto& consumer : consumers) {
430 switch (consumer->postEvent(event)) {
431 case NO_ERROR:
432 break;
433
434 case -EAGAIN:
435 // TODO: Try again if pipe is full.
436 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
437 toString(*consumer).c_str());
438 break;
439
440 default:
441 // Treat EPIPE and other errors as fatal.
442 removeDisplayEventConnectionLocked(consumer);
443 }
444 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700445}
446
Yiwei Zhang5434a782018-12-05 18:06:32 -0800447void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800448 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800449
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800450 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
451 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800452 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
453 mVSyncState->displayId, mVSyncState->count,
454 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800455 } else {
456 StringAppendF(&result, "none\n");
457 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800458
459 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
460 for (const auto& event : mPendingEvents) {
461 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700462 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800463
464 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
465 for (const auto& ptr : mDisplayEventConnections) {
466 if (const auto connection = ptr.promote()) {
467 StringAppendF(&result, " %s\n", toString(*connection).c_str());
468 }
469 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800470}
471
Dominik Laskowski029cc122019-01-23 19:52:06 -0800472const char* EventThread::toCString(State state) {
473 switch (state) {
474 case State::Idle:
475 return "Idle";
476 case State::Quit:
477 return "Quit";
478 case State::SyntheticVSync:
479 return "SyntheticVSync";
480 case State::VSync:
481 return "VSync";
482 }
483}
484
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800485} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800486} // namespace android