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