blob: e388a6f40f40f5b18a38405bfc49c29ede8479e1 [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
Ana Krulecc84d09b2019-11-02 23:10:29 +010024#include "BufferStateLayer.h"
Vishnu Nairfa247b12020-02-11 08:58:26 -080025#include "EffectLayer.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010026#include "Layer.h"
27#include "TestableSurfaceFlinger.h"
28#include "mock/DisplayHardware/MockComposer.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010029#include "mock/MockEventThread.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070030#include "mock/MockVsyncController.h"
Ana Krulecc84d09b2019-11-02 23:10:29 +010031
32namespace android {
33
34using testing::_;
35using testing::DoAll;
36using testing::Mock;
37using testing::Return;
38using testing::SetArgPointee;
39
40using android::Hwc2::IComposer;
41using android::Hwc2::IComposerClient;
42
43using FakeHwcDisplayInjector = TestableSurfaceFlinger::FakeHwcDisplayInjector;
44
45/**
46 * This class covers all the test that are related to refresh rate selection.
47 */
48class RefreshRateSelectionTest : public testing::Test {
49public:
50 RefreshRateSelectionTest();
51 ~RefreshRateSelectionTest() override;
52
53protected:
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();
Ana Krulecc84d09b2019-11-02 23:10:29 +010062 sp<BufferStateLayer> createBufferStateLayer();
Vishnu Nairfa247b12020-02-11 08:58:26 -080063 sp<EffectLayer> createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +010064
65 void setParent(Layer* child, Layer* parent);
66 void commitTransaction(Layer* layer);
67
68 TestableSurfaceFlinger mFlinger;
Ana Krulecc84d09b2019-11-02 23:10:29 +010069
70 sp<Client> mClient;
71 sp<Layer> mParent;
72 sp<Layer> mChild;
73 sp<Layer> mGrandChild;
74};
75
76RefreshRateSelectionTest::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();
Dominik Laskowski13948602021-03-08 20:48:28 -080082 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
Ana Krulecc84d09b2019-11-02 23:10:29 +010083}
84
85RefreshRateSelectionTest::~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
Ana Krulecc84d09b2019-11-02 23:10:29 +010091
92sp<BufferStateLayer> RefreshRateSelectionTest::createBufferStateLayer() {
93 sp<Client> client;
94 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", WIDTH, HEIGHT,
95 LAYER_FLAGS, LayerMetadata());
96 return new BufferStateLayer(args);
97}
98
Vishnu Nairfa247b12020-02-11 08:58:26 -080099sp<EffectLayer> RefreshRateSelectionTest::createEffectLayer() {
Ana Krulecc84d09b2019-11-02 23:10:29 +0100100 sp<Client> client;
101 LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", WIDTH, HEIGHT, LAYER_FLAGS,
102 LayerMetadata());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800103 return new EffectLayer(args);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100104}
105
106void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) {
107 child->setParent(parent);
108}
109
110void RefreshRateSelectionTest::commitTransaction(Layer* layer) {
Robert Carr6a160312021-05-17 12:08:20 -0700111 auto c = layer->getDrawingState();
Robert Carr0758e5d2021-03-11 22:15:04 -0800112 layer->commitTransaction(c);
Ana Krulecc84d09b2019-11-02 23:10:29 +0100113}
114
115void RefreshRateSelectionTest::setupScheduler() {
116 auto eventThread = std::make_unique<mock::EventThread>();
117 auto sfEventThread = std::make_unique<mock::EventThread>();
118
119 EXPECT_CALL(*eventThread, registerDisplayEventConnection(_));
120 EXPECT_CALL(*eventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -0700121 .WillOnce(Return(new EventThreadConnection(eventThread.get(), /*callingUid=*/0,
Ady Abraham62f216c2020-10-13 19:07:23 -0700122 ResyncCallback())));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100123
124 EXPECT_CALL(*sfEventThread, registerDisplayEventConnection(_));
125 EXPECT_CALL(*sfEventThread, createEventConnection(_, _))
Ady Abraham0bb6a472020-10-12 10:22:13 -0700126 .WillOnce(Return(new EventThreadConnection(sfEventThread.get(), /*callingUid=*/0,
Ady Abraham62f216c2020-10-13 19:07:23 -0700127 ResyncCallback())));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100128
Ady Abraham8cb21882020-08-26 18:22:05 -0700129 auto vsyncController = std::make_unique<mock::VsyncController>();
130 auto vsyncTracker = std::make_unique<mock::VSyncTracker>();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100131
Ady Abraham8cb21882020-08-26 18:22:05 -0700132 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
133 EXPECT_CALL(*vsyncTracker, currentPeriod())
Marin Shalamanov045b7002021-01-07 16:56:24 +0100134 .WillRepeatedly(Return(FakeHwcDisplayInjector::DEFAULT_VSYNC_PERIOD));
Ady Abraham8cb21882020-08-26 18:22:05 -0700135 EXPECT_CALL(*vsyncTracker, nextAnticipatedVSyncTimeFrom(_)).WillRepeatedly(Return(0));
136 mFlinger.setupScheduler(std::move(vsyncController), std::move(vsyncTracker),
137 std::move(eventThread), std::move(sfEventThread));
Ana Krulecc84d09b2019-11-02 23:10:29 +0100138}
139
Ana Krulecc84d09b2019-11-02 23:10:29 +0100140namespace {
141/* ------------------------------------------------------------------------
142 * Test cases
143 */
Ana Krulecc84d09b2019-11-02 23:10:29 +0100144TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) {
145 mParent = createBufferStateLayer();
146 mChild = createBufferStateLayer();
147 setParent(mChild.get(), mParent.get());
148 mGrandChild = createBufferStateLayer();
149 setParent(mGrandChild.get(), mChild.get());
150
151 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
152 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
153 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
154
155 // Child has its own priority.
156 mGrandChild->setFrameRateSelectionPriority(1);
157 commitTransaction(mGrandChild.get());
158 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
159 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
160 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
161
162 // Child inherits from his parent.
163 mChild->setFrameRateSelectionPriority(1);
164 commitTransaction(mChild.get());
165 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
166 commitTransaction(mGrandChild.get());
167 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
168 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
169 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
170
171 // Grandchild inherits from his grand parent.
172 mParent->setFrameRateSelectionPriority(1);
173 commitTransaction(mParent.get());
174 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
175 commitTransaction(mChild.get());
176 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
177 commitTransaction(mGrandChild.get());
178 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
179 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
180 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
181}
182
Vishnu Nairfa247b12020-02-11 08:58:26 -0800183TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) {
184 mParent = createEffectLayer();
185 mChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100186 setParent(mChild.get(), mParent.get());
Vishnu Nairfa247b12020-02-11 08:58:26 -0800187 mGrandChild = createEffectLayer();
Ana Krulecc84d09b2019-11-02 23:10:29 +0100188 setParent(mGrandChild.get(), mChild.get());
189
190 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
191 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
192 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
193
194 // Child has its own priority.
195 mGrandChild->setFrameRateSelectionPriority(1);
196 commitTransaction(mGrandChild.get());
197 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
198 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
199 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
200
201 // Child inherits from his parent.
202 mChild->setFrameRateSelectionPriority(1);
203 commitTransaction(mChild.get());
204 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
205 commitTransaction(mGrandChild.get());
206 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
207 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
208 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
209
210 // Grandchild inherits from his grand parent.
211 mParent->setFrameRateSelectionPriority(1);
212 commitTransaction(mParent.get());
213 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
214 commitTransaction(mChild.get());
215 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
216 commitTransaction(mGrandChild.get());
217 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
218 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
219 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
220}
221
222} // namespace
Ady Abraham2b55c3b2020-01-16 16:38:51 -0800223} // namespace android