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 #include <ui/Rotation.h>
25
26 namespace android {
27 namespace {
28
29 using FakeDisplayDeviceInjector = TestableSurfaceFlinger::FakeDisplayDeviceInjector;
30
31 class DisplayDeviceSetProjectionTest : public DisplayTransactionTest {
32 public:
33 static constexpr int32_t DEFAULT_DISPLAY_WIDTH = 1080; // arbitrary
34 static constexpr int32_t DEFAULT_DISPLAY_HEIGHT = 1920; // arbitrary
35
36 static constexpr int32_t TRANSFORM_FLAGS_ROT_0 = 0;
37 static constexpr int32_t TRANSFORM_FLAGS_ROT_90 = HAL_TRANSFORM_ROT_90;
38 static constexpr int32_t TRANSFORM_FLAGS_ROT_180 = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V;
39 static constexpr int32_t TRANSFORM_FLAGS_ROT_270 =
40 HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
41
DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize,ui::Size hardwareDisplaySize,ui::Rotation physicalOrientation)42 DisplayDeviceSetProjectionTest(ui::Size flingerDisplaySize, ui::Size hardwareDisplaySize,
43 ui::Rotation physicalOrientation)
44 : mFlingerDisplaySize(flingerDisplaySize),
45 mHardwareDisplaySize(hardwareDisplaySize),
46 mPhysicalOrientation(physicalOrientation),
47 mDisplayDevice(createDisplayDevice()) {}
48
createDisplayDevice()49 sp<DisplayDevice> createDisplayDevice() {
50 return injectDefaultInternalDisplay([this](FakeDisplayDeviceInjector& injector) {
51 injector.setPhysicalOrientation(mPhysicalOrientation);
52 });
53 }
54
swapWH(const ui::Size size) const55 ui::Size swapWH(const ui::Size size) const { return ui::Size(size.height, size.width); }
56
setDefaultProjection()57 void setDefaultProjection() {
58 // INVALID_RECT pulls from the physical display dimensions.
59 mDisplayDevice->setProjection(ui::ROTATION_0, Rect::INVALID_RECT, Rect::INVALID_RECT);
60 }
61
setProjectionForRotation0()62 void setProjectionForRotation0() {
63 // A logical rotation of 0 uses the SurfaceFlinger display size
64 mDisplayDevice->setProjection(ui::ROTATION_0, Rect(mFlingerDisplaySize),
65 Rect(mFlingerDisplaySize));
66 }
67
setProjectionForRotation90()68 void setProjectionForRotation90() {
69 // A logical rotation of 90 uses the SurfaceFlinger display size with
70 // the width/height swapped.
71 mDisplayDevice->setProjection(ui::ROTATION_90, Rect(swapWH(mFlingerDisplaySize)),
72 Rect(swapWH(mFlingerDisplaySize)));
73 }
74
setProjectionForRotation180()75 void setProjectionForRotation180() {
76 // A logical rotation of 180 uses the SurfaceFlinger display size
77 mDisplayDevice->setProjection(ui::ROTATION_180, Rect(mFlingerDisplaySize),
78 Rect(mFlingerDisplaySize));
79 }
80
setProjectionForRotation270()81 void setProjectionForRotation270() {
82 // A logical rotation of 270 uses the SurfaceFlinger display size with
83 // the width/height swapped.
84 mDisplayDevice->setProjection(ui::ROTATION_270, Rect(swapWH(mFlingerDisplaySize)),
85 Rect(swapWH(mFlingerDisplaySize)));
86 }
87
expectDefaultState()88 void expectDefaultState() {
89 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
90 EXPECT_EQ(ui::Transform(ui::Transform::toRotationFlags(mPhysicalOrientation),
91 mHardwareDisplaySize.width, mHardwareDisplaySize.height),
92 compositionState.transform);
93 EXPECT_EQ(mPhysicalOrientation, compositionState.displaySpace.getOrientation());
94 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.getContent());
95 EXPECT_EQ(mHardwareDisplaySize, compositionState.displaySpace.getBounds());
96 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.framebufferSpace.getContent());
97 EXPECT_EQ(mHardwareDisplaySize, compositionState.framebufferSpace.getBounds());
98
99 const ui::Size expectedLogicalSize = (mPhysicalOrientation == ui::ROTATION_270 ||
100 mPhysicalOrientation == ui::ROTATION_90)
101 ? swapWH(mHardwareDisplaySize)
102 : mHardwareDisplaySize;
103
104 EXPECT_EQ(Rect(expectedLogicalSize), compositionState.orientedDisplaySpace.getContent());
105 EXPECT_EQ(expectedLogicalSize, compositionState.orientedDisplaySpace.getBounds());
106 EXPECT_EQ(Rect(expectedLogicalSize), compositionState.layerStackSpace.getContent());
107 EXPECT_EQ(expectedLogicalSize, compositionState.layerStackSpace.getBounds());
108
109 EXPECT_EQ(false, compositionState.needsFiltering);
110 }
111
expectStateForHardwareTransform0()112 void expectStateForHardwareTransform0() {
113 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
114 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_0, mHardwareDisplaySize.width,
115 mHardwareDisplaySize.height),
116 compositionState.transform);
117 EXPECT_EQ(ui::ROTATION_0, compositionState.displaySpace.getOrientation());
118 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.getContent());
119 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.orientedDisplaySpace.getContent());
120 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.layerStackSpace.getContent());
121 EXPECT_EQ(false, compositionState.needsFiltering);
122 }
123
expectStateForHardwareTransform90()124 void expectStateForHardwareTransform90() {
125 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
126 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_90, mHardwareDisplaySize.width,
127 mHardwareDisplaySize.height),
128 compositionState.transform);
129 EXPECT_EQ(ui::ROTATION_90, compositionState.displaySpace.getOrientation());
130 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.getContent());
131 // For 90, the orientedDisplaySpaceRect and layerStackSpaceRect have the hardware display
132 // size width and height swapped
133 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
134 compositionState.orientedDisplaySpace.getContent());
135 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
136 compositionState.layerStackSpace.getContent());
137 EXPECT_EQ(false, compositionState.needsFiltering);
138 }
139
expectStateForHardwareTransform180()140 void expectStateForHardwareTransform180() {
141 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
142 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_180, mHardwareDisplaySize.width,
143 mHardwareDisplaySize.height),
144 compositionState.transform);
145 EXPECT_EQ(ui::ROTATION_180, compositionState.displaySpace.getOrientation());
146 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.orientedDisplaySpace.getContent());
147 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.layerStackSpace.getContent());
148 EXPECT_EQ(false, compositionState.needsFiltering);
149 }
150
expectStateForHardwareTransform270()151 void expectStateForHardwareTransform270() {
152 const auto& compositionState = mDisplayDevice->getCompositionDisplay()->getState();
153 EXPECT_EQ(ui::Transform(TRANSFORM_FLAGS_ROT_270, mHardwareDisplaySize.width,
154 mHardwareDisplaySize.height),
155 compositionState.transform);
156 EXPECT_EQ(ui::ROTATION_270, compositionState.displaySpace.getOrientation());
157 EXPECT_EQ(Rect(mHardwareDisplaySize), compositionState.displaySpace.getContent());
158 // For 270, the orientedDisplaySpaceRect and layerStackSpaceRect have the hardware display
159 // size width and height swapped
160 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
161 compositionState.orientedDisplaySpace.getContent());
162 EXPECT_EQ(Rect(swapWH(mHardwareDisplaySize)),
163 compositionState.layerStackSpace.getContent());
164 EXPECT_EQ(false, compositionState.needsFiltering);
165 }
166
167 const ui::Size mFlingerDisplaySize;
168 const ui::Size mHardwareDisplaySize;
169 const ui::Rotation mPhysicalOrientation;
170 const sp<DisplayDevice> mDisplayDevice;
171 };
172
173 struct DisplayDeviceSetProjectionTest_Installed0 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed0android::__anon6018912e0111::DisplayDeviceSetProjectionTest_Installed0174 DisplayDeviceSetProjectionTest_Installed0()
175 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
176 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
177 ui::ROTATION_0) {}
178 };
179
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkDefaultProjection)180 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkDefaultProjection) {
181 setDefaultProjection();
182 expectDefaultState();
183 }
184
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith0OutputRotation)185 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith0OutputRotation) {
186 setProjectionForRotation0();
187 expectStateForHardwareTransform0();
188 }
189
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith90OutputRotation)190 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith90OutputRotation) {
191 setProjectionForRotation90();
192 expectStateForHardwareTransform90();
193 }
194
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith180OutputRotation)195 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith180OutputRotation) {
196 setProjectionForRotation180();
197 expectStateForHardwareTransform180();
198 }
199
TEST_F(DisplayDeviceSetProjectionTest_Installed0,checkWith270OutputRotation)200 TEST_F(DisplayDeviceSetProjectionTest_Installed0, checkWith270OutputRotation) {
201 setProjectionForRotation270();
202 expectStateForHardwareTransform270();
203 }
204
205 struct DisplayDeviceSetProjectionTest_Installed90 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed90android::__anon6018912e0111::DisplayDeviceSetProjectionTest_Installed90206 DisplayDeviceSetProjectionTest_Installed90()
207 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
208 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
209 ui::ROTATION_90) {}
210 };
211
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkDefaultProjection)212 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkDefaultProjection) {
213 setDefaultProjection();
214 expectDefaultState();
215 }
216
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith0OutputRotation)217 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith0OutputRotation) {
218 setProjectionForRotation0();
219 expectStateForHardwareTransform90();
220 }
221
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith90OutputRotation)222 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith90OutputRotation) {
223 setProjectionForRotation90();
224 expectStateForHardwareTransform180();
225 }
226
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith180OutputRotation)227 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith180OutputRotation) {
228 setProjectionForRotation180();
229 expectStateForHardwareTransform270();
230 }
231
TEST_F(DisplayDeviceSetProjectionTest_Installed90,checkWith270OutputRotation)232 TEST_F(DisplayDeviceSetProjectionTest_Installed90, checkWith270OutputRotation) {
233 setProjectionForRotation270();
234 expectStateForHardwareTransform0();
235 }
236
237 struct DisplayDeviceSetProjectionTest_Installed180 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed180android::__anon6018912e0111::DisplayDeviceSetProjectionTest_Installed180238 DisplayDeviceSetProjectionTest_Installed180()
239 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
240 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
241 ui::ROTATION_180) {}
242 };
243
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkDefaultProjection)244 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkDefaultProjection) {
245 setDefaultProjection();
246 expectDefaultState();
247 }
248
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith0OutputRotation)249 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith0OutputRotation) {
250 setProjectionForRotation0();
251 expectStateForHardwareTransform180();
252 }
253
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith90OutputRotation)254 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith90OutputRotation) {
255 setProjectionForRotation90();
256 expectStateForHardwareTransform270();
257 }
258
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith180OutputRotation)259 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith180OutputRotation) {
260 setProjectionForRotation180();
261 expectStateForHardwareTransform0();
262 }
263
TEST_F(DisplayDeviceSetProjectionTest_Installed180,checkWith270OutputRotation)264 TEST_F(DisplayDeviceSetProjectionTest_Installed180, checkWith270OutputRotation) {
265 setProjectionForRotation270();
266 expectStateForHardwareTransform90();
267 }
268
269 struct DisplayDeviceSetProjectionTest_Installed270 : public DisplayDeviceSetProjectionTest {
DisplayDeviceSetProjectionTest_Installed270android::__anon6018912e0111::DisplayDeviceSetProjectionTest_Installed270270 DisplayDeviceSetProjectionTest_Installed270()
271 : DisplayDeviceSetProjectionTest(ui::Size(DEFAULT_DISPLAY_HEIGHT, DEFAULT_DISPLAY_WIDTH),
272 ui::Size(DEFAULT_DISPLAY_WIDTH, DEFAULT_DISPLAY_HEIGHT),
273 ui::ROTATION_270) {}
274 };
275
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkDefaultProjection)276 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkDefaultProjection) {
277 setDefaultProjection();
278 expectDefaultState();
279 }
280
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith0OutputRotation)281 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith0OutputRotation) {
282 setProjectionForRotation0();
283 expectStateForHardwareTransform270();
284 }
285
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith90OutputRotation)286 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith90OutputRotation) {
287 setProjectionForRotation90();
288 expectStateForHardwareTransform0();
289 }
290
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith180OutputRotation)291 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith180OutputRotation) {
292 setProjectionForRotation180();
293 expectStateForHardwareTransform90();
294 }
295
TEST_F(DisplayDeviceSetProjectionTest_Installed270,checkWith270OutputRotation)296 TEST_F(DisplayDeviceSetProjectionTest_Installed270, checkWith270OutputRotation) {
297 setProjectionForRotation270();
298 expectStateForHardwareTransform180();
299 }
300
301 } // namespace
302 } // namespace android
303