blob: a451ab2b77b8c276a1c62128310b6e6a2287d07a [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
Lloyd Piqueab039b52019-02-13 14:22:42 -080017#include <compositionengine/CompositionRefreshArgs.h>
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080018#include <compositionengine/LayerFECompositionState.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070019#include <compositionengine/impl/CompositionEngine.h>
Lloyd Piqueab039b52019-02-13 14:22:42 -080020#include <compositionengine/mock/LayerFE.h>
21#include <compositionengine/mock/Output.h>
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080022#include <compositionengine/mock/OutputLayer.h>
Leon Scroggins III2f60d732022-09-12 14:42:38 -040023#include <ftl/future.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070024#include <gtest/gtest.h>
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070025#include <renderengine/mock/RenderEngine.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070026
Lloyd Pique441d5042018-10-18 16:49:51 -070027#include "MockHWComposer.h"
Alec Mourie4034bb2019-11-19 12:45:54 -080028#include "TimeStats/TimeStats.h"
Lloyd Pique441d5042018-10-18 16:49:51 -070029
Leon Scroggins III2f60d732022-09-12 14:42:38 -040030#include <variant>
31
Lloyd Pique70d91362018-10-18 16:02:55 -070032namespace android::compositionengine {
33namespace {
34
Lloyd Piqueab039b52019-02-13 14:22:42 -080035using ::testing::_;
Haibo Huangf66b7522020-09-10 18:20:30 -070036using ::testing::DoAll;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080037using ::testing::InSequence;
38using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080039using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080040using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080041using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070042using ::testing::StrictMock;
43
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080044struct CompositionEngineTest : public testing::Test {
Alec Mourie4034bb2019-11-19 12:45:54 -080045 std::shared_ptr<TimeStats> mTimeStats;
46
Lloyd Pique441d5042018-10-18 16:49:51 -070047 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080048 CompositionRefreshArgs mRefreshArgs;
49
50 std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()};
51 std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()};
52 std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()};
Lloyd Pique70d91362018-10-18 16:02:55 -070053};
54
Lloyd Pique70d91362018-10-18 16:02:55 -070055TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
56 auto engine = impl::createCompositionEngine();
57 EXPECT_TRUE(engine.get() != nullptr);
58}
59
Lloyd Pique441d5042018-10-18 16:49:51 -070060TEST_F(CompositionEngineTest, canSetHWComposer) {
Ady Abrahameca9d752021-03-03 12:20:00 -080061 android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
62 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
Lloyd Pique441d5042018-10-18 16:49:51 -070063
Ady Abrahameca9d752021-03-03 12:20:00 -080064 EXPECT_EQ(hwc, &mEngine.getHwComposer());
Lloyd Pique441d5042018-10-18 16:49:51 -070065}
66
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070067TEST_F(CompositionEngineTest, canSetRenderEngine) {
Patrick Williams0a525a42022-10-26 20:20:50 +000068 auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
69 mEngine.setRenderEngine(renderEngine.get());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070070
Patrick Williams0a525a42022-10-26 20:20:50 +000071 EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070072}
73
Alec Mourie4034bb2019-11-19 12:45:54 -080074TEST_F(CompositionEngineTest, canSetTimeStats) {
75 mEngine.setTimeStats(mTimeStats);
76
Patrick Williams74c0bf62022-11-02 23:59:26 +000077 EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
Alec Mourie4034bb2019-11-19 12:45:54 -080078}
79
Lloyd Piqueab039b52019-02-13 14:22:42 -080080/*
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080081 * CompositionEngine::present
82 */
83
84struct CompositionEnginePresentTest : public CompositionEngineTest {
85 struct CompositionEnginePartialMock : public impl::CompositionEngine {
86 // These are the overridable functions CompositionEngine::present() may
87 // call, and have separate test coverage.
88 MOCK_METHOD1(preComposition, void(CompositionRefreshArgs&));
89 };
90
91 StrictMock<CompositionEnginePartialMock> mEngine;
92};
93
94TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
95 // present() always calls preComposition()
96 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
97
98 mEngine.present(mRefreshArgs);
99}
100
101TEST_F(CompositionEnginePresentTest, worksAsExpected) {
102 // Expect calls to in a certain sequence
103 InSequence seq;
104
105 // present() always calls preComposition()
106 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
107
108 // The first step in presenting is to make sure all outputs are prepared.
109 EXPECT_CALL(*mOutput1, prepare(Ref(mRefreshArgs), _));
110 EXPECT_CALL(*mOutput2, prepare(Ref(mRefreshArgs), _));
111 EXPECT_CALL(*mOutput3, prepare(Ref(mRefreshArgs), _));
112
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400113 if (FlagManager::getInstance().multithreaded_present()) {
114 EXPECT_CALL(*mOutput1, getDisplayId()).WillOnce(Return(std::nullopt));
115 EXPECT_CALL(*mOutput2, getDisplayId()).WillOnce(Return(std::nullopt));
116 EXPECT_CALL(*mOutput3, getDisplayId()).WillOnce(Return(std::nullopt));
117 }
118
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800119 // The last step is to actually present each output.
Leon Scroggins III2f60d732022-09-12 14:42:38 -0400120 EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs)))
121 .WillOnce(Return(ftl::yield<std::monostate>({})));
122 EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs)))
123 .WillOnce(Return(ftl::yield<std::monostate>({})));
124 EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs)))
125 .WillOnce(Return(ftl::yield<std::monostate>({})));
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800126
127 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
128 mEngine.present(mRefreshArgs);
129}
130
131/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800132 * CompositionEngine::updateCursorAsync
133 */
134
135struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
136public:
Lloyd Piquede196652020-01-22 17:29:58 -0800137 struct Layer {
Ady Abrahameca9d752021-03-03 12:20:00 -0800138 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
Lloyd Piquede196652020-01-22 17:29:58 -0800139
140 StrictMock<mock::OutputLayer> outputLayer;
Ady Abrahameca9d752021-03-03 12:20:00 -0800141 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -0800142 LayerFECompositionState layerFEState;
143 };
144
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800145 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800146 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800147 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
148
Lloyd Pique0a456232020-01-16 17:51:13 -0800149 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800150 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800151 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800152
Lloyd Pique0a456232020-01-16 17:51:13 -0800153 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800154 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800155 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800156 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800157 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800158 }
159
Lloyd Piquede196652020-01-22 17:29:58 -0800160 Layer mOutput2Layer1;
161 Layer mOutput3Layer1;
162 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800163};
164
165TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
166 mEngine.updateCursorAsync(mRefreshArgs);
167}
168
169TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800170 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
171 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
172 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800173
174 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
175
176 mEngine.updateCursorAsync(mRefreshArgs);
177}
178
179TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
180 {
181 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800182 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800183 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800184 }
185
186 {
187 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800188 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800189 EXPECT_CALL(mOutput3Layer1.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(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800195 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800196 }
197
198 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
199
200 mEngine.updateCursorAsync(mRefreshArgs);
201}
202
203/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800204 * CompositionEngine::preComposition
205 */
206
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800207struct CompositionTestPreComposition : public CompositionEngineTest {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800208 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
209 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
210 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piqueab039b52019-02-13 14:22:42 -0800211};
212
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800213TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800214 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800215 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800216 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
217
218 // The frame timestamp should be between the before and after timestamps
219 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
220 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
221}
222
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800223TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800224 nsecs_t ts1 = 0;
225 nsecs_t ts2 = 0;
226 nsecs_t ts3 = 0;
Vishnu Nairba354102022-08-01 00:14:18 +0000227 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _))
228 .WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
229 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _))
230 .WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
231 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _))
232 .WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800233
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800234 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800235 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800236
Lloyd Piqueab039b52019-02-13 14:22:42 -0800237 mEngine.preComposition(mRefreshArgs);
238
239 // Each of the onPreComposition calls should used the same refresh timestamp
240 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
241 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
242 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
243}
244
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800245TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Vishnu Nairba354102022-08-01 00:14:18 +0000246 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(false));
247 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
248 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800249
250 mEngine.setNeedsAnotherUpdateForTest(true);
251
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800252 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800253 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800254
Lloyd Piqueab039b52019-02-13 14:22:42 -0800255 mEngine.preComposition(mRefreshArgs);
256
257 // The call should have cleared the needsAnotherUpdate flag
258 EXPECT_FALSE(mEngine.needsAnotherUpdate());
259}
260
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800261TEST_F(CompositionTestPreComposition,
262 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Vishnu Nairba354102022-08-01 00:14:18 +0000263 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(true));
264 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
265 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800266
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800267 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800268 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800269
Lloyd Piqueab039b52019-02-13 14:22:42 -0800270 mEngine.preComposition(mRefreshArgs);
271
272 EXPECT_TRUE(mEngine.needsAnotherUpdate());
273}
274
Lloyd Pique70d91362018-10-18 16:02:55 -0700275} // namespace
276} // namespace android::compositionengine