Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "SchedulerUnittests" |
| 23 | |
| 24 | #include <gmock/gmock.h> |
| 25 | #include <log/log.h> |
| 26 | #include <thread> |
| 27 | |
| 28 | #include "Scheduler/PhaseOffsets.h" |
| 29 | |
| 30 | using namespace testing; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace scheduler { |
| 34 | |
| 35 | class TestablePhaseOffsetsAsDurations : public impl::PhaseDurations { |
| 36 | public: |
| 37 | TestablePhaseOffsetsAsDurations(float currentFps, nsecs_t sfDuration, nsecs_t appDuration, |
| 38 | nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration, |
| 39 | nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration) |
| 40 | : impl::PhaseDurations({60.0f, 90.0f}, currentFps, sfDuration, appDuration, |
| 41 | sfEarlyDuration, appEarlyDuration, sfEarlyGlDuration, |
| 42 | appEarlyGlDuration) {} |
| 43 | }; |
| 44 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 45 | class PhaseDurationTest : public testing::Test { |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 46 | protected: |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 47 | PhaseDurationTest() |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 48 | : mPhaseDurations(60.0f, 10'500'000, 20'500'000, 16'000'000, 16'500'000, 13'500'000, |
| 49 | 21'000'000) {} |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 50 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 51 | ~PhaseDurationTest() = default; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 52 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 53 | TestablePhaseOffsetsAsDurations mPhaseDurations; |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | namespace { |
| 57 | /* ------------------------------------------------------------------------ |
| 58 | * Test cases |
| 59 | */ |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 60 | TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_60Hz) { |
| 61 | mPhaseDurations.setRefreshRateFps(60.0f); |
| 62 | auto currentOffsets = mPhaseDurations.getCurrentOffsets(); |
| 63 | auto offsets = mPhaseDurations.getOffsetsForRefreshRate(60.0f); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 64 | |
| 65 | EXPECT_EQ(currentOffsets, offsets); |
| 66 | EXPECT_EQ(offsets.late.sf, 6'166'667); |
| 67 | |
| 68 | EXPECT_EQ(offsets.late.app, 2'333'334); |
| 69 | |
| 70 | EXPECT_EQ(offsets.early.sf, 666'667); |
| 71 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 72 | EXPECT_EQ(offsets.early.app, 833'334); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 73 | |
| 74 | EXPECT_EQ(offsets.earlyGl.sf, 3'166'667); |
| 75 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 76 | EXPECT_EQ(offsets.earlyGl.app, 15'500'001); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 79 | TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_90Hz) { |
| 80 | mPhaseDurations.setRefreshRateFps(90.0f); |
| 81 | auto currentOffsets = mPhaseDurations.getCurrentOffsets(); |
| 82 | auto offsets = mPhaseDurations.getOffsetsForRefreshRate(90.0f); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 83 | |
| 84 | EXPECT_EQ(currentOffsets, offsets); |
| 85 | EXPECT_EQ(offsets.late.sf, 611'111); |
| 86 | |
| 87 | EXPECT_EQ(offsets.late.app, 2'333'333); |
| 88 | |
| 89 | EXPECT_EQ(offsets.early.sf, -4'888'889); |
| 90 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 91 | EXPECT_EQ(offsets.early.app, 833'333); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 92 | |
| 93 | EXPECT_EQ(offsets.earlyGl.sf, -2'388'889); |
| 94 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 95 | EXPECT_EQ(offsets.earlyGl.app, 9'944'444); |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 98 | TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_DefaultOffsets) { |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 99 | TestablePhaseOffsetsAsDurations phaseOffsetsWithDefaultValues(60.0f, -1, -1, -1, -1, -1, -1); |
| 100 | |
| 101 | auto validateOffsets = [](auto& offsets) { |
| 102 | EXPECT_EQ(offsets.late.sf, 1'000'000); |
| 103 | |
| 104 | EXPECT_EQ(offsets.late.app, 1'000'000); |
| 105 | |
| 106 | EXPECT_EQ(offsets.early.sf, 1'000'000); |
| 107 | |
| 108 | EXPECT_EQ(offsets.early.app, 1'000'000); |
| 109 | |
| 110 | EXPECT_EQ(offsets.earlyGl.sf, 1'000'000); |
| 111 | |
| 112 | EXPECT_EQ(offsets.earlyGl.app, 1'000'000); |
| 113 | }; |
| 114 | |
| 115 | phaseOffsetsWithDefaultValues.setRefreshRateFps(90.0f); |
| 116 | auto currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets(); |
| 117 | auto offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f); |
| 118 | EXPECT_EQ(currentOffsets, offsets); |
| 119 | validateOffsets(offsets); |
| 120 | |
| 121 | phaseOffsetsWithDefaultValues.setRefreshRateFps(60.0f); |
| 122 | currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets(); |
| 123 | offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f); |
| 124 | EXPECT_EQ(currentOffsets, offsets); |
| 125 | validateOffsets(offsets); |
| 126 | } |
| 127 | |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 128 | TEST_F(PhaseDurationTest, getOffsetsForRefreshRate_unknownRefreshRate) { |
| 129 | auto offsets = mPhaseDurations.getOffsetsForRefreshRate(14.7f); |
| 130 | |
| 131 | EXPECT_EQ(offsets.late.sf, 57'527'208); |
| 132 | |
| 133 | EXPECT_EQ(offsets.late.app, 37'027'208); |
| 134 | |
| 135 | EXPECT_EQ(offsets.early.sf, 52'027'208); |
| 136 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 137 | EXPECT_EQ(offsets.early.app, 35'527'208); |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 138 | |
| 139 | EXPECT_EQ(offsets.earlyGl.sf, 54'527'208); |
| 140 | |
Ady Abraham | c43b88e | 2020-04-06 16:25:31 -0700 | [diff] [blame] | 141 | EXPECT_EQ(offsets.earlyGl.app, 33'527'208); |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | } // namespace |
| 145 | |
| 146 | class TestablePhaseOffsets : public impl::PhaseOffsets { |
| 147 | public: |
Ady Abraham | c6c8182 | 2020-04-28 10:28:00 -0700 | [diff] [blame] | 148 | TestablePhaseOffsets() |
| 149 | : impl::PhaseOffsets({60.0f, 90.0f}, 60.0f, 1'000'000, 1'000'000, {}, {}, {}, {}, |
| 150 | 10'000'000) {} |
Ady Abraham | 60120a0 | 2020-03-23 11:23:26 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | class PhaseOffsetsTest : public testing::Test { |
| 154 | protected: |
| 155 | PhaseOffsetsTest() = default; |
| 156 | ~PhaseOffsetsTest() = default; |
| 157 | |
| 158 | TestablePhaseOffsets mPhaseOffsets; |
| 159 | }; |
| 160 | |
| 161 | namespace { |
| 162 | TEST_F(PhaseOffsetsTest, getOffsetsForRefreshRate_unknownRefreshRate) { |
| 163 | auto offsets = mPhaseOffsets.getOffsetsForRefreshRate(14.7f); |
| 164 | |
| 165 | EXPECT_EQ(offsets.late.sf, 1'000'000); |
| 166 | |
| 167 | EXPECT_EQ(offsets.late.app, 1'000'000); |
| 168 | |
| 169 | EXPECT_EQ(offsets.early.sf, 1'000'000); |
| 170 | |
| 171 | EXPECT_EQ(offsets.early.app, 1'000'000); |
| 172 | |
| 173 | EXPECT_EQ(offsets.earlyGl.sf, 1'000'000); |
| 174 | |
| 175 | EXPECT_EQ(offsets.earlyGl.app, 1'000'000); |
| 176 | } |
| 177 | |
Ady Abraham | 9e16a48 | 2019-12-03 17:19:41 -0800 | [diff] [blame] | 178 | } // namespace |
| 179 | } // namespace scheduler |
| 180 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 181 | |
| 182 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 183 | #pragma clang diagnostic pop // ignored "-Wconversion" |