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 <ui/DisplayState.h>
22
23 #include "LayerTransactionTest.h"
24
25 namespace android {
26
27 using android::hardware::graphics::common::V1_1::BufferUsage;
28
29 ::testing::Environment* const binderEnv =
30 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
31
32 class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
33 protected:
SetUp()34 virtual void SetUp() {
35 LayerTransactionTest::SetUp();
36 ASSERT_EQ(NO_ERROR, mClient->initCheck());
37
38 mMainDisplay = SurfaceComposerClient::getInternalDisplayToken();
39 SurfaceComposerClient::getDisplayState(mMainDisplay, &mMainDisplayState);
40 SurfaceComposerClient::getActiveDisplayMode(mMainDisplay, &mMainDisplayMode);
41
42 sp<IGraphicBufferConsumer> consumer;
43 BufferQueue::createBufferQueue(&mProducer, &consumer);
44 consumer->setConsumerName(String8("Virtual disp consumer"));
45 consumer->setDefaultBufferSize(mMainDisplayMode.resolution.getWidth(),
46 mMainDisplayMode.resolution.getHeight());
47 }
48
TearDown()49 virtual void TearDown() {
50 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
51 LayerTransactionTest::TearDown();
52 mColorLayer = 0;
53 }
54
createDisplay(const ui::Size & layerStackSize,uint32_t layerStack)55 void createDisplay(const ui::Size& layerStackSize, uint32_t layerStack) {
56 mVirtualDisplay =
57 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
58 asTransaction([&](Transaction& t) {
59 t.setDisplaySurface(mVirtualDisplay, mProducer);
60 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
61 t.setDisplayProjection(mVirtualDisplay, mMainDisplayState.orientation,
62 Rect(layerStackSize), Rect(mMainDisplayMode.resolution));
63 });
64 }
65
createColorLayer(uint32_t layerStack)66 void createColorLayer(uint32_t layerStack) {
67 mColorLayer =
68 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
69 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect);
70 ASSERT_TRUE(mColorLayer != nullptr);
71 ASSERT_TRUE(mColorLayer->isValid());
72 asTransaction([&](Transaction& t) {
73 t.setLayerStack(mColorLayer, layerStack);
74 t.setCrop(mColorLayer, Rect(0, 0, 30, 40));
75 t.setLayer(mColorLayer, INT32_MAX - 2);
76 t.setColor(mColorLayer,
77 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
78 mExpectedColor.b / 255.0f});
79 t.show(mColorLayer);
80 });
81 }
82
83 ui::DisplayState mMainDisplayState;
84 ui::DisplayMode mMainDisplayMode;
85 sp<IBinder> mMainDisplay;
86 sp<IBinder> mVirtualDisplay;
87 sp<IGraphicBufferProducer> mProducer;
88 sp<SurfaceControl> mColorLayer;
89 Color mExpectedColor = {63, 63, 195, 255};
90 };
91
TEST_F(MultiDisplayLayerBoundsTest,RenderLayerInVirtualDisplay)92 TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
93 createDisplay(mMainDisplayState.layerStackSpaceRect, 1 /* layerStack */);
94 createColorLayer(1 /* layerStack */);
95
96 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
97
98 // Verify color layer does not render on main display.
99 std::unique_ptr<ScreenCapture> sc;
100 ScreenCapture::captureScreen(&sc, mMainDisplay);
101 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
102 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
103
104 // Verify color layer renders correctly on virtual display.
105 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
106 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
107 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 255});
108 }
109
TEST_F(MultiDisplayLayerBoundsTest,RenderLayerInMirroredVirtualDisplay)110 TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
111 // Create a display and set its layer stack to the main display's layer stack so
112 // the contents of the main display are mirrored on to the virtual display.
113
114 // Assumption here is that the new mirrored display has the same layer stack rect as the
115 // primary display that it is mirroring.
116 createDisplay(mMainDisplayState.layerStackSpaceRect, 0 /* layerStack */);
117 createColorLayer(0 /* layerStack */);
118
119 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
120
121 // Verify color layer renders correctly on main display and it is mirrored on the
122 // virtual display.
123 std::unique_ptr<ScreenCapture> sc;
124 ScreenCapture::captureScreen(&sc, mMainDisplay);
125 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
126 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
127
128 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
129 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
130 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
131 }
132
133 } // namespace android
134
135 // TODO(b/129481165): remove the #pragma below and fix conversion issues
136 #pragma clang diagnostic pop // ignored "-Wconversion"
137