Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "LibSurfaceFlingerUnittests" |
| 19 | |
| 20 | #include <ftl/fake_guard.h> |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <log/log.h> |
| 24 | |
| 25 | #include <scheduler/Fps.h> |
| 26 | #include "Scheduler/VsyncSchedule.h" |
| 27 | #include "ThreadContext.h" |
| 28 | #include "mock/MockSchedulerCallback.h" |
| 29 | #include "mock/MockVSyncDispatch.h" |
| 30 | #include "mock/MockVSyncTracker.h" |
| 31 | #include "mock/MockVsyncController.h" |
| 32 | |
| 33 | using testing::_; |
| 34 | |
| 35 | namespace android { |
| 36 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 37 | constexpr PhysicalDisplayId DEFAULT_DISPLAY_ID = PhysicalDisplayId::fromPort(42u); |
| 38 | |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 39 | class VsyncScheduleTest : public testing::Test { |
| 40 | protected: |
| 41 | VsyncScheduleTest(); |
| 42 | ~VsyncScheduleTest() override; |
| 43 | |
| 44 | scheduler::mock::SchedulerCallback mCallback; |
| 45 | const std::unique_ptr<scheduler::VsyncSchedule> mVsyncSchedule = |
| 46 | std::unique_ptr<scheduler::VsyncSchedule>( |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 47 | new scheduler::VsyncSchedule(DEFAULT_DISPLAY_ID, |
| 48 | std::make_shared<mock::VSyncTracker>(), |
| 49 | std::make_shared<mock::VSyncDispatch>(), |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 50 | std::make_unique<mock::VsyncController>())); |
| 51 | |
| 52 | mock::VsyncController& getController() { |
| 53 | return *static_cast<mock::VsyncController*>(&mVsyncSchedule->getController()); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | VsyncScheduleTest::VsyncScheduleTest() { |
| 58 | const ::testing::TestInfo* const test_info = |
| 59 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 60 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 61 | } |
| 62 | |
| 63 | VsyncScheduleTest::~VsyncScheduleTest() { |
| 64 | const ::testing::TestInfo* const test_info = |
| 65 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 66 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 67 | } |
| 68 | |
| 69 | namespace { |
| 70 | |
| 71 | using namespace testing; |
| 72 | |
| 73 | TEST_F(VsyncScheduleTest, InitiallyDisallowed) { |
| 74 | ASSERT_FALSE(mVsyncSchedule->isHardwareVsyncAllowed(false /* makeAllowed */)); |
| 75 | } |
| 76 | |
| 77 | TEST_F(VsyncScheduleTest, EnableDoesNothingWhenDisallowed) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 78 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 79 | |
| 80 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 81 | } |
| 82 | |
| 83 | TEST_F(VsyncScheduleTest, DisableDoesNothingWhenDisallowed) { |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 84 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 85 | |
| 86 | mVsyncSchedule->disableHardwareVsync(mCallback, false /* disallow */); |
| 87 | } |
| 88 | |
| 89 | TEST_F(VsyncScheduleTest, MakeAllowed) { |
| 90 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 91 | } |
| 92 | |
| 93 | TEST_F(VsyncScheduleTest, DisableDoesNothingWhenDisabled) { |
| 94 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 95 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 96 | |
| 97 | mVsyncSchedule->disableHardwareVsync(mCallback, false /* disallow */); |
| 98 | } |
| 99 | |
| 100 | TEST_F(VsyncScheduleTest, EnableWorksWhenDisabled) { |
| 101 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 102 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, true)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 103 | |
| 104 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 105 | } |
| 106 | |
| 107 | TEST_F(VsyncScheduleTest, EnableWorksOnce) { |
| 108 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 109 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, true)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 110 | |
| 111 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 112 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 113 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 114 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 115 | } |
| 116 | |
| 117 | TEST_F(VsyncScheduleTest, AllowedIsSticky) { |
| 118 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 119 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(false /* makeAllowed */)); |
| 120 | } |
| 121 | |
| 122 | TEST_F(VsyncScheduleTest, EnableDisable) { |
| 123 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 124 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, true)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 125 | |
| 126 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 127 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 128 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, false)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 129 | mVsyncSchedule->disableHardwareVsync(mCallback, false /* disallow */); |
| 130 | } |
| 131 | |
| 132 | TEST_F(VsyncScheduleTest, StartPeriodTransition) { |
| 133 | // Note: startPeriodTransition is only called when hardware vsyncs are |
| 134 | // allowed. |
| 135 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 136 | |
| 137 | const Period period = (60_Hz).getPeriod(); |
| 138 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 139 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, true)); |
| 140 | EXPECT_CALL(getController(), startPeriodTransition(period.ns(), false)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 141 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 142 | mVsyncSchedule->startPeriodTransition(mCallback, period, false); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | TEST_F(VsyncScheduleTest, StartPeriodTransitionAlreadyEnabled) { |
| 146 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 147 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 148 | |
| 149 | const Period period = (60_Hz).getPeriod(); |
| 150 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 151 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
| 152 | EXPECT_CALL(getController(), startPeriodTransition(period.ns(), false)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 153 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 154 | mVsyncSchedule->startPeriodTransition(mCallback, period, false); |
| 155 | } |
| 156 | |
| 157 | TEST_F(VsyncScheduleTest, StartPeriodTransitionForce) { |
| 158 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 159 | |
| 160 | const Period period = (60_Hz).getPeriod(); |
| 161 | |
| 162 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, true)); |
| 163 | EXPECT_CALL(getController(), startPeriodTransition(period.ns(), true)); |
| 164 | |
| 165 | mVsyncSchedule->startPeriodTransition(mCallback, period, true); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_F(VsyncScheduleTest, AddResyncSampleDisallowed) { |
| 169 | const Period period = (60_Hz).getPeriod(); |
| 170 | const auto timestamp = TimePoint::now(); |
| 171 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 172 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 173 | EXPECT_CALL(getController(), addHwVsyncTimestamp(_, _, _)).Times(0); |
| 174 | |
| 175 | mVsyncSchedule->addResyncSample(mCallback, timestamp, period); |
| 176 | } |
| 177 | |
| 178 | TEST_F(VsyncScheduleTest, AddResyncSampleDisabled) { |
| 179 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 180 | const Period period = (60_Hz).getPeriod(); |
| 181 | const auto timestamp = TimePoint::now(); |
| 182 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 183 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 184 | EXPECT_CALL(getController(), addHwVsyncTimestamp(_, _, _)).Times(0); |
| 185 | |
| 186 | mVsyncSchedule->addResyncSample(mCallback, timestamp, period); |
| 187 | } |
| 188 | |
| 189 | TEST_F(VsyncScheduleTest, AddResyncSampleReturnsTrue) { |
| 190 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 191 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 192 | |
| 193 | const Period period = (60_Hz).getPeriod(); |
| 194 | const auto timestamp = TimePoint::now(); |
| 195 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 196 | EXPECT_CALL(mCallback, setVsyncEnabled(_, _)).Times(0); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 197 | EXPECT_CALL(getController(), |
| 198 | addHwVsyncTimestamp(timestamp.ns(), std::optional<nsecs_t>(period.ns()), _)) |
| 199 | .WillOnce(Return(true)); |
| 200 | |
| 201 | mVsyncSchedule->addResyncSample(mCallback, timestamp, period); |
| 202 | } |
| 203 | |
| 204 | TEST_F(VsyncScheduleTest, AddResyncSampleReturnsFalse) { |
| 205 | ASSERT_TRUE(mVsyncSchedule->isHardwareVsyncAllowed(true /* makeAllowed */)); |
| 206 | mVsyncSchedule->enableHardwareVsync(mCallback); |
| 207 | |
| 208 | const Period period = (60_Hz).getPeriod(); |
| 209 | const auto timestamp = TimePoint::now(); |
| 210 | |
Leon Scroggins III | 6738862 | 2023-02-06 20:36:20 -0500 | [diff] [blame] | 211 | EXPECT_CALL(mCallback, setVsyncEnabled(DEFAULT_DISPLAY_ID, false)); |
Leon Scroggins III | 4d5db7a | 2023-02-13 15:24:20 -0500 | [diff] [blame] | 212 | EXPECT_CALL(getController(), |
| 213 | addHwVsyncTimestamp(timestamp.ns(), std::optional<nsecs_t>(period.ns()), _)) |
| 214 | .WillOnce(Return(false)); |
| 215 | |
| 216 | mVsyncSchedule->addResyncSample(mCallback, timestamp, period); |
| 217 | } |
| 218 | |
| 219 | TEST_F(VsyncScheduleTest, PendingState) FTL_FAKE_GUARD(kMainThreadContext) { |
| 220 | ASSERT_FALSE(mVsyncSchedule->getPendingHardwareVsyncState()); |
| 221 | mVsyncSchedule->setPendingHardwareVsyncState(true); |
| 222 | ASSERT_TRUE(mVsyncSchedule->getPendingHardwareVsyncState()); |
| 223 | |
| 224 | mVsyncSchedule->setPendingHardwareVsyncState(false); |
| 225 | ASSERT_FALSE(mVsyncSchedule->getPendingHardwareVsyncState()); |
| 226 | } |
| 227 | |
| 228 | } // namespace |
| 229 | } // namespace android |