• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #include <private/android_filesystem_config.h>
22 #include "LayerTransactionTest.h"
23 #include "utils/TransactionUtils.h"
24 
25 namespace android {
26 
27 class MirrorLayerTest : public LayerTransactionTest {
28 protected:
SetUp()29     virtual void SetUp() {
30         LayerTransactionTest::SetUp();
31         ASSERT_EQ(NO_ERROR, mClient->initCheck());
32 
33         const auto display = SurfaceComposerClient::getInternalDisplayToken();
34         ASSERT_FALSE(display == nullptr);
35 
36         mParentLayer = createColorLayer("Parent layer", Color::RED);
37         mChildLayer = createColorLayer("Child layer", Color::GREEN, mParentLayer.get());
38         asTransaction([&](Transaction& t) {
39             t.setDisplayLayerStack(display, ui::DEFAULT_LAYER_STACK);
40             t.setLayer(mParentLayer, INT32_MAX - 2).show(mParentLayer);
41             t.setCrop(mChildLayer, Rect(0, 0, 400, 400)).show(mChildLayer);
42             t.setPosition(mChildLayer, 50, 50);
43             t.setFlags(mParentLayer, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
44             t.setFlags(mChildLayer, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
45         });
46     }
47 
TearDown()48     virtual void TearDown() {
49         LayerTransactionTest::TearDown();
50         mParentLayer = 0;
51         mChildLayer = 0;
52     }
53 
54     sp<SurfaceControl> mParentLayer;
55     sp<SurfaceControl> mChildLayer;
56 };
57 
TEST_F(MirrorLayerTest,MirrorColorLayer)58 TEST_F(MirrorLayerTest, MirrorColorLayer) {
59     sp<SurfaceControl> grandchild =
60             createColorLayer("Grandchild layer", Color::BLUE, mChildLayer.get());
61     Transaction()
62             .setFlags(grandchild, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque)
63             .setCrop(grandchild, Rect(0, 0, 200, 200))
64             .show(grandchild)
65             .apply();
66 
67     // Mirror mChildLayer
68     sp<SurfaceControl> mirrorLayer = mClient->mirrorSurface(mChildLayer.get());
69     ASSERT_NE(mirrorLayer, nullptr);
70 
71     // Add mirrorLayer as child of mParentLayer so it's shown on the display
72     Transaction()
73             .reparent(mirrorLayer, mParentLayer)
74             .setPosition(mirrorLayer, 500, 500)
75             .show(mirrorLayer)
76             .apply();
77 
78     {
79         SCOPED_TRACE("Initial Mirror");
80         auto shot = screenshot();
81         // Grandchild mirror
82         shot->expectColor(Rect(550, 550, 750, 750), Color::BLUE);
83         // Child mirror
84         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
85     }
86 
87     // Set color to white on grandchild layer.
88     Transaction().setColor(grandchild, half3{1, 1, 1}).apply();
89     {
90         SCOPED_TRACE("Updated Grandchild Layer Color");
91         auto shot = screenshot();
92         // Grandchild mirror
93         shot->expectColor(Rect(550, 550, 750, 750), Color::WHITE);
94         // Child mirror
95         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
96     }
97 
98     // Set color to black on child layer.
99     Transaction().setColor(mChildLayer, half3{0, 0, 0}).apply();
100     {
101         SCOPED_TRACE("Updated Child Layer Color");
102         auto shot = screenshot();
103         // Grandchild mirror
104         shot->expectColor(Rect(550, 550, 750, 750), Color::WHITE);
105         // Child mirror
106         shot->expectColor(Rect(750, 750, 950, 950), Color::BLACK);
107     }
108 
109     // Remove grandchild layer
110     Transaction().reparent(grandchild, nullptr).apply();
111     {
112         SCOPED_TRACE("Removed Grandchild Layer");
113         auto shot = screenshot();
114         // Grandchild mirror
115         shot->expectColor(Rect(550, 550, 750, 750), Color::BLACK);
116         // Child mirror
117         shot->expectColor(Rect(750, 750, 950, 950), Color::BLACK);
118     }
119 
120     // Remove child layer
121     Transaction().reparent(mChildLayer, nullptr).apply();
122     {
123         SCOPED_TRACE("Removed Child Layer");
124         auto shot = screenshot();
125         // Grandchild mirror
126         shot->expectColor(Rect(550, 550, 750, 750), Color::RED);
127         // Child mirror
128         shot->expectColor(Rect(750, 750, 950, 950), Color::RED);
129     }
130 
131     // Add grandchild layer to offscreen layer
132     Transaction().reparent(grandchild, mChildLayer).apply();
133     {
134         SCOPED_TRACE("Added Grandchild Layer");
135         auto shot = screenshot();
136         // Grandchild mirror
137         shot->expectColor(Rect(550, 550, 750, 750), Color::RED);
138         // Child mirror
139         shot->expectColor(Rect(750, 750, 950, 950), Color::RED);
140     }
141 
142     // Add child layer
143     Transaction().reparent(mChildLayer, mParentLayer).apply();
144     {
145         SCOPED_TRACE("Added Child Layer");
146         auto shot = screenshot();
147         // Grandchild mirror
148         shot->expectColor(Rect(550, 550, 750, 750), Color::WHITE);
149         // Child mirror
150         shot->expectColor(Rect(750, 750, 950, 950), Color::BLACK);
151     }
152 }
153 
TEST_F(MirrorLayerTest,MirrorBufferLayer)154 TEST_F(MirrorLayerTest, MirrorBufferLayer) {
155     sp<SurfaceControl> bufferQueueLayer =
156             createLayer("BufferQueueLayer", 200, 200, 0, mChildLayer.get());
157     fillBufferQueueLayerColor(bufferQueueLayer, Color::BLUE, 200, 200);
158     Transaction().show(bufferQueueLayer).apply();
159 
160     sp<SurfaceControl> mirrorLayer = mClient->mirrorSurface(mChildLayer.get());
161     Transaction()
162             .reparent(mirrorLayer, mParentLayer)
163             .setPosition(mirrorLayer, 500, 500)
164             .show(mirrorLayer)
165             .apply();
166 
167     {
168         SCOPED_TRACE("Initial Mirror BufferQueueLayer");
169         auto shot = screenshot();
170         // Buffer mirror
171         shot->expectColor(Rect(550, 550, 750, 750), Color::BLUE);
172         // Child mirror
173         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
174     }
175 
176     fillBufferQueueLayerColor(bufferQueueLayer, Color::WHITE, 200, 200);
177     {
178         SCOPED_TRACE("Update BufferQueueLayer");
179         auto shot = screenshot();
180         // Buffer mirror
181         shot->expectColor(Rect(550, 550, 750, 750), Color::WHITE);
182         // Child mirror
183         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
184     }
185 
186     Transaction().reparent(bufferQueueLayer, nullptr).apply();
187     {
188         SCOPED_TRACE("Removed BufferQueueLayer");
189         auto shot = screenshot();
190         // Buffer mirror
191         shot->expectColor(Rect(550, 550, 750, 750), Color::GREEN);
192         // Child mirror
193         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
194     }
195 
196     sp<SurfaceControl> bufferStateLayer =
197             createLayer("BufferStateLayer", 200, 200, ISurfaceComposerClient::eFXSurfaceBufferState,
198                         mChildLayer.get());
199     fillBufferStateLayerColor(bufferStateLayer, Color::BLUE, 200, 200);
200     Transaction().show(bufferStateLayer).apply();
201 
202     {
203         SCOPED_TRACE("Initial Mirror BufferStateLayer");
204         auto shot = screenshot();
205         // Buffer mirror
206         shot->expectColor(Rect(550, 550, 750, 750), Color::BLUE);
207         // Child mirror
208         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
209     }
210 
211     fillBufferStateLayerColor(bufferStateLayer, Color::WHITE, 200, 200);
212     {
213         SCOPED_TRACE("Update BufferStateLayer");
214         auto shot = screenshot();
215         // Buffer mirror
216         shot->expectColor(Rect(550, 550, 750, 750), Color::WHITE);
217         // Child mirror
218         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
219     }
220 
221     Transaction().reparent(bufferStateLayer, nullptr).apply();
222     {
223         SCOPED_TRACE("Removed BufferStateLayer");
224         auto shot = screenshot();
225         // Buffer mirror
226         shot->expectColor(Rect(550, 550, 750, 750), Color::GREEN);
227         // Child mirror
228         shot->expectColor(Rect(750, 750, 950, 950), Color::GREEN);
229     }
230 }
231 
232 // Test that the mirror layer is initially offscreen.
TEST_F(MirrorLayerTest,InitialMirrorState)233 TEST_F(MirrorLayerTest, InitialMirrorState) {
234     const auto display = SurfaceComposerClient::getInternalDisplayToken();
235     ui::DisplayMode mode;
236     SurfaceComposerClient::getActiveDisplayMode(display, &mode);
237     const ui::Size& size = mode.resolution;
238 
239     sp<SurfaceControl> mirrorLayer = nullptr;
240     {
241         // Run as system to get the ACCESS_SURFACE_FLINGER permission when mirroring
242         UIDFaker f(AID_SYSTEM);
243         // Mirror mChildLayer
244         mirrorLayer = mClient->mirrorSurface(mChildLayer.get());
245         ASSERT_NE(mirrorLayer, nullptr);
246     }
247 
248     // Show the mirror layer, but don't reparent to a layer on screen.
249     Transaction()
250             .setPosition(mirrorLayer, 500, 500)
251             .show(mirrorLayer)
252             .setLayer(mirrorLayer, INT32_MAX - 1)
253             .apply();
254 
255     {
256         SCOPED_TRACE("Offscreen Mirror");
257         auto shot = screenshot();
258         shot->expectColor(Rect(0, 0, size.getWidth(), 50), Color::RED);
259         shot->expectColor(Rect(0, 0, 50, size.getHeight()), Color::RED);
260         shot->expectColor(Rect(450, 0, size.getWidth(), size.getHeight()), Color::RED);
261         shot->expectColor(Rect(0, 450, size.getWidth(), size.getHeight()), Color::RED);
262         shot->expectColor(Rect(50, 50, 450, 450), Color::GREEN);
263     }
264 
265     // Add mirrorLayer as child of mParentLayer so it's shown on the display
266     Transaction().reparent(mirrorLayer, mParentLayer).apply();
267 
268     {
269         SCOPED_TRACE("On Screen Mirror");
270         auto shot = screenshot();
271         // Child mirror
272         shot->expectColor(Rect(550, 550, 950, 950), Color::GREEN);
273     }
274 }
275 
276 // Test that a mirror layer can be screenshot when offscreen
TEST_F(MirrorLayerTest,OffscreenMirrorScreenshot)277 TEST_F(MirrorLayerTest, OffscreenMirrorScreenshot) {
278     const auto display = SurfaceComposerClient::getInternalDisplayToken();
279     ui::DisplayMode mode;
280     SurfaceComposerClient::getActiveDisplayMode(display, &mode);
281     const ui::Size& size = mode.resolution;
282 
283     sp<SurfaceControl> grandchild =
284             createLayer("Grandchild layer", 50, 50, ISurfaceComposerClient::eFXSurfaceBufferState,
285                         mChildLayer.get());
286     ASSERT_NO_FATAL_FAILURE(fillBufferStateLayerColor(grandchild, Color::BLUE, 50, 50));
287     Rect childBounds = Rect(50, 50, 450, 450);
288 
289     asTransaction([&](Transaction& t) {
290         t.setCrop(grandchild, Rect(0, 0, 50, 50)).show(grandchild);
291         t.setFlags(grandchild, layer_state_t::eLayerOpaque, layer_state_t::eLayerOpaque);
292     });
293 
294     sp<SurfaceControl> mirrorLayer = nullptr;
295     {
296         // Run as system to get the ACCESS_SURFACE_FLINGER permission when mirroring
297         UIDFaker f(AID_SYSTEM);
298         // Mirror mChildLayer
299         mirrorLayer = mClient->mirrorSurface(mChildLayer.get());
300         ASSERT_NE(mirrorLayer, nullptr);
301     }
302 
303     // Show the mirror layer, but don't reparent to a layer on screen.
304     Transaction().show(mirrorLayer).apply();
305 
306     {
307         SCOPED_TRACE("Offscreen Mirror");
308         auto shot = screenshot();
309         shot->expectColor(Rect(0, 0, size.getWidth(), 50), Color::RED);
310         shot->expectColor(Rect(0, 0, 50, size.getHeight()), Color::RED);
311         shot->expectColor(Rect(450, 0, size.getWidth(), size.getHeight()), Color::RED);
312         shot->expectColor(Rect(0, 450, size.getWidth(), size.getHeight()), Color::RED);
313         shot->expectColor(Rect(100, 100, 450, 450), Color::GREEN);
314         shot->expectColor(Rect(50, 50, 100, 100), Color::BLUE);
315     }
316 
317     {
318         SCOPED_TRACE("Capture Mirror");
319         // Capture just the mirror layer and child.
320         LayerCaptureArgs captureArgs;
321         captureArgs.layerHandle = mirrorLayer->getHandle();
322         captureArgs.sourceCrop = childBounds;
323         std::unique_ptr<ScreenCapture> shot;
324         ScreenCapture::captureLayers(&shot, captureArgs);
325         shot->expectSize(childBounds.width(), childBounds.height());
326         shot->expectColor(Rect(0, 0, 50, 50), Color::BLUE);
327         shot->expectColor(Rect(50, 50, 400, 400), Color::GREEN);
328     }
329 }
330 
331 } // namespace android
332 
333 // TODO(b/129481165): remove the #pragma below and fix conversion issues
334 #pragma clang diagnostic pop // ignored "-Wconversion"
335