blob: 14e3ec6eadb46b4cbc0107cb593c577adab9ab94 [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Mathias Agopian841cde52012-03-01 15:44:37 -080021#define ATRACE_TAG ATRACE_TAG_GRAPHICS
22
Lloyd Pique46a46b32018-01-31 19:01:18 -080023#include <pthread.h>
24#include <sched.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080025#include <sys/types.h>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080026
Lloyd Pique46a46b32018-01-31 19:01:18 -080027#include <chrono>
28#include <cstdint>
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080029#include <optional>
30#include <type_traits>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080031
Yiwei Zhang5434a782018-12-05 18:06:32 -080032#include <android-base/stringprintf.h>
33
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070034#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080035#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070036
Mathias Agopiand0566bc2011-11-17 17:49:17 -080037#include <gui/DisplayEventReceiver.h>
38
39#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080040#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080041
Mathias Agopiand0566bc2011-11-17 17:49:17 -080042#include "EventThread.h"
Ady Abraham2139f732019-11-13 18:56:40 -080043#include "HwcStrongTypes.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044
Lloyd Pique46a46b32018-01-31 19:01:18 -080045using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080046
Lloyd Pique46a46b32018-01-31 19:01:18 -080047namespace android {
48
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080049using base::StringAppendF;
50using base::StringPrintf;
51
52namespace {
53
54auto vsyncPeriod(VSyncRequest request) {
55 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
56}
57
58std::string toString(VSyncRequest request) {
59 switch (request) {
60 case VSyncRequest::None:
61 return "VSyncRequest::None";
62 case VSyncRequest::Single:
63 return "VSyncRequest::Single";
64 default:
65 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
66 }
67}
68
69std::string toString(const EventThreadConnection& connection) {
70 return StringPrintf("Connection{%p, %s}", &connection,
71 toString(connection.vsyncRequest).c_str());
72}
73
74std::string toString(const DisplayEventReceiver::Event& event) {
75 switch (event.header.type) {
76 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080077 return StringPrintf("Hotplug{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", %s}",
78 event.header.displayId,
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080079 event.hotplug.connected ? "connected" : "disconnected");
80 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080081 return StringPrintf("VSync{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
82 ", count=%u}",
83 event.header.displayId, event.vsync.count);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070084 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
85 return StringPrintf("ConfigChanged{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT
86 ", configId=%u}",
87 event.header.displayId, event.config.configId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080088 default:
89 return "Event{}";
90 }
91}
92
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080093DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
94 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080095 DisplayEventReceiver::Event event;
96 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
97 event.hotplug.connected = connected;
98 return event;
99}
100
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800101DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
102 uint32_t count) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800103 DisplayEventReceiver::Event event;
104 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
105 event.vsync.count = count;
106 return event;
107}
108
Ady Abraham2139f732019-11-13 18:56:40 -0800109DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700110 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800111 DisplayEventReceiver::Event event;
112 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
Ady Abraham2139f732019-11-13 18:56:40 -0800113 event.config.configId = configId.value();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700114 event.config.vsyncPeriod = vsyncPeriod;
Ady Abraham447052e2019-02-13 16:07:27 -0800115 return event;
116}
117
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800118} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800119
Dominik Laskowskif654d572018-12-20 11:03:06 -0800120EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700121 ResyncCallback resyncCallback,
122 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800123 : resyncCallback(std::move(resyncCallback)),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700124 mConfigChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800125 mEventThread(eventThread),
126 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800127
128EventThreadConnection::~EventThreadConnection() {
129 // do nothing here -- clean-up will happen automatically
130 // when the main thread wakes up
131}
132
133void EventThreadConnection::onFirstRef() {
134 // NOTE: mEventThread doesn't hold a strong reference on us
135 mEventThread->registerDisplayEventConnection(this);
136}
137
138status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
139 outChannel->setReceiveFd(mChannel.moveReceiveFd());
140 return NO_ERROR;
141}
142
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800143status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
144 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800145 return NO_ERROR;
146}
147
148void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800149 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700150 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800151}
152
Alec Mouri60aee1c2019-10-28 16:18:59 -0700153void EventThreadConnection::toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) {
154 ATRACE_NAME("enableConfigEvents");
155 mConfigChanged = configChangeFlag;
156
157 // In principle it's possible for rapidly toggling config events to drop an
158 // event here, but it's unlikely in practice.
159 if (configChangeFlag == ISurfaceComposer::eConfigChangedDispatch) {
160 mForcedConfigChangeDispatch = true;
161 mEventThread->requestLatestConfig();
162 }
163}
164
Ana Krulec85c39af2018-12-26 17:29:57 -0800165status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
166 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
167 return size < 0 ? status_t(size) : status_t(NO_ERROR);
168}
169
170// ---------------------------------------------------------------------------
171
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800172EventThread::~EventThread() = default;
173
174namespace impl {
175
Dominik Laskowski6505f792019-09-18 11:10:05 -0700176EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
177 InterceptVSyncsCallback interceptVSyncsCallback)
178 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800179 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700180 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800181 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800182
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800183 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
184 std::unique_lock<std::mutex> lock(mMutex);
185 threadMain(lock);
186 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800187
Dominik Laskowski6505f792019-09-18 11:10:05 -0700188 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189
190 pid_t tid = pthread_gettid_np(mThread.native_handle());
191
192 // Use SCHED_FIFO to minimize jitter
193 constexpr int EVENT_THREAD_PRIORITY = 2;
194 struct sched_param param = {0};
195 param.sched_priority = EVENT_THREAD_PRIORITY;
196 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
197 ALOGE("Couldn't set SCHED_FIFO for EventThread");
198 }
199
200 set_sched_policy(tid, SP_FOREGROUND);
201}
202
203EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800204 mVSyncSource->setCallback(nullptr);
205
Lloyd Pique46a46b32018-01-31 19:01:18 -0800206 {
207 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800208 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800209 mCondition.notify_all();
210 }
211 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700212}
213
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700214void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800215 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700216 mVSyncSource->setPhaseOffset(phaseOffset);
217}
218
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700219sp<EventThreadConnection> EventThread::createEventConnection(
220 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
221 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
222 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800223}
224
Ana Krulec85c39af2018-12-26 17:29:57 -0800225status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800226 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700227
228 // this should never happen
229 auto it = std::find(mDisplayEventConnections.cbegin(),
230 mDisplayEventConnections.cend(), connection);
231 if (it != mDisplayEventConnections.cend()) {
232 ALOGW("DisplayEventConnection %p already exists", connection.get());
233 mCondition.notify_all();
234 return ALREADY_EXISTS;
235 }
236
237 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800238 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800239 return NO_ERROR;
240}
241
Ana Krulec85c39af2018-12-26 17:29:57 -0800242void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700243 auto it = std::find(mDisplayEventConnections.cbegin(),
244 mDisplayEventConnections.cend(), connection);
245 if (it != mDisplayEventConnections.cend()) {
246 mDisplayEventConnections.erase(it);
247 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800248}
249
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800250void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
251 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
252 return;
253 }
254
255 std::lock_guard<std::mutex> lock(mMutex);
256
257 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
258 if (connection->vsyncRequest != request) {
259 connection->vsyncRequest = request;
260 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800261 }
262}
263
Ady Abraham8532d012019-05-08 14:50:56 -0700264void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800265 if (connection->resyncCallback) {
266 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800267 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000268
Ana Krulec7d1d6832018-12-27 11:10:09 -0800269 std::lock_guard<std::mutex> lock(mMutex);
270
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800271 if (connection->vsyncRequest == VSyncRequest::None) {
272 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800273 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800274 }
Mathias Agopian23748662011-12-05 14:33:34 -0800275}
276
Alec Mouri60aee1c2019-10-28 16:18:59 -0700277void EventThread::requestLatestConfig() {
278 std::lock_guard<std::mutex> lock(mMutex);
279 auto pendingConfigChange =
280 std::find_if(std::begin(mPendingEvents), std::end(mPendingEvents),
281 [&](const DisplayEventReceiver::Event& event) {
282 return event.header.type ==
283 DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED;
284 });
285
286 // If we didn't find a pending config change event, then push out the
287 // latest one we've ever seen.
288 if (pendingConfigChange == std::end(mPendingEvents)) {
289 mPendingEvents.push_back(mLastConfigChangeEvent);
290 }
291
292 mCondition.notify_all();
293}
294
Mathias Agopian22ffb112012-04-10 21:04:02 -0700295void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800296 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800297 if (!mVSyncState || mVSyncState->synthetic) {
298 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700299 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800300
301 mVSyncState->synthetic = true;
302 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700303}
304
305void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800306 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800307 if (!mVSyncState || !mVSyncState->synthetic) {
308 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700309 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800310
311 mVSyncState->synthetic = false;
312 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700313}
314
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700315void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800316 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800317
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800318 LOG_FATAL_IF(!mVSyncState);
319 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800320 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700321}
322
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800323void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800324 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700325
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800326 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700327 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700328}
329
Alec Mouri60aee1c2019-10-28 16:18:59 -0700330void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
331 nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800332 std::lock_guard<std::mutex> lock(mMutex);
333
Alec Mouri60aee1c2019-10-28 16:18:59 -0700334 mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod));
Ady Abraham447052e2019-02-13 16:07:27 -0800335 mCondition.notify_all();
336}
337
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800338void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
339 DisplayEventConsumers consumers;
340
Dominik Laskowski029cc122019-01-23 19:52:06 -0800341 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800342 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700343
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800344 // Determine next event to dispatch.
345 if (!mPendingEvents.empty()) {
346 event = mPendingEvents.front();
347 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800348
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800349 switch (event->header.type) {
350 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
351 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800352 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800353 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800354 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800355 mVSyncState.reset();
356 }
357 break;
358
359 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
360 if (mInterceptVSyncsCallback) {
361 mInterceptVSyncsCallback(event->header.timestamp);
362 }
363 break;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700364 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
365 mLastConfigChangeEvent = *event;
366 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800367 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700368 }
369
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800370 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700371
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800372 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700373 auto it = mDisplayEventConnections.begin();
374 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800375 if (const auto connection = it->promote()) {
376 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
377
378 if (event && shouldConsumeEvent(*event, connection)) {
379 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700380 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700381
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700382 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700383 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700384 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700385 }
386 }
387
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800388 if (!consumers.empty()) {
389 dispatchEvent(*event, consumers);
390 consumers.clear();
391 }
392
Dominik Laskowski029cc122019-01-23 19:52:06 -0800393 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800394 if (mVSyncState && vsyncRequested) {
395 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800396 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800397 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800398 nextState = State::Idle;
399 }
400
401 if (mState != nextState) {
402 if (mState == State::VSync) {
403 mVSyncSource->setVSyncEnabled(false);
404 } else if (nextState == State::VSync) {
405 mVSyncSource->setVSyncEnabled(true);
406 }
407
408 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700409 }
410
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800411 if (event) {
412 continue;
413 }
414
415 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800416 if (mState == State::Idle) {
417 mCondition.wait(lock);
418 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800419 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
420 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800421 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800422 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800423 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800424
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800425 LOG_FATAL_IF(!mVSyncState);
426 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800427 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800428 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700429 }
430 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800431 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800432}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700433
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800434bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
435 const sp<EventThreadConnection>& connection) const {
436 switch (event.header.type) {
437 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
438 return true;
439
Alec Mouri60aee1c2019-10-28 16:18:59 -0700440 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: {
441 const bool forcedDispatch = connection->mForcedConfigChangeDispatch.exchange(false);
442 return forcedDispatch ||
443 connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch;
444 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700445
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800446 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
447 switch (connection->vsyncRequest) {
448 case VSyncRequest::None:
449 return false;
450 case VSyncRequest::Single:
451 connection->vsyncRequest = VSyncRequest::None;
452 return true;
453 case VSyncRequest::Periodic:
454 return true;
455 default:
456 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
457 }
458
459 default:
460 return false;
461 }
462}
463
464void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
465 const DisplayEventConsumers& consumers) {
466 for (const auto& consumer : consumers) {
467 switch (consumer->postEvent(event)) {
468 case NO_ERROR:
469 break;
470
471 case -EAGAIN:
472 // TODO: Try again if pipe is full.
473 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
474 toString(*consumer).c_str());
475 break;
476
477 default:
478 // Treat EPIPE and other errors as fatal.
479 removeDisplayEventConnectionLocked(consumer);
480 }
481 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700482}
483
Yiwei Zhang5434a782018-12-05 18:06:32 -0800484void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800485 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800486
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800487 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
488 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800489 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
490 mVSyncState->displayId, mVSyncState->count,
491 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800492 } else {
493 StringAppendF(&result, "none\n");
494 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800495
496 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
497 for (const auto& event : mPendingEvents) {
498 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700499 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800500
501 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
502 for (const auto& ptr : mDisplayEventConnections) {
503 if (const auto connection = ptr.promote()) {
504 StringAppendF(&result, " %s\n", toString(*connection).c_str());
505 }
506 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800507}
508
Dominik Laskowski029cc122019-01-23 19:52:06 -0800509const char* EventThread::toCString(State state) {
510 switch (state) {
511 case State::Idle:
512 return "Idle";
513 case State::Quit:
514 return "Quit";
515 case State::SyntheticVSync:
516 return "SyntheticVSync";
517 case State::VSync:
518 return "VSync";
519 }
520}
521
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800522} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800523} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800524
525// TODO(b/129481165): remove the #pragma below and fix conversion issues
526#pragma clang diagnostic pop // ignored "-Wconversion"