blob: 91ae087adbc0b9a98d2332327085f257b852c700 [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,
103 ResyncCallback resyncCallback)
104 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800105 mEventThread(eventThread),
106 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800107
108EventThreadConnection::~EventThreadConnection() {
109 // do nothing here -- clean-up will happen automatically
110 // when the main thread wakes up
111}
112
113void EventThreadConnection::onFirstRef() {
114 // NOTE: mEventThread doesn't hold a strong reference on us
115 mEventThread->registerDisplayEventConnection(this);
116}
117
118status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
119 outChannel->setReceiveFd(mChannel.moveReceiveFd());
120 return NO_ERROR;
121}
122
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800123status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
124 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800125 return NO_ERROR;
126}
127
128void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800129 ATRACE_NAME("requestNextVsync");
130 mEventThread->requestNextVsync(this, true);
131}
132
133void EventThreadConnection::requestNextVsyncForHWC() {
134 ATRACE_NAME("requestNextVsyncForHWC");
135 mEventThread->requestNextVsync(this, false);
Ana Krulec85c39af2018-12-26 17:29:57 -0800136}
137
138status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
139 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
140 return size < 0 ? status_t(size) : status_t(NO_ERROR);
141}
142
143// ---------------------------------------------------------------------------
144
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800145EventThread::~EventThread() = default;
146
147namespace impl {
148
Ana Krulec98b5b242018-08-10 15:03:23 -0700149EventThread::EventThread(std::unique_ptr<VSyncSource> src,
Ana Krulecfb772822018-11-30 10:44:07 +0100150 const InterceptVSyncsCallback& interceptVSyncsCallback,
151 const ResetIdleTimerCallback& resetIdleTimerCallback,
152 const char* threadName)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800153 : EventThread(nullptr, std::move(src), interceptVSyncsCallback, threadName) {
Ana Krulecfb772822018-11-30 10:44:07 +0100154 mResetIdleTimer = resetIdleTimerCallback;
155}
Ana Krulec98b5b242018-08-10 15:03:23 -0700156
Dominik Laskowskif654d572018-12-20 11:03:06 -0800157EventThread::EventThread(VSyncSource* src, InterceptVSyncsCallback interceptVSyncsCallback,
158 const char* threadName)
159 : EventThread(src, nullptr, interceptVSyncsCallback, threadName) {}
Ana Krulec98b5b242018-08-10 15:03:23 -0700160
161EventThread::EventThread(VSyncSource* src, std::unique_ptr<VSyncSource> uniqueSrc,
Ana Krulec98b5b242018-08-10 15:03:23 -0700162 InterceptVSyncsCallback interceptVSyncsCallback, const char* threadName)
Lloyd Pique24b0a482018-03-09 18:52:26 -0800163 : mVSyncSource(src),
Ana Krulec98b5b242018-08-10 15:03:23 -0700164 mVSyncSourceUnique(std::move(uniqueSrc)),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800165 mInterceptVSyncsCallback(interceptVSyncsCallback),
166 mThreadName(threadName) {
Ana Krulec98b5b242018-08-10 15:03:23 -0700167 if (src == nullptr) {
168 mVSyncSource = mVSyncSourceUnique.get();
169 }
Dominik Laskowski029cc122019-01-23 19:52:06 -0800170 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800171
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800172 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
173 std::unique_lock<std::mutex> lock(mMutex);
174 threadMain(lock);
175 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800176
177 pthread_setname_np(mThread.native_handle(), threadName);
178
179 pid_t tid = pthread_gettid_np(mThread.native_handle());
180
181 // Use SCHED_FIFO to minimize jitter
182 constexpr int EVENT_THREAD_PRIORITY = 2;
183 struct sched_param param = {0};
184 param.sched_priority = EVENT_THREAD_PRIORITY;
185 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
186 ALOGE("Couldn't set SCHED_FIFO for EventThread");
187 }
188
189 set_sched_policy(tid, SP_FOREGROUND);
190}
191
192EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800193 mVSyncSource->setCallback(nullptr);
194
Lloyd Pique46a46b32018-01-31 19:01:18 -0800195 {
196 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800197 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 mCondition.notify_all();
199 }
200 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700201}
202
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700203void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800204 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700205 mVSyncSource->setPhaseOffset(phaseOffset);
206}
207
Dominik Laskowskif654d572018-12-20 11:03:06 -0800208sp<EventThreadConnection> EventThread::createEventConnection(ResyncCallback resyncCallback) const {
209 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800210}
211
Ana Krulec85c39af2018-12-26 17:29:57 -0800212status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800213 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700214
215 // this should never happen
216 auto it = std::find(mDisplayEventConnections.cbegin(),
217 mDisplayEventConnections.cend(), connection);
218 if (it != mDisplayEventConnections.cend()) {
219 ALOGW("DisplayEventConnection %p already exists", connection.get());
220 mCondition.notify_all();
221 return ALREADY_EXISTS;
222 }
223
224 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800225 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800226 return NO_ERROR;
227}
228
Ana Krulec85c39af2018-12-26 17:29:57 -0800229void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700230 auto it = std::find(mDisplayEventConnections.cbegin(),
231 mDisplayEventConnections.cend(), connection);
232 if (it != mDisplayEventConnections.cend()) {
233 mDisplayEventConnections.erase(it);
234 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800235}
236
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800237void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
238 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
239 return;
240 }
241
242 std::lock_guard<std::mutex> lock(mMutex);
243
244 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
245 if (connection->vsyncRequest != request) {
246 connection->vsyncRequest = request;
247 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800248 }
249}
250
Ana Krulec7d1d6832018-12-27 11:10:09 -0800251void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection, bool reset) {
252 if (mResetIdleTimer && reset) {
253 ATRACE_NAME("resetIdleTimer");
Ana Krulecfb772822018-11-30 10:44:07 +0100254 mResetIdleTimer();
255 }
Dominik Laskowskif654d572018-12-20 11:03:06 -0800256
257 if (connection->resyncCallback) {
258 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800259 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000260
Ana Krulec7d1d6832018-12-27 11:10:09 -0800261 std::lock_guard<std::mutex> lock(mMutex);
262
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800263 if (connection->vsyncRequest == VSyncRequest::None) {
264 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800265 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800266 }
Mathias Agopian23748662011-12-05 14:33:34 -0800267}
268
Mathias Agopian22ffb112012-04-10 21:04:02 -0700269void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800270 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800271 if (!mVSyncState || mVSyncState->synthetic) {
272 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700273 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800274
275 mVSyncState->synthetic = true;
276 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700277}
278
279void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800280 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800281 if (!mVSyncState || !mVSyncState->synthetic) {
282 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700283 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800284
285 mVSyncState->synthetic = false;
286 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700287}
288
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700289void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800290 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800291
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800292 LOG_FATAL_IF(!mVSyncState);
293 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800294 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700295}
296
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800297void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800298 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700299
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800300 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700301 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700302}
303
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800304void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
305 DisplayEventConsumers consumers;
306
Dominik Laskowski029cc122019-01-23 19:52:06 -0800307 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800308 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700309
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800310 // Determine next event to dispatch.
311 if (!mPendingEvents.empty()) {
312 event = mPendingEvents.front();
313 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800314
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800315 switch (event->header.type) {
316 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
317 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800318 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800319 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800320 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800321 mVSyncState.reset();
322 }
323 break;
324
325 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
326 if (mInterceptVSyncsCallback) {
327 mInterceptVSyncsCallback(event->header.timestamp);
328 }
329 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800330 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700331 }
332
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800333 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700334
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800335 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700336 auto it = mDisplayEventConnections.begin();
337 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800338 if (const auto connection = it->promote()) {
339 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
340
341 if (event && shouldConsumeEvent(*event, connection)) {
342 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700343 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700344
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700345 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700346 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700347 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700348 }
349 }
350
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800351 if (!consumers.empty()) {
352 dispatchEvent(*event, consumers);
353 consumers.clear();
354 }
355
Dominik Laskowski029cc122019-01-23 19:52:06 -0800356 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800357 if (mVSyncState && vsyncRequested) {
358 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800359 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800360 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800361 nextState = State::Idle;
362 }
363
364 if (mState != nextState) {
365 if (mState == State::VSync) {
366 mVSyncSource->setVSyncEnabled(false);
367 } else if (nextState == State::VSync) {
368 mVSyncSource->setVSyncEnabled(true);
369 }
370
371 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700372 }
373
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800374 if (event) {
375 continue;
376 }
377
378 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800379 if (mState == State::Idle) {
380 mCondition.wait(lock);
381 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800382 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
383 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800384 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800385 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800386 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800387
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800388 LOG_FATAL_IF(!mVSyncState);
389 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800390 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800391 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700392 }
393 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800394 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800395}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700396
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800397bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
398 const sp<EventThreadConnection>& connection) const {
399 switch (event.header.type) {
400 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
401 return true;
402
403 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
404 switch (connection->vsyncRequest) {
405 case VSyncRequest::None:
406 return false;
407 case VSyncRequest::Single:
408 connection->vsyncRequest = VSyncRequest::None;
409 return true;
410 case VSyncRequest::Periodic:
411 return true;
412 default:
413 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
414 }
415
416 default:
417 return false;
418 }
419}
420
421void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
422 const DisplayEventConsumers& consumers) {
423 for (const auto& consumer : consumers) {
424 switch (consumer->postEvent(event)) {
425 case NO_ERROR:
426 break;
427
428 case -EAGAIN:
429 // TODO: Try again if pipe is full.
430 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
431 toString(*consumer).c_str());
432 break;
433
434 default:
435 // Treat EPIPE and other errors as fatal.
436 removeDisplayEventConnectionLocked(consumer);
437 }
438 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700439}
440
Yiwei Zhang5434a782018-12-05 18:06:32 -0800441void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800442 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800443
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800444 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
445 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800446 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
447 mVSyncState->displayId, mVSyncState->count,
448 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800449 } else {
450 StringAppendF(&result, "none\n");
451 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800452
453 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
454 for (const auto& event : mPendingEvents) {
455 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700456 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800457
458 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
459 for (const auto& ptr : mDisplayEventConnections) {
460 if (const auto connection = ptr.promote()) {
461 StringAppendF(&result, " %s\n", toString(*connection).c_str());
462 }
463 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800464}
465
Dominik Laskowski029cc122019-01-23 19:52:06 -0800466const char* EventThread::toCString(State state) {
467 switch (state) {
468 case State::Idle:
469 return "Idle";
470 case State::Quit:
471 return "Quit";
472 case State::SyntheticVSync:
473 return "SyntheticVSync";
474 case State::VSync:
475 return "VSync";
476 }
477}
478
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800479} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800480} // namespace android