blob: ff800c3dbf52f8583b9b9b53d1f54097a6358abe [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"
Ady Abraham2139f732019-11-13 18:56:40 -080039#include "HwcStrongTypes.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080040
Lloyd Pique46a46b32018-01-31 19:01:18 -080041using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042
Lloyd Pique46a46b32018-01-31 19:01:18 -080043namespace android {
44
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080045using base::StringAppendF;
46using base::StringPrintf;
47
48namespace {
49
50auto vsyncPeriod(VSyncRequest request) {
51 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
52}
53
54std::string toString(VSyncRequest request) {
55 switch (request) {
56 case VSyncRequest::None:
57 return "VSyncRequest::None";
58 case VSyncRequest::Single:
59 return "VSyncRequest::Single";
60 default:
61 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
62 }
63}
64
65std::string toString(const EventThreadConnection& connection) {
66 return StringPrintf("Connection{%p, %s}", &connection,
67 toString(connection.vsyncRequest).c_str());
68}
69
70std::string toString(const DisplayEventReceiver::Event& event) {
71 switch (event.header.type) {
72 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080073 return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}",
74 event.header.displayId,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080075 event.hotplug.connected ? "connected" : "disconnected");
76 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080077 return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
78 ", count=%u}",
79 event.header.displayId, event.vsync.count);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070080 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
81 return StringPrintf("ConfigChanged{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
82 ", configId=%u}",
83 event.header.displayId, event.config.configId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080084 default:
85 return "Event{}";
86 }
87}
88
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080089DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
90 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080091 DisplayEventReceiver::Event event;
92 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
93 event.hotplug.connected = connected;
94 return event;
95}
96
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080097DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
98 uint32_t count) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080099 DisplayEventReceiver::Event event;
100 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
101 event.vsync.count = count;
102 return event;
103}
104
Ady Abraham2139f732019-11-13 18:56:40 -0800105DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId,
106 HwcConfigIndexType configId) {
Ady Abraham447052e2019-02-13 16:07:27 -0800107 DisplayEventReceiver::Event event;
108 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
Ady Abraham2139f732019-11-13 18:56:40 -0800109 event.config.configId = configId.value();
Ady Abraham447052e2019-02-13 16:07:27 -0800110 return event;
111}
112
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800113} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800114
Dominik Laskowskif654d572018-12-20 11:03:06 -0800115EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700116 ResyncCallback resyncCallback,
117 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800118 : resyncCallback(std::move(resyncCallback)),
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700119 configChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800120 mEventThread(eventThread),
121 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800122
123EventThreadConnection::~EventThreadConnection() {
124 // do nothing here -- clean-up will happen automatically
125 // when the main thread wakes up
126}
127
128void EventThreadConnection::onFirstRef() {
129 // NOTE: mEventThread doesn't hold a strong reference on us
130 mEventThread->registerDisplayEventConnection(this);
131}
132
133status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
134 outChannel->setReceiveFd(mChannel.moveReceiveFd());
135 return NO_ERROR;
136}
137
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800138status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
139 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800140 return NO_ERROR;
141}
142
143void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800144 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700145 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800146}
147
148status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
149 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
150 return size < 0 ? status_t(size) : status_t(NO_ERROR);
151}
152
153// ---------------------------------------------------------------------------
154
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800155EventThread::~EventThread() = default;
156
157namespace impl {
158
Dominik Laskowski6505f792019-09-18 11:10:05 -0700159EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
160 InterceptVSyncsCallback interceptVSyncsCallback)
161 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800162 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700163 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800164 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800165
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800166 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
167 std::unique_lock<std::mutex> lock(mMutex);
168 threadMain(lock);
169 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800170
Dominik Laskowski6505f792019-09-18 11:10:05 -0700171 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800172
173 pid_t tid = pthread_gettid_np(mThread.native_handle());
174
175 // Use SCHED_FIFO to minimize jitter
176 constexpr int EVENT_THREAD_PRIORITY = 2;
177 struct sched_param param = {0};
178 param.sched_priority = EVENT_THREAD_PRIORITY;
179 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
180 ALOGE("Couldn't set SCHED_FIFO for EventThread");
181 }
182
183 set_sched_policy(tid, SP_FOREGROUND);
184}
185
186EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800187 mVSyncSource->setCallback(nullptr);
188
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189 {
190 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800191 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800192 mCondition.notify_all();
193 }
194 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700195}
196
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700197void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800198 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700199 mVSyncSource->setPhaseOffset(phaseOffset);
200}
201
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700202sp<EventThreadConnection> EventThread::createEventConnection(
203 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
204 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
205 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800206}
207
Ana Krulec85c39af2018-12-26 17:29:57 -0800208status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800209 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700210
211 // this should never happen
212 auto it = std::find(mDisplayEventConnections.cbegin(),
213 mDisplayEventConnections.cend(), connection);
214 if (it != mDisplayEventConnections.cend()) {
215 ALOGW("DisplayEventConnection %p already exists", connection.get());
216 mCondition.notify_all();
217 return ALREADY_EXISTS;
218 }
219
220 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800221 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800222 return NO_ERROR;
223}
224
Ana Krulec85c39af2018-12-26 17:29:57 -0800225void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700226 auto it = std::find(mDisplayEventConnections.cbegin(),
227 mDisplayEventConnections.cend(), connection);
228 if (it != mDisplayEventConnections.cend()) {
229 mDisplayEventConnections.erase(it);
230 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800231}
232
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800233void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
234 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
235 return;
236 }
237
238 std::lock_guard<std::mutex> lock(mMutex);
239
240 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
241 if (connection->vsyncRequest != request) {
242 connection->vsyncRequest = request;
243 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800244 }
245}
246
Ady Abraham8532d012019-05-08 14:50:56 -0700247void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800248 if (connection->resyncCallback) {
249 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800250 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000251
Ana Krulec7d1d6832018-12-27 11:10:09 -0800252 std::lock_guard<std::mutex> lock(mMutex);
253
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800254 if (connection->vsyncRequest == VSyncRequest::None) {
255 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800256 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800257 }
Mathias Agopian23748662011-12-05 14:33:34 -0800258}
259
Mathias Agopian22ffb112012-04-10 21:04:02 -0700260void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800261 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800262 if (!mVSyncState || mVSyncState->synthetic) {
263 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700264 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800265
266 mVSyncState->synthetic = true;
267 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700268}
269
270void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800271 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800272 if (!mVSyncState || !mVSyncState->synthetic) {
273 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700274 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800275
276 mVSyncState->synthetic = false;
277 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700278}
279
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700280void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800281 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800282
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800283 LOG_FATAL_IF(!mVSyncState);
284 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800285 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700286}
287
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800288void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800289 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700290
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800291 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700292 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700293}
294
Ady Abraham2139f732019-11-13 18:56:40 -0800295void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId) {
Ady Abraham447052e2019-02-13 16:07:27 -0800296 std::lock_guard<std::mutex> lock(mMutex);
297
298 mPendingEvents.push_back(makeConfigChanged(displayId, configId));
299 mCondition.notify_all();
300}
301
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800302void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
303 DisplayEventConsumers consumers;
304
Dominik Laskowski029cc122019-01-23 19:52:06 -0800305 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800306 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700307
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800308 // Determine next event to dispatch.
309 if (!mPendingEvents.empty()) {
310 event = mPendingEvents.front();
311 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800312
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800313 switch (event->header.type) {
314 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
315 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800316 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800317 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800318 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800319 mVSyncState.reset();
320 }
321 break;
322
323 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
324 if (mInterceptVSyncsCallback) {
325 mInterceptVSyncsCallback(event->header.timestamp);
326 }
327 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800328 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700329 }
330
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800331 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700332
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800333 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700334 auto it = mDisplayEventConnections.begin();
335 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800336 if (const auto connection = it->promote()) {
337 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
338
339 if (event && shouldConsumeEvent(*event, connection)) {
340 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700341 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700342
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700343 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700344 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700345 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700346 }
347 }
348
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800349 if (!consumers.empty()) {
350 dispatchEvent(*event, consumers);
351 consumers.clear();
352 }
353
Dominik Laskowski029cc122019-01-23 19:52:06 -0800354 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800355 if (mVSyncState && vsyncRequested) {
356 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800357 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800358 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800359 nextState = State::Idle;
360 }
361
362 if (mState != nextState) {
363 if (mState == State::VSync) {
364 mVSyncSource->setVSyncEnabled(false);
365 } else if (nextState == State::VSync) {
366 mVSyncSource->setVSyncEnabled(true);
367 }
368
369 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700370 }
371
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800372 if (event) {
373 continue;
374 }
375
376 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800377 if (mState == State::Idle) {
378 mCondition.wait(lock);
379 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800380 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
381 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800382 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800383 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800384 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800385
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800386 LOG_FATAL_IF(!mVSyncState);
387 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800388 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800389 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700390 }
391 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800392 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800393}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700394
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800395bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
396 const sp<EventThreadConnection>& connection) const {
397 switch (event.header.type) {
398 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
399 return true;
400
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700401 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
402 return connection->configChanged == ISurfaceComposer::eConfigChangedDispatch;
403
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800404 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