| 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 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 21 | #include <ui/DisplayState.h> | 
 | 22 |  | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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: | 
 | 34 |     virtual void SetUp() { | 
 | 35 |         LayerTransactionTest::SetUp(); | 
 | 36 |         ASSERT_EQ(NO_ERROR, mClient->initCheck()); | 
 | 37 |  | 
 | 38 |         mMainDisplay = SurfaceComposerClient::getInternalDisplayToken(); | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 39 |         SurfaceComposerClient::getDisplayState(mMainDisplay, &mMainDisplayState); | 
 | 40 |         SurfaceComposerClient::getActiveDisplayConfig(mMainDisplay, &mMainDisplayConfig); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 41 |  | 
 | 42 |         sp<IGraphicBufferConsumer> consumer; | 
 | 43 |         BufferQueue::createBufferQueue(&mProducer, &consumer); | 
 | 44 |         consumer->setConsumerName(String8("Virtual disp consumer")); | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 45 |         consumer->setDefaultBufferSize(mMainDisplayConfig.resolution.getWidth(), | 
 | 46 |                                        mMainDisplayConfig.resolution.getHeight()); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 47 |     } | 
 | 48 |  | 
 | 49 |     virtual void TearDown() { | 
 | 50 |         SurfaceComposerClient::destroyDisplay(mVirtualDisplay); | 
 | 51 |         LayerTransactionTest::TearDown(); | 
 | 52 |         mColorLayer = 0; | 
 | 53 |     } | 
 | 54 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 55 |     void createDisplay(const ui::Size& layerStackSize, uint32_t layerStack) { | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 56 |         mVirtualDisplay = | 
 | 57 |                 SurfaceComposerClient::createDisplay(String8("VirtualDisplay"), false /*secure*/); | 
 | 58 |         asTransaction([&](Transaction& t) { | 
 | 59 |             t.setDisplaySurface(mVirtualDisplay, mProducer); | 
 | 60 |             t.setDisplayLayerStack(mVirtualDisplay, layerStack); | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 61 |             t.setDisplayProjection(mVirtualDisplay, mMainDisplayState.orientation, | 
 | 62 |                                    Rect(layerStackSize), Rect(mMainDisplayConfig.resolution)); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 63 |         }); | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |     void createColorLayer(uint32_t layerStack) { | 
 | 67 |         mColorLayer = | 
 | 68 |                 createSurface(mClient, "ColorLayer", 0 /* buffer width */, 0 /* buffer height */, | 
| Vishnu Nair | fa247b1 | 2020-02-11 08:58:26 -0800 | [diff] [blame] | 69 |                               PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eFXSurfaceEffect); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 70 |         ASSERT_TRUE(mColorLayer != nullptr); | 
 | 71 |         ASSERT_TRUE(mColorLayer->isValid()); | 
 | 72 |         asTransaction([&](Transaction& t) { | 
 | 73 |             t.setLayerStack(mColorLayer, layerStack); | 
 | 74 |             t.setCrop_legacy(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 |  | 
| Dominik Laskowski | 3cb3d4e | 2019-11-21 11:14:45 -0800 | [diff] [blame] | 83 |     ui::DisplayState mMainDisplayState; | 
 | 84 |     DisplayConfig mMainDisplayConfig; | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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 |  | 
 | 92 | TEST_F(MultiDisplayLayerBoundsTest, RenderLayerInVirtualDisplay) { | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 93 |     createDisplay(mMainDisplayState.layerStackSpaceRect, 1 /* layerStack */); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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); | 
| Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 107 |     sc->expectColor(Rect(1, 1, 9, 9), {0, 0, 0, 255}); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 108 | } | 
 | 109 |  | 
 | 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 |  | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 114 |     // 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] | 115 |     // primary display that it is mirroring. | 
| Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 116 |     createDisplay(mMainDisplayState.layerStackSpaceRect, 0 /* layerStack */); | 
| Valerie Hau | 9cfc6d8 | 2019-09-23 13:54:07 -0700 | [diff] [blame] | 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 | 
| Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 134 |  | 
 | 135 | // TODO(b/129481165): remove the #pragma below and fix conversion issues | 
 | 136 | #pragma clang diagnostic pop // ignored "-Wconversion" |