blob: d889d74f285b898d4614a0359bb6aadb0dec0c46 [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::_;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080033using ::testing::InSequence;
34using ::testing::Ref;
Lloyd Piqueab039b52019-02-13 14:22:42 -080035using ::testing::Return;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -080036using ::testing::ReturnRef;
Lloyd Piqueab039b52019-02-13 14:22:42 -080037using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070038using ::testing::StrictMock;
39
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080040struct CompositionEngineTest : public testing::Test {
Lloyd Piqueab039b52019-02-13 14:22:42 -080041 android::mock::HWComposer* mHwc = new StrictMock<android::mock::HWComposer>();
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070042 renderengine::mock::RenderEngine* mRenderEngine =
43 new StrictMock<renderengine::mock::RenderEngine>();
Alec Mourie4034bb2019-11-19 12:45:54 -080044 std::shared_ptr<TimeStats> mTimeStats;
45
Lloyd Pique441d5042018-10-18 16:49:51 -070046 impl::CompositionEngine mEngine;
Lloyd Piquee82ed2d2019-11-13 19:28:12 -080047 CompositionRefreshArgs mRefreshArgs;
48
49 std::shared_ptr<mock::Output> mOutput1{std::make_shared<StrictMock<mock::Output>>()};
50 std::shared_ptr<mock::Output> mOutput2{std::make_shared<StrictMock<mock::Output>>()};
51 std::shared_ptr<mock::Output> mOutput3{std::make_shared<StrictMock<mock::Output>>()};
Lloyd Pique70d91362018-10-18 16:02:55 -070052};
53
Lloyd Pique70d91362018-10-18 16:02:55 -070054TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
55 auto engine = impl::createCompositionEngine();
56 EXPECT_TRUE(engine.get() != nullptr);
57}
58
Lloyd Pique441d5042018-10-18 16:49:51 -070059TEST_F(CompositionEngineTest, canSetHWComposer) {
60 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(mHwc));
61
62 EXPECT_EQ(mHwc, &mEngine.getHwComposer());
63}
64
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070065TEST_F(CompositionEngineTest, canSetRenderEngine) {
66 mEngine.setRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
67
68 EXPECT_EQ(mRenderEngine, &mEngine.getRenderEngine());
69}
70
Alec Mourie4034bb2019-11-19 12:45:54 -080071TEST_F(CompositionEngineTest, canSetTimeStats) {
72 mEngine.setTimeStats(mTimeStats);
73
74 EXPECT_EQ(mTimeStats.get(), &mEngine.getTimeStats());
75}
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
110 // The next step in presenting is to make sure all outputs have the latest
111 // state from the front-end (SurfaceFlinger).
112 EXPECT_CALL(*mOutput1, updateLayerStateFromFE(Ref(mRefreshArgs)));
113 EXPECT_CALL(*mOutput2, updateLayerStateFromFE(Ref(mRefreshArgs)));
114 EXPECT_CALL(*mOutput3, updateLayerStateFromFE(Ref(mRefreshArgs)));
115
116 // The last step is to actually present each output.
117 EXPECT_CALL(*mOutput1, present(Ref(mRefreshArgs)));
118 EXPECT_CALL(*mOutput2, present(Ref(mRefreshArgs)));
119 EXPECT_CALL(*mOutput3, present(Ref(mRefreshArgs)));
120
121 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
122 mEngine.present(mRefreshArgs);
123}
124
125/*
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800126 * CompositionEngine::updateCursorAsync
127 */
128
129struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
130public:
Lloyd Piquede196652020-01-22 17:29:58 -0800131 struct Layer {
132 Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(layerFE)); }
133
134 StrictMock<mock::OutputLayer> outputLayer;
135 StrictMock<mock::LayerFE> layerFE;
136 LayerFECompositionState layerFEState;
137 };
138
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800139 CompositionEngineUpdateCursorAsyncTest() {
Lloyd Pique0a456232020-01-16 17:51:13 -0800140 EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800141 EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
142
Lloyd Pique0a456232020-01-16 17:51:13 -0800143 EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800144 EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800145 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800146
Lloyd Pique0a456232020-01-16 17:51:13 -0800147 EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800148 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
Lloyd Piquede196652020-01-22 17:29:58 -0800149 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800150 EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
Lloyd Piquede196652020-01-22 17:29:58 -0800151 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800152 }
153
Lloyd Piquede196652020-01-22 17:29:58 -0800154 Layer mOutput2Layer1;
155 Layer mOutput3Layer1;
156 Layer mOutput3Layer2;
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800157};
158
159TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
160 mEngine.updateCursorAsync(mRefreshArgs);
161}
162
163TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
Lloyd Piquede196652020-01-22 17:29:58 -0800164 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
165 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
166 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(false));
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800167
168 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
169
170 mEngine.updateCursorAsync(mRefreshArgs);
171}
172
173TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
174 {
175 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800176 EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
177 EXPECT_CALL(mOutput2Layer1.layerFE, prepareCompositionState(LayerFE::StateSubset::Cursor));
178 EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800179 }
180
181 {
182 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800183 EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
184 EXPECT_CALL(mOutput3Layer1.layerFE, prepareCompositionState(LayerFE::StateSubset::Cursor));
185 EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800186 }
187
188 {
189 InSequence seq;
Lloyd Piquede196652020-01-22 17:29:58 -0800190 EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
191 EXPECT_CALL(mOutput3Layer2.layerFE, prepareCompositionState(LayerFE::StateSubset::Cursor));
192 EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
Lloyd Piqueaed56ab2019-11-13 22:34:11 -0800193 }
194
195 mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
196
197 mEngine.updateCursorAsync(mRefreshArgs);
198}
199
200/*
Lloyd Piqueab039b52019-02-13 14:22:42 -0800201 * CompositionEngine::preComposition
202 */
203
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800204struct CompositionTestPreComposition : public CompositionEngineTest {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800205 sp<StrictMock<mock::LayerFE>> mLayer1FE{new StrictMock<mock::LayerFE>()};
206 sp<StrictMock<mock::LayerFE>> mLayer2FE{new StrictMock<mock::LayerFE>()};
207 sp<StrictMock<mock::LayerFE>> mLayer3FE{new StrictMock<mock::LayerFE>()};
Lloyd Piqueab039b52019-02-13 14:22:42 -0800208};
209
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800210TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800211 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800212 mEngine.preComposition(mRefreshArgs);
Lloyd Piqueab039b52019-02-13 14:22:42 -0800213 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
214
215 // The frame timestamp should be between the before and after timestamps
216 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
217 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
218}
219
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800220TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800221 nsecs_t ts1 = 0;
222 nsecs_t ts2 = 0;
223 nsecs_t ts3 = 0;
224 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
225 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
226 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
227
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800228 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800229 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800230
Lloyd Piqueab039b52019-02-13 14:22:42 -0800231 mEngine.preComposition(mRefreshArgs);
232
233 // Each of the onPreComposition calls should used the same refresh timestamp
234 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
235 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
236 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
237}
238
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800239TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800240 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false));
241 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
242 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
243
244 mEngine.setNeedsAnotherUpdateForTest(true);
245
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800246 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800247 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800248
Lloyd Piqueab039b52019-02-13 14:22:42 -0800249 mEngine.preComposition(mRefreshArgs);
250
251 // The call should have cleared the needsAnotherUpdate flag
252 EXPECT_FALSE(mEngine.needsAnotherUpdate());
253}
254
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800255TEST_F(CompositionTestPreComposition,
256 preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
Lloyd Piqueab039b52019-02-13 14:22:42 -0800257 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true));
258 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
259 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
260
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800261 mRefreshArgs.outputs = {mOutput1};
Lloyd Piquede196652020-01-22 17:29:58 -0800262 mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
Lloyd Piquee82ed2d2019-11-13 19:28:12 -0800263
Lloyd Piqueab039b52019-02-13 14:22:42 -0800264 mEngine.preComposition(mRefreshArgs);
265
266 EXPECT_TRUE(mEngine.needsAnotherUpdate());
267}
268
Lloyd Pique70d91362018-10-18 16:02:55 -0700269} // namespace
270} // namespace android::compositionengine