blob: 1180cacf7fe235950c18948452a6e1aa62b83152 [file] [log] [blame]
Valerie Hau9cfc6d82019-09-23 13:54:07 -07001/*
2 * Copyright (C) 2019 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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Valerie Hau9cfc6d82019-09-23 13:54:07 -070021#include "LayerTransactionTest.h"
22
23namespace android {
24
25using android::hardware::graphics::common::V1_1::BufferUsage;
26
27::testing::Environment* const binderEnv =
28 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
29
30class RelativeZTest : public LayerTransactionTest {
31protected:
32 virtual void SetUp() {
33 LayerTransactionTest::SetUp();
34 ASSERT_EQ(NO_ERROR, mClient->initCheck());
35
36 const auto display = SurfaceComposerClient::getInternalDisplayToken();
37 ASSERT_FALSE(display == nullptr);
38
39 // Back layer
40 mBackgroundLayer = createColorLayer("Background layer", Color::RED);
41
42 // Front layer
43 mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN);
44
45 asTransaction([&](Transaction& t) {
46 t.setDisplayLayerStack(display, 0);
47 t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer);
48 t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer);
49 });
50 }
51
52 virtual void TearDown() {
53 LayerTransactionTest::TearDown();
54 mBackgroundLayer = 0;
55 mForegroundLayer = 0;
56 }
57
58 sp<SurfaceControl> mBackgroundLayer;
59 sp<SurfaceControl> mForegroundLayer;
60};
61
62// When a layer is reparented offscreen, remove relative z order if the relative parent
63// is still onscreen so that the layer is not drawn.
64TEST_F(RelativeZTest, LayerRemoved) {
65 std::unique_ptr<ScreenCapture> sc;
66
67 // Background layer (RED)
68 // Child layer (WHITE) (relative to foregroud layer)
69 // Foregroud layer (GREEN)
70 sp<SurfaceControl> childLayer =
71 createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get());
72
73 Transaction{}
74 .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1)
75 .show(childLayer)
76 .apply();
77
78 {
79 // The childLayer should be in front of the FG control.
80 ScreenCapture::captureScreen(&sc);
81 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
82 }
83
84 // Background layer (RED)
85 // Foregroud layer (GREEN)
86 Transaction{}.reparent(childLayer, nullptr).apply();
87
88 // Background layer (RED)
89 // Child layer (WHITE)
90 // Foregroud layer (GREEN)
91 Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply();
92
93 {
94 // The relative z info for child layer should be reset, leaving FG control on top.
95 ScreenCapture::captureScreen(&sc);
96 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
97 }
98}
99
100// When a layer is reparented offscreen, preseve relative z order if the relative parent
101// is also offscreen. Regression test b/132613412
102TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) {
103 std::unique_ptr<ScreenCapture> sc;
104
105 // Background layer (RED)
106 // Foregroud layer (GREEN)
107 // child level 1 (WHITE)
108 // child level 2a (BLUE)
109 // child level 3 (GREEN) (relative to child level 2b)
110 // child level 2b (BLACK)
111 sp<SurfaceControl> childLevel1 =
112 createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get());
113 sp<SurfaceControl> childLevel2a =
114 createColorLayer("child level 2a", Color::BLUE, childLevel1.get());
115 sp<SurfaceControl> childLevel2b =
116 createColorLayer("child level 2b", Color::BLACK, childLevel1.get());
117 sp<SurfaceControl> childLevel3 =
118 createColorLayer("child level 3", Color::GREEN, childLevel2a.get());
119
120 Transaction{}
121 .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1)
122 .show(childLevel2a)
123 .show(childLevel2b)
124 .show(childLevel3)
125 .apply();
126
127 {
128 // The childLevel3 should be in front of childLevel2b.
129 ScreenCapture::captureScreen(&sc);
130 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
131 }
132
133 // Background layer (RED)
134 // Foregroud layer (GREEN)
135 Transaction{}.reparent(childLevel1, nullptr).apply();
136
137 // Background layer (RED)
138 // Foregroud layer (GREEN)
139 // child level 1 (WHITE)
140 // child level 2 back (BLUE)
141 // child level 3 (GREEN) (relative to child level 2b)
142 // child level 2 front (BLACK)
143 Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply();
144
145 {
146 // Nothing should change at this point since relative z info was preserved.
147 ScreenCapture::captureScreen(&sc);
148 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
149 }
150}
chaviwbdb8b802019-10-14 09:17:12 -0700151
152TEST_F(RelativeZTest, LayerAndRelativeRemoved) {
153 std::unique_ptr<ScreenCapture> sc;
154
155 // Background layer (RED)
156 // Foregroud layer (GREEN)
157 // Child layer (BLUE) (relative to relativeToLayer layer)
158 // Relative layer (WHITE)
159 sp<SurfaceControl> childLayer =
160 createColorLayer("Child layer", Color::BLUE, mForegroundLayer.get());
161 sp<SurfaceControl> relativeToLayer =
162 createColorLayer("Relative layer", Color::WHITE, mForegroundLayer.get());
163
164 Transaction{}
165 .setRelativeLayer(childLayer, relativeToLayer->getHandle(), 1)
166 .show(childLayer)
167 .show(relativeToLayer)
168 .apply();
169
170 {
171 // The childLayer should be in front of relativeToLayer.
172 ScreenCapture::captureScreen(&sc);
173 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
174 }
175
176 // Remove layer that childLayer is relative to
177 // Background layer (RED)
178 // Foregroud layer (GREEN)
179 // Child layer (BLUE) (relative to relativeToLayer layer)
180 Transaction{}.reparent(relativeToLayer, nullptr).apply();
181 relativeToLayer = 0;
182
183 {
184 // The child layer is relative to an deleted layer so it won't be drawn.
185 ScreenCapture::captureScreen(&sc);
186 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
187 }
188
189 // Background layer (RED)
190 // Foregroud layer (GREEN)
191 Transaction{}.reparent(childLayer, nullptr).apply();
192
193 {
194 // The child layer is offscreen, so it won't be drawn.
195 ScreenCapture::captureScreen(&sc);
196 sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b);
197 }
198
199 // Background layer (RED)
200 // Foregroud layer (GREEN)
201 // Child layer (BLUE)
202 Transaction{}.reparent(childLayer, mForegroundLayer->getHandle()).apply();
203
204 {
205 // The relative z info for child layer should be reset, leaving the child layer on top.
206 ScreenCapture::captureScreen(&sc);
207 sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b);
208 }
209}
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700210} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800211
212// TODO(b/129481165): remove the #pragma below and fix conversion issues
213#pragma clang diagnostic pop // ignored "-Wconversion"