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 "Layer.h"
25 #include "TestableSurfaceFlinger.h"
26 #include "mock/DisplayHardware/MockComposer.h"
27
28 namespace android {
29
30 using testing::_;
31 using testing::DoAll;
32 using testing::Mock;
33 using testing::Return;
34 using testing::SetArgPointee;
35
36 using android::Hwc2::IComposer;
37 using android::Hwc2::IComposerClient;
38
39 /**
40 * This class covers all the test that are related to refresh rate selection.
41 */
42 class RefreshRateSelectionTest : public testing::Test {
43 public:
44 RefreshRateSelectionTest();
45 ~RefreshRateSelectionTest() override;
46
47 protected:
48 static constexpr int DEFAULT_DISPLAY_WIDTH = 1920;
49 static constexpr int DEFAULT_DISPLAY_HEIGHT = 1024;
50 static constexpr uint32_t WIDTH = 100;
51 static constexpr uint32_t HEIGHT = 100;
52 static constexpr uint32_t LAYER_FLAGS = 0;
53 static constexpr int32_t PRIORITY_UNSET = -1;
54
55 sp<Layer> createBufferStateLayer();
56 sp<Layer> createEffectLayer();
57
58 void setParent(Layer* child, Layer* parent);
59 void commitTransaction(Layer* layer);
60
61 TestableSurfaceFlinger mFlinger;
62
63 sp<Client> mClient;
64 sp<Layer> mParent;
65 sp<Layer> mChild;
66 sp<Layer> mGrandChild;
67 };
68
RefreshRateSelectionTest()69 RefreshRateSelectionTest::RefreshRateSelectionTest() {
70 const ::testing::TestInfo* const test_info =
71 ::testing::UnitTest::GetInstance()->current_test_info();
72 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
73
74 mFlinger.setupMockScheduler();
75 mFlinger.setupComposer(std::make_unique<Hwc2::mock::Composer>());
76 }
77
~RefreshRateSelectionTest()78 RefreshRateSelectionTest::~RefreshRateSelectionTest() {
79 const ::testing::TestInfo* const test_info =
80 ::testing::UnitTest::GetInstance()->current_test_info();
81 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
82 }
83
createBufferStateLayer()84 sp<Layer> RefreshRateSelectionTest::createBufferStateLayer() {
85 sp<Client> client;
86 LayerCreationArgs args(mFlinger.flinger(), client, "buffer-queue-layer", LAYER_FLAGS,
87 LayerMetadata());
88 return sp<Layer>::make(args);
89 }
90
createEffectLayer()91 sp<Layer> RefreshRateSelectionTest::createEffectLayer() {
92 sp<Client> client;
93 LayerCreationArgs args(mFlinger.flinger(), client, "color-layer", LAYER_FLAGS, LayerMetadata());
94 return sp<Layer>::make(args);
95 }
96
setParent(Layer * child,Layer * parent)97 void RefreshRateSelectionTest::setParent(Layer* child, Layer* parent) {
98 child->setParent(sp<Layer>::fromExisting(parent));
99 }
100
commitTransaction(Layer * layer)101 void RefreshRateSelectionTest::commitTransaction(Layer* layer) {
102 layer->commitTransaction();
103 }
104
105 namespace {
106
TEST_F(RefreshRateSelectionTest,testPriorityOnBufferStateLayers)107 TEST_F(RefreshRateSelectionTest, testPriorityOnBufferStateLayers) {
108 mParent = createBufferStateLayer();
109 mChild = createBufferStateLayer();
110 setParent(mChild.get(), mParent.get());
111 mGrandChild = createBufferStateLayer();
112 setParent(mGrandChild.get(), mChild.get());
113
114 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
115 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
116 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
117
118 // Child has its own priority.
119 mGrandChild->setFrameRateSelectionPriority(1);
120 commitTransaction(mGrandChild.get());
121 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
122 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
123 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
124
125 // Child inherits from his parent.
126 mChild->setFrameRateSelectionPriority(1);
127 commitTransaction(mChild.get());
128 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
129 commitTransaction(mGrandChild.get());
130 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
131 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
132 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
133
134 // Grandchild inherits from his grand parent.
135 mParent->setFrameRateSelectionPriority(1);
136 commitTransaction(mParent.get());
137 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
138 commitTransaction(mChild.get());
139 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
140 commitTransaction(mGrandChild.get());
141 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
142 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
143 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
144 }
145
TEST_F(RefreshRateSelectionTest,testPriorityOnEffectLayers)146 TEST_F(RefreshRateSelectionTest, testPriorityOnEffectLayers) {
147 mParent = createEffectLayer();
148 mChild = createEffectLayer();
149 setParent(mChild.get(), mParent.get());
150 mGrandChild = createEffectLayer();
151 setParent(mGrandChild.get(), mChild.get());
152
153 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
154 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
155 ASSERT_EQ(PRIORITY_UNSET, mGrandChild->getFrameRateSelectionPriority());
156
157 // Child has its own priority.
158 mGrandChild->setFrameRateSelectionPriority(1);
159 commitTransaction(mGrandChild.get());
160 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
161 ASSERT_EQ(PRIORITY_UNSET, mChild->getFrameRateSelectionPriority());
162 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
163
164 // Child inherits from his parent.
165 mChild->setFrameRateSelectionPriority(1);
166 commitTransaction(mChild.get());
167 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
168 commitTransaction(mGrandChild.get());
169 ASSERT_EQ(PRIORITY_UNSET, mParent->getFrameRateSelectionPriority());
170 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
171 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
172
173 // Grandchild inherits from his grand parent.
174 mParent->setFrameRateSelectionPriority(1);
175 commitTransaction(mParent.get());
176 mChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
177 commitTransaction(mChild.get());
178 mGrandChild->setFrameRateSelectionPriority(PRIORITY_UNSET);
179 commitTransaction(mGrandChild.get());
180 ASSERT_EQ(1, mParent->getFrameRateSelectionPriority());
181 ASSERT_EQ(1, mChild->getFrameRateSelectionPriority());
182 ASSERT_EQ(1, mGrandChild->getFrameRateSelectionPriority());
183 }
184
185 } // namespace
186 } // namespace android
187