blob: 60ed660c7a24c799b27510072f88b06ec8d836b8 [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>
Lloyd Pique70d91362018-10-18 16:02:55 -070023#include <gtest/gtest.h>
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070024#include <renderengine/mock/RenderEngine.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070025
Lloyd Pique441d5042018-10-18 16:49:51 -070026#include "MockHWComposer.h"
Alec Mourie4034bb2019-11-19 12:45:54 -080027#include "TimeStats/TimeStats.h"
Lloyd Pique441d5042018-10-18 16:49:51 -070028
Lloyd Pique70d91362018-10-18 16:02:55 -070029namespace android::compositionengine {
30namespace {
31
Lloyd Piqueab039b52019-02-13 14:22:42 -080032using ::testing::_;
Haibo Huangf66b7522020-09-10 18:20:30 -070033using ::testing::DoAll;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080034using ::testing::InSequence;
35using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080036using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080037using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080038using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070039using ::testing::StrictMock;
40
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080041struct CompositionEngineTest : public testing::Test {
Alec Mourie4034bb2019-11-19 12:45:54 -080042 std::shared_ptr<TimeStats> mTimeStats;
43
Lloyd Pique441d5042018-10-18 16:49:51 -070044 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080045 CompositionRefreshArgs mRefreshArgs;
46
47 std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()};
48 std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()};
49 std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()};
Lloyd Pique70d91362018-10-18 16:02:55 -070050};
51
Lloyd Pique70d91362018-10-18 16:02:55 -070052TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
53 auto engine = impl::createCompositionEngine();
54 EXPECT_TRUE(engine.get() != nullptr);
55}
56
Lloyd Pique441d5042018-10-18 16:49:51 -070057TEST_F(CompositionEngineTest, canSetHWComposer) {
Ady Abrahameca9d752021-03-03 12:20:00 -080058 android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
59 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
Lloyd Pique441d5042018-10-18 16:49:51 -070060
Ady Abrahameca9d752021-03-03 12:20:00 -080061 EXPECT_EQ(hwc, &mEngine.getHwComposer());
Lloyd Pique441d5042018-10-18 16:49:51 -070062}
63
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070064TEST_F(CompositionEngineTest, canSetRenderEngine) {
Patrick Williams0a525a42022-10-26 20:20:50 +000065 auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
66 mEngine.setRenderEngine(renderEngine.get());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070067
Patrick Williams0a525a42022-10-26 20:20:50 +000068 EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070069}
70
Alec Mourie4034bb2019-11-19 12:45:54 -080071TEST_F(CompositionEngineTest, canSetTimeStats) {
72 mEngine.setTimeStats(mTimeStats);
73
Patrick Williams74c0bf62022-11-02 23:59:26 +000074 EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
Alec Mourie4034bb2019-11-19 12:45:54 -080075}
76
Lloyd Piqueab039b52019-02-13 14:22:42 -080077/*
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080078 * CompositionEngine::present
79 */
80
81struct CompositionEnginePresentTest : public CompositionEngineTest {
82 struct CompositionEnginePartialMock : public impl::CompositionEngine {
83 // These are the overridable functions CompositionEngine::present() may
84 // call, and have separate test coverage.
85 MOCK_METHOD1(preComposition, void(CompositionRefreshArgs&));
86 };
87
88 StrictMock<CompositionEnginePartialMock> mEngine;
89};
90
91TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
92 // present() always calls preComposition()
93 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
94
95 mEngine.present(mRefreshArgs);
96}
97
98TEST_F(CompositionEnginePresentTest, worksAsExpected) {
99 // Expect calls to in a certain sequence
100 InSequence seq;
101
102 // present() always calls preComposition()
103 EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
104
105 // The first step in presenting is to make sure all outputs are prepared.
106 EXPECT_CALL(*mOutput1, prepare(Ref(mRefreshArgs), _));
107 EXPECT_CALL(*mOutput2, prepare(Ref(mRefreshArgs), _));
108 EXPECT_CALL(*mOutput3, prepare(Ref(mRefreshArgs), _));
109
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800110 // The last step is to actually present each output.
111 EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs)));
112 EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs)));
113 EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs)));
114
115 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
116 mEngine.present(mRefreshArgs);
117}
118
119/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800120 * CompositionEngine::updateCursorAsync
121 */
122
123struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
124public:
Lloyd Piquede196652020-01-22 17:29:58 -0800125 struct Layer {
Ady Abrahameca9d752021-03-03 12:20:00 -0800126 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
Lloyd Piquede196652020-01-22 17:29:58 -0800127
128 StrictMock<mock::OutputLayer> outputLayer;
Ady Abrahameca9d752021-03-03 12:20:00 -0800129 sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piquede196652020-01-22 17:29:58 -0800130 LayerFECompositionState layerFEState;
131 };
132
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800133 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800134 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800135 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
136
Lloyd Pique0a456232020-01-16 17:51:13 -0800137 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800138 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800139 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800140
Lloyd Pique0a456232020-01-16 17:51:13 -0800141 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800142 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800143 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800144 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800145 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800146 }
147
Lloyd Piquede196652020-01-22 17:29:58 -0800148 Layer mOutput2Layer1;
149 Layer mOutput3Layer1;
150 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800151};
152
153TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
154 mEngine.updateCursorAsync(mRefreshArgs);
155}
156
157TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800158 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
159 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
160 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800161
162 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
163
164 mEngine.updateCursorAsync(mRefreshArgs);
165}
166
167TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
168 {
169 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800170 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800171 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800172 }
173
174 {
175 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800176 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800177 EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800178 }
179
180 {
181 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800182 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
Lloyd Piquede196652020-01-22 17:29:58 -0800183 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800184 }
185
186 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
187
188 mEngine.updateCursorAsync(mRefreshArgs);
189}
190
191/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800192 * CompositionEngine::preComposition
193 */
194
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800195struct CompositionTestPreComposition : public CompositionEngineTest {
Ady Abrahame0eafa82022-02-02 19:30:47 -0800196 sp<StrictMock<mock::LayerFE>> mLayer1FE = sp<StrictMock<mock::LayerFE>>::make();
197 sp<StrictMock<mock::LayerFE>> mLayer2FE = sp<StrictMock<mock::LayerFE>>::make();
198 sp<StrictMock<mock::LayerFE>> mLayer3FE = sp<StrictMock<mock::LayerFE>>::make();
Lloyd Piqueab039b52019-02-13 14:22:42 -0800199};
200
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800201TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800202 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800203 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800204 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
205
206 // The frame timestamp should be between the before and after timestamps
207 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
208 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
209}
210
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800211TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800212 nsecs_t ts1 = 0;
213 nsecs_t ts2 = 0;
214 nsecs_t ts3 = 0;
Vishnu Nairba354102022-08-01 00:14:18 +0000215 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _))
216 .WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
217 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _))
218 .WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
219 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _))
220 .WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800221
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800222 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800223 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800224
Lloyd Piqueab039b52019-02-13 14:22:42 -0800225 mEngine.preComposition(mRefreshArgs);
226
227 // Each of the onPreComposition calls should used the same refresh timestamp
228 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
229 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
230 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
231}
232
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800233TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Vishnu Nairba354102022-08-01 00:14:18 +0000234 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(false));
235 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
236 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800237
238 mEngine.setNeedsAnotherUpdateForTest(true);
239
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 // The call should have cleared the needsAnotherUpdate flag
246 EXPECT_FALSE(mEngine.needsAnotherUpdate());
247}
248
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800249TEST_F(CompositionTestPreComposition,
250 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Vishnu Nairba354102022-08-01 00:14:18 +0000251 EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(true));
252 EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
253 EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
Lloyd Piqueab039b52019-02-13 14:22:42 -0800254
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 EXPECT_TRUE(mEngine.needsAnotherUpdate());
261}
262
Lloyd Pique70d91362018-10-18 16:02:55 -0700263} // namespace
264} // namespace android::compositionengine