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 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "SchedulerUnittests" |
| 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <log/log.h> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include "Scheduler/PhaseOffsets.h" |
| 25 | |
| 26 | using namespace testing; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace scheduler { |
| 30 | |
| 31 | class TestablePhaseOffsetsAsDurations : public impl::PhaseDurations { |
| 32 | public: |
| 33 | TestablePhaseOffsetsAsDurations(float currentFps, nsecs_t sfDuration, nsecs_t appDuration, |
| 34 | nsecs_t sfEarlyDuration, nsecs_t appEarlyDuration, |
| 35 | nsecs_t sfEarlyGlDuration, nsecs_t appEarlyGlDuration) |
| 36 | : impl::PhaseDurations({60.0f, 90.0f}, currentFps, sfDuration, appDuration, |
| 37 | sfEarlyDuration, appEarlyDuration, sfEarlyGlDuration, |
| 38 | appEarlyGlDuration) {} |
| 39 | }; |
| 40 | |
| 41 | class PhaseOffsetsTest : public testing::Test { |
| 42 | protected: |
| 43 | PhaseOffsetsTest() |
| 44 | : mPhaseOffsets(60.0f, 10'500'000, 20'500'000, 16'000'000, 33'500'000, 13'500'000, |
| 45 | 38'000'000) {} |
| 46 | |
| 47 | ~PhaseOffsetsTest() = default; |
| 48 | |
| 49 | TestablePhaseOffsetsAsDurations mPhaseOffsets; |
| 50 | }; |
| 51 | |
| 52 | namespace { |
| 53 | /* ------------------------------------------------------------------------ |
| 54 | * Test cases |
| 55 | */ |
| 56 | TEST_F(PhaseOffsetsTest, getOffsetsForRefreshRate_60Hz) { |
| 57 | mPhaseOffsets.setRefreshRateFps(60.0f); |
| 58 | auto currentOffsets = mPhaseOffsets.getCurrentOffsets(); |
| 59 | auto offsets = mPhaseOffsets.getOffsetsForRefreshRate(60.0f); |
| 60 | |
| 61 | EXPECT_EQ(currentOffsets, offsets); |
| 62 | EXPECT_EQ(offsets.late.sf, 6'166'667); |
| 63 | |
| 64 | EXPECT_EQ(offsets.late.app, 2'333'334); |
| 65 | |
| 66 | EXPECT_EQ(offsets.early.sf, 666'667); |
| 67 | |
| 68 | EXPECT_EQ(offsets.early.app, 500'001); |
| 69 | |
| 70 | EXPECT_EQ(offsets.earlyGl.sf, 3'166'667); |
| 71 | |
| 72 | EXPECT_EQ(offsets.earlyGl.app, 15'166'668); |
| 73 | } |
| 74 | |
| 75 | TEST_F(PhaseOffsetsTest, getOffsetsForRefreshRate_90Hz) { |
| 76 | mPhaseOffsets.setRefreshRateFps(90.0f); |
| 77 | auto currentOffsets = mPhaseOffsets.getCurrentOffsets(); |
| 78 | auto offsets = mPhaseOffsets.getOffsetsForRefreshRate(90.0f); |
| 79 | |
| 80 | EXPECT_EQ(currentOffsets, offsets); |
| 81 | EXPECT_EQ(offsets.late.sf, 611'111); |
| 82 | |
| 83 | EXPECT_EQ(offsets.late.app, 2'333'333); |
| 84 | |
| 85 | EXPECT_EQ(offsets.early.sf, -4'888'889); |
| 86 | |
| 87 | EXPECT_EQ(offsets.early.app, 6'055'555); |
| 88 | |
| 89 | EXPECT_EQ(offsets.earlyGl.sf, -2'388'889); |
| 90 | |
| 91 | EXPECT_EQ(offsets.earlyGl.app, 4'055'555); |
| 92 | } |
| 93 | |
| 94 | TEST_F(PhaseOffsetsTest, getOffsetsForRefreshRate_DefaultOffsets) { |
| 95 | TestablePhaseOffsetsAsDurations phaseOffsetsWithDefaultValues(60.0f, -1, -1, -1, -1, -1, -1); |
| 96 | |
| 97 | auto validateOffsets = [](auto& offsets) { |
| 98 | EXPECT_EQ(offsets.late.sf, 1'000'000); |
| 99 | |
| 100 | EXPECT_EQ(offsets.late.app, 1'000'000); |
| 101 | |
| 102 | EXPECT_EQ(offsets.early.sf, 1'000'000); |
| 103 | |
| 104 | EXPECT_EQ(offsets.early.app, 1'000'000); |
| 105 | |
| 106 | EXPECT_EQ(offsets.earlyGl.sf, 1'000'000); |
| 107 | |
| 108 | EXPECT_EQ(offsets.earlyGl.app, 1'000'000); |
| 109 | }; |
| 110 | |
| 111 | phaseOffsetsWithDefaultValues.setRefreshRateFps(90.0f); |
| 112 | auto currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets(); |
| 113 | auto offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f); |
| 114 | EXPECT_EQ(currentOffsets, offsets); |
| 115 | validateOffsets(offsets); |
| 116 | |
| 117 | phaseOffsetsWithDefaultValues.setRefreshRateFps(60.0f); |
| 118 | currentOffsets = phaseOffsetsWithDefaultValues.getCurrentOffsets(); |
| 119 | offsets = phaseOffsetsWithDefaultValues.getOffsetsForRefreshRate(90.0f); |
| 120 | EXPECT_EQ(currentOffsets, offsets); |
| 121 | validateOffsets(offsets); |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | } // namespace scheduler |
| 126 | } // namespace android |