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 RelativeZTest : public LayerTransactionTest { |
| 27 | protected: |
| 28 | virtual void SetUp() { |
| 29 | LayerTransactionTest::SetUp(); |
| 30 | ASSERT_EQ(NO_ERROR, mClient->initCheck()); |
| 31 | |
| 32 | const auto display = SurfaceComposerClient::getInternalDisplayToken(); |
| 33 | ASSERT_FALSE(display == nullptr); |
| 34 | |
| 35 | // Back layer |
| 36 | mBackgroundLayer = createColorLayer("Background layer", Color::RED); |
| 37 | |
| 38 | // Front layer |
| 39 | mForegroundLayer = createColorLayer("Foreground layer", Color::GREEN); |
| 40 | |
| 41 | asTransaction([&](Transaction& t) { |
| 42 | t.setDisplayLayerStack(display, 0); |
| 43 | t.setLayer(mBackgroundLayer, INT32_MAX - 2).show(mBackgroundLayer); |
| 44 | t.setLayer(mForegroundLayer, INT32_MAX - 1).show(mForegroundLayer); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | virtual void TearDown() { |
| 49 | LayerTransactionTest::TearDown(); |
| 50 | mBackgroundLayer = 0; |
| 51 | mForegroundLayer = 0; |
| 52 | } |
| 53 | |
| 54 | sp<SurfaceControl> mBackgroundLayer; |
| 55 | sp<SurfaceControl> mForegroundLayer; |
| 56 | }; |
| 57 | |
| 58 | // When a layer is reparented offscreen, remove relative z order if the relative parent |
| 59 | // is still onscreen so that the layer is not drawn. |
| 60 | TEST_F(RelativeZTest, LayerRemoved) { |
| 61 | std::unique_ptr<ScreenCapture> sc; |
| 62 | |
| 63 | // Background layer (RED) |
| 64 | // Child layer (WHITE) (relative to foregroud layer) |
| 65 | // Foregroud layer (GREEN) |
| 66 | sp<SurfaceControl> childLayer = |
| 67 | createColorLayer("Child layer", Color::BLUE, mBackgroundLayer.get()); |
| 68 | |
| 69 | Transaction{} |
| 70 | .setRelativeLayer(childLayer, mForegroundLayer->getHandle(), 1) |
| 71 | .show(childLayer) |
| 72 | .apply(); |
| 73 | |
| 74 | { |
| 75 | // The childLayer should be in front of the FG control. |
| 76 | ScreenCapture::captureScreen(&sc); |
| 77 | sc->checkPixel(1, 1, Color::BLUE.r, Color::BLUE.g, Color::BLUE.b); |
| 78 | } |
| 79 | |
| 80 | // Background layer (RED) |
| 81 | // Foregroud layer (GREEN) |
| 82 | Transaction{}.reparent(childLayer, nullptr).apply(); |
| 83 | |
| 84 | // Background layer (RED) |
| 85 | // Child layer (WHITE) |
| 86 | // Foregroud layer (GREEN) |
| 87 | Transaction{}.reparent(childLayer, mBackgroundLayer->getHandle()).apply(); |
| 88 | |
| 89 | { |
| 90 | // The relative z info for child layer should be reset, leaving FG control on top. |
| 91 | ScreenCapture::captureScreen(&sc); |
| 92 | sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // When a layer is reparented offscreen, preseve relative z order if the relative parent |
| 97 | // is also offscreen. Regression test b/132613412 |
| 98 | TEST_F(RelativeZTest, LayerRemovedOffscreenRelativeParent) { |
| 99 | std::unique_ptr<ScreenCapture> sc; |
| 100 | |
| 101 | // Background layer (RED) |
| 102 | // Foregroud layer (GREEN) |
| 103 | // child level 1 (WHITE) |
| 104 | // child level 2a (BLUE) |
| 105 | // child level 3 (GREEN) (relative to child level 2b) |
| 106 | // child level 2b (BLACK) |
| 107 | sp<SurfaceControl> childLevel1 = |
| 108 | createColorLayer("child level 1", Color::WHITE, mForegroundLayer.get()); |
| 109 | sp<SurfaceControl> childLevel2a = |
| 110 | createColorLayer("child level 2a", Color::BLUE, childLevel1.get()); |
| 111 | sp<SurfaceControl> childLevel2b = |
| 112 | createColorLayer("child level 2b", Color::BLACK, childLevel1.get()); |
| 113 | sp<SurfaceControl> childLevel3 = |
| 114 | createColorLayer("child level 3", Color::GREEN, childLevel2a.get()); |
| 115 | |
| 116 | Transaction{} |
| 117 | .setRelativeLayer(childLevel3, childLevel2b->getHandle(), 1) |
| 118 | .show(childLevel2a) |
| 119 | .show(childLevel2b) |
| 120 | .show(childLevel3) |
| 121 | .apply(); |
| 122 | |
| 123 | { |
| 124 | // The childLevel3 should be in front of childLevel2b. |
| 125 | ScreenCapture::captureScreen(&sc); |
| 126 | sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b); |
| 127 | } |
| 128 | |
| 129 | // Background layer (RED) |
| 130 | // Foregroud layer (GREEN) |
| 131 | Transaction{}.reparent(childLevel1, nullptr).apply(); |
| 132 | |
| 133 | // Background layer (RED) |
| 134 | // Foregroud layer (GREEN) |
| 135 | // child level 1 (WHITE) |
| 136 | // child level 2 back (BLUE) |
| 137 | // child level 3 (GREEN) (relative to child level 2b) |
| 138 | // child level 2 front (BLACK) |
| 139 | Transaction{}.reparent(childLevel1, mForegroundLayer->getHandle()).apply(); |
| 140 | |
| 141 | { |
| 142 | // Nothing should change at this point since relative z info was preserved. |
| 143 | ScreenCapture::captureScreen(&sc); |
| 144 | sc->checkPixel(1, 1, Color::GREEN.r, Color::GREEN.g, Color::GREEN.b); |
| 145 | } |
| 146 | } |
| 147 | } // namespace android |