blob: 05bad4ddd8896deacfb1f1aff7c97085d1d35aae [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 Abrahamaf0ec272019-03-28 11:38:31 -0700100DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId, int32_t configId) {
Ady Abraham447052e2019-02-13 16:07:27 -0800101 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,
Ady Abraham8532d012019-05-08 14:50:56 -0700110 ResyncCallback resyncCallback)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800111 : resyncCallback(std::move(resyncCallback)),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800112 mEventThread(eventThread),
113 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800114
115EventThreadConnection::~EventThreadConnection() {
116 // do nothing here -- clean-up will happen automatically
117 // when the main thread wakes up
118}
119
120void EventThreadConnection::onFirstRef() {
121 // NOTE: mEventThread doesn't hold a strong reference on us
122 mEventThread->registerDisplayEventConnection(this);
123}
124
125status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
126 outChannel->setReceiveFd(mChannel.moveReceiveFd());
127 return NO_ERROR;
128}
129
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800130status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
131 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800132 return NO_ERROR;
133}
134
135void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800136 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700137 mEventThread->requestNextVsync(this);
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 Abraham8532d012019-05-08 14:50:56 -0700206sp<EventThreadConnection> EventThread::createEventConnection(ResyncCallback resyncCallback) const {
207 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback));
Mathias Agopian8aedd472012-01-24 16:39:14 -0800208}
209
Ana Krulec85c39af2018-12-26 17:29:57 -0800210status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700212
213 // this should never happen
214 auto it = std::find(mDisplayEventConnections.cbegin(),
215 mDisplayEventConnections.cend(), connection);
216 if (it != mDisplayEventConnections.cend()) {
217 ALOGW("DisplayEventConnection %p already exists", connection.get());
218 mCondition.notify_all();
219 return ALREADY_EXISTS;
220 }
221
222 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800223 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800224 return NO_ERROR;
225}
226
Ana Krulec85c39af2018-12-26 17:29:57 -0800227void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700228 auto it = std::find(mDisplayEventConnections.cbegin(),
229 mDisplayEventConnections.cend(), connection);
230 if (it != mDisplayEventConnections.cend()) {
231 mDisplayEventConnections.erase(it);
232 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800233}
234
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800235void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
236 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
237 return;
238 }
239
240 std::lock_guard<std::mutex> lock(mMutex);
241
242 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
243 if (connection->vsyncRequest != request) {
244 connection->vsyncRequest = request;
245 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800246 }
247}
248
Ady Abraham8532d012019-05-08 14:50:56 -0700249void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800250 if (connection->resyncCallback) {
251 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800252 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000253
Ana Krulec7d1d6832018-12-27 11:10:09 -0800254 std::lock_guard<std::mutex> lock(mMutex);
255
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800256 if (connection->vsyncRequest == VSyncRequest::None) {
257 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800258 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800259 }
Mathias Agopian23748662011-12-05 14:33:34 -0800260}
261
Mathias Agopian22ffb112012-04-10 21:04:02 -0700262void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800263 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800264 if (!mVSyncState || mVSyncState->synthetic) {
265 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700266 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800267
268 mVSyncState->synthetic = true;
269 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700270}
271
272void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800273 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800274 if (!mVSyncState || !mVSyncState->synthetic) {
275 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700276 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800277
278 mVSyncState->synthetic = false;
279 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700280}
281
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700282void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800283 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800284
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800285 LOG_FATAL_IF(!mVSyncState);
286 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800287 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700288}
289
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800290void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800291 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700292
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800293 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700294 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700295}
296
Ady Abraham447052e2019-02-13 16:07:27 -0800297void EventThread::onConfigChanged(PhysicalDisplayId displayId, int32_t configId) {
298 std::lock_guard<std::mutex> lock(mMutex);
299
300 mPendingEvents.push_back(makeConfigChanged(displayId, configId));
301 mCondition.notify_all();
302}
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:
Ady Abraham447052e2019-02-13 16:07:27 -0800401 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800402 return true;
403
404 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
405 switch (connection->vsyncRequest) {
406 case VSyncRequest::None:
407 return false;
408 case VSyncRequest::Single:
409 connection->vsyncRequest = VSyncRequest::None;
410 return true;
411 case VSyncRequest::Periodic:
412 return true;
413 default:
414 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
415 }
416
417 default:
418 return false;
419 }
420}
421
422void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
423 const DisplayEventConsumers& consumers) {
424 for (const auto& consumer : consumers) {
425 switch (consumer->postEvent(event)) {
426 case NO_ERROR:
427 break;
428
429 case -EAGAIN:
430 // TODO: Try again if pipe is full.
431 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
432 toString(*consumer).c_str());
433 break;
434
435 default:
436 // Treat EPIPE and other errors as fatal.
437 removeDisplayEventConnectionLocked(consumer);
438 }
439 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700440}
441
Yiwei Zhang5434a782018-12-05 18:06:32 -0800442void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800443 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800444
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800445 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
446 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800447 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
448 mVSyncState->displayId, mVSyncState->count,
449 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800450 } else {
451 StringAppendF(&result, "none\n");
452 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800453
454 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
455 for (const auto& event : mPendingEvents) {
456 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700457 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800458
459 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
460 for (const auto& ptr : mDisplayEventConnections) {
461 if (const auto connection = ptr.promote()) {
462 StringAppendF(&result, " %s\n", toString(*connection).c_str());
463 }
464 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800465}
466
Dominik Laskowski029cc122019-01-23 19:52:06 -0800467const char* EventThread::toCString(State state) {
468 switch (state) {
469 case State::Idle:
470 return "Idle";
471 case State::Quit:
472 return "Quit";
473 case State::SyntheticVSync:
474 return "SyntheticVSync";
475 case State::VSync:
476 return "VSync";
477 }
478}
479
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800480} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800481} // namespace android