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 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 17 | #include <com_android_graphics_surfaceflinger_flags.h> |
| 18 | #include <common/test/FlagUtils.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 19 | #include <compositionengine/CompositionRefreshArgs.h> |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 20 | #include <compositionengine/LayerFECompositionState.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 21 | #include <compositionengine/impl/CompositionEngine.h> |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 22 | #include <compositionengine/mock/LayerFE.h> |
| 23 | #include <compositionengine/mock/Output.h> |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 24 | #include <compositionengine/mock/OutputLayer.h> |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 25 | #include <ftl/future.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 27 | #include <renderengine/mock/RenderEngine.h> |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 28 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 29 | #include "MockHWComposer.h" |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 30 | #include "TimeStats/TimeStats.h" |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 31 | #include "gmock/gmock.h" |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 32 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 33 | using namespace com::android::graphics::surfaceflinger; |
| 34 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 35 | namespace android::compositionengine { |
| 36 | namespace { |
| 37 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 38 | using ::testing::_; |
Haibo Huang | f66b752 | 2020-09-10 18:20:30 -0700 | [diff] [blame] | 39 | using ::testing::DoAll; |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 40 | using ::testing::InSequence; |
| 41 | using ::testing::Ref; |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 42 | using ::testing::Return; |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 43 | using ::testing::ReturnRef; |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 44 | using ::testing::SaveArg; |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 45 | using ::testing::StrictMock; |
| 46 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 47 | struct CompositionEngineTest : public testing::Test { |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 48 | std::shared_ptr<TimeStats> mTimeStats; |
| 49 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 50 | impl::CompositionEngine mEngine; |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 51 | CompositionRefreshArgs mRefreshArgs; |
| 52 | |
| 53 | std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()}; |
| 54 | std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()}; |
| 55 | std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()}; |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 58 | TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) { |
| 59 | auto engine = impl::createCompositionEngine(); |
| 60 | EXPECT_TRUE(engine.get() != nullptr); |
| 61 | } |
| 62 | |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 63 | TEST_F(CompositionEngineTest, canSetHWComposer) { |
Ady Abraham | eca9d75 | 2021-03-03 12:20:00 -0800 | [diff] [blame] | 64 | android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>(); |
| 65 | mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc)); |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 66 | |
Ady Abraham | eca9d75 | 2021-03-03 12:20:00 -0800 | [diff] [blame] | 67 | EXPECT_EQ(hwc, &mEngine.getHwComposer()); |
Lloyd Pique | 441d504 | 2018-10-18 16:49:51 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 70 | TEST_F(CompositionEngineTest, canSetRenderEngine) { |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 71 | auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>(); |
| 72 | mEngine.setRenderEngine(renderEngine.get()); |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 73 | |
Patrick Williams | 0a525a4 | 2022-10-26 20:20:50 +0000 | [diff] [blame] | 74 | EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine()); |
Lloyd Pique | b97e04f | 2018-10-18 17:07:05 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 77 | TEST_F(CompositionEngineTest, canSetTimeStats) { |
| 78 | mEngine.setTimeStats(mTimeStats); |
| 79 | |
Patrick Williams | 74c0bf6 | 2022-11-02 23:59:26 +0000 | [diff] [blame] | 80 | EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats()); |
Alec Mouri | e4034bb | 2019-11-19 12:45:54 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 83 | /* |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 84 | * CompositionEngine::present |
| 85 | */ |
| 86 | |
| 87 | struct CompositionEnginePresentTest : public CompositionEngineTest { |
| 88 | struct CompositionEnginePartialMock : public impl::CompositionEngine { |
| 89 | // These are the overridable functions CompositionEngine::present() may |
| 90 | // call, and have separate test coverage. |
| 91 | MOCK_METHOD1(preComposition, void(CompositionRefreshArgs&)); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 92 | MOCK_METHOD1(postComposition, void(CompositionRefreshArgs&)); |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | StrictMock<CompositionEnginePartialMock> mEngine; |
| 96 | }; |
| 97 | |
| 98 | TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) { |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 99 | // present() always calls preComposition() and postComposition() |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 100 | EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs))); |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 101 | EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs))); |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 102 | |
| 103 | mEngine.present(mRefreshArgs); |
| 104 | } |
| 105 | |
| 106 | TEST_F(CompositionEnginePresentTest, worksAsExpected) { |
| 107 | // Expect calls to in a certain sequence |
| 108 | InSequence seq; |
| 109 | |
| 110 | // present() always calls preComposition() |
| 111 | EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs))); |
| 112 | |
| 113 | // The first step in presenting is to make sure all outputs are prepared. |
| 114 | EXPECT_CALL(*mOutput1, prepare(Ref(mRefreshArgs), _)); |
| 115 | EXPECT_CALL(*mOutput2, prepare(Ref(mRefreshArgs), _)); |
| 116 | EXPECT_CALL(*mOutput3, prepare(Ref(mRefreshArgs), _)); |
| 117 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 118 | // All of mOutput<i> are StrictMocks. If the flag is true, it will introduce |
| 119 | // calls to getDisplayId, which are not relevant to this test. |
| 120 | SET_FLAG_FOR_TEST(flags::multithreaded_present, false); |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 121 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 122 | // The last step is to actually present each output. |
Leon Scroggins III | 2f60d73 | 2022-09-12 14:42:38 -0400 | [diff] [blame] | 123 | EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs))) |
| 124 | .WillOnce(Return(ftl::yield<std::monostate>({}))); |
| 125 | EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs))) |
| 126 | .WillOnce(Return(ftl::yield<std::monostate>({}))); |
| 127 | EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs))) |
| 128 | .WillOnce(Return(ftl::yield<std::monostate>({}))); |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 129 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 130 | // present() always calls postComposition() |
| 131 | EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs))); |
| 132 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 133 | mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3}; |
| 134 | mEngine.present(mRefreshArgs); |
| 135 | } |
| 136 | |
| 137 | /* |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 138 | * CompositionEngine::updateCursorAsync |
| 139 | */ |
| 140 | |
| 141 | struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest { |
| 142 | public: |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 143 | struct Layer { |
Ady Abraham | eca9d75 | 2021-03-03 12:20:00 -0800 | [diff] [blame] | 144 | Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); } |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 145 | |
| 146 | StrictMock<mock::OutputLayer> outputLayer; |
Ady Abraham | eca9d75 | 2021-03-03 12:20:00 -0800 | [diff] [blame] | 147 | sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make(); |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 148 | LayerFECompositionState layerFEState; |
| 149 | }; |
| 150 | |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 151 | CompositionEngineUpdateCursorAsyncTest() { |
Lloyd Pique | 0a45623 | 2020-01-16 17:51:13 -0800 | [diff] [blame] | 152 | EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 153 | EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0); |
| 154 | |
Lloyd Pique | 0a45623 | 2020-01-16 17:51:13 -0800 | [diff] [blame] | 155 | EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 156 | EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0)) |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 157 | .WillRepeatedly(Return(&mOutput2Layer1.outputLayer)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 158 | |
Lloyd Pique | 0a45623 | 2020-01-16 17:51:13 -0800 | [diff] [blame] | 159 | EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 160 | EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0)) |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 161 | .WillRepeatedly(Return(&mOutput3Layer1.outputLayer)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 162 | EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1)) |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 163 | .WillRepeatedly(Return(&mOutput3Layer2.outputLayer)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 166 | Layer mOutput2Layer1; |
| 167 | Layer mOutput3Layer1; |
| 168 | Layer mOutput3Layer2; |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) { |
| 172 | mEngine.updateCursorAsync(mRefreshArgs); |
| 173 | } |
| 174 | |
| 175 | TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) { |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 176 | EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false)); |
| 177 | EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false)); |
| 178 | EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false)); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 179 | |
| 180 | mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3}; |
| 181 | |
| 182 | mEngine.updateCursorAsync(mRefreshArgs); |
| 183 | } |
| 184 | |
| 185 | TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) { |
| 186 | { |
| 187 | InSequence seq; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 188 | EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true)); |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 189 | EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC()); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | { |
| 193 | InSequence seq; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 194 | EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true)); |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 195 | EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC()); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | { |
| 199 | InSequence seq; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 200 | EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true)); |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 201 | EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC()); |
Lloyd Pique | aed56ab | 2019-11-13 22:34:11 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3}; |
| 205 | |
| 206 | mEngine.updateCursorAsync(mRefreshArgs); |
| 207 | } |
| 208 | |
| 209 | /* |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 210 | * CompositionEngine::preComposition |
| 211 | */ |
| 212 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 213 | struct CompositionTestPreComposition : public CompositionEngineTest { |
Ady Abraham | e0eafa8 | 2022-02-02 19:30:47 -0800 | [diff] [blame] | 214 | sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make(); |
| 215 | sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make(); |
| 216 | sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make(); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 217 | }; |
| 218 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 219 | TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) { |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 220 | const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC); |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 221 | mRefreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC); |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 222 | mEngine.preComposition(mRefreshArgs); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 223 | const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC); |
| 224 | |
| 225 | // The frame timestamp should be between the before and after timestamps |
| 226 | EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before); |
| 227 | EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after); |
| 228 | } |
| 229 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 230 | TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) { |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 231 | nsecs_t ts1 = 0; |
| 232 | nsecs_t ts2 = 0; |
| 233 | nsecs_t ts3 = 0; |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 234 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false))); |
| 235 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false))); |
| 236 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false))); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 237 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 238 | mRefreshArgs.outputs = {mOutput1}; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 239 | mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE}; |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 240 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 241 | mEngine.preComposition(mRefreshArgs); |
| 242 | |
| 243 | // Each of the onPreComposition calls should used the same refresh timestamp |
| 244 | EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp()); |
| 245 | EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp()); |
| 246 | EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp()); |
| 247 | } |
| 248 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 249 | TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) { |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 250 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false)); |
| 251 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false)); |
| 252 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false)); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 253 | |
| 254 | mEngine.setNeedsAnotherUpdateForTest(true); |
| 255 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 256 | mRefreshArgs.outputs = {mOutput1}; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 257 | mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE}; |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 258 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 259 | mEngine.preComposition(mRefreshArgs); |
| 260 | |
| 261 | // The call should have cleared the needsAnotherUpdate flag |
| 262 | EXPECT_FALSE(mEngine.needsAnotherUpdate()); |
| 263 | } |
| 264 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 265 | TEST_F(CompositionTestPreComposition, |
| 266 | preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) { |
Melody Hsu | c949cde | 2024-03-12 01:43:34 +0000 | [diff] [blame] | 267 | EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true)); |
| 268 | EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false)); |
| 269 | EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false)); |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 270 | |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 271 | mRefreshArgs.outputs = {mOutput1}; |
Lloyd Pique | de19665 | 2020-01-22 17:29:58 -0800 | [diff] [blame] | 272 | mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE}; |
Lloyd Pique | e82ed2d | 2019-11-13 19:28:12 -0800 | [diff] [blame] | 273 | |
Lloyd Pique | ab039b5 | 2019-02-13 14:22:42 -0800 | [diff] [blame] | 274 | mEngine.preComposition(mRefreshArgs); |
| 275 | |
| 276 | EXPECT_TRUE(mEngine.needsAnotherUpdate()); |
| 277 | } |
| 278 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 279 | struct CompositionEngineOffloadTest : public testing::Test { |
| 280 | impl::CompositionEngine mEngine; |
| 281 | CompositionRefreshArgs mRefreshArgs; |
| 282 | |
| 283 | std::shared_ptr<mock::Output> mDisplay1{std::make_shared<StrictMock<mock::Output>>()}; |
| 284 | std::shared_ptr<mock::Output> mDisplay2{std::make_shared<StrictMock<mock::Output>>()}; |
| 285 | std::shared_ptr<mock::Output> mVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()}; |
| 286 | std::shared_ptr<mock::Output> mHalVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()}; |
| 287 | |
| 288 | static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(123u); |
| 289 | static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(234u); |
| 290 | static constexpr GpuVirtualDisplayId kGpuVirtualDisplayId{789u}; |
| 291 | static constexpr HalVirtualDisplayId kHalVirtualDisplayId{456u}; |
| 292 | |
Leon Scroggins III | cbc929d | 2023-12-01 16:21:37 -0500 | [diff] [blame] | 293 | std::array<impl::OutputCompositionState, 4> mOutputStates; |
| 294 | |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 295 | void SetUp() override { |
| 296 | EXPECT_CALL(*mDisplay1, getDisplayId) |
| 297 | .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId1))); |
| 298 | EXPECT_CALL(*mDisplay2, getDisplayId) |
| 299 | .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId2))); |
| 300 | EXPECT_CALL(*mVirtualDisplay, getDisplayId) |
| 301 | .WillRepeatedly(Return(std::make_optional<DisplayId>(kGpuVirtualDisplayId))); |
| 302 | EXPECT_CALL(*mHalVirtualDisplay, getDisplayId) |
| 303 | .WillRepeatedly(Return(std::make_optional<DisplayId>(kHalVirtualDisplayId))); |
Leon Scroggins III | cbc929d | 2023-12-01 16:21:37 -0500 | [diff] [blame] | 304 | |
| 305 | // Most tests will depend on the outputs being enabled. |
| 306 | for (auto& state : mOutputStates) { |
| 307 | state.isEnabled = true; |
| 308 | } |
| 309 | |
| 310 | EXPECT_CALL(*mDisplay1, getState).WillRepeatedly(ReturnRef(mOutputStates[0])); |
| 311 | EXPECT_CALL(*mDisplay2, getState).WillRepeatedly(ReturnRef(mOutputStates[1])); |
| 312 | EXPECT_CALL(*mVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[2])); |
| 313 | EXPECT_CALL(*mHalVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[3])); |
Leon Scroggins III | ab551a3 | 2023-11-22 10:08:25 -0500 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void setOutputs(std::initializer_list<std::shared_ptr<mock::Output>> outputs) { |
| 317 | for (auto& output : outputs) { |
| 318 | // If we call mEngine.present, prepare and present will be called on all the |
| 319 | // outputs in mRefreshArgs, but that's not the interesting part of the test. |
| 320 | EXPECT_CALL(*output, prepare(Ref(mRefreshArgs), _)).Times(1); |
| 321 | EXPECT_CALL(*output, present(Ref(mRefreshArgs))) |
| 322 | .WillOnce(Return(ftl::yield<std::monostate>({}))); |
| 323 | |
| 324 | mRefreshArgs.outputs.push_back(std::move(output)); |
| 325 | } |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | TEST_F(CompositionEngineOffloadTest, basic) { |
| 330 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 331 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true)); |
| 332 | |
| 333 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1); |
| 334 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 335 | |
| 336 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 337 | setOutputs({mDisplay1, mDisplay2}); |
| 338 | |
| 339 | mEngine.present(mRefreshArgs); |
| 340 | } |
| 341 | |
| 342 | TEST_F(CompositionEngineOffloadTest, dependsOnSupport) { |
| 343 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(false)); |
| 344 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0); |
| 345 | |
| 346 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 347 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 348 | |
| 349 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 350 | setOutputs({mDisplay1, mDisplay2}); |
| 351 | |
| 352 | mEngine.present(mRefreshArgs); |
| 353 | } |
| 354 | |
| 355 | TEST_F(CompositionEngineOffloadTest, dependsOnSupport2) { |
| 356 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 357 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(false)); |
| 358 | |
| 359 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 360 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 361 | |
| 362 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 363 | setOutputs({mDisplay1, mDisplay2}); |
| 364 | |
| 365 | mEngine.present(mRefreshArgs); |
| 366 | } |
| 367 | |
| 368 | TEST_F(CompositionEngineOffloadTest, dependsOnFlag) { |
| 369 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0); |
| 370 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0); |
| 371 | |
| 372 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 373 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 374 | |
| 375 | SET_FLAG_FOR_TEST(flags::multithreaded_present, false); |
| 376 | setOutputs({mDisplay1, mDisplay2}); |
| 377 | |
| 378 | mEngine.present(mRefreshArgs); |
| 379 | } |
| 380 | |
| 381 | TEST_F(CompositionEngineOffloadTest, oneDisplay) { |
| 382 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0); |
| 383 | |
| 384 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 385 | |
| 386 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 387 | setOutputs({mDisplay1}); |
| 388 | |
| 389 | mEngine.present(mRefreshArgs); |
| 390 | } |
| 391 | |
| 392 | TEST_F(CompositionEngineOffloadTest, virtualDisplay) { |
| 393 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 394 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true)); |
| 395 | EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0); |
| 396 | |
| 397 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1); |
| 398 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 399 | EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0); |
| 400 | |
| 401 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 402 | setOutputs({mDisplay1, mDisplay2, mVirtualDisplay}); |
| 403 | |
| 404 | mEngine.present(mRefreshArgs); |
| 405 | } |
| 406 | |
| 407 | TEST_F(CompositionEngineOffloadTest, virtualDisplay2) { |
| 408 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 409 | EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0); |
| 410 | |
| 411 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 412 | EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0); |
| 413 | |
| 414 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 415 | setOutputs({mDisplay1, mVirtualDisplay}); |
| 416 | |
| 417 | mEngine.present(mRefreshArgs); |
| 418 | } |
| 419 | |
| 420 | TEST_F(CompositionEngineOffloadTest, halVirtual) { |
| 421 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 422 | EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true)); |
| 423 | |
| 424 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1); |
| 425 | EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0); |
| 426 | |
| 427 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 428 | setOutputs({mDisplay1, mHalVirtualDisplay}); |
| 429 | |
| 430 | mEngine.present(mRefreshArgs); |
| 431 | } |
| 432 | |
| 433 | TEST_F(CompositionEngineOffloadTest, ordering) { |
| 434 | EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0); |
| 435 | EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true)); |
| 436 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 437 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true)); |
| 438 | |
| 439 | EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0); |
| 440 | EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(1); |
| 441 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1); |
| 442 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 443 | |
| 444 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 445 | setOutputs({mVirtualDisplay, mHalVirtualDisplay, mDisplay1, mDisplay2}); |
| 446 | |
| 447 | mEngine.present(mRefreshArgs); |
| 448 | } |
| 449 | |
Leon Scroggins III | cbc929d | 2023-12-01 16:21:37 -0500 | [diff] [blame] | 450 | TEST_F(CompositionEngineOffloadTest, dependsOnEnabled) { |
| 451 | // Disable mDisplay2. |
| 452 | mOutputStates[1].isEnabled = false; |
| 453 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 454 | |
| 455 | // This is not actually called, because it is not enabled, but this distinguishes |
| 456 | // from the case where it did not return true. |
| 457 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true)); |
| 458 | |
| 459 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0); |
| 460 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 461 | |
| 462 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 463 | setOutputs({mDisplay1, mDisplay2}); |
| 464 | |
| 465 | mEngine.present(mRefreshArgs); |
| 466 | } |
| 467 | |
| 468 | TEST_F(CompositionEngineOffloadTest, disabledDisplaysDoNotPreventOthersFromOffloading) { |
| 469 | // Disable mDisplay2. |
| 470 | mOutputStates[1].isEnabled = false; |
| 471 | EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true)); |
| 472 | |
| 473 | // This is not actually called, because it is not enabled, but this distinguishes |
| 474 | // from the case where it did not return true. |
| 475 | EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true)); |
| 476 | EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true)); |
| 477 | |
| 478 | EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1); |
| 479 | EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0); |
| 480 | EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0); |
| 481 | |
| 482 | SET_FLAG_FOR_TEST(flags::multithreaded_present, true); |
| 483 | setOutputs({mDisplay1, mDisplay2, mHalVirtualDisplay}); |
| 484 | |
| 485 | mEngine.present(mRefreshArgs); |
| 486 | } |
| 487 | |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 488 | struct CompositionEnginePostCompositionTest : public CompositionEngineTest { |
| 489 | sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make(); |
| 490 | sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make(); |
| 491 | sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make(); |
| 492 | }; |
| 493 | |
| 494 | TEST_F(CompositionEnginePostCompositionTest, postCompositionReleasesAllFences) { |
Melody Hsu | 793f836 | 2024-01-08 20:00:35 +0000 | [diff] [blame] | 495 | EXPECT_CALL(*mLayer1FE, getReleaseFencePromiseStatus) |
| 496 | .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED)); |
| 497 | EXPECT_CALL(*mLayer2FE, getReleaseFencePromiseStatus) |
| 498 | .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED)); |
| 499 | EXPECT_CALL(*mLayer3FE, getReleaseFencePromiseStatus) |
| 500 | .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::INITIALIZED)); |
| 501 | mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE}; |
| 502 | |
| 503 | EXPECT_CALL(*mLayer1FE, setReleaseFence(_)).Times(0); |
| 504 | EXPECT_CALL(*mLayer2FE, setReleaseFence(_)).Times(0); |
| 505 | EXPECT_CALL(*mLayer3FE, setReleaseFence(_)).Times(1); |
| 506 | |
| 507 | mEngine.postComposition(mRefreshArgs); |
| 508 | } |
Lloyd Pique | 70d9136 | 2018-10-18 16:02:55 -0700 | [diff] [blame] | 509 | } // namespace |
| 510 | } // namespace android::compositionengine |