blob: 7fce7e9af98cc5f71d534ca88de4d12116dbad78 [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>
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080022#include <ui/DisplayState.h>
23
Valerie Hau9cfc6d82019-09-23 13:54:07 -070024#include "LayerTransactionTest.h"
Melody Hsu793f8362024-01-08 20:00:35 +000025#include "gui/SurfaceComposerClient.h"
26#include "ui/DisplayId.h"
Valerie Hau9cfc6d82019-09-23 13:54:07 -070027
28namespace android {
29
30using android::hardware::graphics::common::V1_1::BufferUsage;
31
32::testing::Environment* const binderEnv =
33 ::testing::AddGlobalTestEnvironment(new BinderEnvironment());
34
35class MultiDisplayLayerBoundsTest : public LayerTransactionTest {
36protected:
37 virtual void SetUp() {
38 LayerTransactionTest::SetUp();
39 ASSERT_EQ(NO_ERROR, mClient->initCheck());
40
Huihong Luo31b5ac22022-08-15 20:38:10 -070041 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
42 ASSERT_FALSE(ids.empty());
Melody Hsu793f8362024-01-08 20:00:35 +000043 mMainDisplayId = ids.front();
44 mMainDisplay = SurfaceComposerClient::getPhysicalDisplayToken(mMainDisplayId);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080045 SurfaceComposerClient::getDisplayState(mMainDisplay, &mMainDisplayState);
Marin Shalamanova7fe3042021-01-29 21:02:08 +010046 SurfaceComposerClient::getActiveDisplayMode(mMainDisplay, &mMainDisplayMode);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070047
48 sp<IGraphicBufferConsumer> consumer;
49 BufferQueue::createBufferQueue(&mProducer, &consumer);
50 consumer->setConsumerName(String8("Virtual disp consumer"));
Marin Shalamanova7fe3042021-01-29 21:02:08 +010051 consumer->setDefaultBufferSize(mMainDisplayMode.resolution.getWidth(),
52 mMainDisplayMode.resolution.getHeight());
Valerie Hau9cfc6d82019-09-23 13:54:07 -070053 }
54
55 virtual void TearDown() {
56 SurfaceComposerClient::destroyDisplay(mVirtualDisplay);
57 LayerTransactionTest::TearDown();
58 mColorLayer = 0;
59 }
60
Dominik Laskowski29fa1462021-04-27 15:51:50 -070061 void createDisplay(const ui::Size& layerStackSize, ui::LayerStack layerStack) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -070062 mVirtualDisplay =
63 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/);
64 asTransaction([&](Transaction& t) {
65 t.setDisplaySurface(mVirtualDisplay, mProducer);
66 t.setDisplayLayerStack(mVirtualDisplay, layerStack);
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080067 t.setDisplayProjection(mVirtualDisplay, mMainDisplayState.orientation,
Marin Shalamanova7fe3042021-01-29 21:02:08 +010068 Rect(layerStackSize), Rect(mMainDisplayMode.resolution));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070069 });
70 }
71
Dominik Laskowski29fa1462021-04-27 15:51:50 -070072 void createColorLayer(ui::LayerStack layerStack) {
Valerie Hau9cfc6d82019-09-23 13:54:07 -070073 mColorLayer =
74 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */,
Vishnu Nairfa247b12020-02-11 08:58:26 -080075 PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect);
Valerie Hau9cfc6d82019-09-23 13:54:07 -070076 ASSERT_TRUE(mColorLayer != nullptr);
77 ASSERT_TRUE(mColorLayer->isValid());
78 asTransaction([&](Transaction& t) {
79 t.setLayerStack(mColorLayer, layerStack);
chaviw25714502021-02-11 10:01:08 -080080 t.setCrop(mColorLayer, Rect(0, 0, 30, 40));
Valerie Hau9cfc6d82019-09-23 13:54:07 -070081 t.setLayer(mColorLayer, INT32_MAX - 2);
82 t.setColor(mColorLayer,
83 half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f,
84 mExpectedColor.b / 255.0f});
85 t.show(mColorLayer);
86 });
87 }
88
Dominik Laskowski3cb3d4e2019-11-21 11:14:45 -080089 ui::DisplayState mMainDisplayState;
Marin Shalamanova7fe3042021-01-29 21:02:08 +010090 ui::DisplayMode mMainDisplayMode;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070091 sp<IBinder> mMainDisplay;
Melody Hsu793f8362024-01-08 20:00:35 +000092 PhysicalDisplayId mMainDisplayId;
Valerie Hau9cfc6d82019-09-23 13:54:07 -070093 sp<IBinder> mVirtualDisplay;
94 sp<IGraphicBufferProducer> mProducer;
95 sp<SurfaceControl> mColorLayer;
96 Color mExpectedColor = {63, 63, 195, 255};
97};
98
99TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) {
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700100 constexpr ui::LayerStack kLayerStack{1u};
101 createDisplay(mMainDisplayState.layerStackSpaceRect, kLayerStack);
102 createColorLayer(kLayerStack);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700103
104 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
105
106 // Verify color layer does not render on main display.
107 std::unique_ptr<ScreenCapture> sc;
108 ScreenCapture::captureScreen(&sc, mMainDisplay);
109 sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255});
110 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
111
112 // Verify color layer renders correctly on virtual display.
113 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
114 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
Alec Mouri5a6d8572020-03-23 23:56:15 -0700115 sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 255});
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700116}
117
118TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) {
119 // Create a display and set its layer stack to the main display's layer stack so
120 // the contents of the main display are mirrored on to the virtual display.
121
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200122 // Assumption here is that the new mirrored display has the same layer stack rect as the
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700123 // primary display that it is mirroring.
Dominik Laskowski29fa1462021-04-27 15:51:50 -0700124 createDisplay(mMainDisplayState.layerStackSpaceRect, ui::DEFAULT_LAYER_STACK);
125 createColorLayer(ui::DEFAULT_LAYER_STACK);
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700126
Melody Hsu793f8362024-01-08 20:00:35 +0000127 sp<SurfaceControl> mirrorSc =
128 SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId);
129
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700130 asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); });
131
132 // Verify color layer renders correctly on main display and it is mirrored on the
133 // virtual display.
134 std::unique_ptr<ScreenCapture> sc;
135 ScreenCapture::captureScreen(&sc, mMainDisplay);
136 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
137 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
138
139 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
140 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
141 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
142}
143
Melody Hsu793f8362024-01-08 20:00:35 +0000144TEST_F(MultiDisplayLayerBoundsTest, RenderLayerWithPromisedFenceInMirroredVirtualDisplay) {
145 // Create a display and use a unique layerstack ID for mirrorDisplay() so
146 // the contents of the main display are mirrored on to the virtual display.
147
148 // A unique layerstack ID must be used because sharing the same layerFE
149 // with more than one display is unsupported. A unique layerstack ensures
150 // that a different layerFE is used between displays.
151 constexpr ui::LayerStack layerStack{77687666}; // ASCII for MDLB (MultiDisplayLayerBounds)
152 createDisplay(mMainDisplayState.layerStackSpaceRect, layerStack);
153 createColorLayer(ui::DEFAULT_LAYER_STACK);
154
155 sp<SurfaceControl> mirrorSc =
156 SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId);
157
158 asTransaction([&](Transaction& t) {
159 t.setPosition(mColorLayer, 10, 10);
160 t.setLayerStack(mirrorSc, layerStack);
161 });
162
163 // Verify color layer renders correctly on main display and it is mirrored on the
164 // virtual display.
165 std::unique_ptr<ScreenCapture> sc;
166 ScreenCapture::captureScreen(&sc, mMainDisplay);
167 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
168 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
169
170 ScreenCapture::captureScreen(&sc, mVirtualDisplay);
171 sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor);
172 sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255});
173}
174
Valerie Hau9cfc6d82019-09-23 13:54:07 -0700175} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800176
177// TODO(b/129481165): remove the #pragma below and fix conversion issues
178#pragma clang diagnostic pop // ignored "-Wconversion"