blob: 3e0c390a5d34e97327f6330a32db5ad99d38a312 [file] [log] [blame]
Lloyd Pique70d91362018-10-18 16:02:55 -07001/*
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 IIIab551a32023-11-22 10:08:25 -050017#include <com_android_graphics_surfaceflinger_flags.h>
18#include <common/test/FlagUtils.h>
Lloyd Piqueab039b52019-02-13 14:22:42 -080019#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080020#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070021#include <compositionengine/impl/CompositionEngine.h>
Lloyd Piqueab039b52019-02-13 14:22:42 -080022#include <compositionengine/mock/LayerFE.h>
23#include <compositionengine/mock/Output.h>
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080024#include <compositionengine/mock/OutputLayer.h>
Leon Scroggins III2f60d732022-09-12 14:42:38 -040025#include <ftl/future.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070026#include <gtest/gtest.h>
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070027#include <renderengine/mock/RenderEngine.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070028
Alec Mourie4034bb2019-11-19 12:45:54 -080029#include "TimeStats/TimeStats.h"
Lloyd Piquee98286f2024-09-16 16:23:24 -070030#include "mock/DisplayHardware/MockHWComposer.h"
Lloyd Pique441d5042018-10-18 16:49:51 -070031
Leon Scroggins IIIab551a32023-11-22 10:08:25 -050032using namespace com::android::graphics::surfaceflinger;
33
Lloyd Pique70d91362018-10-18 16:02:55 -070034namespace android::compositionengine {
35namespace {
36
Lloyd Piqueab039b52019-02-13 14:22:42 -080037using ::testing::_;
Haibo Huangf66b7522020-09-10 18:20:30 -070038using ::testing::DoAll;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080039using ::testing::InSequence;
40using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080041using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080042using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080043using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070044using ::testing::StrictMock;
45
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080046struct CompositionEngineTest : public testing::Test {
Alec Mourie4034bb2019-11-19 12:45:54 -080047 std::shared_ptr<TimeStats> mTimeStats;
48
Lloyd Pique441d5042018-10-18 16:49:51 -070049 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080050 CompositionRefreshArgs mRefreshArgs;
51
52 std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()};
53 std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()};
54 std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()};
Lloyd Pique70d91362018-10-18 16:02:55 -070055};
56
Lloyd Pique70d91362018-10-18 16:02:55 -070057TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
58 auto engine = impl::createCompositionEngine();
59 EXPECT_TRUE(engine.get() != nullptr);
60}
61
Lloyd Pique441d5042018-10-18 16:49:51 -070062TEST_F(CompositionEngineTest, canSetHWComposer) {
Ady Abrahameca9d752021-03-03 12:20:00 -080063 android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
64 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
Lloyd Pique441d5042018-10-18 16:49:51 -070065
Ady Abrahameca9d752021-03-03 12:20:00 -080066 EXPECT_EQ(hwc, &mEngine.getHwComposer());
Lloyd Pique441d5042018-10-18 16:49:51 -070067}
68
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070069TEST_F(CompositionEngineTest, canSetRenderEngine) {
Patrick Williams0a525a42022-10-26 20:20:50 +000070 auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
71 mEngine.setRenderEngine(renderEngine.get());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070072
Patrick Williams0a525a42022-10-26 20:20:50 +000073 EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070074}
75
Alec Mourie4034bb2019-11-19 12:45:54 -080076TEST_F(CompositionEngineTest, canSetTimeStats) {
77 mEngine.setTimeStats(mTimeStats);
78
Patrick Williams74c0bf62022-11-02 23:59:26 +000079 EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
Alec Mourie4034bb2019-11-19 12:45:54 -080080}
81
Lloyd Piqueab039b52019-02-13 14:22:42 -080082/*
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080083 * CompositionEngine::present
84 */
85
86struct CompositionEnginePresentTest : public CompositionEngineTest {
87 struct CompositionEnginePartialMock : public impl::CompositionEngine {
88 // These are the overridable functions CompositionEngine::present() may
89 // call, and have separate test coverage.
90 MOCK_METHOD1(preComposition, void(CompositionRefreshArgs&));
Melody Hsu793f8362024-01-08 20:00:35 +000091 MOCK_METHOD1(postComposition, void(CompositionRefreshArgs&));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080092 };
93
94 StrictMock<CompositionEnginePartialMock> mEngine;
95};
96
97TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
Melody Hsu793f8362024-01-08 20:00:35 +000098 // present() always calls preComposition() and postComposition()
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080099 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
Melody Hsu793f8362024-01-08 20:00:35 +0000100 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800101
102 mEngine.present(mRefreshArgs);
103}
104
105TEST_F(CompositionEnginePresentTest, worksAsExpected) {
106 // Expect calls to in a certain sequence
107 InSequence seq;
108
109 // present() always calls preComposition()
110 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
111
112 // The first step in presenting is to make sure all outputs are prepared.
113 EXPECT_CALL(*mOutput1, prepare(Ref(mRefreshArgs), _));
114 EXPECT_CALL(*mOutput2, prepare(Ref(mRefreshArgs), _));
115 EXPECT_CALL(*mOutput3, prepare(Ref(mRefreshArgs), _));
116
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500117 // All of mOutput<i> are StrictMocks. If the flag is true, it will introduce
118 // calls to getDisplayId, which are not relevant to this test.
119 SET_FLAG_FOR_TEST(flags::multithreaded_present, false);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400120
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800121 // The last step is to actually present each output.
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400122 EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs)))
123 .WillOnce(Return(ftl::yield<std::monostate>({})));
124 EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs)))
125 .WillOnce(Return(ftl::yield<std::monostate>({})));
126 EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs)))
127 .WillOnce(Return(ftl::yield<std::monostate>({})));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800128
Melody Hsu793f8362024-01-08 20:00:35 +0000129 // present() always calls postComposition()
130 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
131
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800132 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
133 mEngine.present(mRefreshArgs);
134}
135
136/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800137 * CompositionEngine::updateCursorAsync
138 */
139
140struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
141public:
Lloyd Piquede196652020-01-22 17:29:58 -0800142 struct Layer {
Ady Abrahameca9d752021-03-03 12:20:00 -0800143 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
Lloyd Piquede196652020-01-22 17:29:58 -0800144
145 StrictMock<mock::OutputLayer> outputLayer;
Ady Abrahameca9d752021-03-03 12:20:00 -0800146 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -0800147 LayerFECompositionState layerFEState;
148 };
149
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800150 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800151 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800152 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
153
Lloyd Pique0a456232020-01-16 17:51:13 -0800154 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800155 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800156 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800157
Lloyd Pique0a456232020-01-16 17:51:13 -0800158 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800159 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800160 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800161 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800162 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800163 }
164
Lloyd Piquede196652020-01-22 17:29:58 -0800165 Layer mOutput2Layer1;
166 Layer mOutput3Layer1;
167 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800168};
169
170TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
171 mEngine.updateCursorAsync(mRefreshArgs);
172}
173
174TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800175 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
176 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
177 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800178
179 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
180
181 mEngine.updateCursorAsync(mRefreshArgs);
182}
183
184TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
185 {
186 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800187 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800188 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800189 }
190
191 {
192 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800193 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800194 EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800195 }
196
197 {
198 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800199 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800200 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800201 }
202
203 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
204
205 mEngine.updateCursorAsync(mRefreshArgs);
206}
207
208/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800209 * CompositionEngine::preComposition
210 */
211
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800212struct CompositionTestPreComposition : public CompositionEngineTest {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800213 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
214 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
215 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piqueab039b52019-02-13 14:22:42 -0800216};
217
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800218TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800219 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Melody Hsuc949cde2024-03-12 01:43:34 +0000220 mRefreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800221 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800222 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
223
224 // The frame timestamp should be between the before and after timestamps
225 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
226 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
227}
228
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800229TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800230 nsecs_t ts1 = 0;
231 nsecs_t ts2 = 0;
232 nsecs_t ts3 = 0;
Melody Hsuc949cde2024-03-12 01:43:34 +0000233 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
234 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
235 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800236
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800237 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800238 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800239
Lloyd Piqueab039b52019-02-13 14:22:42 -0800240 mEngine.preComposition(mRefreshArgs);
241
242 // Each of the onPreComposition calls should used the same refresh timestamp
243 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
244 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
245 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
246}
247
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800248TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000249 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false));
250 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
251 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800252
253 mEngine.setNeedsAnotherUpdateForTest(true);
254
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800255 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800256 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800257
Lloyd Piqueab039b52019-02-13 14:22:42 -0800258 mEngine.preComposition(mRefreshArgs);
259
260 // The call should have cleared the needsAnotherUpdate flag
261 EXPECT_FALSE(mEngine.needsAnotherUpdate());
262}
263
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800264TEST_F(CompositionTestPreComposition,
265 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000266 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true));
267 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
268 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800269
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800270 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800271 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800272
Lloyd Piqueab039b52019-02-13 14:22:42 -0800273 mEngine.preComposition(mRefreshArgs);
274
275 EXPECT_TRUE(mEngine.needsAnotherUpdate());
276}
277
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500278struct CompositionEngineOffloadTest : public testing::Test {
279 impl::CompositionEngine mEngine;
280 CompositionRefreshArgs mRefreshArgs;
281
282 std::shared_ptr<mock::Output> mDisplay1{std::make_shared<StrictMock<mock::Output>>()};
283 std::shared_ptr<mock::Output> mDisplay2{std::make_shared<StrictMock<mock::Output>>()};
284 std::shared_ptr<mock::Output> mVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()};
285 std::shared_ptr<mock::Output> mHalVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()};
286
287 static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(123u);
288 static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(234u);
289 static constexpr GpuVirtualDisplayId kGpuVirtualDisplayId{789u};
290 static constexpr HalVirtualDisplayId kHalVirtualDisplayId{456u};
291
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500292 std::array<impl::OutputCompositionState, 4> mOutputStates;
293
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500294 void SetUp() override {
295 EXPECT_CALL(*mDisplay1, getDisplayId)
296 .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId1)));
297 EXPECT_CALL(*mDisplay2, getDisplayId)
298 .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId2)));
299 EXPECT_CALL(*mVirtualDisplay, getDisplayId)
300 .WillRepeatedly(Return(std::make_optional<DisplayId>(kGpuVirtualDisplayId)));
301 EXPECT_CALL(*mHalVirtualDisplay, getDisplayId)
302 .WillRepeatedly(Return(std::make_optional<DisplayId>(kHalVirtualDisplayId)));
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500303
304 // Most tests will depend on the outputs being enabled.
305 for (auto& state : mOutputStates) {
306 state.isEnabled = true;
307 }
308
309 EXPECT_CALL(*mDisplay1, getState).WillRepeatedly(ReturnRef(mOutputStates[0]));
310 EXPECT_CALL(*mDisplay2, getState).WillRepeatedly(ReturnRef(mOutputStates[1]));
311 EXPECT_CALL(*mVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[2]));
312 EXPECT_CALL(*mHalVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[3]));
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500313 }
314
315 void setOutputs(std::initializer_list<std::shared_ptr<mock::Output>> outputs) {
316 for (auto& output : outputs) {
317 // If we call mEngine.present, prepare and present will be called on all the
318 // outputs in mRefreshArgs, but that's not the interesting part of the test.
319 EXPECT_CALL(*output, prepare(Ref(mRefreshArgs), _)).Times(1);
320 EXPECT_CALL(*output, present(Ref(mRefreshArgs)))
321 .WillOnce(Return(ftl::yield<std::monostate>({})));
322
323 mRefreshArgs.outputs.push_back(std::move(output));
324 }
325 }
326};
327
328TEST_F(CompositionEngineOffloadTest, basic) {
329 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
330 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
331
332 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
333 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
334
335 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
336 setOutputs({mDisplay1, mDisplay2});
337
338 mEngine.present(mRefreshArgs);
339}
340
341TEST_F(CompositionEngineOffloadTest, dependsOnSupport) {
342 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(false));
343 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0);
344
345 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
346 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
347
348 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
349 setOutputs({mDisplay1, mDisplay2});
350
351 mEngine.present(mRefreshArgs);
352}
353
354TEST_F(CompositionEngineOffloadTest, dependsOnSupport2) {
355 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
356 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(false));
357
358 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
359 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
360
361 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
362 setOutputs({mDisplay1, mDisplay2});
363
364 mEngine.present(mRefreshArgs);
365}
366
367TEST_F(CompositionEngineOffloadTest, dependsOnFlag) {
368 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0);
369 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0);
370
371 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
372 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
373
374 SET_FLAG_FOR_TEST(flags::multithreaded_present, false);
375 setOutputs({mDisplay1, mDisplay2});
376
377 mEngine.present(mRefreshArgs);
378}
379
380TEST_F(CompositionEngineOffloadTest, oneDisplay) {
381 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0);
382
383 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
384
385 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
386 setOutputs({mDisplay1});
387
388 mEngine.present(mRefreshArgs);
389}
390
391TEST_F(CompositionEngineOffloadTest, virtualDisplay) {
392 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
393 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
394 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
395
396 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
397 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
398 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
399
400 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
401 setOutputs({mDisplay1, mDisplay2, mVirtualDisplay});
402
403 mEngine.present(mRefreshArgs);
404}
405
406TEST_F(CompositionEngineOffloadTest, virtualDisplay2) {
407 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
408 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
409
410 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
411 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
412
413 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
414 setOutputs({mDisplay1, mVirtualDisplay});
415
416 mEngine.present(mRefreshArgs);
417}
418
419TEST_F(CompositionEngineOffloadTest, halVirtual) {
420 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
421 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
422
423 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
424 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0);
425
426 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
427 setOutputs({mDisplay1, mHalVirtualDisplay});
428
429 mEngine.present(mRefreshArgs);
430}
431
432TEST_F(CompositionEngineOffloadTest, ordering) {
433 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
434 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
435 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
436 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
437
438 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
439 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(1);
440 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
441 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
442
443 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
444 setOutputs({mVirtualDisplay, mHalVirtualDisplay, mDisplay1, mDisplay2});
445
446 mEngine.present(mRefreshArgs);
447}
448
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500449TEST_F(CompositionEngineOffloadTest, dependsOnEnabled) {
450 // Disable mDisplay2.
451 mOutputStates[1].isEnabled = false;
452 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
453
454 // This is not actually called, because it is not enabled, but this distinguishes
455 // from the case where it did not return true.
456 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true));
457
458 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
459 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
460
461 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
462 setOutputs({mDisplay1, mDisplay2});
463
464 mEngine.present(mRefreshArgs);
465}
466
467TEST_F(CompositionEngineOffloadTest, disabledDisplaysDoNotPreventOthersFromOffloading) {
468 // Disable mDisplay2.
469 mOutputStates[1].isEnabled = false;
470 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
471
472 // This is not actually called, because it is not enabled, but this distinguishes
473 // from the case where it did not return true.
474 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true));
475 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
476
477 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
478 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
479 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0);
480
481 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
482 setOutputs({mDisplay1, mDisplay2, mHalVirtualDisplay});
483
484 mEngine.present(mRefreshArgs);
485}
486
Melody Hsu793f8362024-01-08 20:00:35 +0000487struct CompositionEnginePostCompositionTest : public CompositionEngineTest {
488 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
489 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
490 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
491};
492
493TEST_F(CompositionEnginePostCompositionTest, postCompositionReleasesAllFences) {
Melody Hsu793f8362024-01-08 20:00:35 +0000494 EXPECT_CALL(*mLayer1FE, getReleaseFencePromiseStatus)
495 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED));
496 EXPECT_CALL(*mLayer2FE, getReleaseFencePromiseStatus)
497 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED));
498 EXPECT_CALL(*mLayer3FE, getReleaseFencePromiseStatus)
499 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::INITIALIZED));
500 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
501
502 EXPECT_CALL(*mLayer1FE, setReleaseFence(_)).Times(0);
503 EXPECT_CALL(*mLayer2FE, setReleaseFence(_)).Times(0);
504 EXPECT_CALL(*mLayer3FE, setReleaseFence(_)).Times(1);
505
506 mEngine.postComposition(mRefreshArgs);
507}
Lloyd Pique70d91362018-10-18 16:02:55 -0700508} // namespace
509} // namespace android::compositionengine