blob: 65add6316553567e8372f861bc9f5fc1b592ab14 [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
Melody Hsu793f8362024-01-08 20:00:35 +000021#include <common/FlagManager.h>
Jim Shargof48bef82024-12-04 18:56:59 +000022#include <gui/IConsumerListener.h>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080023#include <ui/DisplayState.h>
24
Valerie Hau9cfc6d82019-09-23 13:54:07 -070025#include "LayerTransactionTest.h"
Melody Hsu793f8362024-01-08 20:00:35 +000026#include "gui/SurfaceComposerClient.h"
27#include "ui/DisplayId.h"
Valerie Hau9cfc6d82019-09-23 13:54:07 -070028
29namespace android {
30
31using android::hardware::graphics::common::V1_1::BufferUsage;
32
33::testing::Environment* const binderEnv =
34 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
35
36class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
37protected:
38 virtual void SetUp() {
39 LayerTransactionTest::SetUp();
40 ASSERT_EQ(NO_ERROR, mClient->initCheck());
41
Huihong Luo31b5ac22022-08-15 20:38:10 -070042 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
43 ASSERT_FALSE(ids.empty());
Melody Hsu793f8362024-01-08 20:00:35 +000044 mMainDisplayId = ids.front();
45 mMainDisplay = SurfaceComposerClient::getPhysicalDisplayToken(mMainDisplayId);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080046 SurfaceComposerClient::getDisplayState(mMainDisplay, &mMainDisplayState);
Marin Shalamanova7fe3042021-01-29 21:02:08 +010047 SurfaceComposerClient::getActiveDisplayMode(mMainDisplay, &mMainDisplayMode);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070048
Jim Shargof48bef82024-12-04 18:56:59 +000049 BufferQueue::createBufferQueue(&mProducer, &mConsumer);
50 mConsumer->setConsumerName(String8("Virtual disp consumer (MultiDisplayLayerBounds)"));
51 mConsumer->setDefaultBufferSize(mMainDisplayMode.resolution.getWidth(),
52 mMainDisplayMode.resolution.getHeight());
53
54 class StubConsumerListener : public BnConsumerListener {
55 virtual void onFrameAvailable(const BufferItem&) override {}
56 virtual void onBuffersReleased() override {}
57 virtual void onSidebandStreamChanged() override {}
58 };
59 mConsumer->consumerConnect(sp<StubConsumerListener>::make(), true);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070060 }
61
62 virtual void TearDown() {
Alan Dingd53801c2024-05-08 16:45:29 -070063 EXPECT_EQ(NO_ERROR, SurfaceComposerClient::destroyVirtualDisplay(mVirtualDisplay));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070064 LayerTransactionTest::TearDown();
65 mColorLayer = 0;
66 }
67
Dominik Laskowski29fa1462021-04-27 15:51:50 -070068 void createDisplay(const ui::Size& layerStackSize, ui::LayerStack layerStack) {
Alan Dingd53801c2024-05-08 16:45:29 -070069 static const std::string kDisplayName("VirtualDisplay");
Valerie Hau9cfc6d82019-09-23 13:54:07 -070070 mVirtualDisplay =
Alan Dingd53801c2024-05-08 16:45:29 -070071 SurfaceComposerClient::createVirtualDisplay(kDisplayName, false /*isSecure*/);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070072 asTransaction([&](Transaction& t) {
73 t.setDisplaySurface(mVirtualDisplay, mProducer);
74 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080075 t.setDisplayProjection(mVirtualDisplay, mMainDisplayState.orientation,
Marin Shalamanova7fe3042021-01-29 21:02:08 +010076 Rect(layerStackSize), Rect(mMainDisplayMode.resolution));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070077 });
78 }
79
Dominik Laskowski29fa1462021-04-27 15:51:50 -070080 void createColorLayer(ui::LayerStack layerStack) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -070081 mColorLayer =
82 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080083 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070084 ASSERT_TRUE(mColorLayer != nullptr);
85 ASSERT_TRUE(mColorLayer->isValid());
86 asTransaction([&](Transaction& t) {
87 t.setLayerStack(mColorLayer, layerStack);
chaviw25714502021-02-11 10:01:08 -080088 t.setCrop(mColorLayer, Rect(0, 0, 30, 40));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070089 t.setLayer(mColorLayer, INT32_MAX - 2);
90 t.setColor(mColorLayer,
91 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
92 mExpectedColor.b / 255.0f});
93 t.show(mColorLayer);
94 });
95 }
96
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080097 ui::DisplayState mMainDisplayState;
Marin Shalamanova7fe3042021-01-29 21:02:08 +010098 ui::DisplayMode mMainDisplayMode;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070099 sp<IBinder> mMainDisplay;
Melody Hsu793f8362024-01-08 20:00:35 +0000100 PhysicalDisplayId mMainDisplayId;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700101 sp<IBinder> mVirtualDisplay;
Jim Shargof48bef82024-12-04 18:56:59 +0000102 sp<IGraphicBufferConsumer> mConsumer;
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700103 sp<IGraphicBufferProducer> mProducer;
104 sp<SurfaceControl> mColorLayer;
105 Color mExpectedColor = {63, 63, 195, 255};
106};
107
108TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700109 constexpr ui::LayerStack kLayerStack{1u};
110 createDisplay(mMainDisplayState.layerStackSpaceRect, kLayerStack);
111 createColorLayer(kLayerStack);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700112
113 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
114
115 // Verify color layer does not render on main display.
116 std::unique_ptr<ScreenCapture> sc;
117 ScreenCapture::captureScreen(&sc, mMainDisplay);
118 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
119 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
120
121 // Verify color layer renders correctly on virtual display.
122 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
123 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700124 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 255});
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700125}
126
127TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
128 // Create a display and set its layer stack to the main display's layer stack so
129 // the contents of the main display are mirrored on to the virtual display.
130
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200131 // Assumption here is that the new mirrored display has the same layer stack rect as the
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700132 // primary display that it is mirroring.
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700133 createDisplay(mMainDisplayState.layerStackSpaceRect, ui::DEFAULT_LAYER_STACK);
134 createColorLayer(ui::DEFAULT_LAYER_STACK);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700135
Melody Hsu793f8362024-01-08 20:00:35 +0000136 sp<SurfaceControl> mirrorSc =
137 SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId);
138
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700139 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
140
141 // Verify color layer renders correctly on main display and it is mirrored on the
142 // virtual display.
143 std::unique_ptr<ScreenCapture> sc;
144 ScreenCapture::captureScreen(&sc, mMainDisplay);
145 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
146 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
147
148 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
149 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
150 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
151}
152
Melody Hsu793f8362024-01-08 20:00:35 +0000153TEST_F(MultiDisplayLayerBoundsTest, RenderLayerWithPromisedFenceInMirroredVirtualDisplay) {
154 // Create a display and use a unique layerstack ID for mirrorDisplay() so
155 // the contents of the main display are mirrored on to the virtual display.
156
157 // A unique layerstack ID must be used because sharing the same layerFE
158 // with more than one display is unsupported. A unique layerstack ensures
159 // that a different layerFE is used between displays.
160 constexpr ui::LayerStack layerStack{77687666}; // ASCII for MDLB (MultiDisplayLayerBounds)
161 createDisplay(mMainDisplayState.layerStackSpaceRect, layerStack);
162 createColorLayer(ui::DEFAULT_LAYER_STACK);
163
164 sp<SurfaceControl> mirrorSc =
165 SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId);
166
167 asTransaction([&](Transaction& t) {
168 t.setPosition(mColorLayer, 10, 10);
169 t.setLayerStack(mirrorSc, layerStack);
170 });
171
172 // Verify color layer renders correctly on main display and it is mirrored on the
173 // virtual display.
174 std::unique_ptr<ScreenCapture> sc;
175 ScreenCapture::captureScreen(&sc, mMainDisplay);
176 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
177 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
178
179 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
180 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
181 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
182}
183
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700184} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800185
186// TODO(b/129481165): remove the #pragma below and fix conversion issues
187#pragma clang diagnostic pop // ignored "-Wconversion"