blob: fd3e564cd9c28f96097fdb320c2fcf8b2ee11c04 [file] [log] [blame]
Ana Krulecc84d09b2019-11-02 23:10:29 +01001/*
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 "BufferQueueLayer.h"
25#include "BufferStateLayer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080026#include "EffectLayer.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010027#include "Layer.h"
28#include "TestableSurfaceFlinger.h"
29#include "mock/DisplayHardware/MockComposer.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010030#include "mock/MockEventThread.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070031#include "mock/MockVsyncController.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010032
33namespace android {
34
35using testing::_;
36using testing::DoAll;
37using testing::Mock;
38using testing::Return;
39using testing::SetArgPointee;
40
41using android::Hwc2::IComposer;
42using android::Hwc2::IComposerClient;
43
44using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
45
46/**
47 * This class covers all the test that are related to refresh rate selection.
48 */
49class RefreshRateSelectionTest : public testing::Test {
50public:
51 RefreshRateSelectionTest();
52 ~RefreshRateSelectionTest() override;
53
54protected:
55 static constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
56 static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
57 static constexpr uint32_t WIDTH = 100;
58 static constexpr uint32_t HEIGHT = 100;
59 static constexpr uint32_t LAYER_FLAGS = 0;
60 static constexpr int32_t PRIORITY_UNSET = -1;
61
62 void setupScheduler();
Ana Krulecc84d09b2019-11-02 23:10:29 +010063 sp<BufferQueueLayer> createBufferQueueLayer();
64 sp<BufferStateLayer> createBufferStateLayer();
Vishnu Nairfa247b12020-02-11 08:58:26 -080065 sp<EffectLayer> createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +010066
67 void setParent(Layer* child, Layer* parent);
68 void commitTransaction(Layer* layer);
69
70 TestableSurfaceFlinger mFlinger;
Ana Krulecc84d09b2019-11-02 23:10:29 +010071
72 sp<Client> mClient;
73 sp<Layer> mParent;
74 sp<Layer> mChild;
75 sp<Layer> mGrandChild;
76};
77
78RefreshRateSelectionTest::RefreshRateSelectionTest() {
79 const ::testing::TestInfo* const test_info =
80 ::testing::UnitTest::GetInstance()->current_test_info();
81 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
82
83 setupScheduler();
Dominik Laskowski13948602021-03-08 20:48:28 -080084 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
Ana Krulecc84d09b2019-11-02 23:10:29 +010085}
86
87RefreshRateSelectionTest::~RefreshRateSelectionTest() {
88 const ::testing::TestInfo* const test_info =
89 ::testing::UnitTest::GetInstance()->current_test_info();
90 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
91}
92
93sp<BufferQueueLayer> RefreshRateSelectionTest::createBufferQueueLayer() {
94 sp<Client> client;
95 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
96 LAYER_FLAGS, LayerMetadata());
97 return new BufferQueueLayer(args);
98}
99
100sp<BufferStateLayer> RefreshRateSelectionTest::createBufferStateLayer() {
101 sp<Client> client;
102 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
103 LAYER_FLAGS, LayerMetadata());
104 return new BufferStateLayer(args);
105}
106
Vishnu Nairfa247b12020-02-11 08:58:26 -0800107sp<EffectLayer> RefreshRateSelectionTest::createEffectLayer() {
Ana Krulecc84d09b2019-11-02 23:10:29 +0100108 sp<Client> client;
109 LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS,
110 LayerMetadata());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800111 return new EffectLayer(args);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100112}
113
114void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) {
115 child->setParent(parent);
116}
117
118void RefreshRateSelectionTest::commitTransaction(Layer* layer) {
Ady Abraham22c7b5c2020-09-22 19:33:40 -0700119 auto c = layer->getCurrentState();
Robert Carr0758e5d2021-03-11 22:15:04 -0800120 layer->commitTransaction(c);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100121}
122
123void RefreshRateSelectionTest::setupScheduler() {
124 auto eventThread = std::make_unique<mock::EventThread>();
125 auto sfEventThread = std::make_unique<mock::EventThread>();
126
127 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
128 EXPECT_CALL(*eventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -0700129 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
Ady Abraham62f216c2020-10-13 19:07:23 -0700130 ResyncCallback())));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100131
132 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
133 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -0700134 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
Ady Abraham62f216c2020-10-13 19:07:23 -0700135 ResyncCallback())));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100136
Ady Abraham8cb21882020-08-26 18:22:05 -0700137 auto vsyncController = std::make_unique<mock::VsyncController>();
138 auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100139
Ady Abraham8cb21882020-08-26 18:22:05 -0700140 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
141 EXPECT_CALL(*vsyncTracker, currentPeriod())
Marin Shalamanov045b7002021-01-07 16:56:24 +0100142 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
Ady Abraham8cb21882020-08-26 18:22:05 -0700143 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
144 mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
145 std::move(eventThread), std::move(sfEventThread));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100146}
147
Ana Krulecc84d09b2019-11-02 23:10:29 +0100148namespace {
149/* ------------------------------------------------------------------------
150 * Test cases
151 */
152TEST_F(RefreshRateSelectionTest, testPriorityOnBufferQueueLayers) {
153 mParent = createBufferQueueLayer();
154 mChild = createBufferQueueLayer();
155 setParent(mChild.get(), mParent.get());
156 mGrandChild = createBufferQueueLayer();
157 setParent(mGrandChild.get(), mChild.get());
158
159 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
160 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
161 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
162
163 // Child has its own priority.
164 mGrandChild->setFrameRateSelectionPriority(1);
165 commitTransaction(mGrandChild.get());
166 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
167 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
168 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
169
170 // Child inherits from his parent.
171 mChild->setFrameRateSelectionPriority(1);
172 commitTransaction(mChild.get());
173 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
174 commitTransaction(mGrandChild.get());
175
176 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
177 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
178 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
179
180 // Grandchild inherits from his grand parent.
181 mParent->setFrameRateSelectionPriority(1);
182 commitTransaction(mParent.get());
183 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
184 commitTransaction(mChild.get());
185 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
186 commitTransaction(mGrandChild.get());
187 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
188 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
189 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
190}
191
192TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) {
193 mParent = createBufferStateLayer();
194 mChild = createBufferStateLayer();
195 setParent(mChild.get(), mParent.get());
196 mGrandChild = createBufferStateLayer();
197 setParent(mGrandChild.get(), mChild.get());
198
199 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
200 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
201 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
202
203 // Child has its own priority.
204 mGrandChild->setFrameRateSelectionPriority(1);
205 commitTransaction(mGrandChild.get());
206 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
207 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
208 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
209
210 // Child inherits from his parent.
211 mChild->setFrameRateSelectionPriority(1);
212 commitTransaction(mChild.get());
213 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
214 commitTransaction(mGrandChild.get());
215 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
216 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
217 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
218
219 // Grandchild inherits from his grand parent.
220 mParent->setFrameRateSelectionPriority(1);
221 commitTransaction(mParent.get());
222 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
223 commitTransaction(mChild.get());
224 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
225 commitTransaction(mGrandChild.get());
226 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
227 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
228 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
229}
230
Vishnu Nairfa247b12020-02-11 08:58:26 -0800231TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) {
232 mParent = createEffectLayer();
233 mChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100234 setParent(mChild.get(), mParent.get());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800235 mGrandChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100236 setParent(mGrandChild.get(), mChild.get());
237
238 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
239 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
240 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
241
242 // Child has its own priority.
243 mGrandChild->setFrameRateSelectionPriority(1);
244 commitTransaction(mGrandChild.get());
245 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
246 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
247 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
248
249 // Child inherits from his parent.
250 mChild->setFrameRateSelectionPriority(1);
251 commitTransaction(mChild.get());
252 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
253 commitTransaction(mGrandChild.get());
254 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
255 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
256 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
257
258 // Grandchild inherits from his grand parent.
259 mParent->setFrameRateSelectionPriority(1);
260 commitTransaction(mParent.get());
261 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
262 commitTransaction(mChild.get());
263 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
264 commitTransaction(mGrandChild.get());
265 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
266 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
267 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
268}
269
270} // namespace
Ady Abraham2b55c3b2020-01-16 16:38:51 -0800271} // namespace android