blob: 8d9adc85254a7444b2d6018138e4930eb9566d6f [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);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070079 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
80 return StringPrintf("ConfigChanged{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
81 ", configId=%u}",
82 event.header.displayId, event.config.configId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080083 default:
84 return "Event{}";
85 }
86}
87
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080088DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
89 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080090 DisplayEventReceiver::Event event;
91 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
92 event.hotplug.connected = connected;
93 return event;
94}
95
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080096DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
97 uint32_t count) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080098 DisplayEventReceiver::Event event;
99 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
100 event.vsync.count = count;
101 return event;
102}
103
Ady Abrahamaf0ec272019-03-28 11:38:31 -0700104DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId, int32_t configId) {
Ady Abraham447052e2019-02-13 16:07:27 -0800105 DisplayEventReceiver::Event event;
106 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
107 event.config.configId = configId;
108 return event;
109}
110
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800111} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800112
Dominik Laskowskif654d572018-12-20 11:03:06 -0800113EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700114 ResyncCallback resyncCallback,
115 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800116 : resyncCallback(std::move(resyncCallback)),
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700117 configChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800118 mEventThread(eventThread),
119 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800120
121EventThreadConnection::~EventThreadConnection() {
122 // do nothing here -- clean-up will happen automatically
123 // when the main thread wakes up
124}
125
126void EventThreadConnection::onFirstRef() {
127 // NOTE: mEventThread doesn't hold a strong reference on us
128 mEventThread->registerDisplayEventConnection(this);
129}
130
131status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
132 outChannel->setReceiveFd(mChannel.moveReceiveFd());
133 return NO_ERROR;
134}
135
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800136status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
137 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800138 return NO_ERROR;
139}
140
141void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800142 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700143 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800144}
145
146status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
147 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
148 return size < 0 ? status_t(size) : status_t(NO_ERROR);
149}
150
151// ---------------------------------------------------------------------------
152
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800153EventThread::~EventThread() = default;
154
155namespace impl {
156
Dominik Laskowski6505f792019-09-18 11:10:05 -0700157EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
158 InterceptVSyncsCallback interceptVSyncsCallback)
159 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800160 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700161 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800162 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800163
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800164 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
165 std::unique_lock<std::mutex> lock(mMutex);
166 threadMain(lock);
167 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800168
Dominik Laskowski6505f792019-09-18 11:10:05 -0700169 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800170
171 pid_t tid = pthread_gettid_np(mThread.native_handle());
172
173 // Use SCHED_FIFO to minimize jitter
174 constexpr int EVENT_THREAD_PRIORITY = 2;
175 struct sched_param param = {0};
176 param.sched_priority = EVENT_THREAD_PRIORITY;
177 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
178 ALOGE("Couldn't set SCHED_FIFO for EventThread");
179 }
180
181 set_sched_policy(tid, SP_FOREGROUND);
182}
183
184EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800185 mVSyncSource->setCallback(nullptr);
186
Lloyd Pique46a46b32018-01-31 19:01:18 -0800187 {
188 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800189 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800190 mCondition.notify_all();
191 }
192 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700193}
194
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700195void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800196 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700197 mVSyncSource->setPhaseOffset(phaseOffset);
198}
199
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700200sp<EventThreadConnection> EventThread::createEventConnection(
201 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
202 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
203 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800204}
205
Ana Krulec85c39af2018-12-26 17:29:57 -0800206status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800207 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700208
209 // this should never happen
210 auto it = std::find(mDisplayEventConnections.cbegin(),
211 mDisplayEventConnections.cend(), connection);
212 if (it != mDisplayEventConnections.cend()) {
213 ALOGW("DisplayEventConnection %p already exists", connection.get());
214 mCondition.notify_all();
215 return ALREADY_EXISTS;
216 }
217
218 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800219 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800220 return NO_ERROR;
221}
222
Ana Krulec85c39af2018-12-26 17:29:57 -0800223void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700224 auto it = std::find(mDisplayEventConnections.cbegin(),
225 mDisplayEventConnections.cend(), connection);
226 if (it != mDisplayEventConnections.cend()) {
227 mDisplayEventConnections.erase(it);
228 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800229}
230
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800231void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
232 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
233 return;
234 }
235
236 std::lock_guard<std::mutex> lock(mMutex);
237
238 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
239 if (connection->vsyncRequest != request) {
240 connection->vsyncRequest = request;
241 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800242 }
243}
244
Ady Abraham8532d012019-05-08 14:50:56 -0700245void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800246 if (connection->resyncCallback) {
247 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800248 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000249
Ana Krulec7d1d6832018-12-27 11:10:09 -0800250 std::lock_guard<std::mutex> lock(mMutex);
251
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800252 if (connection->vsyncRequest == VSyncRequest::None) {
253 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800254 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800255 }
Mathias Agopian23748662011-12-05 14:33:34 -0800256}
257
Mathias Agopian22ffb112012-04-10 21:04:02 -0700258void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800259 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800260 if (!mVSyncState || mVSyncState->synthetic) {
261 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700262 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800263
264 mVSyncState->synthetic = true;
265 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700266}
267
268void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800269 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800270 if (!mVSyncState || !mVSyncState->synthetic) {
271 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700272 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800273
274 mVSyncState->synthetic = false;
275 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700276}
277
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700278void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800279 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800280
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800281 LOG_FATAL_IF(!mVSyncState);
282 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800283 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700284}
285
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800286void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800287 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700288
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800289 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700290 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700291}
292
Ady Abraham447052e2019-02-13 16:07:27 -0800293void EventThread::onConfigChanged(PhysicalDisplayId displayId, int32_t configId) {
294 std::lock_guard<std::mutex> lock(mMutex);
295
296 mPendingEvents.push_back(makeConfigChanged(displayId, configId));
297 mCondition.notify_all();
298}
299
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800300void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
301 DisplayEventConsumers consumers;
302
Dominik Laskowski029cc122019-01-23 19:52:06 -0800303 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800304 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700305
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800306 // Determine next event to dispatch.
307 if (!mPendingEvents.empty()) {
308 event = mPendingEvents.front();
309 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800310
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800311 switch (event->header.type) {
312 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
313 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800314 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800315 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800316 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800317 mVSyncState.reset();
318 }
319 break;
320
321 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
322 if (mInterceptVSyncsCallback) {
323 mInterceptVSyncsCallback(event->header.timestamp);
324 }
325 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800326 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700327 }
328
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800329 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700330
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800331 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700332 auto it = mDisplayEventConnections.begin();
333 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800334 if (const auto connection = it->promote()) {
335 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
336
337 if (event && shouldConsumeEvent(*event, connection)) {
338 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700339 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700340
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700341 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700342 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700343 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700344 }
345 }
346
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800347 if (!consumers.empty()) {
348 dispatchEvent(*event, consumers);
349 consumers.clear();
350 }
351
Dominik Laskowski029cc122019-01-23 19:52:06 -0800352 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800353 if (mVSyncState && vsyncRequested) {
354 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800355 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800356 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800357 nextState = State::Idle;
358 }
359
360 if (mState != nextState) {
361 if (mState == State::VSync) {
362 mVSyncSource->setVSyncEnabled(false);
363 } else if (nextState == State::VSync) {
364 mVSyncSource->setVSyncEnabled(true);
365 }
366
367 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700368 }
369
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800370 if (event) {
371 continue;
372 }
373
374 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800375 if (mState == State::Idle) {
376 mCondition.wait(lock);
377 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800378 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
379 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800380 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800381 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800382 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800383
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800384 LOG_FATAL_IF(!mVSyncState);
385 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800386 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800387 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700388 }
389 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800390 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800391}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700392
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800393bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
394 const sp<EventThreadConnection>& connection) const {
395 switch (event.header.type) {
396 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
397 return true;
398
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700399 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
400 return connection->configChanged == ISurfaceComposer::eConfigChangedDispatch;
401
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800402 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
403 switch (connection->vsyncRequest) {
404 case VSyncRequest::None:
405 return false;
406 case VSyncRequest::Single:
407 connection->vsyncRequest = VSyncRequest::None;
408 return true;
409 case VSyncRequest::Periodic:
410 return true;
411 default:
412 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
413 }
414
415 default:
416 return false;
417 }
418}
419
420void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
421 const DisplayEventConsumers& consumers) {
422 for (const auto& consumer : consumers) {
423 switch (consumer->postEvent(event)) {
424 case NO_ERROR:
425 break;
426
427 case -EAGAIN:
428 // TODO: Try again if pipe is full.
429 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
430 toString(*consumer).c_str());
431 break;
432
433 default:
434 // Treat EPIPE and other errors as fatal.
435 removeDisplayEventConnectionLocked(consumer);
436 }
437 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700438}
439
Yiwei Zhang5434a782018-12-05 18:06:32 -0800440void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800441 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800442
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800443 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
444 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800445 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
446 mVSyncState->displayId, mVSyncState->count,
447 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800448 } else {
449 StringAppendF(&result, "none\n");
450 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800451
452 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
453 for (const auto& event : mPendingEvents) {
454 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700455 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800456
457 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
458 for (const auto& ptr : mDisplayEventConnections) {
459 if (const auto connection = ptr.promote()) {
460 StringAppendF(&result, " %s\n", toString(*connection).c_str());
461 }
462 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800463}
464
Dominik Laskowski029cc122019-01-23 19:52:06 -0800465const char* EventThread::toCString(State state) {
466 switch (state) {
467 case State::Idle:
468 return "Idle";
469 case State::Quit:
470 return "Quit";
471 case State::SyntheticVSync:
472 return "SyntheticVSync";
473 case State::VSync:
474 return "VSync";
475 }
476}
477
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800478} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800479} // namespace android