• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 "FpsReporterTest"
19 
20 #include <chrono>
21 
22 #include <android/gui/BnFpsListener.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <gui/LayerMetadata.h>
26 
27 #include "BufferQueueLayer.h"
28 #include "BufferStateLayer.h"
29 #include "EffectLayer.h"
30 #include "FpsReporter.h"
31 #include "Layer.h"
32 #include "TestableSurfaceFlinger.h"
33 #include "fake/FakeClock.h"
34 #include "mock/DisplayHardware/MockComposer.h"
35 #include "mock/MockEventThread.h"
36 #include "mock/MockFrameTimeline.h"
37 #include "mock/MockVsyncController.h"
38 
39 namespace android {
40 
41 using namespace std::chrono_literals;
42 
43 using testing::_;
44 using testing::DoAll;
45 using testing::Mock;
46 using testing::Return;
47 using testing::SetArgPointee;
48 using testing::UnorderedElementsAre;
49 
50 using android::Hwc2::IComposer;
51 using android::Hwc2::IComposerClient;
52 
53 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
54 
55 struct TestableFpsListener : public gui::BnFpsListener {
TestableFpsListenerandroid::TestableFpsListener56     TestableFpsListener() {}
57 
58     float lastReportedFps = 0;
59 
onFpsReportedandroid::TestableFpsListener60     binder::Status onFpsReported(float fps) override {
61         lastReportedFps = fps;
62         return binder::Status::ok();
63     }
64 };
65 
66 /**
67  * This class covers all the test that are related to refresh rate selection.
68  */
69 class FpsReporterTest : public testing::Test {
70 public:
71     FpsReporterTest();
72     ~FpsReporterTest() override;
73 
74 protected:
75     static constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
76     static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
77     static constexpr uint32_t WIDTH = 100;
78     static constexpr uint32_t HEIGHT = 100;
79     static constexpr uint32_t LAYER_FLAGS = 0;
80     static constexpr int32_t PRIORITY_UNSET = -1;
81 
82     void setupScheduler();
83     sp<BufferStateLayer> createBufferStateLayer(LayerMetadata metadata);
84 
85     TestableSurfaceFlinger mFlinger;
86     mock::FrameTimeline mFrameTimeline =
87             mock::FrameTimeline(std::make_shared<impl::TimeStats>(), 0);
88 
89     sp<Client> mClient;
90     sp<Layer> mParent;
91     sp<Layer> mTarget;
92     sp<Layer> mChild;
93     sp<Layer> mGrandChild;
94     sp<Layer> mUnrelated;
95 
96     sp<TestableFpsListener> mFpsListener;
97     fake::FakeClock* mClock = new fake::FakeClock();
98     sp<FpsReporter> mFpsReporter =
99             new FpsReporter(mFrameTimeline, *(mFlinger.flinger()), std::unique_ptr<Clock>(mClock));
100 };
101 
FpsReporterTest()102 FpsReporterTest::FpsReporterTest() {
103     const ::testing::TestInfo* const test_info =
104             ::testing::UnitTest::GetInstance()->current_test_info();
105     ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
106 
107     setupScheduler();
108     mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
109 
110     mFpsListener = new TestableFpsListener();
111 }
112 
~FpsReporterTest()113 FpsReporterTest::~FpsReporterTest() {
114     const ::testing::TestInfo* const test_info =
115             ::testing::UnitTest::GetInstance()->current_test_info();
116     ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
117 }
118 
createBufferStateLayer(LayerMetadata metadata={})119 sp<BufferStateLayer> FpsReporterTest::createBufferStateLayer(LayerMetadata metadata = {}) {
120     sp<Client> client;
121     LayerCreationArgs args(mFlinger.flinger(), client, "buffer-state-layer", LAYER_FLAGS, metadata);
122     return new BufferStateLayer(args);
123 }
124 
setupScheduler()125 void FpsReporterTest::setupScheduler() {
126     auto eventThread = std::make_unique<mock::EventThread>();
127     auto sfEventThread = std::make_unique<mock::EventThread>();
128 
129     EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
130     EXPECT_CALL(*eventThread, createEventConnection(_, _))
131             .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
132                                                        ResyncCallback())));
133 
134     EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
135     EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
136             .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
137                                                        ResyncCallback())));
138 
139     auto vsyncController = std::make_unique<mock::VsyncController>();
140     auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
141 
142     EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
143     EXPECT_CALL(*vsyncTracker, currentPeriod())
144             .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
145     EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
146     mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
147                             std::move(eventThread), std::move(sfEventThread));
148 }
149 
150 namespace {
151 
TEST_F(FpsReporterTest,callsListeners)152 TEST_F(FpsReporterTest, callsListeners) {
153     mParent = createBufferStateLayer();
154     constexpr int32_t kTaskId = 12;
155     LayerMetadata targetMetadata;
156     targetMetadata.setInt32(METADATA_TASK_ID, kTaskId);
157     mTarget = createBufferStateLayer(targetMetadata);
158     mChild = createBufferStateLayer();
159     mGrandChild = createBufferStateLayer();
160     mUnrelated = createBufferStateLayer();
161     mParent->addChild(mTarget);
162     mTarget->addChild(mChild);
163     mChild->addChild(mGrandChild);
164     mParent->commitChildList();
165     mFlinger.mutableCurrentState().layersSortedByZ.add(mParent);
166     mFlinger.mutableCurrentState().layersSortedByZ.add(mTarget);
167     mFlinger.mutableCurrentState().layersSortedByZ.add(mChild);
168     mFlinger.mutableCurrentState().layersSortedByZ.add(mGrandChild);
169 
170     float expectedFps = 44.0;
171 
172     EXPECT_CALL(mFrameTimeline,
173                 computeFps(UnorderedElementsAre(mTarget->getSequence(), mChild->getSequence(),
174                                                 mGrandChild->getSequence())))
175             .WillOnce(Return(expectedFps));
176 
177     mFpsReporter->addListener(mFpsListener, kTaskId);
178     mClock->advanceTime(600ms);
179     mFpsReporter->dispatchLayerFps();
180     EXPECT_EQ(expectedFps, mFpsListener->lastReportedFps);
181     mFpsReporter->removeListener(mFpsListener);
182     Mock::VerifyAndClearExpectations(&mFrameTimeline);
183 
184     EXPECT_CALL(mFrameTimeline, computeFps(_)).Times(0);
185     mFpsReporter->dispatchLayerFps();
186 }
187 
TEST_F(FpsReporterTest,rateLimits)188 TEST_F(FpsReporterTest, rateLimits) {
189     const constexpr int32_t kTaskId = 12;
190     LayerMetadata targetMetadata;
191     targetMetadata.setInt32(METADATA_TASK_ID, kTaskId);
192     mTarget = createBufferStateLayer(targetMetadata);
193     mFlinger.mutableCurrentState().layersSortedByZ.add(mTarget);
194 
195     float firstFps = 44.0;
196     float secondFps = 53.0;
197 
198     EXPECT_CALL(mFrameTimeline, computeFps(UnorderedElementsAre(mTarget->getSequence())))
199             .WillOnce(Return(firstFps))
200             .WillOnce(Return(secondFps));
201 
202     mFpsReporter->addListener(mFpsListener, kTaskId);
203     mClock->advanceTime(600ms);
204     mFpsReporter->dispatchLayerFps();
205     EXPECT_EQ(firstFps, mFpsListener->lastReportedFps);
206     mClock->advanceTime(200ms);
207     mFpsReporter->dispatchLayerFps();
208     EXPECT_EQ(firstFps, mFpsListener->lastReportedFps);
209     mClock->advanceTime(200ms);
210     mFpsReporter->dispatchLayerFps();
211     EXPECT_EQ(firstFps, mFpsListener->lastReportedFps);
212     mClock->advanceTime(200ms);
213     mFpsReporter->dispatchLayerFps();
214     EXPECT_EQ(secondFps, mFpsListener->lastReportedFps);
215 }
216 
217 } // namespace
218 } // namespace android
219