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 "DisplayTransactionTestHelpers.h"
21
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24
25 namespace android {
26 namespace {
27
28 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
29
30 class DisplayDeviceSetProjectionTest : public DisplayTransactionTest {
31 public:
32 static constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1080; // arbitrary
33 static constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1920; // arbitrary
34
35 static constexpr int32_t TRANSFORM_FLAGS_ROT_0 = 0;
36 static constexpr int32_t TRANSFORM_FLAGS_ROT_90 = HAL_TRANSFORM_ROT_90;
37 static constexpr int32_t TRANSFORM_FLAGS_ROT_180 = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V;
38 static constexpr int32_t TRANSFORM_FLAGS_ROT_270 =
39 HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
40
DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize,ui::Size hardwareDisplaySize,ui::Rotation physicalOrientation)41 DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize, ui::Size hardwareDisplaySize,
42 ui::Rotation physicalOrientation)
43 : mFlingerDisplaySize(flingerDisplaySize),
44 mHardwareDisplaySize(hardwareDisplaySize),
45 mPhysicalOrientation(physicalOrientation),
46 mDisplayDevice(createDisplayDevice()) {}
47
createDisplayDevice()48 sp<DisplayDevice> createDisplayDevice() {
49 return injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
50 injector.setPhysicalOrientation(mPhysicalOrientation);
51 });
52 }
53
swapWH(const ui::Size size) const54 ui::Size swapWH(const ui::Size size) const { return ui::Size(size.height, size.width); }
55
setProjectionForRotation0()56 void setProjectionForRotation0() {
57 // A logical rotation of 0 uses the SurfaceFlinger display size
58 mDisplayDevice->setProjection(ui::ROTATION_0, Rect(mFlingerDisplaySize),
59 Rect(mFlingerDisplaySize));
60 }
61
setProjectionForRotation90()62 void setProjectionForRotation90() {
63 // A logical rotation of 90 uses the SurfaceFlinger display size with
64 // the width/height swapped.
65 mDisplayDevice->setProjection(ui::ROTATION_90, Rect(swapWH(mFlingerDisplaySize)),
66 Rect(swapWH(mFlingerDisplaySize)));
67 }
68
setProjectionForRotation180()69 void setProjectionForRotation180() {
70 // A logical rotation of 180 uses the SurfaceFlinger display size
71 mDisplayDevice->setProjection(ui::ROTATION_180, Rect(mFlingerDisplaySize),
72 Rect(mFlingerDisplaySize));
73 }
74
setProjectionForRotation270()75 void setProjectionForRotation270() {
76 // A logical rotation of 270 uses the SurfaceFlinger display size with
77 // the width/height swapped.
78 mDisplayDevice->setProjection(ui::ROTATION_270, Rect(swapWH(mFlingerDisplaySize)),
79 Rect(swapWH(mFlingerDisplaySize)));
80 }
81
expectStateForHardwareTransform0()82 void expectStateForHardwareTransform0() {
83 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
84 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_0, mHardwareDisplaySize.width,
85 mHardwareDisplaySize.height),
86 compositionState.transform);
87 EXPECT_EQ(ui::ROTATION_0, compositionState.displaySpace.orientation);
88 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.content);
89 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.orientedDisplaySpace.content);
90 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.layerStackSpace.content);
91 EXPECT_EQ(false, compositionState.needsFiltering);
92 }
93
expectStateForHardwareTransform90()94 void expectStateForHardwareTransform90() {
95 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
96 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_90, mHardwareDisplaySize.width,
97 mHardwareDisplaySize.height),
98 compositionState.transform);
99 EXPECT_EQ(ui::ROTATION_90, compositionState.displaySpace.orientation);
100 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.content);
101 // For 90, the orientedDisplaySpaceRect and layerStackSpaceRect have the hardware display
102 // size width and height swapped
103 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
104 compositionState.orientedDisplaySpace.content);
105 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)), compositionState.layerStackSpace.content);
106 EXPECT_EQ(false, compositionState.needsFiltering);
107 }
108
expectStateForHardwareTransform180()109 void expectStateForHardwareTransform180() {
110 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
111 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_180, mHardwareDisplaySize.width,
112 mHardwareDisplaySize.height),
113 compositionState.transform);
114 EXPECT_EQ(ui::ROTATION_180, compositionState.displaySpace.orientation);
115 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.orientedDisplaySpace.content);
116 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.layerStackSpace.content);
117 EXPECT_EQ(false, compositionState.needsFiltering);
118 }
119
expectStateForHardwareTransform270()120 void expectStateForHardwareTransform270() {
121 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
122 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_270, mHardwareDisplaySize.width,
123 mHardwareDisplaySize.height),
124 compositionState.transform);
125 EXPECT_EQ(ui::ROTATION_270, compositionState.displaySpace.orientation);
126 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.content);
127 // For 270, the orientedDisplaySpaceRect and layerStackSpaceRect have the hardware display
128 // size width and height swapped
129 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
130 compositionState.orientedDisplaySpace.content);
131 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)), compositionState.layerStackSpace.content);
132 EXPECT_EQ(false, compositionState.needsFiltering);
133 }
134
135 const ui::Size mFlingerDisplaySize;
136 const ui::Size mHardwareDisplaySize;
137 const ui::Rotation mPhysicalOrientation;
138 const sp<DisplayDevice> mDisplayDevice;
139 };
140
141 struct DisplayDeviceSetProjectionTest_Installed0 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed0android::__anon6b800e610111::DisplayDeviceSetProjectionTest_Installed0142 DisplayDeviceSetProjectionTest_Installed0()
143 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
144 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
145 ui::ROTATION_0) {}
146 };
147
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith0OutputRotation)148 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith0OutputRotation) {
149 setProjectionForRotation0();
150 expectStateForHardwareTransform0();
151 }
152
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith90OutputRotation)153 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith90OutputRotation) {
154 setProjectionForRotation90();
155 expectStateForHardwareTransform90();
156 }
157
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith180OutputRotation)158 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith180OutputRotation) {
159 setProjectionForRotation180();
160 expectStateForHardwareTransform180();
161 }
162
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith270OutputRotation)163 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith270OutputRotation) {
164 setProjectionForRotation270();
165 expectStateForHardwareTransform270();
166 }
167
168 struct DisplayDeviceSetProjectionTest_Installed90 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed90android::__anon6b800e610111::DisplayDeviceSetProjectionTest_Installed90169 DisplayDeviceSetProjectionTest_Installed90()
170 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
171 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
172 ui::ROTATION_90) {}
173 };
174
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith0OutputRotation)175 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith0OutputRotation) {
176 setProjectionForRotation0();
177 expectStateForHardwareTransform90();
178 }
179
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith90OutputRotation)180 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith90OutputRotation) {
181 setProjectionForRotation90();
182 expectStateForHardwareTransform180();
183 }
184
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith180OutputRotation)185 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith180OutputRotation) {
186 setProjectionForRotation180();
187 expectStateForHardwareTransform270();
188 }
189
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith270OutputRotation)190 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith270OutputRotation) {
191 setProjectionForRotation270();
192 expectStateForHardwareTransform0();
193 }
194
195 struct DisplayDeviceSetProjectionTest_Installed180 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed180android::__anon6b800e610111::DisplayDeviceSetProjectionTest_Installed180196 DisplayDeviceSetProjectionTest_Installed180()
197 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
198 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
199 ui::ROTATION_180) {}
200 };
201
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith0OutputRotation)202 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith0OutputRotation) {
203 setProjectionForRotation0();
204 expectStateForHardwareTransform180();
205 }
206
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith90OutputRotation)207 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith90OutputRotation) {
208 setProjectionForRotation90();
209 expectStateForHardwareTransform270();
210 }
211
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith180OutputRotation)212 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith180OutputRotation) {
213 setProjectionForRotation180();
214 expectStateForHardwareTransform0();
215 }
216
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith270OutputRotation)217 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith270OutputRotation) {
218 setProjectionForRotation270();
219 expectStateForHardwareTransform90();
220 }
221
222 struct DisplayDeviceSetProjectionTest_Installed270 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed270android::__anon6b800e610111::DisplayDeviceSetProjectionTest_Installed270223 DisplayDeviceSetProjectionTest_Installed270()
224 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
225 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
226 ui::ROTATION_270) {}
227 };
228
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith0OutputRotation)229 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith0OutputRotation) {
230 setProjectionForRotation0();
231 expectStateForHardwareTransform270();
232 }
233
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith90OutputRotation)234 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith90OutputRotation) {
235 setProjectionForRotation90();
236 expectStateForHardwareTransform0();
237 }
238
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith180OutputRotation)239 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith180OutputRotation) {
240 setProjectionForRotation180();
241 expectStateForHardwareTransform90();
242 }
243
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith270OutputRotation)244 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith270OutputRotation) {
245 setProjectionForRotation270();
246 expectStateForHardwareTransform180();
247 }
248
249 } // namespace
250 } // namespace android
251