blob: 5bdef5807ff79dab34319ef0eae83e49a70d657a [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,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700106 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
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();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700110 event.config.vsyncPeriod = vsyncPeriod;
Ady Abraham447052e2019-02-13 16:07:27 -0800111 return event;
112}
113
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800114} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800115
Dominik Laskowskif654d572018-12-20 11:03:06 -0800116EventThreadConnection::EventThreadConnection(EventThread* eventThread,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700117 ResyncCallback resyncCallback,
118 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800119 : resyncCallback(std::move(resyncCallback)),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700120 mConfigChanged(configChanged),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800121 mEventThread(eventThread),
122 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800123
124EventThreadConnection::~EventThreadConnection() {
125 // do nothing here -- clean-up will happen automatically
126 // when the main thread wakes up
127}
128
129void EventThreadConnection::onFirstRef() {
130 // NOTE: mEventThread doesn't hold a strong reference on us
131 mEventThread->registerDisplayEventConnection(this);
132}
133
134status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
135 outChannel->setReceiveFd(mChannel.moveReceiveFd());
136 return NO_ERROR;
137}
138
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800139status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
140 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800141 return NO_ERROR;
142}
143
144void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800145 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700146 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800147}
148
Alec Mouri60aee1c2019-10-28 16:18:59 -0700149void EventThreadConnection::toggleConfigEvents(ISurfaceComposer::ConfigChanged configChangeFlag) {
150 ATRACE_NAME("enableConfigEvents");
151 mConfigChanged = configChangeFlag;
152
153 // In principle it's possible for rapidly toggling config events to drop an
154 // event here, but it's unlikely in practice.
155 if (configChangeFlag == ISurfaceComposer::eConfigChangedDispatch) {
156 mForcedConfigChangeDispatch = true;
157 mEventThread->requestLatestConfig();
158 }
159}
160
Ana Krulec85c39af2018-12-26 17:29:57 -0800161status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
162 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
163 return size < 0 ? status_t(size) : status_t(NO_ERROR);
164}
165
166// ---------------------------------------------------------------------------
167
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800168EventThread::~EventThread() = default;
169
170namespace impl {
171
Dominik Laskowski6505f792019-09-18 11:10:05 -0700172EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
173 InterceptVSyncsCallback interceptVSyncsCallback)
174 : mVSyncSource(std::move(vsyncSource)),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800175 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700176 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800177 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800178
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800179 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
180 std::unique_lock<std::mutex> lock(mMutex);
181 threadMain(lock);
182 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800183
Dominik Laskowski6505f792019-09-18 11:10:05 -0700184 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800185
186 pid_t tid = pthread_gettid_np(mThread.native_handle());
187
188 // Use SCHED_FIFO to minimize jitter
189 constexpr int EVENT_THREAD_PRIORITY = 2;
190 struct sched_param param = {0};
191 param.sched_priority = EVENT_THREAD_PRIORITY;
192 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
193 ALOGE("Couldn't set SCHED_FIFO for EventThread");
194 }
195
196 set_sched_policy(tid, SP_FOREGROUND);
197}
198
199EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800200 mVSyncSource->setCallback(nullptr);
201
Lloyd Pique46a46b32018-01-31 19:01:18 -0800202 {
203 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800204 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800205 mCondition.notify_all();
206 }
207 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700208}
209
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700210void EventThread::setPhaseOffset(nsecs_t phaseOffset) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211 std::lock_guard<std::mutex> lock(mMutex);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700212 mVSyncSource->setPhaseOffset(phaseOffset);
213}
214
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700215sp<EventThreadConnection> EventThread::createEventConnection(
216 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
217 return new EventThreadConnection(const_cast<EventThread*>(this), std::move(resyncCallback),
218 configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800219}
220
Ana Krulec85c39af2018-12-26 17:29:57 -0800221status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800222 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700223
224 // this should never happen
225 auto it = std::find(mDisplayEventConnections.cbegin(),
226 mDisplayEventConnections.cend(), connection);
227 if (it != mDisplayEventConnections.cend()) {
228 ALOGW("DisplayEventConnection %p already exists", connection.get());
229 mCondition.notify_all();
230 return ALREADY_EXISTS;
231 }
232
233 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800234 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800235 return NO_ERROR;
236}
237
Ana Krulec85c39af2018-12-26 17:29:57 -0800238void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700239 auto it = std::find(mDisplayEventConnections.cbegin(),
240 mDisplayEventConnections.cend(), connection);
241 if (it != mDisplayEventConnections.cend()) {
242 mDisplayEventConnections.erase(it);
243 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800244}
245
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800246void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
247 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
248 return;
249 }
250
251 std::lock_guard<std::mutex> lock(mMutex);
252
253 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
254 if (connection->vsyncRequest != request) {
255 connection->vsyncRequest = request;
256 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800257 }
258}
259
Ady Abraham8532d012019-05-08 14:50:56 -0700260void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800261 if (connection->resyncCallback) {
262 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800263 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000264
Ana Krulec7d1d6832018-12-27 11:10:09 -0800265 std::lock_guard<std::mutex> lock(mMutex);
266
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800267 if (connection->vsyncRequest == VSyncRequest::None) {
268 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800269 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800270 }
Mathias Agopian23748662011-12-05 14:33:34 -0800271}
272
Alec Mouri60aee1c2019-10-28 16:18:59 -0700273void EventThread::requestLatestConfig() {
274 std::lock_guard<std::mutex> lock(mMutex);
275 auto pendingConfigChange =
276 std::find_if(std::begin(mPendingEvents), std::end(mPendingEvents),
277 [&](const DisplayEventReceiver::Event& event) {
278 return event.header.type ==
279 DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED;
280 });
281
282 // If we didn't find a pending config change event, then push out the
283 // latest one we've ever seen.
284 if (pendingConfigChange == std::end(mPendingEvents)) {
285 mPendingEvents.push_back(mLastConfigChangeEvent);
286 }
287
288 mCondition.notify_all();
289}
290
Mathias Agopian22ffb112012-04-10 21:04:02 -0700291void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800292 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800293 if (!mVSyncState || mVSyncState->synthetic) {
294 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700295 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800296
297 mVSyncState->synthetic = true;
298 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700299}
300
301void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800302 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800303 if (!mVSyncState || !mVSyncState->synthetic) {
304 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700305 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800306
307 mVSyncState->synthetic = false;
308 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700309}
310
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700311void EventThread::onVSyncEvent(nsecs_t timestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800312 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800313
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800314 LOG_FATAL_IF(!mVSyncState);
315 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800316 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700317}
318
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800319void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800320 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700321
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800322 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700323 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700324}
325
Alec Mouri60aee1c2019-10-28 16:18:59 -0700326void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
327 nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800328 std::lock_guard<std::mutex> lock(mMutex);
329
Alec Mouri60aee1c2019-10-28 16:18:59 -0700330 mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod));
Ady Abraham447052e2019-02-13 16:07:27 -0800331 mCondition.notify_all();
332}
333
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800334void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
335 DisplayEventConsumers consumers;
336
Dominik Laskowski029cc122019-01-23 19:52:06 -0800337 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800338 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700339
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800340 // Determine next event to dispatch.
341 if (!mPendingEvents.empty()) {
342 event = mPendingEvents.front();
343 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800344
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800345 switch (event->header.type) {
346 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
347 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800348 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800349 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800350 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800351 mVSyncState.reset();
352 }
353 break;
354
355 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
356 if (mInterceptVSyncsCallback) {
357 mInterceptVSyncsCallback(event->header.timestamp);
358 }
359 break;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700360 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
361 mLastConfigChangeEvent = *event;
362 break;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800363 }
Mathias Agopianff28e202012-09-20 23:24:19 -0700364 }
365
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800366 bool vsyncRequested = false;
Mathias Agopian148994e2012-09-19 17:31:36 -0700367
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800368 // Find connections that should consume this event.
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700369 auto it = mDisplayEventConnections.begin();
370 while (it != mDisplayEventConnections.end()) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800371 if (const auto connection = it->promote()) {
372 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
373
374 if (event && shouldConsumeEvent(*event, connection)) {
375 consumers.push_back(connection);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700376 }
Mathias Agopianb4d18ed2012-09-20 23:14:05 -0700377
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700378 ++it;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700379 } else {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700380 it = mDisplayEventConnections.erase(it);
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700381 }
382 }
383
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800384 if (!consumers.empty()) {
385 dispatchEvent(*event, consumers);
386 consumers.clear();
387 }
388
Dominik Laskowski029cc122019-01-23 19:52:06 -0800389 State nextState;
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800390 if (mVSyncState && vsyncRequested) {
391 nextState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
Dominik Laskowski029cc122019-01-23 19:52:06 -0800392 } else {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800393 ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
Dominik Laskowski029cc122019-01-23 19:52:06 -0800394 nextState = State::Idle;
395 }
396
397 if (mState != nextState) {
398 if (mState == State::VSync) {
399 mVSyncSource->setVSyncEnabled(false);
400 } else if (nextState == State::VSync) {
401 mVSyncSource->setVSyncEnabled(true);
402 }
403
404 mState = nextState;
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700405 }
406
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800407 if (event) {
408 continue;
409 }
410
411 // Wait for event or client registration/request.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800412 if (mState == State::Idle) {
413 mCondition.wait(lock);
414 } else {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800415 // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
416 // display is off, keep feeding clients at 60 Hz.
Dominik Laskowski029cc122019-01-23 19:52:06 -0800417 const auto timeout = mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800418 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800419 ALOGW_IF(mState == State::VSync, "Faking VSYNC due to driver stall");
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800420
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800421 LOG_FATAL_IF(!mVSyncState);
422 mPendingEvents.push_back(makeVSync(mVSyncState->displayId,
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800423 systemTime(SYSTEM_TIME_MONOTONIC),
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800424 ++mVSyncState->count));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700425 }
426 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800427 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800428}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700429
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800430bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
431 const sp<EventThreadConnection>& connection) const {
432 switch (event.header.type) {
433 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
434 return true;
435
Alec Mouri60aee1c2019-10-28 16:18:59 -0700436 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: {
437 const bool forcedDispatch = connection->mForcedConfigChangeDispatch.exchange(false);
438 return forcedDispatch ||
439 connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch;
440 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700441
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800442 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
443 switch (connection->vsyncRequest) {
444 case VSyncRequest::None:
445 return false;
446 case VSyncRequest::Single:
447 connection->vsyncRequest = VSyncRequest::None;
448 return true;
449 case VSyncRequest::Periodic:
450 return true;
451 default:
452 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
453 }
454
455 default:
456 return false;
457 }
458}
459
460void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
461 const DisplayEventConsumers& consumers) {
462 for (const auto& consumer : consumers) {
463 switch (consumer->postEvent(event)) {
464 case NO_ERROR:
465 break;
466
467 case -EAGAIN:
468 // TODO: Try again if pipe is full.
469 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
470 toString(*consumer).c_str());
471 break;
472
473 default:
474 // Treat EPIPE and other errors as fatal.
475 removeDisplayEventConnectionLocked(consumer);
476 }
477 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700478}
479
Yiwei Zhang5434a782018-12-05 18:06:32 -0800480void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800481 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800482
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800483 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
484 if (mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800485 StringAppendF(&result, "{displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", count=%u%s}\n",
486 mVSyncState->displayId, mVSyncState->count,
487 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800488 } else {
489 StringAppendF(&result, "none\n");
490 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800491
492 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
493 for (const auto& event : mPendingEvents) {
494 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700495 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800496
497 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
498 for (const auto& ptr : mDisplayEventConnections) {
499 if (const auto connection = ptr.promote()) {
500 StringAppendF(&result, " %s\n", toString(*connection).c_str());
501 }
502 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800503}
504
Dominik Laskowski029cc122019-01-23 19:52:06 -0800505const char* EventThread::toCString(State state) {
506 switch (state) {
507 case State::Idle:
508 return "Idle";
509 case State::Quit:
510 return "Quit";
511 case State::SyntheticVSync:
512 return "SyntheticVSync";
513 case State::VSync:
514 return "VSync";
515 }
516}
517
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800518} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800519} // namespace android