SF: Delegate ICompositor interfacing to Scheduler
Factor out MessageQueue's calls to commit/composite/sample into an
onFrameSignal delegate that Scheduler implements, since the policy
for ICompositor interfacing (e.g. whether to composite displays
sequentially or concurrently) will be the latter's responsibility.
Improve type correctness by replacing more nsecs_t with TimePoint/
Duration, and int64_t with a new VsyncId type.
Bug: 241285191
Test: Boot
Change-Id: I5039894038b62a562c1f93b7cea41751f7f76ca6
diff --git a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
index e0aa0b1..f20b9f9 100644
--- a/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
+++ b/services/surfaceflinger/tests/unittests/MessageQueueTest.cpp
@@ -32,8 +32,8 @@
using CallbackToken = scheduler::VSyncDispatch::CallbackToken;
struct NoOpCompositor final : ICompositor {
- bool commit(nsecs_t, int64_t, nsecs_t) override { return false; }
- void composite(nsecs_t, int64_t) override {}
+ bool commit(TimePoint, VsyncId, TimePoint) override { return false; }
+ void composite(TimePoint, VsyncId) override {}
void sample() override {}
} gNoOpCompositor;
@@ -41,12 +41,15 @@
struct MockHandler : MessageQueue::Handler {
using MessageQueue::Handler::Handler;
- MOCK_METHOD(void, dispatchFrame, (int64_t, nsecs_t), (override));
+ MOCK_METHOD(void, dispatchFrame, (VsyncId, TimePoint), (override));
};
explicit TestableMessageQueue(sp<MockHandler> handler)
: impl::MessageQueue(gNoOpCompositor, handler), mHandler(std::move(handler)) {}
+ // impl::MessageQueue overrides:
+ void onFrameSignal(ICompositor&, VsyncId, TimePoint) override {}
+
public:
TestableMessageQueue() : TestableMessageQueue(sp<MockHandler>::make(*this)) {}
@@ -71,7 +74,7 @@
struct MessageQueueTest : testing::Test {
void SetUp() override {
EXPECT_CALL(mVSyncDispatch, registerCallback(_, "sf")).WillOnce(Return(mCallbackToken));
- EXPECT_NO_FATAL_FAILURE(mEventQueue.initVsync(mVSyncDispatch, mTokenManager, mDuration));
+ EXPECT_NO_FATAL_FAILURE(mEventQueue.initVsync(mVSyncDispatch, mTokenManager, kDuration));
EXPECT_CALL(mVSyncDispatch, unregisterCallback(mCallbackToken)).Times(1);
}
@@ -80,16 +83,15 @@
TestableMessageQueue mEventQueue;
const CallbackToken mCallbackToken{5};
- constexpr static auto mDuration = std::chrono::nanoseconds(100ms);
- constexpr static auto mDifferentDuration = std::chrono::nanoseconds(250ms);
+
+ static constexpr Duration kDuration = 100ms;
+ static constexpr Duration kDifferentDuration = 250ms;
};
namespace {
-/* ------------------------------------------------------------------------
- * Test cases
- */
+
TEST_F(MessageQueueTest, commit) {
- const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
+ const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = kDuration.ns(),
.readyDuration = 0,
.earliestVsync = 0};
EXPECT_FALSE(mEventQueue.getScheduledFrameTime());
@@ -103,7 +105,7 @@
TEST_F(MessageQueueTest, commitTwice) {
InSequence s;
- const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
+ const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = kDuration.ns(),
.readyDuration = 0,
.earliestVsync = 0};
@@ -122,7 +124,7 @@
TEST_F(MessageQueueTest, commitTwiceWithCallback) {
InSequence s;
- const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
+ const auto timing = scheduler::VSyncDispatch::ScheduleTiming{.workDuration = kDuration.ns(),
.readyDuration = 0,
.earliestVsync = 0};
@@ -132,33 +134,36 @@
ASSERT_TRUE(mEventQueue.getScheduledFrameTime());
EXPECT_EQ(1234, mEventQueue.getScheduledFrameTime()->time_since_epoch().count());
- const auto startTime = 100;
- const auto endTime = startTime + mDuration.count();
- const auto presentTime = 500;
- const auto vsyncId = 42;
+ constexpr TimePoint kStartTime = TimePoint::fromNs(100);
+ constexpr TimePoint kEndTime = kStartTime + kDuration;
+ constexpr TimePoint kPresentTime = TimePoint::fromNs(500);
+ constexpr VsyncId vsyncId{42};
+
EXPECT_CALL(mTokenManager,
- generateTokenForPredictions(
- frametimeline::TimelineItem(startTime, endTime, presentTime)))
- .WillOnce(Return(vsyncId));
- EXPECT_CALL(*mEventQueue.mHandler, dispatchFrame(vsyncId, presentTime)).Times(1);
- EXPECT_NO_FATAL_FAILURE(mEventQueue.vsyncCallback(presentTime, startTime, endTime));
+ generateTokenForPredictions(frametimeline::TimelineItem(kStartTime.ns(),
+ kEndTime.ns(),
+ kPresentTime.ns())))
+ .WillOnce(Return(vsyncId.value));
+ EXPECT_CALL(*mEventQueue.mHandler, dispatchFrame(vsyncId, kPresentTime)).Times(1);
+ EXPECT_NO_FATAL_FAILURE(
+ mEventQueue.vsyncCallback(kPresentTime.ns(), kStartTime.ns(), kEndTime.ns()));
EXPECT_FALSE(mEventQueue.getScheduledFrameTime());
const auto timingAfterCallback =
- scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDuration.count(),
+ scheduler::VSyncDispatch::ScheduleTiming{.workDuration = kDuration.ns(),
.readyDuration = 0,
- .earliestVsync = presentTime};
+ .earliestVsync = kPresentTime.ns()};
EXPECT_CALL(mVSyncDispatch, schedule(mCallbackToken, timingAfterCallback)).WillOnce(Return(0));
EXPECT_NO_FATAL_FAILURE(mEventQueue.scheduleFrame());
}
TEST_F(MessageQueueTest, commitWithDurationChange) {
- EXPECT_NO_FATAL_FAILURE(mEventQueue.setDuration(mDifferentDuration));
+ EXPECT_NO_FATAL_FAILURE(mEventQueue.setDuration(kDifferentDuration));
const auto timing =
- scheduler::VSyncDispatch::ScheduleTiming{.workDuration = mDifferentDuration.count(),
+ scheduler::VSyncDispatch::ScheduleTiming{.workDuration = kDifferentDuration.ns(),
.readyDuration = 0,
.earliestVsync = 0};
diff --git a/services/surfaceflinger/tests/unittests/SurfaceFlinger_PowerHintTest.cpp b/services/surfaceflinger/tests/unittests/SurfaceFlinger_PowerHintTest.cpp
index 18e0a1c..e256d2c 100644
--- a/services/surfaceflinger/tests/unittests/SurfaceFlinger_PowerHintTest.cpp
+++ b/services/surfaceflinger/tests/unittests/SurfaceFlinger_PowerHintTest.cpp
@@ -133,22 +133,20 @@
TEST_F(SurfaceFlingerPowerHintTest, sendDurationsIncludingHwcWaitTime) {
ON_CALL(*mPowerAdvisor, usePowerHintSession()).WillByDefault(Return(true));
- const std::chrono::nanoseconds mockVsyncPeriod = 15ms;
EXPECT_CALL(*mPowerAdvisor, setTargetWorkDuration(_)).Times(1);
-
- const nsecs_t now = systemTime();
- const std::chrono::nanoseconds mockHwcRunTime = 20ms;
EXPECT_CALL(*mDisplaySurface,
prepareFrame(compositionengine::DisplaySurface::CompositionType::Hwc))
.Times(1);
- EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _))
- .WillOnce([mockHwcRunTime] {
- std::this_thread::sleep_for(mockHwcRunTime);
- return hardware::graphics::composer::V2_1::Error::NONE;
- });
+ EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _)).WillOnce([] {
+ constexpr Duration kMockHwcRunTime = 20ms;
+ std::this_thread::sleep_for(kMockHwcRunTime);
+ return hardware::graphics::composer::V2_1::Error::NONE;
+ });
EXPECT_CALL(*mPowerAdvisor, sendActualWorkDuration()).Times(1);
- static constexpr bool kVsyncId = 123; // arbitrary
- mFlinger.commitAndComposite(now, kVsyncId, now + mockVsyncPeriod.count());
+
+ const TimePoint frameTime = scheduler::SchedulerClock::now();
+ constexpr Period kMockVsyncPeriod = 15ms;
+ mFlinger.commitAndComposite(frameTime, VsyncId{123}, frameTime + kMockVsyncPeriod);
}
TEST_F(SurfaceFlingerPowerHintTest, inactiveOnDisplayDoze) {
@@ -156,22 +154,20 @@
mDisplay->setPowerMode(hal::PowerMode::DOZE);
- const std::chrono::nanoseconds mockVsyncPeriod = 15ms;
EXPECT_CALL(*mPowerAdvisor, setTargetWorkDuration(_)).Times(0);
-
- const nsecs_t now = systemTime();
- const std::chrono::nanoseconds mockHwcRunTime = 20ms;
EXPECT_CALL(*mDisplaySurface,
prepareFrame(compositionengine::DisplaySurface::CompositionType::Hwc))
.Times(1);
- EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _))
- .WillOnce([mockHwcRunTime] {
- std::this_thread::sleep_for(mockHwcRunTime);
- return hardware::graphics::composer::V2_1::Error::NONE;
- });
+ EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _)).WillOnce([] {
+ constexpr Duration kMockHwcRunTime = 20ms;
+ std::this_thread::sleep_for(kMockHwcRunTime);
+ return hardware::graphics::composer::V2_1::Error::NONE;
+ });
EXPECT_CALL(*mPowerAdvisor, sendActualWorkDuration()).Times(0);
- static constexpr bool kVsyncId = 123; // arbitrary
- mFlinger.commitAndComposite(now, kVsyncId, now + mockVsyncPeriod.count());
+
+ const TimePoint frameTime = scheduler::SchedulerClock::now();
+ constexpr Period kMockVsyncPeriod = 15ms;
+ mFlinger.commitAndComposite(frameTime, VsyncId{123}, frameTime + kMockVsyncPeriod);
}
} // namespace
diff --git a/services/surfaceflinger/tests/unittests/TestableScheduler.h b/services/surfaceflinger/tests/unittests/TestableScheduler.h
index 4708572..66cdfd7 100644
--- a/services/surfaceflinger/tests/unittests/TestableScheduler.h
+++ b/services/surfaceflinger/tests/unittests/TestableScheduler.h
@@ -109,8 +109,8 @@
private:
// ICompositor overrides:
- bool commit(nsecs_t, int64_t, nsecs_t) override { return false; }
- void composite(nsecs_t, int64_t) override {}
+ bool commit(TimePoint, VsyncId, TimePoint) override { return false; }
+ void composite(TimePoint, VsyncId) override {}
void sample() override {}
};
diff --git a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
index 66eb186..407706d 100644
--- a/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
+++ b/services/surfaceflinger/tests/unittests/TestableSurfaceFlinger.h
@@ -27,6 +27,7 @@
#include <compositionengine/impl/Display.h>
#include <compositionengine/impl/OutputLayerCompositionState.h>
#include <compositionengine/mock/DisplaySurface.h>
+#include <ftl/fake_guard.h>
#include <gui/ScreenCaptureResults.h>
#include "BufferStateLayer.h"
@@ -316,25 +317,22 @@
* Forwarding for functions being tested
*/
- nsecs_t commit(nsecs_t frameTime, int64_t vsyncId, nsecs_t expectedVSyncTime) {
+ TimePoint commit(TimePoint frameTime, VsyncId vsyncId, TimePoint expectedVSyncTime) {
mFlinger->commit(frameTime, vsyncId, expectedVSyncTime);
return frameTime;
}
- nsecs_t commit(nsecs_t frameTime, int64_t vsyncId) {
- std::chrono::nanoseconds period = 10ms;
- return commit(frameTime, vsyncId, frameTime + period.count());
+ TimePoint commit(TimePoint frameTime, VsyncId vsyncId) {
+ return commit(frameTime, vsyncId, frameTime + Period(10ms));
}
- nsecs_t commit() {
- const nsecs_t now = systemTime();
- const nsecs_t expectedVsyncTime = now + 10'000'000;
- return commit(now, kVsyncId, expectedVsyncTime);
+ TimePoint commit() {
+ const TimePoint frameTime = scheduler::SchedulerClock::now();
+ return commit(frameTime, kVsyncId);
}
- void commitAndComposite(const nsecs_t frameTime, const int64_t vsyncId,
- const nsecs_t expectedVsyncTime) {
- mFlinger->composite(commit(frameTime, vsyncId, expectedVsyncTime), kVsyncId);
+ void commitAndComposite(TimePoint frameTime, VsyncId vsyncId, TimePoint expectedVsyncTime) {
+ mFlinger->composite(commit(frameTime, vsyncId, expectedVsyncTime), vsyncId);
}
void commitAndComposite() { mFlinger->composite(commit(), kVsyncId); }
@@ -432,7 +430,9 @@
listenerCallbacks, transactionId);
}
- auto flushTransactionQueues() { return mFlinger->flushTransactionQueues(0); };
+ auto flushTransactionQueues() {
+ return FTL_FAKE_GUARD(kMainThreadContext, mFlinger->flushTransactionQueues(kVsyncId));
+ }
auto onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
return mFlinger->onTransact(code, data, reply, flags);
@@ -870,7 +870,7 @@
};
private:
- constexpr static int64_t kVsyncId = 123;
+ static constexpr VsyncId kVsyncId{123};
surfaceflinger::test::Factory mFactory;
sp<SurfaceFlinger> mFlinger;