Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 21 | #include <common/FlagManager.h> |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 22 | #include <ui/DisplayState.h> |
| 23 | |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 24 | #include "LayerTransactionTest.h" |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 25 | #include "gui/SurfaceComposerClient.h" |
| 26 | #include "ui/DisplayId.h" |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | using android::hardware::graphics::common::V1_1::BufferUsage; |
| 31 | |
| 32 | ::testing::Environment* const binderEnv = |
| 33 | ::testing::AddGlobalTestEnvironment(new BinderEnvironment()); |
| 34 | |
| 35 | class MultiDisplayLayerBoundsTest : public LayerTransactionTest { |
| 36 | protected: |
| 37 | virtual void SetUp() { |
| 38 | LayerTransactionTest::SetUp(); |
| 39 | ASSERT_EQ(NO_ERROR, mClient->initCheck()); |
| 40 | |
Huihong Luo | 31b5ac2 | 2022-08-15 20:38:10 -0700 | [diff] [blame] | 41 | const auto ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 42 | ASSERT_FALSE(ids.empty()); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 43 | mMainDisplayId = ids.front(); |
| 44 | mMainDisplay = SurfaceComposerClient::getPhysicalDisplayToken(mMainDisplayId); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 45 | SurfaceComposerClient::getDisplayState(mMainDisplay, &mMainDisplayState); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 46 | SurfaceComposerClient::getActiveDisplayMode(mMainDisplay, &mMainDisplayMode); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 47 | |
| 48 | sp<IGraphicBufferConsumer> consumer; |
| 49 | BufferQueue::createBufferQueue(&mProducer, &consumer); |
| 50 | consumer->setConsumerName(String8("Virtual disp consumer")); |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 51 | consumer->setDefaultBufferSize(mMainDisplayMode.resolution.getWidth(), |
| 52 | mMainDisplayMode.resolution.getHeight()); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual void TearDown() { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame^] | 56 | EXPECT_EQ(NO_ERROR, SurfaceComposerClient::destroyVirtualDisplay(mVirtualDisplay)); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 57 | LayerTransactionTest::TearDown(); |
| 58 | mColorLayer = 0; |
| 59 | } |
| 60 | |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 61 | void createDisplay(const ui::Size& layerStackSize, ui::LayerStack layerStack) { |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame^] | 62 | static const std::string kDisplayName("VirtualDisplay"); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 63 | mVirtualDisplay = |
Alan Ding | d53801c | 2024-05-08 16:45:29 -0700 | [diff] [blame^] | 64 | SurfaceComposerClient::createVirtualDisplay(kDisplayName, false /*isSecure*/); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 65 | asTransaction([&](Transaction& t) { |
| 66 | t.setDisplaySurface(mVirtualDisplay, mProducer); |
| 67 | t.setDisplayLayerStack(mVirtualDisplay, layerStack); |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 68 | t.setDisplayProjection(mVirtualDisplay, mMainDisplayState.orientation, |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 69 | Rect(layerStackSize), Rect(mMainDisplayMode.resolution)); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 70 | }); |
| 71 | } |
| 72 | |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 73 | void createColorLayer(ui::LayerStack layerStack) { |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 74 | mColorLayer = |
| 75 | createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */, |
Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 76 | PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(mColorLayer != nullptr); |
| 78 | ASSERT_TRUE(mColorLayer->isValid()); |
| 79 | asTransaction([&](Transaction& t) { |
| 80 | t.setLayerStack(mColorLayer, layerStack); |
chaviw | 2571450 | 2021-02-11 10:01:08 -0800 | [diff] [blame] | 81 | t.setCrop(mColorLayer, Rect(0, 0, 30, 40)); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 82 | t.setLayer(mColorLayer, INT32_MAX - 2); |
| 83 | t.setColor(mColorLayer, |
| 84 | half3{mExpectedColor.r / 255.0f, mExpectedColor.g / 255.0f, |
| 85 | mExpectedColor.b / 255.0f}); |
| 86 | t.show(mColorLayer); |
| 87 | }); |
| 88 | } |
| 89 | |
Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 90 | ui::DisplayState mMainDisplayState; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 91 | ui::DisplayMode mMainDisplayMode; |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 92 | sp<IBinder> mMainDisplay; |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 93 | PhysicalDisplayId mMainDisplayId; |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 94 | sp<IBinder> mVirtualDisplay; |
| 95 | sp<IGraphicBufferProducer> mProducer; |
| 96 | sp<SurfaceControl> mColorLayer; |
| 97 | Color mExpectedColor = {63, 63, 195, 255}; |
| 98 | }; |
| 99 | |
| 100 | TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) { |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 101 | constexpr ui::LayerStack kLayerStack{1u}; |
| 102 | createDisplay(mMainDisplayState.layerStackSpaceRect, kLayerStack); |
| 103 | createColorLayer(kLayerStack); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 104 | |
| 105 | asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); }); |
| 106 | |
| 107 | // Verify color layer does not render on main display. |
| 108 | std::unique_ptr<ScreenCapture> sc; |
| 109 | ScreenCapture::captureScreen(&sc, mMainDisplay); |
| 110 | sc->expectColor(Rect(10, 10, 40, 50), {0, 0, 0, 255}); |
| 111 | sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255}); |
| 112 | |
| 113 | // Verify color layer renders correctly on virtual display. |
| 114 | ScreenCapture::captureScreen(&sc, mVirtualDisplay); |
| 115 | sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor); |
Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 116 | sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 255}); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInMirroredVirtualDisplay) { |
| 120 | // Create a display and set its layer stack to the main display's layer stack so |
| 121 | // the contents of the main display are mirrored on to the virtual display. |
| 122 | |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 123 | // Assumption here is that the new mirrored display has the same layer stack rect as the |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 124 | // primary display that it is mirroring. |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 125 | createDisplay(mMainDisplayState.layerStackSpaceRect, ui::DEFAULT_LAYER_STACK); |
| 126 | createColorLayer(ui::DEFAULT_LAYER_STACK); |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 127 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 128 | sp<SurfaceControl> mirrorSc = |
| 129 | SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId); |
| 130 | |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 131 | asTransaction([&](Transaction& t) { t.setPosition(mColorLayer, 10, 10); }); |
| 132 | |
| 133 | // Verify color layer renders correctly on main display and it is mirrored on the |
| 134 | // virtual display. |
| 135 | std::unique_ptr<ScreenCapture> sc; |
| 136 | ScreenCapture::captureScreen(&sc, mMainDisplay); |
| 137 | sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor); |
| 138 | sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255}); |
| 139 | |
| 140 | ScreenCapture::captureScreen(&sc, mVirtualDisplay); |
| 141 | sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor); |
| 142 | sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255}); |
| 143 | } |
| 144 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 145 | TEST_F(MultiDisplayLayerBoundsTest, RenderLayerWithPromisedFenceInMirroredVirtualDisplay) { |
| 146 | // Create a display and use a unique layerstack ID for mirrorDisplay() so |
| 147 | // the contents of the main display are mirrored on to the virtual display. |
| 148 | |
| 149 | // A unique layerstack ID must be used because sharing the same layerFE |
| 150 | // with more than one display is unsupported. A unique layerstack ensures |
| 151 | // that a different layerFE is used between displays. |
| 152 | constexpr ui::LayerStack layerStack{77687666}; // ASCII for MDLB (MultiDisplayLayerBounds) |
| 153 | createDisplay(mMainDisplayState.layerStackSpaceRect, layerStack); |
| 154 | createColorLayer(ui::DEFAULT_LAYER_STACK); |
| 155 | |
| 156 | sp<SurfaceControl> mirrorSc = |
| 157 | SurfaceComposerClient::getDefault()->mirrorDisplay(mMainDisplayId); |
| 158 | |
| 159 | asTransaction([&](Transaction& t) { |
| 160 | t.setPosition(mColorLayer, 10, 10); |
| 161 | t.setLayerStack(mirrorSc, layerStack); |
| 162 | }); |
| 163 | |
| 164 | // Verify color layer renders correctly on main display and it is mirrored on the |
| 165 | // virtual display. |
| 166 | std::unique_ptr<ScreenCapture> sc; |
| 167 | ScreenCapture::captureScreen(&sc, mMainDisplay); |
| 168 | sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor); |
| 169 | sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255}); |
| 170 | |
| 171 | ScreenCapture::captureScreen(&sc, mVirtualDisplay); |
| 172 | sc->expectColor(Rect(10, 10, 40, 50), mExpectedColor); |
| 173 | sc->expectColor(Rect(0, 0, 9, 9), {0, 0, 0, 255}); |
| 174 | } |
| 175 | |
Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 176 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 177 | |
| 178 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 179 | #pragma clang diagnostic pop // ignored "-Wconversion" |