blob: 639164d8c943da1b550049765801f7c9bb5fcc99 [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
Lloyd Pique441d5042018-10-18 16:49:51 -070029#include "MockHWComposer.h"
Alec Mourie4034bb2019-11-19 12:45:54 -080030#include "TimeStats/TimeStats.h"
Melody Hsu793f8362024-01-08 20:00:35 +000031#include "gmock/gmock.h"
Lloyd Pique441d5042018-10-18 16:49:51 -070032
Leon Scroggins III2f60d732022-09-12 14:42:38 -040033#include <variant>
34
Leon Scroggins IIIab551a32023-11-22 10:08:25 -050035using namespace com::android::graphics::surfaceflinger;
36
Lloyd Pique70d91362018-10-18 16:02:55 -070037namespace android::compositionengine {
38namespace {
39
Lloyd Piqueab039b52019-02-13 14:22:42 -080040using ::testing::_;
Haibo Huangf66b7522020-09-10 18:20:30 -070041using ::testing::DoAll;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080042using ::testing::InSequence;
43using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080044using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080045using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080046using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070047using ::testing::StrictMock;
48
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080049struct CompositionEngineTest : public testing::Test {
Alec Mourie4034bb2019-11-19 12:45:54 -080050 std::shared_ptr<TimeStats> mTimeStats;
51
Lloyd Pique441d5042018-10-18 16:49:51 -070052 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080053 CompositionRefreshArgs mRefreshArgs;
54
55 std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()};
56 std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()};
57 std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()};
Lloyd Pique70d91362018-10-18 16:02:55 -070058};
59
Lloyd Pique70d91362018-10-18 16:02:55 -070060TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
61 auto engine = impl::createCompositionEngine();
62 EXPECT_TRUE(engine.get() != nullptr);
63}
64
Lloyd Pique441d5042018-10-18 16:49:51 -070065TEST_F(CompositionEngineTest, canSetHWComposer) {
Ady Abrahameca9d752021-03-03 12:20:00 -080066 android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
67 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
Lloyd Pique441d5042018-10-18 16:49:51 -070068
Ady Abrahameca9d752021-03-03 12:20:00 -080069 EXPECT_EQ(hwc, &mEngine.getHwComposer());
Lloyd Pique441d5042018-10-18 16:49:51 -070070}
71
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070072TEST_F(CompositionEngineTest, canSetRenderEngine) {
Patrick Williams0a525a42022-10-26 20:20:50 +000073 auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
74 mEngine.setRenderEngine(renderEngine.get());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070075
Patrick Williams0a525a42022-10-26 20:20:50 +000076 EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070077}
78
Alec Mourie4034bb2019-11-19 12:45:54 -080079TEST_F(CompositionEngineTest, canSetTimeStats) {
80 mEngine.setTimeStats(mTimeStats);
81
Patrick Williams74c0bf62022-11-02 23:59:26 +000082 EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
Alec Mourie4034bb2019-11-19 12:45:54 -080083}
84
Lloyd Piqueab039b52019-02-13 14:22:42 -080085/*
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080086 * CompositionEngine::present
87 */
88
89struct CompositionEnginePresentTest : public CompositionEngineTest {
90 struct CompositionEnginePartialMock : public impl::CompositionEngine {
91 // These are the overridable functions CompositionEngine::present() may
92 // call, and have separate test coverage.
93 MOCK_METHOD1(preComposition, void(CompositionRefreshArgs&));
Melody Hsu793f8362024-01-08 20:00:35 +000094 MOCK_METHOD1(postComposition, void(CompositionRefreshArgs&));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080095 };
96
97 StrictMock<CompositionEnginePartialMock> mEngine;
98};
99
100TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
Melody Hsu793f8362024-01-08 20:00:35 +0000101 // present() always calls preComposition() and postComposition()
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800102 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
Melody Hsu793f8362024-01-08 20:00:35 +0000103 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800104
105 mEngine.present(mRefreshArgs);
106}
107
108TEST_F(CompositionEnginePresentTest, worksAsExpected) {
109 // Expect calls to in a certain sequence
110 InSequence seq;
111
112 // present() always calls preComposition()
113 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
114
115 // The first step in presenting is to make sure all outputs are prepared.
116 EXPECT_CALL(*mOutput1, prepare(Ref(mRefreshArgs), _));
117 EXPECT_CALL(*mOutput2, prepare(Ref(mRefreshArgs), _));
118 EXPECT_CALL(*mOutput3, prepare(Ref(mRefreshArgs), _));
119
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500120 // All of mOutput<i> are StrictMocks. If the flag is true, it will introduce
121 // calls to getDisplayId, which are not relevant to this test.
122 SET_FLAG_FOR_TEST(flags::multithreaded_present, false);
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400123
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800124 // The last step is to actually present each output.
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400125 EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs)))
126 .WillOnce(Return(ftl::yield<std::monostate>({})));
127 EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs)))
128 .WillOnce(Return(ftl::yield<std::monostate>({})));
129 EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs)))
130 .WillOnce(Return(ftl::yield<std::monostate>({})));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800131
Melody Hsu793f8362024-01-08 20:00:35 +0000132 // present() always calls postComposition()
133 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
134
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800135 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
136 mEngine.present(mRefreshArgs);
137}
138
139/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800140 * CompositionEngine::updateCursorAsync
141 */
142
143struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
144public:
Lloyd Piquede196652020-01-22 17:29:58 -0800145 struct Layer {
Ady Abrahameca9d752021-03-03 12:20:00 -0800146 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
Lloyd Piquede196652020-01-22 17:29:58 -0800147
148 StrictMock<mock::OutputLayer> outputLayer;
Ady Abrahameca9d752021-03-03 12:20:00 -0800149 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -0800150 LayerFECompositionState layerFEState;
151 };
152
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800153 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800154 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800155 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
156
Lloyd Pique0a456232020-01-16 17:51:13 -0800157 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800158 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800159 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800160
Lloyd Pique0a456232020-01-16 17:51:13 -0800161 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800162 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800163 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800164 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800165 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800166 }
167
Lloyd Piquede196652020-01-22 17:29:58 -0800168 Layer mOutput2Layer1;
169 Layer mOutput3Layer1;
170 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800171};
172
173TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
174 mEngine.updateCursorAsync(mRefreshArgs);
175}
176
177TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800178 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
179 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
180 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800181
182 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
183
184 mEngine.updateCursorAsync(mRefreshArgs);
185}
186
187TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
188 {
189 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800190 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800191 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800192 }
193
194 {
195 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800196 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800197 EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800198 }
199
200 {
201 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800202 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800203 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800204 }
205
206 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
207
208 mEngine.updateCursorAsync(mRefreshArgs);
209}
210
211/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800212 * CompositionEngine::preComposition
213 */
214
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800215struct CompositionTestPreComposition : public CompositionEngineTest {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800216 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
217 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
218 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piqueab039b52019-02-13 14:22:42 -0800219};
220
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800221TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800222 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Melody Hsuc949cde2024-03-12 01:43:34 +0000223 mRefreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800224 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800225 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
226
227 // The frame timestamp should be between the before and after timestamps
228 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
229 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
230}
231
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800232TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800233 nsecs_t ts1 = 0;
234 nsecs_t ts2 = 0;
235 nsecs_t ts3 = 0;
Melody Hsuc949cde2024-03-12 01:43:34 +0000236 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
237 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
238 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800239
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800240 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800241 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800242
Lloyd Piqueab039b52019-02-13 14:22:42 -0800243 mEngine.preComposition(mRefreshArgs);
244
245 // Each of the onPreComposition calls should used the same refresh timestamp
246 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
247 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
248 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
249}
250
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800251TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000252 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false));
253 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
254 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800255
256 mEngine.setNeedsAnotherUpdateForTest(true);
257
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800258 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800259 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800260
Lloyd Piqueab039b52019-02-13 14:22:42 -0800261 mEngine.preComposition(mRefreshArgs);
262
263 // The call should have cleared the needsAnotherUpdate flag
264 EXPECT_FALSE(mEngine.needsAnotherUpdate());
265}
266
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800267TEST_F(CompositionTestPreComposition,
268 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000269 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true));
270 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
271 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800272
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800273 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800274 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800275
Lloyd Piqueab039b52019-02-13 14:22:42 -0800276 mEngine.preComposition(mRefreshArgs);
277
278 EXPECT_TRUE(mEngine.needsAnotherUpdate());
279}
280
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500281struct CompositionEngineOffloadTest : public testing::Test {
282 impl::CompositionEngine mEngine;
283 CompositionRefreshArgs mRefreshArgs;
284
285 std::shared_ptr<mock::Output> mDisplay1{std::make_shared<StrictMock<mock::Output>>()};
286 std::shared_ptr<mock::Output> mDisplay2{std::make_shared<StrictMock<mock::Output>>()};
287 std::shared_ptr<mock::Output> mVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()};
288 std::shared_ptr<mock::Output> mHalVirtualDisplay{std::make_shared<StrictMock<mock::Output>>()};
289
290 static constexpr PhysicalDisplayId kDisplayId1 = PhysicalDisplayId::fromPort(123u);
291 static constexpr PhysicalDisplayId kDisplayId2 = PhysicalDisplayId::fromPort(234u);
292 static constexpr GpuVirtualDisplayId kGpuVirtualDisplayId{789u};
293 static constexpr HalVirtualDisplayId kHalVirtualDisplayId{456u};
294
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500295 std::array<impl::OutputCompositionState, 4> mOutputStates;
296
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500297 void SetUp() override {
298 EXPECT_CALL(*mDisplay1, getDisplayId)
299 .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId1)));
300 EXPECT_CALL(*mDisplay2, getDisplayId)
301 .WillRepeatedly(Return(std::make_optional<DisplayId>(kDisplayId2)));
302 EXPECT_CALL(*mVirtualDisplay, getDisplayId)
303 .WillRepeatedly(Return(std::make_optional<DisplayId>(kGpuVirtualDisplayId)));
304 EXPECT_CALL(*mHalVirtualDisplay, getDisplayId)
305 .WillRepeatedly(Return(std::make_optional<DisplayId>(kHalVirtualDisplayId)));
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500306
307 // Most tests will depend on the outputs being enabled.
308 for (auto& state : mOutputStates) {
309 state.isEnabled = true;
310 }
311
312 EXPECT_CALL(*mDisplay1, getState).WillRepeatedly(ReturnRef(mOutputStates[0]));
313 EXPECT_CALL(*mDisplay2, getState).WillRepeatedly(ReturnRef(mOutputStates[1]));
314 EXPECT_CALL(*mVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[2]));
315 EXPECT_CALL(*mHalVirtualDisplay, getState).WillRepeatedly(ReturnRef(mOutputStates[3]));
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500316 }
317
318 void setOutputs(std::initializer_list<std::shared_ptr<mock::Output>> outputs) {
319 for (auto& output : outputs) {
320 // If we call mEngine.present, prepare and present will be called on all the
321 // outputs in mRefreshArgs, but that's not the interesting part of the test.
322 EXPECT_CALL(*output, prepare(Ref(mRefreshArgs), _)).Times(1);
323 EXPECT_CALL(*output, present(Ref(mRefreshArgs)))
324 .WillOnce(Return(ftl::yield<std::monostate>({})));
325
326 mRefreshArgs.outputs.push_back(std::move(output));
327 }
328 }
329};
330
331TEST_F(CompositionEngineOffloadTest, basic) {
332 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
333 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
334
335 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
336 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
337
338 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
339 setOutputs({mDisplay1, mDisplay2});
340
341 mEngine.present(mRefreshArgs);
342}
343
344TEST_F(CompositionEngineOffloadTest, dependsOnSupport) {
345 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(false));
346 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0);
347
348 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
349 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
350
351 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
352 setOutputs({mDisplay1, mDisplay2});
353
354 mEngine.present(mRefreshArgs);
355}
356
357TEST_F(CompositionEngineOffloadTest, dependsOnSupport2) {
358 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
359 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(false));
360
361 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
362 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
363
364 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
365 setOutputs({mDisplay1, mDisplay2});
366
367 mEngine.present(mRefreshArgs);
368}
369
370TEST_F(CompositionEngineOffloadTest, dependsOnFlag) {
371 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0);
372 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).Times(0);
373
374 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
375 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
376
377 SET_FLAG_FOR_TEST(flags::multithreaded_present, false);
378 setOutputs({mDisplay1, mDisplay2});
379
380 mEngine.present(mRefreshArgs);
381}
382
383TEST_F(CompositionEngineOffloadTest, oneDisplay) {
384 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).Times(0);
385
386 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
387
388 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
389 setOutputs({mDisplay1});
390
391 mEngine.present(mRefreshArgs);
392}
393
394TEST_F(CompositionEngineOffloadTest, virtualDisplay) {
395 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
396 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
397 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
398
399 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
400 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
401 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
402
403 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
404 setOutputs({mDisplay1, mDisplay2, mVirtualDisplay});
405
406 mEngine.present(mRefreshArgs);
407}
408
409TEST_F(CompositionEngineOffloadTest, virtualDisplay2) {
410 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
411 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
412
413 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
414 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
415
416 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
417 setOutputs({mDisplay1, mVirtualDisplay});
418
419 mEngine.present(mRefreshArgs);
420}
421
422TEST_F(CompositionEngineOffloadTest, halVirtual) {
423 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
424 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
425
426 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
427 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0);
428
429 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
430 setOutputs({mDisplay1, mHalVirtualDisplay});
431
432 mEngine.present(mRefreshArgs);
433}
434
435TEST_F(CompositionEngineOffloadTest, ordering) {
436 EXPECT_CALL(*mVirtualDisplay, supportsOffloadPresent).Times(0);
437 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
438 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
439 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillOnce(Return(true));
440
441 EXPECT_CALL(*mVirtualDisplay, offloadPresentNextFrame).Times(0);
442 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(1);
443 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
444 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
445
446 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
447 setOutputs({mVirtualDisplay, mHalVirtualDisplay, mDisplay1, mDisplay2});
448
449 mEngine.present(mRefreshArgs);
450}
451
Leon Scroggins IIIcbc929d2023-12-01 16:21:37 -0500452TEST_F(CompositionEngineOffloadTest, dependsOnEnabled) {
453 // Disable mDisplay2.
454 mOutputStates[1].isEnabled = false;
455 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
456
457 // This is not actually called, because it is not enabled, but this distinguishes
458 // from the case where it did not return true.
459 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true));
460
461 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(0);
462 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
463
464 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
465 setOutputs({mDisplay1, mDisplay2});
466
467 mEngine.present(mRefreshArgs);
468}
469
470TEST_F(CompositionEngineOffloadTest, disabledDisplaysDoNotPreventOthersFromOffloading) {
471 // Disable mDisplay2.
472 mOutputStates[1].isEnabled = false;
473 EXPECT_CALL(*mDisplay1, supportsOffloadPresent).WillOnce(Return(true));
474
475 // This is not actually called, because it is not enabled, but this distinguishes
476 // from the case where it did not return true.
477 EXPECT_CALL(*mDisplay2, supportsOffloadPresent).WillRepeatedly(Return(true));
478 EXPECT_CALL(*mHalVirtualDisplay, supportsOffloadPresent).WillOnce(Return(true));
479
480 EXPECT_CALL(*mDisplay1, offloadPresentNextFrame).Times(1);
481 EXPECT_CALL(*mDisplay2, offloadPresentNextFrame).Times(0);
482 EXPECT_CALL(*mHalVirtualDisplay, offloadPresentNextFrame).Times(0);
483
484 SET_FLAG_FOR_TEST(flags::multithreaded_present, true);
485 setOutputs({mDisplay1, mDisplay2, mHalVirtualDisplay});
486
487 mEngine.present(mRefreshArgs);
488}
489
Melody Hsu793f8362024-01-08 20:00:35 +0000490struct CompositionEnginePostCompositionTest : public CompositionEngineTest {
491 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
492 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
493 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
494};
495
496TEST_F(CompositionEnginePostCompositionTest, postCompositionReleasesAllFences) {
497 SET_FLAG_FOR_TEST(com::android::graphics::surfaceflinger::flags::ce_fence_promise, true);
498 ASSERT_TRUE(FlagManager::getInstance().ce_fence_promise());
499
500 EXPECT_CALL(*mLayer1FE, getReleaseFencePromiseStatus)
501 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED));
502 EXPECT_CALL(*mLayer2FE, getReleaseFencePromiseStatus)
503 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::FULFILLED));
504 EXPECT_CALL(*mLayer3FE, getReleaseFencePromiseStatus)
505 .WillOnce(Return(LayerFE::ReleaseFencePromiseStatus::INITIALIZED));
506 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
507
508 EXPECT_CALL(*mLayer1FE, setReleaseFence(_)).Times(0);
509 EXPECT_CALL(*mLayer2FE, setReleaseFence(_)).Times(0);
510 EXPECT_CALL(*mLayer3FE, setReleaseFence(_)).Times(1);
511
512 mEngine.postComposition(mRefreshArgs);
513}
Lloyd Pique70d91362018-10-18 16:02:55 -0700514} // namespace
515} // namespace android::compositionengine