xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/Scheduler/EventThread.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
22 
23 #include <pthread.h>
24 #include <sched.h>
25 #include <sys/types.h>
26 
27 #include <chrono>
28 #include <cstdint>
29 #include <optional>
30 #include <type_traits>
31 #include <utility>
32 
33 #include <android-base/stringprintf.h>
34 
35 #include <binder/IPCThreadState.h>
36 #include <common/trace.h>
37 #include <cutils/compiler.h>
38 #include <cutils/sched_policy.h>
39 
40 #include <gui/DisplayEventReceiver.h>
41 #include <gui/SchedulingPolicy.h>
42 
43 #include <utils/Errors.h>
44 
45 #include <common/FlagManager.h>
46 #include <scheduler/FrameRateMode.h>
47 #include <scheduler/VsyncConfig.h>
48 #include "FrameTimeline.h"
49 #include "VSyncDispatch.h"
50 
51 #include "EventThread.h"
52 
53 #undef LOG_TAG
54 #define LOG_TAG "EventThread"
55 
56 using namespace std::chrono_literals;
57 
58 namespace android {
59 
60 using base::StringAppendF;
61 using base::StringPrintf;
62 
63 namespace {
64 
vsyncPeriod(VSyncRequest request)65 auto vsyncPeriod(VSyncRequest request) {
66     return static_cast<std::underlying_type_t<VSyncRequest>>(request);
67 }
68 
toString(VSyncRequest request)69 std::string toString(VSyncRequest request) {
70     switch (request) {
71         case VSyncRequest::None:
72             return "VSyncRequest::None";
73         case VSyncRequest::Single:
74             return "VSyncRequest::Single";
75         case VSyncRequest::SingleSuppressCallback:
76             return "VSyncRequest::SingleSuppressCallback";
77         default:
78             return StringPrintf("VSyncRequest::Periodic{period=%d}", vsyncPeriod(request));
79     }
80 }
81 
toString(const EventThreadConnection & connection)82 std::string toString(const EventThreadConnection& connection) {
83     return StringPrintf("Connection{%p, %s}", &connection,
84                         toString(connection.vsyncRequest).c_str());
85 }
86 
toString(const DisplayEventReceiver::Event & event)87 std::string toString(const DisplayEventReceiver::Event& event) {
88     switch (event.header.type) {
89         case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
90             return StringPrintf("Hotplug{displayId=%s, %s}",
91                                 to_string(event.header.displayId).c_str(),
92                                 event.hotplug.connected ? "connected" : "disconnected");
93         case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
94             return StringPrintf("VSync{displayId=%s, count=%u, expectedPresentationTime=%" PRId64
95                                 "}",
96                                 to_string(event.header.displayId).c_str(), event.vsync.count,
97                                 event.vsync.vsyncData.preferredExpectedPresentationTime());
98         case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE:
99             return StringPrintf("ModeChanged{displayId=%s, modeId=%u}",
100                                 to_string(event.header.displayId).c_str(), event.modeChange.modeId);
101         case DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
102             return StringPrintf("HdcpLevelsChange{displayId=%s, connectedLevel=%d, maxLevel=%d}",
103                                 to_string(event.header.displayId).c_str(),
104                                 event.hdcpLevelsChange.connectedLevel,
105                                 event.hdcpLevelsChange.maxLevel);
106         case DisplayEventReceiver::DISPLAY_EVENT_MODE_REJECTION:
107             return StringPrintf("ModeRejected{displayId=%s, modeId=%u}",
108                                 to_string(event.header.displayId).c_str(),
109                                 event.modeRejection.modeId);
110         default:
111             return "Event{}";
112     }
113 }
114 
makeHotplug(PhysicalDisplayId displayId,nsecs_t timestamp,bool connected)115 DisplayEventReceiver::Event makeHotplug(PhysicalDisplayId displayId, nsecs_t timestamp,
116                                         bool connected) {
117     DisplayEventReceiver::Event event;
118     event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, displayId, timestamp};
119     event.hotplug.connected = connected;
120     return event;
121 }
122 
makeHotplugError(nsecs_t timestamp,int32_t connectionError)123 DisplayEventReceiver::Event makeHotplugError(nsecs_t timestamp, int32_t connectionError) {
124     DisplayEventReceiver::Event event;
125     PhysicalDisplayId unusedDisplayId;
126     event.header = {DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG, unusedDisplayId, timestamp};
127     event.hotplug.connected = false;
128     event.hotplug.connectionError = connectionError;
129     return event;
130 }
131 
makeVSync(PhysicalDisplayId displayId,nsecs_t timestamp,uint32_t count,nsecs_t expectedPresentationTime,nsecs_t deadlineTimestamp)132 DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t timestamp,
133                                       uint32_t count, nsecs_t expectedPresentationTime,
134                                       nsecs_t deadlineTimestamp) {
135     DisplayEventReceiver::Event event;
136     event.header = {DisplayEventReceiver::DISPLAY_EVENT_VSYNC, displayId, timestamp};
137     event.vsync.count = count;
138     event.vsync.vsyncData.preferredFrameTimelineIndex = 0;
139     // Temporarily store the current vsync information in frameTimelines[0], marked as
140     // platform-preferred. When the event is dispatched later, the frame interval at that time is
141     // used with this information to generate multiple frame timeline choices.
142     event.vsync.vsyncData.frameTimelines[0] = {.vsyncId = FrameTimelineInfo::INVALID_VSYNC_ID,
143                                                .deadlineTimestamp = deadlineTimestamp,
144                                                .expectedPresentationTime =
145                                                        expectedPresentationTime};
146     return event;
147 }
148 
makeModeChanged(const scheduler::FrameRateMode & mode)149 DisplayEventReceiver::Event makeModeChanged(const scheduler::FrameRateMode& mode) {
150     DisplayEventReceiver::Event event;
151     event.header = {DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE,
152                     mode.modePtr->getPhysicalDisplayId(), systemTime()};
153     event.modeChange.modeId = ftl::to_underlying(mode.modePtr->getId());
154     event.modeChange.vsyncPeriod = mode.fps.getPeriodNsecs();
155     return event;
156 }
157 
makeFrameRateOverrideEvent(PhysicalDisplayId displayId,FrameRateOverride frameRateOverride)158 DisplayEventReceiver::Event makeFrameRateOverrideEvent(PhysicalDisplayId displayId,
159                                                        FrameRateOverride frameRateOverride) {
160     return DisplayEventReceiver::Event{
161             .header =
162                     DisplayEventReceiver::Event::Header{
163                             .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE,
164                             .displayId = displayId,
165                             .timestamp = systemTime(),
166                     },
167             .frameRateOverride = frameRateOverride,
168     };
169 }
170 
makeFrameRateOverrideFlushEvent(PhysicalDisplayId displayId)171 DisplayEventReceiver::Event makeFrameRateOverrideFlushEvent(PhysicalDisplayId displayId) {
172     return DisplayEventReceiver::Event{
173             .header = DisplayEventReceiver::Event::Header{
174                     .type = DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH,
175                     .displayId = displayId,
176                     .timestamp = systemTime(),
177             }};
178 }
179 
makeHdcpLevelsChange(PhysicalDisplayId displayId,int32_t connectedLevel,int32_t maxLevel)180 DisplayEventReceiver::Event makeHdcpLevelsChange(PhysicalDisplayId displayId,
181                                                  int32_t connectedLevel, int32_t maxLevel) {
182     return DisplayEventReceiver::Event{
183             .header =
184                     DisplayEventReceiver::Event::Header{
185                             .type = DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE,
186                             .displayId = displayId,
187                             .timestamp = systemTime(),
188                     },
189             .hdcpLevelsChange.connectedLevel = connectedLevel,
190             .hdcpLevelsChange.maxLevel = maxLevel,
191     };
192 }
193 
makeModeRejection(PhysicalDisplayId displayId,DisplayModeId modeId)194 DisplayEventReceiver::Event makeModeRejection(PhysicalDisplayId displayId, DisplayModeId modeId) {
195     return DisplayEventReceiver::Event{
196             .header =
197                     DisplayEventReceiver::Event::Header{
198                             .type = DisplayEventReceiver::DISPLAY_EVENT_MODE_REJECTION,
199                             .displayId = displayId,
200                             .timestamp = systemTime(),
201                     },
202             .modeRejection.modeId = ftl::to_underlying(modeId),
203     };
204 }
205 
206 } // namespace
207 
EventThreadConnection(EventThread * eventThread,uid_t callingUid,EventRegistrationFlags eventRegistration)208 EventThreadConnection::EventThreadConnection(EventThread* eventThread, uid_t callingUid,
209                                              EventRegistrationFlags eventRegistration)
210       : mOwnerUid(callingUid),
211         mEventRegistration(eventRegistration),
212         mEventThread(eventThread),
213         mChannel(gui::BitTube::DefaultSize) {}
214 
~EventThreadConnection()215 EventThreadConnection::~EventThreadConnection() {
216     // do nothing here -- clean-up will happen automatically
217     // when the main thread wakes up
218 }
219 
onFirstRef()220 void EventThreadConnection::onFirstRef() {
221     // NOTE: mEventThread doesn't hold a strong reference on us
222     mEventThread->registerDisplayEventConnection(sp<EventThreadConnection>::fromExisting(this));
223 }
224 
stealReceiveChannel(gui::BitTube * outChannel)225 binder::Status EventThreadConnection::stealReceiveChannel(gui::BitTube* outChannel) {
226     std::scoped_lock lock(mLock);
227     if (mChannel.initCheck() != NO_ERROR) {
228         return binder::Status::fromStatusT(NAME_NOT_FOUND);
229     }
230 
231     outChannel->setReceiveFd(mChannel.moveReceiveFd());
232     outChannel->setSendFd(base::unique_fd(dup(mChannel.getSendFd())));
233     return binder::Status::ok();
234 }
235 
setVsyncRate(int rate)236 binder::Status EventThreadConnection::setVsyncRate(int rate) {
237     mEventThread->setVsyncRate(static_cast<uint32_t>(rate),
238                                sp<EventThreadConnection>::fromExisting(this));
239     return binder::Status::ok();
240 }
241 
requestNextVsync()242 binder::Status EventThreadConnection::requestNextVsync() {
243     SFTRACE_CALL();
244     mEventThread->requestNextVsync(sp<EventThreadConnection>::fromExisting(this));
245     return binder::Status::ok();
246 }
247 
getLatestVsyncEventData(ParcelableVsyncEventData * outVsyncEventData)248 binder::Status EventThreadConnection::getLatestVsyncEventData(
249         ParcelableVsyncEventData* outVsyncEventData) {
250     SFTRACE_CALL();
251     outVsyncEventData->vsync =
252             mEventThread->getLatestVsyncEventData(sp<EventThreadConnection>::fromExisting(this),
253                                                   systemTime());
254     return binder::Status::ok();
255 }
256 
getSchedulingPolicy(gui::SchedulingPolicy * outPolicy)257 binder::Status EventThreadConnection::getSchedulingPolicy(gui::SchedulingPolicy* outPolicy) {
258     return gui::getSchedulingPolicy(outPolicy);
259 }
260 
postEvent(const DisplayEventReceiver::Event & event)261 status_t EventThreadConnection::postEvent(const DisplayEventReceiver::Event& event) {
262     constexpr auto toStatus = [](ssize_t size) {
263         return size < 0 ? status_t(size) : status_t(NO_ERROR);
264     };
265 
266     if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE ||
267         event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH) {
268         mPendingEvents.emplace_back(event);
269         if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE) {
270             return status_t(NO_ERROR);
271         }
272 
273         auto size = DisplayEventReceiver::sendEvents(&mChannel, mPendingEvents.data(),
274                                                      mPendingEvents.size());
275         mPendingEvents.clear();
276         return toStatus(size);
277     }
278 
279     auto size = DisplayEventReceiver::sendEvents(&mChannel, &event, 1);
280     return toStatus(size);
281 }
282 
283 // ---------------------------------------------------------------------------
284 
285 EventThread::~EventThread() = default;
286 
287 namespace impl {
288 
EventThread(const char * name,std::shared_ptr<scheduler::VsyncSchedule> vsyncSchedule,android::frametimeline::TokenManager * tokenManager,IEventThreadCallback & callback,std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration)289 EventThread::EventThread(const char* name, std::shared_ptr<scheduler::VsyncSchedule> vsyncSchedule,
290                          android::frametimeline::TokenManager* tokenManager,
291                          IEventThreadCallback& callback, std::chrono::nanoseconds workDuration,
292                          std::chrono::nanoseconds readyDuration)
293       : mThreadName(name),
294         mVsyncTracer(base::StringPrintf("VSYNC-%s", name), 0),
295         mWorkDuration(base::StringPrintf("VsyncWorkDuration-%s", name), workDuration),
296         mReadyDuration(readyDuration),
297         mVsyncSchedule(std::move(vsyncSchedule)),
298         mVsyncRegistration(mVsyncSchedule->getDispatch(), createDispatchCallback(), name),
299         mTokenManager(tokenManager),
300         mCallback(callback) {
301     mThread = std::thread([this]() NO_THREAD_SAFETY_ANALYSIS {
302         std::unique_lock<std::mutex> lock(mMutex);
303         threadMain(lock);
304     });
305 
306     pthread_setname_np(mThread.native_handle(), mThreadName);
307 
308     pid_t tid = pthread_gettid_np(mThread.native_handle());
309 
310     // Use SCHED_FIFO to minimize jitter
311     constexpr int EVENT_THREAD_PRIORITY = 2;
312     struct sched_param param = {0};
313     param.sched_priority = EVENT_THREAD_PRIORITY;
314     if (pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param) != 0) {
315         ALOGE("Couldn't set SCHED_FIFO for EventThread");
316     }
317 
318     set_sched_policy(tid, SP_FOREGROUND);
319 }
320 
~EventThread()321 EventThread::~EventThread() {
322     {
323         std::lock_guard<std::mutex> lock(mMutex);
324         mState = State::Quit;
325         mCondition.notify_all();
326     }
327     mThread.join();
328 }
329 
setDuration(std::chrono::nanoseconds workDuration,std::chrono::nanoseconds readyDuration)330 void EventThread::setDuration(std::chrono::nanoseconds workDuration,
331                               std::chrono::nanoseconds readyDuration) {
332     std::lock_guard<std::mutex> lock(mMutex);
333     mWorkDuration = workDuration;
334     mReadyDuration = readyDuration;
335 
336     mVsyncRegistration.update({.workDuration = mWorkDuration.get().count(),
337                                .readyDuration = mReadyDuration.count(),
338                                .lastVsync = mLastVsyncCallbackTime.ns(),
339                                .committedVsyncOpt = mLastCommittedVsyncTime.ns()});
340 }
341 
createEventConnection(EventRegistrationFlags eventRegistration) const342 sp<EventThreadConnection> EventThread::createEventConnection(
343         EventRegistrationFlags eventRegistration) const {
344     auto connection = sp<EventThreadConnection>::make(const_cast<EventThread*>(this),
345                                                       IPCThreadState::self()->getCallingUid(),
346                                                       eventRegistration);
347     if (FlagManager::getInstance().misc1()) {
348         const int policy = SCHED_FIFO;
349         connection->setMinSchedulerPolicy(policy, sched_get_priority_min(policy));
350     }
351     return connection;
352 }
353 
registerDisplayEventConnection(const sp<EventThreadConnection> & connection)354 status_t EventThread::registerDisplayEventConnection(const sp<EventThreadConnection>& connection) {
355     std::lock_guard<std::mutex> lock(mMutex);
356 
357     // this should never happen
358     auto it = std::find(mDisplayEventConnections.cbegin(),
359             mDisplayEventConnections.cend(), connection);
360     if (it != mDisplayEventConnections.cend()) {
361         ALOGW("DisplayEventConnection %p already exists", connection.get());
362         mCondition.notify_all();
363         return ALREADY_EXISTS;
364     }
365 
366     mDisplayEventConnections.push_back(connection);
367     mCondition.notify_all();
368     return NO_ERROR;
369 }
370 
removeDisplayEventConnectionLocked(const wp<EventThreadConnection> & connection)371 void EventThread::removeDisplayEventConnectionLocked(const wp<EventThreadConnection>& connection) {
372     auto it = std::find(mDisplayEventConnections.cbegin(),
373             mDisplayEventConnections.cend(), connection);
374     if (it != mDisplayEventConnections.cend()) {
375         mDisplayEventConnections.erase(it);
376     }
377 }
378 
setVsyncRate(uint32_t rate,const sp<EventThreadConnection> & connection)379 void EventThread::setVsyncRate(uint32_t rate, const sp<EventThreadConnection>& connection) {
380     if (static_cast<std::underlying_type_t<VSyncRequest>>(rate) < 0) {
381         return;
382     }
383 
384     std::lock_guard<std::mutex> lock(mMutex);
385 
386     const auto request = rate == 0 ? VSyncRequest::None : static_cast<VSyncRequest>(rate);
387     if (connection->vsyncRequest != request) {
388         connection->vsyncRequest = request;
389         mCondition.notify_all();
390     }
391 }
392 
requestNextVsync(const sp<EventThreadConnection> & connection)393 void EventThread::requestNextVsync(const sp<EventThreadConnection>& connection) {
394     mCallback.resync();
395 
396     std::lock_guard<std::mutex> lock(mMutex);
397 
398     if (connection->vsyncRequest == VSyncRequest::None) {
399         connection->vsyncRequest = VSyncRequest::Single;
400         mCondition.notify_all();
401     } else if (connection->vsyncRequest == VSyncRequest::SingleSuppressCallback) {
402         connection->vsyncRequest = VSyncRequest::Single;
403     }
404 }
405 
getLatestVsyncEventData(const sp<EventThreadConnection> & connection,nsecs_t now) const406 VsyncEventData EventThread::getLatestVsyncEventData(const sp<EventThreadConnection>& connection,
407                                                     nsecs_t now) const {
408     // Resync so that the vsync is accurate with hardware. getLatestVsyncEventData is an alternate
409     // way to get vsync data (instead of posting callbacks to Choreographer).
410     mCallback.resync();
411 
412     VsyncEventData vsyncEventData;
413     const Period frameInterval = mCallback.getVsyncPeriod(connection->mOwnerUid);
414     vsyncEventData.frameInterval = frameInterval.ns();
415     const auto [presentTime, deadline] = [&]() -> std::pair<nsecs_t, nsecs_t> {
416         std::lock_guard<std::mutex> lock(mMutex);
417         const auto vsyncTime = mVsyncSchedule->getTracker().nextAnticipatedVSyncTimeFrom(
418                 now + mWorkDuration.get().count() + mReadyDuration.count());
419         return {vsyncTime, vsyncTime - mReadyDuration.count()};
420     }();
421     generateFrameTimeline(vsyncEventData, frameInterval.ns(), now, presentTime, deadline);
422     if (FlagManager::getInstance().vrr_config()) {
423         mCallback.onExpectedPresentTimePosted(TimePoint::fromNs(presentTime));
424     }
425     return vsyncEventData;
426 }
427 
enableSyntheticVsync(bool enable)428 void EventThread::enableSyntheticVsync(bool enable) {
429     std::lock_guard<std::mutex> lock(mMutex);
430     if (!mVSyncState || mVSyncState->synthetic == enable) {
431         return;
432     }
433 
434     mVSyncState->synthetic = enable;
435     mCondition.notify_all();
436 }
437 
omitVsyncDispatching(bool omitted)438 void EventThread::omitVsyncDispatching(bool omitted) {
439     std::lock_guard<std::mutex> lock(mMutex);
440     if (!mVSyncState || mVSyncState->omitted == omitted) {
441         return;
442     }
443 
444     mVSyncState->omitted = omitted;
445     mCondition.notify_all();
446 }
447 
onVsync(nsecs_t vsyncTime,nsecs_t wakeupTime,nsecs_t readyTime)448 void EventThread::onVsync(nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) {
449     std::lock_guard<std::mutex> lock(mMutex);
450     mLastVsyncCallbackTime = TimePoint::fromNs(vsyncTime);
451 
452     LOG_FATAL_IF(!mVSyncState);
453     mVsyncTracer = (mVsyncTracer + 1) % 2;
454     mPendingEvents.push_back(makeVSync(mVsyncSchedule->getPhysicalDisplayId(), wakeupTime,
455                                        ++mVSyncState->count, vsyncTime, readyTime));
456     mCondition.notify_all();
457 }
458 
onHotplugReceived(PhysicalDisplayId displayId,bool connected)459 void EventThread::onHotplugReceived(PhysicalDisplayId displayId, bool connected) {
460     std::lock_guard<std::mutex> lock(mMutex);
461 
462     mPendingEvents.push_back(makeHotplug(displayId, systemTime(), connected));
463     mCondition.notify_all();
464 }
465 
onHotplugConnectionError(int32_t errorCode)466 void EventThread::onHotplugConnectionError(int32_t errorCode) {
467     std::lock_guard<std::mutex> lock(mMutex);
468 
469     mPendingEvents.push_back(makeHotplugError(systemTime(), errorCode));
470     mCondition.notify_all();
471 }
472 
onModeChanged(const scheduler::FrameRateMode & mode)473 void EventThread::onModeChanged(const scheduler::FrameRateMode& mode) {
474     std::lock_guard<std::mutex> lock(mMutex);
475 
476     mPendingEvents.push_back(makeModeChanged(mode));
477     mCondition.notify_all();
478 }
479 
onFrameRateOverridesChanged(PhysicalDisplayId displayId,std::vector<FrameRateOverride> overrides)480 void EventThread::onFrameRateOverridesChanged(PhysicalDisplayId displayId,
481                                               std::vector<FrameRateOverride> overrides) {
482     std::lock_guard<std::mutex> lock(mMutex);
483 
484     for (auto frameRateOverride : overrides) {
485         mPendingEvents.push_back(makeFrameRateOverrideEvent(displayId, frameRateOverride));
486     }
487     mPendingEvents.push_back(makeFrameRateOverrideFlushEvent(displayId));
488 
489     mCondition.notify_all();
490 }
491 
onHdcpLevelsChanged(PhysicalDisplayId displayId,int32_t connectedLevel,int32_t maxLevel)492 void EventThread::onHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
493                                       int32_t maxLevel) {
494     std::lock_guard<std::mutex> lock(mMutex);
495 
496     mPendingEvents.push_back(makeHdcpLevelsChange(displayId, connectedLevel, maxLevel));
497     mCondition.notify_all();
498 }
499 
onModeRejected(PhysicalDisplayId displayId,DisplayModeId modeId)500 void EventThread::onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) {
501     std::lock_guard<std::mutex> lock(mMutex);
502 
503     mPendingEvents.push_back(makeModeRejection(displayId, modeId));
504     mCondition.notify_all();
505 }
506 
507 // Merge lists of buffer stuffed Uids
addBufferStuffedUids(BufferStuffingMap bufferStuffedUids)508 void EventThread::addBufferStuffedUids(BufferStuffingMap bufferStuffedUids) {
509     std::lock_guard<std::mutex> lock(mMutex);
510     for (auto& [uid, count] : bufferStuffedUids) {
511         mBufferStuffedUids.emplace_or_replace(uid, count);
512     }
513 }
514 
threadMain(std::unique_lock<std::mutex> & lock)515 void EventThread::threadMain(std::unique_lock<std::mutex>& lock) {
516     DisplayEventConsumers consumers;
517 
518     while (mState != State::Quit) {
519         std::optional<DisplayEventReceiver::Event> event;
520 
521         // Determine next event to dispatch.
522         if (!mPendingEvents.empty()) {
523             event = mPendingEvents.front();
524             mPendingEvents.pop_front();
525 
526             if (event->header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
527                 if (event->hotplug.connectionError == 0) {
528                     if (event->hotplug.connected && !mVSyncState) {
529                         mVSyncState.emplace();
530                     } else if (!event->hotplug.connected &&
531                                mVsyncSchedule->getPhysicalDisplayId() == event->header.displayId) {
532                         mVSyncState.reset();
533                     }
534                 } else {
535                     // Ignore vsync stuff on an error.
536                 }
537             }
538         }
539 
540         bool vsyncRequested = false;
541 
542         // Find connections that should consume this event.
543         auto it = mDisplayEventConnections.begin();
544         while (it != mDisplayEventConnections.end()) {
545             if (const auto connection = it->promote()) {
546                 if (event && shouldConsumeEvent(*event, connection)) {
547                     consumers.push_back(connection);
548                 }
549 
550                 vsyncRequested |= connection->vsyncRequest != VSyncRequest::None;
551 
552                 ++it;
553             } else {
554                 it = mDisplayEventConnections.erase(it);
555             }
556         }
557 
558         if (!consumers.empty()) {
559             dispatchEvent(*event, consumers);
560             consumers.clear();
561         }
562 
563         if (mVSyncState && vsyncRequested) {
564             const bool vsyncOmitted =
565                     FlagManager::getInstance().no_vsyncs_on_screen_off() && mVSyncState->omitted;
566             if (vsyncOmitted) {
567                 mState = State::Idle;
568                 SFTRACE_INT("VsyncPendingScreenOn", 1);
569             } else {
570                 mState = mVSyncState->synthetic ? State::SyntheticVSync : State::VSync;
571                 if (FlagManager::getInstance().no_vsyncs_on_screen_off()) {
572                     SFTRACE_INT("VsyncPendingScreenOn", 0);
573                 }
574             }
575         } else {
576             ALOGW_IF(!mVSyncState, "Ignoring VSYNC request while display is disconnected");
577             mState = State::Idle;
578         }
579 
580         if (mState == State::VSync) {
581             const auto scheduleResult = mVsyncRegistration.schedule(
582                     {.workDuration = mWorkDuration.get().count(),
583                      .readyDuration = mReadyDuration.count(),
584                      .lastVsync = mLastVsyncCallbackTime.ns(),
585                      .committedVsyncOpt = mLastCommittedVsyncTime.ns()});
586             LOG_ALWAYS_FATAL_IF(!scheduleResult, "Error scheduling callback");
587         } else {
588             mVsyncRegistration.cancel();
589         }
590 
591         if (!mPendingEvents.empty()) {
592             continue;
593         }
594 
595         // Wait for event or client registration/request.
596         if (mState == State::Idle) {
597             mCondition.wait(lock);
598         } else {
599             // Generate a fake VSYNC after a long timeout in case the driver stalls. When the
600             // display is off, keep feeding clients at 60 Hz.
601             const std::chrono::nanoseconds timeout =
602                     mState == State::SyntheticVSync ? 16ms : 1000ms;
603             if (mCondition.wait_for(lock, timeout) == std::cv_status::timeout) {
604                 if (mState == State::VSync) {
605                     ALOGW("Faking VSYNC due to driver stall for thread %s", mThreadName);
606                 }
607 
608                 LOG_FATAL_IF(!mVSyncState);
609                 const auto now = systemTime(SYSTEM_TIME_MONOTONIC);
610                 const auto deadlineTimestamp = now + timeout.count();
611                 const auto expectedVSyncTime = deadlineTimestamp + timeout.count();
612                 mPendingEvents.push_back(makeVSync(mVsyncSchedule->getPhysicalDisplayId(), now,
613                                                    ++mVSyncState->count, expectedVSyncTime,
614                                                    deadlineTimestamp));
615             }
616         }
617     }
618     // cancel any pending vsync event before exiting
619     mVsyncRegistration.cancel();
620 }
621 
shouldConsumeEvent(const DisplayEventReceiver::Event & event,const sp<EventThreadConnection> & connection) const622 bool EventThread::shouldConsumeEvent(const DisplayEventReceiver::Event& event,
623                                      const sp<EventThreadConnection>& connection) const {
624     const auto throttleVsync = [&]() REQUIRES(mMutex) {
625         const auto& vsyncData = event.vsync.vsyncData;
626         if (connection->frameRate.isValid()) {
627             return !mVsyncSchedule->getTracker()
628                             .isVSyncInPhase(vsyncData.preferredExpectedPresentationTime(),
629                                             connection->frameRate);
630         }
631 
632         const auto expectedPresentTime =
633                 TimePoint::fromNs(event.vsync.vsyncData.preferredExpectedPresentationTime());
634         return mCallback.throttleVsync(expectedPresentTime, connection->mOwnerUid);
635     };
636 
637     switch (event.header.type) {
638         case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
639             return true;
640 
641         case DisplayEventReceiver::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
642             return true;
643 
644         case DisplayEventReceiver::DISPLAY_EVENT_MODE_CHANGE: {
645             return connection->mEventRegistration.test(
646                     gui::ISurfaceComposer::EventRegistration::modeChanged);
647         }
648 
649         case DisplayEventReceiver::DISPLAY_EVENT_VSYNC:
650             switch (connection->vsyncRequest) {
651                 case VSyncRequest::None:
652                     return false;
653                 case VSyncRequest::SingleSuppressCallback:
654                     connection->vsyncRequest = VSyncRequest::None;
655                     return false;
656                 case VSyncRequest::Single: {
657                     if (throttleVsync()) {
658                         return false;
659                     }
660                     connection->vsyncRequest = VSyncRequest::SingleSuppressCallback;
661                     return true;
662                 }
663                 case VSyncRequest::Periodic:
664                     if (throttleVsync()) {
665                         return false;
666                     }
667                     return true;
668                 default:
669                     // We don't throttle vsync if the app set a vsync request rate
670                     // since there is no easy way to do that and this is a very
671                     // rare case
672                     return event.vsync.count % vsyncPeriod(connection->vsyncRequest) == 0;
673             }
674 
675         case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE:
676             [[fallthrough]];
677         case DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH:
678             return connection->mEventRegistration.test(
679                     gui::ISurfaceComposer::EventRegistration::frameRateOverride);
680 
681         default:
682             return false;
683     }
684 }
685 
generateToken(nsecs_t timestamp,nsecs_t deadlineTimestamp,nsecs_t expectedPresentationTime) const686 int64_t EventThread::generateToken(nsecs_t timestamp, nsecs_t deadlineTimestamp,
687                                    nsecs_t expectedPresentationTime) const {
688     if (mTokenManager != nullptr) {
689         return mTokenManager->generateTokenForPredictions(
690                 {timestamp, deadlineTimestamp, expectedPresentationTime});
691     }
692     return FrameTimelineInfo::INVALID_VSYNC_ID;
693 }
694 
generateFrameTimeline(VsyncEventData & outVsyncEventData,nsecs_t frameInterval,nsecs_t timestamp,nsecs_t preferredExpectedPresentationTime,nsecs_t preferredDeadlineTimestamp) const695 void EventThread::generateFrameTimeline(VsyncEventData& outVsyncEventData, nsecs_t frameInterval,
696                                         nsecs_t timestamp,
697                                         nsecs_t preferredExpectedPresentationTime,
698                                         nsecs_t preferredDeadlineTimestamp) const {
699     uint32_t currentIndex = 0;
700     // Add 1 to ensure the preferredFrameTimelineIndex entry (when multiplier == 0) is included.
701     for (int64_t multiplier = -VsyncEventData::kFrameTimelinesCapacity + 1;
702          currentIndex < VsyncEventData::kFrameTimelinesCapacity; multiplier++) {
703         nsecs_t deadlineTimestamp = preferredDeadlineTimestamp + multiplier * frameInterval;
704         // Valid possible frame timelines must have future values, so find a later frame timeline.
705         if (deadlineTimestamp <= timestamp) {
706             continue;
707         }
708 
709         nsecs_t expectedPresentationTime =
710                 preferredExpectedPresentationTime + multiplier * frameInterval;
711         if (expectedPresentationTime >= preferredExpectedPresentationTime +
712                     scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count()) {
713             if (currentIndex == 0) {
714                 ALOGW("%s: Expected present time is too far in the future but no timelines are "
715                       "valid. preferred EPT=%" PRId64 ", Calculated EPT=%" PRId64
716                       ", multiplier=%" PRId64 ", frameInterval=%" PRId64 ", threshold=%" PRId64,
717                       __func__, preferredExpectedPresentationTime, expectedPresentationTime,
718                       multiplier, frameInterval,
719                       static_cast<int64_t>(
720                               scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count()));
721             }
722             break;
723         }
724 
725         if (multiplier == 0) {
726             outVsyncEventData.preferredFrameTimelineIndex = currentIndex;
727         }
728 
729         outVsyncEventData.frameTimelines[currentIndex] =
730                 {.vsyncId = generateToken(timestamp, deadlineTimestamp, expectedPresentationTime),
731                  .deadlineTimestamp = deadlineTimestamp,
732                  .expectedPresentationTime = expectedPresentationTime};
733         currentIndex++;
734     }
735 
736     if (currentIndex == 0) {
737         ALOGW("%s: No timelines are valid. preferred EPT=%" PRId64 ", frameInterval=%" PRId64
738               ", threshold=%" PRId64,
739               __func__, preferredExpectedPresentationTime, frameInterval,
740               static_cast<int64_t>(scheduler::VsyncConfig::kEarlyLatchMaxThreshold.count()));
741         outVsyncEventData.frameTimelines[currentIndex] =
742                 {.vsyncId = generateToken(timestamp, preferredDeadlineTimestamp,
743                                           preferredExpectedPresentationTime),
744                  .deadlineTimestamp = preferredDeadlineTimestamp,
745                  .expectedPresentationTime = preferredExpectedPresentationTime};
746         currentIndex++;
747     }
748 
749     outVsyncEventData.frameTimelinesLength = currentIndex;
750 }
751 
dispatchEvent(const DisplayEventReceiver::Event & event,const DisplayEventConsumers & consumers)752 void EventThread::dispatchEvent(const DisplayEventReceiver::Event& event,
753                                 const DisplayEventConsumers& consumers) {
754     // List of Uids that have been sent vsync data with queued buffer count.
755     // Used to keep track of which Uids can be removed from the map of
756     // buffer stuffed clients.
757     ftl::SmallVector<uid_t, 10> uidsPostedQueuedBuffers;
758     for (const auto& consumer : consumers) {
759         DisplayEventReceiver::Event copy = event;
760         if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC) {
761             const Period frameInterval = mCallback.getVsyncPeriod(consumer->mOwnerUid);
762             copy.vsync.vsyncData.frameInterval = frameInterval.ns();
763             generateFrameTimeline(copy.vsync.vsyncData, frameInterval.ns(), copy.header.timestamp,
764                                   event.vsync.vsyncData.preferredExpectedPresentationTime(),
765                                   event.vsync.vsyncData.preferredDeadlineTimestamp());
766         }
767         auto it = mBufferStuffedUids.find(consumer->mOwnerUid);
768         if (it != mBufferStuffedUids.end()) {
769             copy.vsync.vsyncData.numberQueuedBuffers = it->second;
770             uidsPostedQueuedBuffers.emplace_back(consumer->mOwnerUid);
771         } else {
772             copy.vsync.vsyncData.numberQueuedBuffers = 0;
773         }
774         switch (consumer->postEvent(copy)) {
775             case NO_ERROR:
776                 break;
777 
778             case -EAGAIN:
779                 // TODO: Try again if pipe is full.
780                 ALOGW("Failed dispatching %s for %s", toString(event).c_str(),
781                       toString(*consumer).c_str());
782                 break;
783 
784             default:
785                 // Treat EPIPE and other errors as fatal.
786                 removeDisplayEventConnectionLocked(consumer);
787         }
788     }
789     // The clients that have already received the queued buffer count
790     // can be removed from the buffer stuffed Uid list to avoid
791     // being sent duplicate messages.
792     for (auto uid : uidsPostedQueuedBuffers) {
793         mBufferStuffedUids.erase(uid);
794     }
795     if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_VSYNC &&
796         FlagManager::getInstance().vrr_config()) {
797         mLastCommittedVsyncTime =
798                 TimePoint::fromNs(event.vsync.vsyncData.preferredExpectedPresentationTime());
799         mCallback.onExpectedPresentTimePosted(mLastCommittedVsyncTime);
800     }
801 }
802 
dump(std::string & result) const803 void EventThread::dump(std::string& result) const {
804     std::lock_guard<std::mutex> lock(mMutex);
805 
806     StringAppendF(&result, "%s: state=%s VSyncState=", mThreadName, toCString(mState));
807     if (mVSyncState) {
808         StringAppendF(&result, "{displayId=%s, count=%u%s}\n",
809                       to_string(mVsyncSchedule->getPhysicalDisplayId()).c_str(), mVSyncState->count,
810                       mVSyncState->synthetic ? ", synthetic" : "");
811     } else {
812         StringAppendF(&result, "none\n");
813     }
814 
815     const auto relativeLastCallTime =
816             ticks<std::milli, float>(mLastVsyncCallbackTime - TimePoint::now());
817     const auto relativeLastCommittedTime =
818             ticks<std::milli, float>(mLastCommittedVsyncTime - TimePoint::now());
819     StringAppendF(&result, "mWorkDuration=%.2f mReadyDuration=%.2f last vsync time ",
820                   mWorkDuration.get().count() / 1e6f, mReadyDuration.count() / 1e6f);
821     StringAppendF(&result, "%.2fms relative to now\n", relativeLastCallTime);
822     StringAppendF(&result, " with vsync committed at %.2fms", relativeLastCommittedTime);
823 
824     StringAppendF(&result, "  pending events (count=%zu):\n", mPendingEvents.size());
825     for (const auto& event : mPendingEvents) {
826         StringAppendF(&result, "    %s\n", toString(event).c_str());
827     }
828 
829     StringAppendF(&result, "  connections (count=%zu):\n", mDisplayEventConnections.size());
830     for (const auto& ptr : mDisplayEventConnections) {
831         if (const auto connection = ptr.promote()) {
832             StringAppendF(&result, "    %s\n", toString(*connection).c_str());
833         }
834     }
835     result += '\n';
836 }
837 
toCString(State state)838 const char* EventThread::toCString(State state) {
839     switch (state) {
840         case State::Idle:
841             return "Idle";
842         case State::Quit:
843             return "Quit";
844         case State::SyntheticVSync:
845             return "SyntheticVSync";
846         case State::VSync:
847             return "VSync";
848     }
849 }
850 
onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule> schedule)851 void EventThread::onNewVsyncSchedule(std::shared_ptr<scheduler::VsyncSchedule> schedule) {
852     // Hold onto the old registration until after releasing the mutex to avoid deadlock.
853     scheduler::VSyncCallbackRegistration oldRegistration =
854             onNewVsyncScheduleInternal(std::move(schedule));
855 }
856 
onNewVsyncScheduleInternal(std::shared_ptr<scheduler::VsyncSchedule> schedule)857 scheduler::VSyncCallbackRegistration EventThread::onNewVsyncScheduleInternal(
858         std::shared_ptr<scheduler::VsyncSchedule> schedule) {
859     std::lock_guard<std::mutex> lock(mMutex);
860     const bool reschedule = mVsyncRegistration.cancel() == scheduler::CancelResult::Cancelled;
861     mVsyncSchedule = std::move(schedule);
862     auto oldRegistration =
863             std::exchange(mVsyncRegistration,
864                           scheduler::VSyncCallbackRegistration(mVsyncSchedule->getDispatch(),
865                                                                createDispatchCallback(),
866                                                                mThreadName));
867     if (reschedule) {
868         mVsyncRegistration.schedule({.workDuration = mWorkDuration.get().count(),
869                                      .readyDuration = mReadyDuration.count(),
870                                      .lastVsync = mLastVsyncCallbackTime.ns(),
871                                      .committedVsyncOpt = mLastCommittedVsyncTime.ns()});
872     }
873     return oldRegistration;
874 }
875 
createDispatchCallback()876 scheduler::VSyncDispatch::Callback EventThread::createDispatchCallback() {
877     return [this](nsecs_t vsyncTime, nsecs_t wakeupTime, nsecs_t readyTime) {
878         onVsync(vsyncTime, wakeupTime, readyTime);
879     };
880 }
881 
882 } // namespace impl
883 } // namespace android
884 
885 // TODO(b/129481165): remove the #pragma below and fix conversion issues
886 #pragma clang diagnostic pop // ignored "-Wconversion"
887