blob: bf5be4790ded1ee91f1fd444e055cdc3fa2e3af1 [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
Ady Abraham0bb6a472020-10-12 10:22:13 -070034#include <binder/IPCThreadState.h>
35
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070036#include <cutils/compiler.h>
Lloyd Pique46a46b32018-01-31 19:01:18 -080037#include <cutils/sched_policy.h>
Mathias Agopiana4cb35a2012-08-20 20:07:34 -070038
Mathias Agopiand0566bc2011-11-17 17:49:17 -080039#include <gui/DisplayEventReceiver.h>
40
41#include <utils/Errors.h>
Mathias Agopian841cde52012-03-01 15:44:37 -080042#include <utils/Trace.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080043
Mathias Agopiand0566bc2011-11-17 17:49:17 -080044#include "EventThread.h"
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -070045#include "FrameTimeline.h"
Ady Abraham2139f732019-11-13 18:56:40 -080046#include "HwcStrongTypes.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Lloyd Pique46a46b32018-01-31 19:01:18 -080048using namespace std::chrono_literals;
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Lloyd Pique46a46b32018-01-31 19:01:18 -080050namespace android {
51
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080052using base::StringAppendF;
53using base::StringPrintf;
54
55namespace {
56
57auto vsyncPeriod(VSyncRequest request) {
58 return static_cast<std::underlying_type_t<VSyncRequest>>(request);
59}
60
61std::string toString(VSyncRequest request) {
62 switch (request) {
63 case VSyncRequest::None:
64 return "VSyncRequest::None";
65 case VSyncRequest::Single:
66 return "VSyncRequest::Single";
Tim Murrayb5daa912020-09-09 21:29:13 +000067 case VSyncRequest::SingleSuppressCallback:
68 return "VSyncRequest::SingleSuppressCallback";
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080069 default:
70 return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
71 }
72}
73
74std::string toString(const EventThreadConnection& connection) {
75 return StringPrintf("Connection{%p, %s}", &connection,
76 toString(connection.vsyncRequest).c_str());
77}
78
79std::string toString(const DisplayEventReceiver::Event& event) {
80 switch (event.header.type) {
81 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
Marin Shalamanova524a092020-07-27 21:39:55 +020082 return StringPrintf("Hotplug{displayId=%s, %s}",
83 to_string(event.header.displayId).c_str(),
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080084 event.hotplug.connected ? "connected" : "disconnected");
85 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
Marin Shalamanova524a092020-07-27 21:39:55 +020086 return StringPrintf("VSync{displayId=%s, count=%u, expectedVSyncTimestamp=%" PRId64 "}",
87 to_string(event.header.displayId).c_str(), event.vsync.count,
Ady Abraham5facfb12020-04-22 15:18:31 -070088 event.vsync.expectedVSyncTimestamp);
Ady Abraham0f4a1b12019-06-04 16:04:04 -070089 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED:
Marin Shalamanova524a092020-07-27 21:39:55 +020090 return StringPrintf("ConfigChanged{displayId=%s, configId=%u}",
91 to_string(event.header.displayId).c_str(), event.config.configId);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -080092 default:
93 return "Event{}";
94 }
95}
96
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080097DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
98 bool connected) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -080099 DisplayEventReceiver::Event event;
100 event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
101 event.hotplug.connected = connected;
102 return event;
103}
104
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800105DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
Ady Abraham8cb21882020-08-26 18:22:05 -0700106 uint32_t count, nsecs_t expectedVSyncTimestamp,
Ady Abraham74e17562020-08-24 18:18:19 -0700107 nsecs_t deadlineTimestamp, int64_t vsyncId) {
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800108 DisplayEventReceiver::Event event;
109 event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
110 event.vsync.count = count;
Ady Abraham5facfb12020-04-22 15:18:31 -0700111 event.vsync.expectedVSyncTimestamp = expectedVSyncTimestamp;
Ady Abraham8cb21882020-08-26 18:22:05 -0700112 event.vsync.deadlineTimestamp = deadlineTimestamp;
Ady Abraham74e17562020-08-24 18:18:19 -0700113 event.vsync.vsyncId = vsyncId;
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800114 return event;
115}
116
Ady Abraham2139f732019-11-13 18:56:40 -0800117DisplayEventReceiver::Event makeConfigChanged(PhysicalDisplayId displayId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700118 HwcConfigIndexType configId, nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800119 DisplayEventReceiver::Event event;
120 event.header = {DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED, displayId, systemTime()};
Ady Abraham2139f732019-11-13 18:56:40 -0800121 event.config.configId = configId.value();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700122 event.config.vsyncPeriod = vsyncPeriod;
Ady Abraham447052e2019-02-13 16:07:27 -0800123 return event;
124}
125
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800126} // namespace
Lloyd Pique46a46b32018-01-31 19:01:18 -0800127
Ady Abraham0bb6a472020-10-12 10:22:13 -0700128EventThreadConnection::EventThreadConnection(EventThread* eventThread, uid_t callingUid,
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700129 ResyncCallback resyncCallback,
130 ISurfaceComposer::ConfigChanged configChanged)
Dominik Laskowskif654d572018-12-20 11:03:06 -0800131 : resyncCallback(std::move(resyncCallback)),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700132 mConfigChanged(configChanged),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700133 mOwnerUid(callingUid),
Dominik Laskowskif654d572018-12-20 11:03:06 -0800134 mEventThread(eventThread),
135 mChannel(gui::BitTube::DefaultSize) {}
Ana Krulec85c39af2018-12-26 17:29:57 -0800136
137EventThreadConnection::~EventThreadConnection() {
138 // do nothing here -- clean-up will happen automatically
139 // when the main thread wakes up
140}
141
142void EventThreadConnection::onFirstRef() {
143 // NOTE: mEventThread doesn't hold a strong reference on us
144 mEventThread->registerDisplayEventConnection(this);
145}
146
147status_t EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
148 outChannel->setReceiveFd(mChannel.moveReceiveFd());
Alec Mouri967d5d72020-08-05 12:50:03 -0700149 outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd())));
Ana Krulec85c39af2018-12-26 17:29:57 -0800150 return NO_ERROR;
151}
152
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800153status_t EventThreadConnection::setVsyncRate(uint32_t rate) {
154 mEventThread->setVsyncRate(rate, this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800155 return NO_ERROR;
156}
157
158void EventThreadConnection::requestNextVsync() {
Ana Krulec7d1d6832018-12-27 11:10:09 -0800159 ATRACE_NAME("requestNextVsync");
Ady Abraham8532d012019-05-08 14:50:56 -0700160 mEventThread->requestNextVsync(this);
Ana Krulec85c39af2018-12-26 17:29:57 -0800161}
162
163status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
164 ssize_t size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
165 return size < 0 ? status_t(size) : status_t(NO_ERROR);
166}
167
168// ---------------------------------------------------------------------------
169
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800170EventThread::~EventThread() = default;
171
172namespace impl {
173
Dominik Laskowski6505f792019-09-18 11:10:05 -0700174EventThread::EventThread(std::unique_ptr<VSyncSource> vsyncSource,
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700175 android::frametimeline::TokenManager* tokenManager,
Ady Abraham0bb6a472020-10-12 10:22:13 -0700176 InterceptVSyncsCallback interceptVSyncsCallback,
177 ThrottleVsyncCallback throttleVsyncCallback)
Dominik Laskowski6505f792019-09-18 11:10:05 -0700178 : mVSyncSource(std::move(vsyncSource)),
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700179 mTokenManager(tokenManager),
Dominik Laskowskiccf37d72019-02-01 16:47:58 -0800180 mInterceptVSyncsCallback(std::move(interceptVSyncsCallback)),
Ady Abraham0bb6a472020-10-12 10:22:13 -0700181 mThrottleVsyncCallback(std::move(throttleVsyncCallback)),
Dominik Laskowski6505f792019-09-18 11:10:05 -0700182 mThreadName(mVSyncSource->getName()) {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800183 mVSyncSource->setCallback(this);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800184
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800185 mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
186 std::unique_lock<std::mutex> lock(mMutex);
187 threadMain(lock);
188 });
Lloyd Pique46a46b32018-01-31 19:01:18 -0800189
Dominik Laskowski6505f792019-09-18 11:10:05 -0700190 pthread_setname_np(mThread.native_handle(), mThreadName);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800191
192 pid_t tid = pthread_gettid_np(mThread.native_handle());
193
194 // Use SCHED_FIFO to minimize jitter
195 constexpr int EVENT_THREAD_PRIORITY = 2;
196 struct sched_param param = {0};
197 param.sched_priority = EVENT_THREAD_PRIORITY;
198 if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
199 ALOGE("Couldn't set SCHED_FIFO for EventThread");
200 }
201
202 set_sched_policy(tid, SP_FOREGROUND);
203}
204
205EventThread::~EventThread() {
Dominik Laskowski029cc122019-01-23 19:52:06 -0800206 mVSyncSource->setCallback(nullptr);
207
Lloyd Pique46a46b32018-01-31 19:01:18 -0800208 {
209 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski029cc122019-01-23 19:52:06 -0800210 mState = State::Quit;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800211 mCondition.notify_all();
212 }
213 mThread.join();
Ruchi Kandoief472ec2014-04-02 12:50:06 -0700214}
215
Ady Abraham9c53ee72020-07-22 21:16:18 -0700216void EventThread::setDuration(std::chrono::nanoseconds workDuration,
217 std::chrono::nanoseconds readyDuration) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800218 std::lock_guard<std::mutex> lock(mMutex);
Ady Abraham9c53ee72020-07-22 21:16:18 -0700219 mVSyncSource->setDuration(workDuration, readyDuration);
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700220}
221
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700222sp<EventThreadConnection> EventThread::createEventConnection(
223 ResyncCallback resyncCallback, ISurfaceComposer::ConfigChanged configChanged) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700224 return new EventThreadConnection(const_cast<EventThread*>(this),
225 IPCThreadState::self()->getCallingUid(),
226 std::move(resyncCallback), configChanged);
Mathias Agopian8aedd472012-01-24 16:39:14 -0800227}
228
Ana Krulec85c39af2018-12-26 17:29:57 -0800229status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800230 std::lock_guard<std::mutex> lock(mMutex);
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700231
232 // this should never happen
233 auto it = std::find(mDisplayEventConnections.cbegin(),
234 mDisplayEventConnections.cend(), connection);
235 if (it != mDisplayEventConnections.cend()) {
236 ALOGW("DisplayEventConnection %p already exists", connection.get());
237 mCondition.notify_all();
238 return ALREADY_EXISTS;
239 }
240
241 mDisplayEventConnections.push_back(connection);
Lloyd Pique46a46b32018-01-31 19:01:18 -0800242 mCondition.notify_all();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800243 return NO_ERROR;
244}
245
Ana Krulec85c39af2018-12-26 17:29:57 -0800246void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
Chia-I Wu98cd38f2018-09-14 11:53:25 -0700247 auto it = std::find(mDisplayEventConnections.cbegin(),
248 mDisplayEventConnections.cend(), connection);
249 if (it != mDisplayEventConnections.cend()) {
250 mDisplayEventConnections.erase(it);
251 }
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800252}
253
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800254void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
255 if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
256 return;
257 }
258
259 std::lock_guard<std::mutex> lock(mMutex);
260
261 const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
262 if (connection->vsyncRequest != request) {
263 connection->vsyncRequest = request;
264 mCondition.notify_all();
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800265 }
266}
267
Ady Abraham8532d012019-05-08 14:50:56 -0700268void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
Dominik Laskowskif654d572018-12-20 11:03:06 -0800269 if (connection->resyncCallback) {
270 connection->resyncCallback();
Lloyd Pique24b0a482018-03-09 18:52:26 -0800271 }
Tim Murray4a4e4a22016-04-19 16:29:23 +0000272
Ana Krulec7d1d6832018-12-27 11:10:09 -0800273 std::lock_guard<std::mutex> lock(mMutex);
274
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800275 if (connection->vsyncRequest == VSyncRequest::None) {
276 connection->vsyncRequest = VSyncRequest::Single;
Lloyd Pique46a46b32018-01-31 19:01:18 -0800277 mCondition.notify_all();
Tim Murrayb5daa912020-09-09 21:29:13 +0000278 } else if (connection->vsyncRequest == VSyncRequest::SingleSuppressCallback) {
279 connection->vsyncRequest = VSyncRequest::Single;
Mathias Agopian478ae5e2011-12-06 17:22:19 -0800280 }
Mathias Agopian23748662011-12-05 14:33:34 -0800281}
282
Mathias Agopian22ffb112012-04-10 21:04:02 -0700283void EventThread::onScreenReleased() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800284 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800285 if (!mVSyncState || mVSyncState->synthetic) {
286 return;
Mathias Agopian22ffb112012-04-10 21:04:02 -0700287 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800288
289 mVSyncState->synthetic = true;
290 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700291}
292
293void EventThread::onScreenAcquired() {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800294 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800295 if (!mVSyncState || !mVSyncState->synthetic) {
296 return;
Mathias Agopian7d886472012-06-14 23:39:35 -0700297 }
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800298
299 mVSyncState->synthetic = false;
300 mCondition.notify_all();
Mathias Agopian22ffb112012-04-10 21:04:02 -0700301}
302
Ady Abraham9c53ee72020-07-22 21:16:18 -0700303void EventThread::onVSyncEvent(nsecs_t timestamp, nsecs_t expectedVSyncTimestamp,
Ady Abraham8cb21882020-08-26 18:22:05 -0700304 nsecs_t deadlineTimestamp) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800305 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800306
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800307 LOG_FATAL_IF(!mVSyncState);
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700308 const int64_t vsyncId = [&] {
309 if (mTokenManager != nullptr) {
310 return mTokenManager->generateTokenForPredictions(
311 {timestamp, deadlineTimestamp, expectedVSyncTimestamp});
312 }
313 return static_cast<int64_t>(0);
314 }();
315
Ady Abraham5facfb12020-04-22 15:18:31 -0700316 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, timestamp, ++mVSyncState->count,
Adithya Srinivasan5f683cf2020-09-15 14:21:04 -0700317 expectedVSyncTimestamp, deadlineTimestamp, vsyncId));
Lloyd Pique46a46b32018-01-31 19:01:18 -0800318 mCondition.notify_all();
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700319}
320
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800321void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800322 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700323
Dominik Laskowski23b867a2019-01-22 12:44:53 -0800324 mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
Dominik Laskowski00a6fa22018-06-06 16:42:02 -0700325 mCondition.notify_all();
Mathias Agopian148994e2012-09-19 17:31:36 -0700326}
327
Alec Mouri60aee1c2019-10-28 16:18:59 -0700328void EventThread::onConfigChanged(PhysicalDisplayId displayId, HwcConfigIndexType configId,
329 nsecs_t vsyncPeriod) {
Ady Abraham447052e2019-02-13 16:07:27 -0800330 std::lock_guard<std::mutex> lock(mMutex);
331
Alec Mouri60aee1c2019-10-28 16:18:59 -0700332 mPendingEvents.push_back(makeConfigChanged(displayId, configId, vsyncPeriod));
Ady Abraham447052e2019-02-13 16:07:27 -0800333 mCondition.notify_all();
334}
335
Alec Mouri717bcb62020-02-10 17:07:19 -0800336size_t EventThread::getEventThreadConnectionCount() {
337 std::lock_guard<std::mutex> lock(mMutex);
338 return mDisplayEventConnections.size();
339}
340
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800341void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
342 DisplayEventConsumers consumers;
343
Dominik Laskowski029cc122019-01-23 19:52:06 -0800344 while (mState != State::Quit) {
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800345 std::optional<DisplayEventReceiver::Event> event;
Mathias Agopian3eb38cb2012-04-03 22:09:52 -0700346
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800347 // Determine next event to dispatch.
348 if (!mPendingEvents.empty()) {
349 event = mPendingEvents.front();
350 mPendingEvents.pop_front();
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800351
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800352 switch (event->header.type) {
353 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
354 if (event->hotplug.connected && !mVSyncState) {
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800355 mVSyncState.emplace(event->header.displayId);
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800356 } else if (!event->hotplug.connected && mVSyncState &&
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800357 mVSyncState->displayId == event->header.displayId) {
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800358 mVSyncState.reset();
359 }
360 break;
361
362 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
363 if (mInterceptVSyncsCallback) {
364 mInterceptVSyncsCallback(event->header.timestamp);
365 }
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.
Ady Abraham5facfb12020-04-22 15:18:31 -0700421 const std::chrono::nanoseconds timeout =
422 mState == State::SyntheticVSync ? 16ms : 1000ms;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800423 if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
Ady Abraham5e7371c2020-03-24 14:47:24 -0700424 if (mState == State::VSync) {
425 ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName);
426 std::string debugInfo = "VsyncSource debug info:\n";
427 mVSyncSource->dump(debugInfo);
Ady Abraham75398722020-04-07 14:08:45 -0700428 // Log the debug info line-by-line to avoid logcat overflow
429 auto pos = debugInfo.find('\n');
430 while (pos != std::string::npos) {
431 ALOGW("%s", debugInfo.substr(0, pos).c_str());
432 debugInfo = debugInfo.substr(pos + 1);
433 pos = debugInfo.find('\n');
434 }
Ady Abraham5e7371c2020-03-24 14:47:24 -0700435 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800436
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800437 LOG_FATAL_IF(!mVSyncState);
Ady Abraham5facfb12020-04-22 15:18:31 -0700438 const auto now = systemTime(SYSTEM_TIME_MONOTONIC);
Ady Abraham8cb21882020-08-26 18:22:05 -0700439 const auto deadlineTimestamp = now + timeout.count();
440 const auto expectedVSyncTime = deadlineTimestamp + timeout.count();
Ady Abraham74e17562020-08-24 18:18:19 -0700441 // TODO(b/162890590): use TokenManager to populate vsyncId
Ady Abraham5facfb12020-04-22 15:18:31 -0700442 mPendingEvents.push_back(makeVSync(mVSyncState->displayId, now,
Ady Abraham8cb21882020-08-26 18:22:05 -0700443 ++mVSyncState->count, expectedVSyncTime,
Ady Abraham74e17562020-08-24 18:18:19 -0700444 deadlineTimestamp, /*vsyncId=*/0));
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700445 }
446 }
Lloyd Pique46a46b32018-01-31 19:01:18 -0800447 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800448}
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700449
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800450bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
451 const sp<EventThreadConnection>& connection) const {
Ady Abraham0bb6a472020-10-12 10:22:13 -0700452 const auto throttleVsync = [&] {
453 return mThrottleVsyncCallback &&
454 mThrottleVsyncCallback(event.vsync.expectedVSyncTimestamp, connection->mOwnerUid);
455 };
456
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800457 switch (event.header.type) {
458 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
459 return true;
460
Alec Mouri60aee1c2019-10-28 16:18:59 -0700461 case DisplayEventReceiver::DISPLAY_EVENT_CONFIG_CHANGED: {
Alec Mouri967d5d72020-08-05 12:50:03 -0700462 return connection->mConfigChanged == ISurfaceComposer::eConfigChangedDispatch;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700463 }
Ady Abraham0f4a1b12019-06-04 16:04:04 -0700464
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800465 case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
466 switch (connection->vsyncRequest) {
467 case VSyncRequest::None:
468 return false;
Tim Murrayb5daa912020-09-09 21:29:13 +0000469 case VSyncRequest::SingleSuppressCallback:
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800470 connection->vsyncRequest = VSyncRequest::None;
Tim Murrayb5daa912020-09-09 21:29:13 +0000471 return false;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700472 case VSyncRequest::Single: {
473 if (throttleVsync()) {
474 return false;
475 }
Tim Murrayb5daa912020-09-09 21:29:13 +0000476 connection->vsyncRequest = VSyncRequest::SingleSuppressCallback;
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800477 return true;
Ady Abraham0bb6a472020-10-12 10:22:13 -0700478 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800479 case VSyncRequest::Periodic:
Ady Abraham0bb6a472020-10-12 10:22:13 -0700480 if (throttleVsync()) {
481 return false;
482 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800483 return true;
484 default:
Ady Abraham0bb6a472020-10-12 10:22:13 -0700485 // We don't throttle vsync if the app set a vsync request rate
486 // since there is no easy way to do that and this is a very
487 // rare case
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800488 return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
489 }
490
491 default:
492 return false;
493 }
494}
495
496void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
497 const DisplayEventConsumers& consumers) {
498 for (const auto& consumer : consumers) {
499 switch (consumer->postEvent(event)) {
500 case NO_ERROR:
501 break;
502
503 case -EAGAIN:
504 // TODO: Try again if pipe is full.
505 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
506 toString(*consumer).c_str());
507 break;
508
509 default:
510 // Treat EPIPE and other errors as fatal.
511 removeDisplayEventConnectionLocked(consumer);
512 }
513 }
Mathias Agopianf6bbd442012-08-21 15:47:28 -0700514}
515
Yiwei Zhang5434a782018-12-05 18:06:32 -0800516void EventThread::dump(std::string& result) const {
Lloyd Pique46a46b32018-01-31 19:01:18 -0800517 std::lock_guard<std::mutex> lock(mMutex);
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800518
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800519 StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
520 if (mVSyncState) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200521 StringAppendF(&result, "{displayId=%s, count=%u%s}\n",
522 to_string(mVSyncState->displayId).c_str(), mVSyncState->count,
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800523 mVSyncState->synthetic ? ", synthetic" : "");
Dominik Laskowski1eba0202019-01-24 09:14:40 -0800524 } else {
525 StringAppendF(&result, "none\n");
526 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800527
528 StringAppendF(&result, " pending events (count=%zu):\n", mPendingEvents.size());
529 for (const auto& event : mPendingEvents) {
530 StringAppendF(&result, " %s\n", toString(event).c_str());
Mathias Agopiane2c4f4e2012-04-10 18:25:31 -0700531 }
Dominik Laskowskid9e4de62019-01-21 14:23:01 -0800532
533 StringAppendF(&result, " connections (count=%zu):\n", mDisplayEventConnections.size());
534 for (const auto& ptr : mDisplayEventConnections) {
535 if (const auto connection = ptr.promote()) {
536 StringAppendF(&result, " %s\n", toString(*connection).c_str());
537 }
538 }
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800539}
540
Dominik Laskowski029cc122019-01-23 19:52:06 -0800541const char* EventThread::toCString(State state) {
542 switch (state) {
543 case State::Idle:
544 return "Idle";
545 case State::Quit:
546 return "Quit";
547 case State::SyntheticVSync:
548 return "SyntheticVSync";
549 case State::VSync:
550 return "VSync";
551 }
552}
553
Lloyd Pique0fcde1b2017-12-20 16:50:21 -0800554} // namespace impl
Lloyd Pique46a46b32018-01-31 19:01:18 -0800555} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800556
557// TODO(b/129481165): remove the #pragma below and fix conversion issues
558#pragma clang diagnostic pop // ignored "-Wconversion"