Skip to content

Commit 992d0eb

Browse files
RamIndaniAndroid (Google) Code Review
authored andcommitted
Merge "[SF] Update onModeChanged to send appVsyncOffset [SF] Update onModeChanged to send presentationDeadline" into main
2 parents 66b1c5a + 64c1d99 commit 992d0eb

15 files changed

+135
-50
lines changed

libs/gui/Choreographer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ void Choreographer::dispatchHotplugConnectionError(nsecs_t, int32_t connectionEr
349349
this, connectionError);
350350
}
351351

352-
void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
352+
void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t, nsecs_t,
353+
nsecs_t) {
353354
LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
354355
}
355356

libs/gui/DisplayEventDispatcher.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
194194
break;
195195
case DisplayEventType::DISPLAY_EVENT_MODE_CHANGE:
196196
dispatchModeChanged(ev.header.timestamp, ev.header.displayId,
197-
ev.modeChange.modeId, ev.modeChange.vsyncPeriod);
197+
ev.modeChange.modeId, ev.modeChange.vsyncPeriod,
198+
ev.modeChange.appVsyncOffset,
199+
ev.modeChange.presentationDeadline);
198200
break;
199201
case DisplayEventType::DISPLAY_EVENT_NULL:
200202
dispatchNullEvent(ev.header.timestamp, ev.header.displayId);

libs/gui/include/gui/Choreographer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ class Choreographer : public DisplayEventDispatcher, public MessageHandler {
123123
void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
124124
void dispatchHotplugConnectionError(nsecs_t timestamp, int32_t connectionError) override;
125125
void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
126-
nsecs_t vsyncPeriod) override;
126+
nsecs_t vsyncPeriod, nsecs_t appVsyncOffset,
127+
nsecs_t presentationDeadline) override;
127128
void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
128129
void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
129130
std::vector<FrameRateOverride> overrides) override;

libs/gui/include/gui/DisplayEventDispatcher.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class DisplayEventDispatcher : public LooperCallback {
5959
virtual void dispatchHotplugConnectionError(nsecs_t timestamp, int32_t connectionError) = 0;
6060

6161
virtual void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
62-
nsecs_t vsyncPeriod) = 0;
62+
nsecs_t vsyncPeriod, nsecs_t appVsyncOffset,
63+
nsecs_t presentationDeadline) = 0;
6364
// AChoreographer-specific hook for processing null-events so that looper
6465
// can be properly poked.
6566
virtual void dispatchNullEvent(nsecs_t timestamp, PhysicalDisplayId displayId) = 0;

libs/gui/include/gui/DisplayEventReceiver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class DisplayEventReceiver {
9595
struct ModeChange {
9696
int32_t modeId;
9797
nsecs_t vsyncPeriod __attribute__((aligned(8)));
98+
nsecs_t appVsyncOffset __attribute__((aligned(8)));
99+
nsecs_t presentationDeadline __attribute__((aligned(8)));
98100
};
99101

100102
struct ModeRejection {

libs/gui/tests/DisplayEventStructLayout_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ TEST(DisplayEventStructLayoutTest, TestEventAlignment) {
6464

6565
CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, modeId, 0);
6666
CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, vsyncPeriod, 8);
67+
CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, appVsyncOffset, 16);
68+
CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, presentationDeadline, 24);
6769

6870
CHECK_OFFSET(DisplayEventReceiver::Event::FrameRateOverride, uid, 0);
6971
CHECK_OFFSET(DisplayEventReceiver::Event::FrameRateOverride, frameRateHz, 8);

services/surfaceflinger/Scheduler/EventThread.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include <utils/Errors.h>
4444

45+
#include <Scheduler/Scheduler.h>
4546
#include <common/FlagManager.h>
4647
#include <scheduler/FrameRateMode.h>
4748
#include <scheduler/VsyncConfig.h>
@@ -96,8 +97,11 @@ std::string toString(const DisplayEventReceiver::Event& event) {
9697
to_string(event.header.displayId).c_str(), event.vsync.count,
9798
event.vsync.vsyncData.preferredExpectedPresentationTime());
9899
case DisplayEventType::DISPLAY_EVENT_MODE_CHANGE:
99-
return StringPrintf("ModeChanged{displayId=%s, modeId=%u}",
100-
to_string(event.header.displayId).c_str(), event.modeChange.modeId);
100+
return StringPrintf("ModeChanged{displayId=%s, modeId=%u, appVsyncOffset=%" PRId64
101+
", presentationDeadline=%" PRId64 "}",
102+
to_string(event.header.displayId).c_str(), event.modeChange.modeId,
103+
event.modeChange.appVsyncOffset,
104+
event.modeChange.presentationDeadline);
101105
case DisplayEventType::DISPLAY_EVENT_HDCP_LEVELS_CHANGE:
102106
return StringPrintf("HdcpLevelsChange{displayId=%s, connectedLevel=%d, maxLevel=%d}",
103107
to_string(event.header.displayId).c_str(),
@@ -153,12 +157,17 @@ DisplayEventReceiver::Event makeVSync(PhysicalDisplayId displayId, nsecs_t times
153157
return event;
154158
}
155159

156-
DisplayEventReceiver::Event makeModeChanged(const scheduler::FrameRateMode& mode) {
160+
DisplayEventReceiver::Event makeModeChanged(const scheduler::FrameRateMode& mode,
161+
scheduler::VsyncConfigSet config) {
157162
DisplayEventReceiver::Event event;
158163
event.header = {DisplayEventType::DISPLAY_EVENT_MODE_CHANGE,
159164
mode.modePtr->getPhysicalDisplayId(), systemTime()};
160165
event.modeChange.modeId = ftl::to_underlying(mode.modePtr->getId());
161166
event.modeChange.vsyncPeriod = mode.fps.getPeriodNsecs();
167+
event.modeChange.appVsyncOffset = config.late.appOffset;
168+
event.modeChange.presentationDeadline =
169+
scheduler::Scheduler::getPresentationDeadline(mode.fps,
170+
Duration::fromNs(config.late.sfOffset));
162171
return event;
163172
}
164173

@@ -478,10 +487,11 @@ void EventThread::onHotplugConnectionError(int32_t errorCode) {
478487
mCondition.notify_all();
479488
}
480489

481-
void EventThread::onModeChanged(const scheduler::FrameRateMode& mode) {
490+
void EventThread::onModeChanged(const scheduler::FrameRateMode& mode,
491+
scheduler::VsyncConfigSet config) {
482492
std::lock_guard<std::mutex> lock(mMutex);
483493

484-
mPendingEvents.push_back(makeModeChanged(mode));
494+
mPendingEvents.push_back(makeModeChanged(mode, config));
485495
mCondition.notify_all();
486496
}
487497

services/surfaceflinger/Scheduler/EventThread.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <utils/Errors.h>
2626

2727
#include <scheduler/FrameRateMode.h>
28+
#include <scheduler/VsyncConfig.h>
2829
#include <condition_variable>
2930
#include <cstdint>
3031
#include <deque>
@@ -113,8 +114,9 @@ class EventThread {
113114

114115
virtual void onHotplugConnectionError(int32_t connectionError) = 0;
115116

116-
// called when SF changes the active mode and apps needs to be notified about the change
117-
virtual void onModeChanged(const scheduler::FrameRateMode&) = 0;
117+
// called when SF changes the active mode or updates the WorkDuration
118+
// and apps needs to be notified about the change
119+
virtual void onModeChanged(const scheduler::FrameRateMode&, scheduler::VsyncConfigSet) = 0;
118120

119121
// called when SF rejects the mode change request
120122
virtual void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) = 0;
@@ -177,7 +179,7 @@ class EventThread : public android::EventThread {
177179

178180
void onHotplugConnectionError(int32_t connectionError) override;
179181

180-
void onModeChanged(const scheduler::FrameRateMode&) override;
182+
void onModeChanged(const scheduler::FrameRateMode&, scheduler::VsyncConfigSet) override;
181183

182184
void onModeRejected(PhysicalDisplayId displayId, DisplayModeId modeId) override;
183185

services/surfaceflinger/Scheduler/Scheduler.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ bool Scheduler::onDisplayModeChanged(PhysicalDisplayId displayId, const FrameRat
461461
}
462462

463463
if (hasEventThreads()) {
464-
eventThreadFor(Cycle::Render).onModeChanged(mode);
464+
const auto vsyncConfigSet = getVsyncConfigsForRefreshRate(mode.fps);
465+
eventThreadFor(Cycle::Render).onModeChanged(mode, vsyncConfigSet);
465466
}
466467

467468
return isPacesetter;
@@ -495,7 +496,8 @@ void Scheduler::emitModeChangeIfNeeded() {
495496
mPolicy.emittedModeOpt = mode;
496497

497498
if (hasEventThreads()) {
498-
eventThreadFor(Cycle::Render).onModeChanged(mode);
499+
const auto vsyncConfigSet = getVsyncConfigsForRefreshRate(mode.fps);
500+
eventThreadFor(Cycle::Render).onModeChanged(mode, vsyncConfigSet);
499501
}
500502
}
501503

@@ -528,15 +530,19 @@ void Scheduler::updatePhaseConfiguration(PhysicalDisplayId displayId, Fps refres
528530
}
529531
#pragma clang diagnostic pop
530532

531-
void Scheduler::reloadPhaseConfiguration(Fps refreshRate, Duration minSfDuration,
533+
void Scheduler::reloadPhaseConfiguration(const FrameRateMode& mode, Duration minSfDuration,
532534
Duration maxSfDuration, Duration appDuration) {
533535
const auto currentConfigs = [=, this] {
534536
std::scoped_lock lock{mVsyncConfigLock};
535-
mVsyncConfiguration = std::make_unique<impl::WorkDuration>(refreshRate, minSfDuration,
537+
mVsyncConfiguration = std::make_unique<impl::WorkDuration>(mode.fps, minSfDuration,
536538
maxSfDuration, appDuration);
537539
return mVsyncConfiguration->getCurrentConfigs();
538540
}();
539-
setVsyncConfig(mVsyncModulator->setVsyncConfigSet(currentConfigs), refreshRate.getPeriod());
541+
setVsyncConfig(mVsyncModulator->setVsyncConfigSet(currentConfigs), mode.fps.getPeriod());
542+
if (hasEventThreads()) {
543+
const auto vsyncConfigSet = getVsyncConfigsForRefreshRate(mode.fps);
544+
eventThreadFor(Cycle::Render).onModeChanged(mode, vsyncConfigSet);
545+
}
540546
}
541547

542548
void Scheduler::setActiveDisplayPowerModeForRefreshRateStats(hal::PowerMode powerMode) {

services/surfaceflinger/Scheduler/Scheduler.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ class Scheduler : public IEventThreadCallback, android::impl::MessageQueue {
190190
}
191191

192192
void updatePhaseConfiguration(PhysicalDisplayId, Fps) EXCLUDES(mVsyncConfigLock);
193-
void reloadPhaseConfiguration(Fps, Duration minSfDuration, Duration maxSfDuration,
194-
Duration appDuration) EXCLUDES(mVsyncConfigLock);
193+
void reloadPhaseConfiguration(const FrameRateMode&, Duration minSfDuration,
194+
Duration maxSfDuration, Duration appDuration)
195+
EXCLUDES(mVsyncConfigLock);
195196

196197
VsyncConfigSet getCurrentVsyncConfigs() const EXCLUDES(mVsyncConfigLock) {
197198
std::scoped_lock lock{mVsyncConfigLock};
@@ -203,6 +204,12 @@ class Scheduler : public IEventThreadCallback, android::impl::MessageQueue {
203204
return mVsyncConfiguration->getConfigsForRefreshRate(refreshRate);
204205
}
205206

207+
static nsecs_t getPresentationDeadline(Fps refreshRate, Duration sfVsyncOffset) {
208+
// We add an additional 1ms to allow for processing time and
209+
// differences between the ideal and actual refresh rate.
210+
return refreshRate.getPeriodNsecs() - sfVsyncOffset.ns() + 1000000;
211+
}
212+
206213
// Sets the render rate for the scheduler to run at.
207214
void setRenderRate(PhysicalDisplayId, Fps, bool applyImmediately);
208215

0 commit comments

Comments
 (0)