Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame^] | 17 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 18 | #include <compositionengine/impl/CompositionEngine.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame^] | 19 | #include <compositionengine/mock/Layer.h> |
| 20 | #include <compositionengine/mock/LayerFE.h> |
| 21 | #include <compositionengine/mock/Output.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 23 | #include <renderengine/mock/RenderEngine.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 24 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 25 | #include "MockHWComposer.h" |
| 26 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 27 | namespace android::compositionengine { |
| 28 | namespace { |
| 29 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame^] | 30 | using ::testing::_; |
| 31 | using ::testing::Return; |
| 32 | using ::testing::SaveArg; |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 33 | using ::testing::StrictMock; |
| 34 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 35 | class CompositionEngineTest : public testing::Test { |
| 36 | public: |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame^] | 37 | android::mock::HWComposer* mHwc = new StrictMock<android::mock::HWComposer>(); |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 38 | renderengine::mock::RenderEngine* mRenderEngine = |
| 39 | new StrictMock<renderengine::mock::RenderEngine>(); |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 40 | impl::CompositionEngine mEngine; |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 43 | TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) { |
| 44 | auto engine = impl::createCompositionEngine(); |
| 45 | EXPECT_TRUE(engine.get() != nullptr); |
| 46 | } |
| 47 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 48 | TEST_F(CompositionEngineTest, canSetHWComposer) { |
| 49 | mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(mHwc)); |
| 50 | |
| 51 | EXPECT_EQ(mHwc, &mEngine.getHwComposer()); |
| 52 | } |
| 53 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 54 | TEST_F(CompositionEngineTest, canSetRenderEngine) { |
| 55 | mEngine.setRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine)); |
| 56 | |
| 57 | EXPECT_EQ(mRenderEngine, &mEngine.getRenderEngine()); |
| 58 | } |
| 59 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame^] | 60 | /* |
| 61 | * CompositionEngine::preComposition |
| 62 | */ |
| 63 | |
| 64 | class PreCompositionTest : public CompositionEngineTest { |
| 65 | public: |
| 66 | PreCompositionTest() { |
| 67 | EXPECT_CALL(*mLayer1, getLayerFE()).WillRepeatedly(Return(mLayer1FE)); |
| 68 | EXPECT_CALL(*mLayer2, getLayerFE()).WillRepeatedly(Return(mLayer2FE)); |
| 69 | EXPECT_CALL(*mLayer3, getLayerFE()).WillRepeatedly(Return(mLayer3FE)); |
| 70 | // getLayerFE() can return nullptr. Ensure that this is handled. |
| 71 | EXPECT_CALL(*mLayer4, getLayerFE()).WillRepeatedly(Return(nullptr)); |
| 72 | |
| 73 | mRefreshArgs.outputs = {mOutput}; |
| 74 | mRefreshArgs.layers = {mLayer1, mLayer2, mLayer3, mLayer4}; |
| 75 | } |
| 76 | |
| 77 | std::shared_ptr<mock::Output> mOutput{std::make_shared<StrictMock<mock::Output>>()}; |
| 78 | std::shared_ptr<mock::Layer> mLayer1{std::make_shared<StrictMock<mock::Layer>>()}; |
| 79 | std::shared_ptr<mock::Layer> mLayer2{std::make_shared<StrictMock<mock::Layer>>()}; |
| 80 | std::shared_ptr<mock::Layer> mLayer3{std::make_shared<StrictMock<mock::Layer>>()}; |
| 81 | std::shared_ptr<mock::Layer> mLayer4{std::make_shared<StrictMock<mock::Layer>>()}; |
| 82 | sp<StrictMock<mock::LayerFE>> mLayer1FE{new StrictMock<mock::LayerFE>()}; |
| 83 | sp<StrictMock<mock::LayerFE>> mLayer2FE{new StrictMock<mock::LayerFE>()}; |
| 84 | sp<StrictMock<mock::LayerFE>> mLayer3FE{new StrictMock<mock::LayerFE>()}; |
| 85 | |
| 86 | CompositionRefreshArgs mRefreshArgs; |
| 87 | }; |
| 88 | |
| 89 | TEST_F(PreCompositionTest, preCompositionSetsFrameTimestamp) { |
| 90 | const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC); |
| 91 | CompositionRefreshArgs emptyArgs; |
| 92 | mEngine.preComposition(emptyArgs); |
| 93 | const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC); |
| 94 | |
| 95 | // The frame timestamp should be between the before and after timestamps |
| 96 | EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before); |
| 97 | EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after); |
| 98 | } |
| 99 | |
| 100 | TEST_F(PreCompositionTest, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) { |
| 101 | nsecs_t ts1 = 0; |
| 102 | nsecs_t ts2 = 0; |
| 103 | nsecs_t ts3 = 0; |
| 104 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false))); |
| 105 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false))); |
| 106 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false))); |
| 107 | |
| 108 | mEngine.preComposition(mRefreshArgs); |
| 109 | |
| 110 | // Each of the onPreComposition calls should used the same refresh timestamp |
| 111 | EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp()); |
| 112 | EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp()); |
| 113 | EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp()); |
| 114 | } |
| 115 | |
| 116 | TEST_F(PreCompositionTest, preCompositionDefaultsToNoUpdateNeeded) { |
| 117 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false)); |
| 118 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false)); |
| 119 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false)); |
| 120 | |
| 121 | mEngine.setNeedsAnotherUpdateForTest(true); |
| 122 | |
| 123 | mEngine.preComposition(mRefreshArgs); |
| 124 | |
| 125 | // The call should have cleared the needsAnotherUpdate flag |
| 126 | EXPECT_FALSE(mEngine.needsAnotherUpdate()); |
| 127 | } |
| 128 | |
| 129 | TEST_F(PreCompositionTest, preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) { |
| 130 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true)); |
| 131 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false)); |
| 132 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false)); |
| 133 | |
| 134 | mEngine.preComposition(mRefreshArgs); |
| 135 | |
| 136 | EXPECT_TRUE(mEngine.needsAnotherUpdate()); |
| 137 | } |
| 138 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 139 | } // namespace |
| 140 | } // namespace android::compositionengine |