blob: 48ebc32eb8c4c99f34225bfb5cbff4266cd91d0a [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 IIIab551a32023-11-22 10:08:25 -050033using namespace com::android::graphics::surfaceflinger;
34
Lloyd Pique70d91362018-10-18 16:02:55 -070035namespace android::compositionengine {
36namespace {
37
Lloyd Piqueab039b52019-02-13 14:22:42 -080038using ::testing::_;
Haibo Huangf66b7522020-09-10 18:20:30 -070039using ::testing::DoAll;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080040using ::testing::InSequence;
41using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080042using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080043using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080044using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070045using ::testing::StrictMock;
46
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080047struct CompositionEngineTest : public testing::Test {
Alec Mourie4034bb2019-11-19 12:45:54 -080048 std::shared_ptr<TimeStats> mTimeStats;
49
Lloyd Pique441d5042018-10-18 16:49:51 -070050 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080051 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 Pique70d91362018-10-18 16:02:55 -070056};
57
Lloyd Pique70d91362018-10-18 16:02:55 -070058TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
59 auto engine = impl::createCompositionEngine();
60 EXPECT_TRUE(engine.get() != nullptr);
61}
62
Lloyd Pique441d5042018-10-18 16:49:51 -070063TEST_F(CompositionEngineTest, canSetHWComposer) {
Ady Abrahameca9d752021-03-03 12:20:00 -080064 android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
65 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
Lloyd Pique441d5042018-10-18 16:49:51 -070066
Ady Abrahameca9d752021-03-03 12:20:00 -080067 EXPECT_EQ(hwc, &mEngine.getHwComposer());
Lloyd Pique441d5042018-10-18 16:49:51 -070068}
69
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070070TEST_F(CompositionEngineTest, canSetRenderEngine) {
Patrick Williams0a525a42022-10-26 20:20:50 +000071 auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
72 mEngine.setRenderEngine(renderEngine.get());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070073
Patrick Williams0a525a42022-10-26 20:20:50 +000074 EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070075}
76
Alec Mourie4034bb2019-11-19 12:45:54 -080077TEST_F(CompositionEngineTest, canSetTimeStats) {
78 mEngine.setTimeStats(mTimeStats);
79
Patrick Williams74c0bf62022-11-02 23:59:26 +000080 EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
Alec Mourie4034bb2019-11-19 12:45:54 -080081}
82
Lloyd Piqueab039b52019-02-13 14:22:42 -080083/*
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080084 * CompositionEngine::present
85 */
86
87struct 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 Hsu793f8362024-01-08 20:00:35 +000092 MOCK_METHOD1(postComposition, void(CompositionRefreshArgs&));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080093 };
94
95 StrictMock<CompositionEnginePartialMock> mEngine;
96};
97
98TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
Melody Hsu793f8362024-01-08 20:00:35 +000099 // present() always calls preComposition() and postComposition()
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800100 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
Melody Hsu793f8362024-01-08 20:00:35 +0000101 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800102
103 mEngine.present(mRefreshArgs);
104}
105
106TEST_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 IIIab551a32023-11-22 10:08:25 -0500118 // 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 III2f60d732022-09-12 14:42:38 -0400121
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800122 // The last step is to actually present each output.
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400123 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 Piquee82ed2d2019-11-13 19:28:12 -0800129
Melody Hsu793f8362024-01-08 20:00:35 +0000130 // present() always calls postComposition()
131 EXPECT_CALL(mEngine, postComposition(Ref(mRefreshArgs)));
132
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800133 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
134 mEngine.present(mRefreshArgs);
135}
136
137/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800138 * CompositionEngine::updateCursorAsync
139 */
140
141struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
142public:
Lloyd Piquede196652020-01-22 17:29:58 -0800143 struct Layer {
Ady Abrahameca9d752021-03-03 12:20:00 -0800144 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
Lloyd Piquede196652020-01-22 17:29:58 -0800145
146 StrictMock<mock::OutputLayer> outputLayer;
Ady Abrahameca9d752021-03-03 12:20:00 -0800147 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -0800148 LayerFECompositionState layerFEState;
149 };
150
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800151 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800152 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800153 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
154
Lloyd Pique0a456232020-01-16 17:51:13 -0800155 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800156 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800157 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800158
Lloyd Pique0a456232020-01-16 17:51:13 -0800159 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800160 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800161 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800162 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800163 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800164 }
165
Lloyd Piquede196652020-01-22 17:29:58 -0800166 Layer mOutput2Layer1;
167 Layer mOutput3Layer1;
168 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800169};
170
171TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
172 mEngine.updateCursorAsync(mRefreshArgs);
173}
174
175TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800176 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 Piqueaed56ab2019-11-13 22:34:11 -0800179
180 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
181
182 mEngine.updateCursorAsync(mRefreshArgs);
183}
184
185TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
186 {
187 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800188 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800189 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800190 }
191
192 {
193 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800194 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800195 EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800196 }
197
198 {
199 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800200 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800201 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800202 }
203
204 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
205
206 mEngine.updateCursorAsync(mRefreshArgs);
207}
208
209/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800210 * CompositionEngine::preComposition
211 */
212
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800213struct CompositionTestPreComposition : public CompositionEngineTest {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800214 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 Piqueab039b52019-02-13 14:22:42 -0800217};
218
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800219TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800220 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Melody Hsuc949cde2024-03-12 01:43:34 +0000221 mRefreshArgs.refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800222 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800223 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 Piquee82ed2d2019-11-13 19:28:12 -0800230TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800231 nsecs_t ts1 = 0;
232 nsecs_t ts2 = 0;
233 nsecs_t ts3 = 0;
Melody Hsuc949cde2024-03-12 01:43:34 +0000234 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 Piqueab039b52019-02-13 14:22:42 -0800237
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800238 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800239 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800240
Lloyd Piqueab039b52019-02-13 14:22:42 -0800241 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 Piquee82ed2d2019-11-13 19:28:12 -0800249TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000250 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false));
251 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
252 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800253
254 mEngine.setNeedsAnotherUpdateForTest(true);
255
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800256 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800257 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800258
Lloyd Piqueab039b52019-02-13 14:22:42 -0800259 mEngine.preComposition(mRefreshArgs);
260
261 // The call should have cleared the needsAnotherUpdate flag
262 EXPECT_FALSE(mEngine.needsAnotherUpdate());
263}
264
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800265TEST_F(CompositionTestPreComposition,
266 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Melody Hsuc949cde2024-03-12 01:43:34 +0000267 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true));
268 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
269 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800270
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800271 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800272 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800273
Lloyd Piqueab039b52019-02-13 14:22:42 -0800274 mEngine.preComposition(mRefreshArgs);
275
276 EXPECT_TRUE(mEngine.needsAnotherUpdate());
277}
278
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500279struct 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 IIIcbc929d2023-12-01 16:21:37 -0500293 std::array<impl::OutputCompositionState, 4> mOutputStates;
294
Leon Scroggins IIIab551a32023-11-22 10:08:25 -0500295 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 IIIcbc929d2023-12-01 16:21:37 -0500304
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 IIIab551a32023-11-22 10:08:25 -0500314 }
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
329TEST_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
342TEST_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
355TEST_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
368TEST_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
381TEST_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
392TEST_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
407TEST_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
420TEST_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
433TEST_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 IIIcbc929d2023-12-01 16:21:37 -0500450TEST_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
468TEST_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 Hsu793f8362024-01-08 20:00:35 +0000488struct 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
494TEST_F(CompositionEnginePostCompositionTest, postCompositionReleasesAllFences) {
Melody Hsu793f8362024-01-08 20:00:35 +0000495 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 Pique70d91362018-10-18 16:02:55 -0700509} // namespace
510} // namespace android::compositionengine