1 /*
2 * Copyright 2020 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 <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include <gui/LayerMetadata.h>
23
24 #include "BufferStateLayer.h"
25 #include "EffectLayer.h"
26 #include "Layer.h"
27 #include "TestableSurfaceFlinger.h"
28 #include "mock/DisplayHardware/MockComposer.h"
29 #include "mock/MockEventThread.h"
30 #include "mock/MockVsyncController.h"
31
32 namespace android {
33
34 using testing::_;
35 using testing::DoAll;
36 using testing::Mock;
37 using testing::Return;
38 using testing::SetArgPointee;
39
40 using android::Hwc2::IComposer;
41 using android::Hwc2::IComposerClient;
42
43 using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
44
45 /**
46 * This class covers all the test that are related to refresh rate selection.
47 */
48 class RefreshRateSelectionTest : public testing::Test {
49 public:
50 RefreshRateSelectionTest();
51 ~RefreshRateSelectionTest() override;
52
53 protected:
54 static constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
55 static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
56 static constexpr uint32_t WIDTH = 100;
57 static constexpr uint32_t HEIGHT = 100;
58 static constexpr uint32_t LAYER_FLAGS = 0;
59 static constexpr int32_t PRIORITY_UNSET = -1;
60
61 void setupScheduler();
62 sp<BufferStateLayer> createBufferStateLayer();
63 sp<EffectLayer> createEffectLayer();
64
65 void setParent(Layer* child, Layer* parent);
66 void commitTransaction(Layer* layer);
67
68 TestableSurfaceFlinger mFlinger;
69
70 sp<Client> mClient;
71 sp<Layer> mParent;
72 sp<Layer> mChild;
73 sp<Layer> mGrandChild;
74 };
75
RefreshRateSelectionTest()76 RefreshRateSelectionTest::RefreshRateSelectionTest() {
77 const ::testing::TestInfo* const test_info =
78 ::testing::UnitTest::GetInstance()->current_test_info();
79 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
80
81 setupScheduler();
82 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
83 }
84
~RefreshRateSelectionTest()85 RefreshRateSelectionTest::~RefreshRateSelectionTest() {
86 const ::testing::TestInfo* const test_info =
87 ::testing::UnitTest::GetInstance()->current_test_info();
88 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
89 }
90
91
createBufferStateLayer()92 sp<BufferStateLayer> RefreshRateSelectionTest::createBufferStateLayer() {
93 sp<Client> client;
94 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", LAYER_FLAGS,
95 LayerMetadata());
96 return new BufferStateLayer(args);
97 }
98
createEffectLayer()99 sp<EffectLayer> RefreshRateSelectionTest::createEffectLayer() {
100 sp<Client> client;
101 LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", LAYER_FLAGS, LayerMetadata());
102 return new EffectLayer(args);
103 }
104
setParent(Layer * child,Layer * parent)105 void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) {
106 child->setParent(parent);
107 }
108
commitTransaction(Layer * layer)109 void RefreshRateSelectionTest::commitTransaction(Layer* layer) {
110 auto c = layer->getDrawingState();
111 layer->commitTransaction(c);
112 }
113
setupScheduler()114 void RefreshRateSelectionTest::setupScheduler() {
115 auto eventThread = std::make_unique<mock::EventThread>();
116 auto sfEventThread = std::make_unique<mock::EventThread>();
117
118 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
119 EXPECT_CALL(*eventThread, createEventConnection(_, _))
120 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
121 ResyncCallback())));
122
123 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
124 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
125 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
126 ResyncCallback())));
127
128 auto vsyncController = std::make_unique<mock::VsyncController>();
129 auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
130
131 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
132 EXPECT_CALL(*vsyncTracker, currentPeriod())
133 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
134 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
135 mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
136 std::move(eventThread), std::move(sfEventThread));
137 }
138
139 namespace {
140 /* ------------------------------------------------------------------------
141 * Test cases
142 */
TEST_F(RefreshRateSelectionTest,testPriorityOnBufferStateLayers)143 TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) {
144 mParent = createBufferStateLayer();
145 mChild = createBufferStateLayer();
146 setParent(mChild.get(), mParent.get());
147 mGrandChild = createBufferStateLayer();
148 setParent(mGrandChild.get(), mChild.get());
149
150 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
151 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
152 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
153
154 // Child has its own priority.
155 mGrandChild->setFrameRateSelectionPriority(1);
156 commitTransaction(mGrandChild.get());
157 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
158 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
159 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
160
161 // Child inherits from his parent.
162 mChild->setFrameRateSelectionPriority(1);
163 commitTransaction(mChild.get());
164 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
165 commitTransaction(mGrandChild.get());
166 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
167 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
168 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
169
170 // Grandchild inherits from his grand parent.
171 mParent->setFrameRateSelectionPriority(1);
172 commitTransaction(mParent.get());
173 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
174 commitTransaction(mChild.get());
175 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
176 commitTransaction(mGrandChild.get());
177 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
178 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
179 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
180 }
181
TEST_F(RefreshRateSelectionTest,testPriorityOnEffectLayers)182 TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) {
183 mParent = createEffectLayer();
184 mChild = createEffectLayer();
185 setParent(mChild.get(), mParent.get());
186 mGrandChild = createEffectLayer();
187 setParent(mGrandChild.get(), mChild.get());
188
189 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
190 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
191 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
192
193 // Child has its own priority.
194 mGrandChild->setFrameRateSelectionPriority(1);
195 commitTransaction(mGrandChild.get());
196 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
197 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
198 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
199
200 // Child inherits from his parent.
201 mChild->setFrameRateSelectionPriority(1);
202 commitTransaction(mChild.get());
203 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
204 commitTransaction(mGrandChild.get());
205 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
206 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
207 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
208
209 // Grandchild inherits from his grand parent.
210 mParent->setFrameRateSelectionPriority(1);
211 commitTransaction(mParent.get());
212 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
213 commitTransaction(mChild.get());
214 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
215 commitTransaction(mGrandChild.get());
216 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
217 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
218 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
219 }
220
221 } // namespace
222 } // namespace android
223