• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <compositionengine/CompositionRefreshArgs.h>
18 #include <compositionengine/LayerFECompositionState.h>
19 #include <compositionengine/impl/CompositionEngine.h>
20 #include <compositionengine/mock/LayerFE.h>
21 #include <compositionengine/mock/Output.h>
22 #include <compositionengine/mock/OutputLayer.h>
23 #include <gtest/gtest.h>
24 #include <renderengine/mock/RenderEngine.h>
25 
26 #include "MockHWComposer.h"
27 #include "TimeStats/TimeStats.h"
28 
29 namespace android::compositionengine {
30 namespace {
31 
32 using ::testing::_;
33 using ::testing::DoAll;
34 using ::testing::InSequence;
35 using ::testing::Ref;
36 using ::testing::Return;
37 using ::testing::ReturnRef;
38 using ::testing::SaveArg;
39 using ::testing::StrictMock;
40 
41 struct CompositionEngineTest : public testing::Test {
42     std::shared_ptr<TimeStats> mTimeStats;
43 
44     impl::CompositionEngine mEngine;
45     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>>()};
50 };
51 
TEST_F(CompositionEngineTest,canInstantiateCompositionEngine)52 TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
53     auto engine = impl::createCompositionEngine();
54     EXPECT_TRUE(engine.get() != nullptr);
55 }
56 
TEST_F(CompositionEngineTest,canSetHWComposer)57 TEST_F(CompositionEngineTest, canSetHWComposer) {
58     android::mock::HWComposer* hwc = new StrictMock<android::mock::HWComposer>();
59     mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(hwc));
60 
61     EXPECT_EQ(hwc, &mEngine.getHwComposer());
62 }
63 
TEST_F(CompositionEngineTest,canSetRenderEngine)64 TEST_F(CompositionEngineTest, canSetRenderEngine) {
65     auto renderEngine = std::make_unique<StrictMock<renderengine::mock::RenderEngine>>();
66     mEngine.setRenderEngine(renderEngine.get());
67 
68     EXPECT_EQ(renderEngine.get(), &mEngine.getRenderEngine());
69 }
70 
TEST_F(CompositionEngineTest,canSetTimeStats)71 TEST_F(CompositionEngineTest, canSetTimeStats) {
72     mEngine.setTimeStats(mTimeStats);
73 
74     EXPECT_EQ(mTimeStats.get(), mEngine.getTimeStats());
75 }
76 
77 /*
78  * CompositionEngine::present
79  */
80 
81 struct 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 
TEST_F(CompositionEnginePresentTest,worksWithEmptyRequest)91 TEST_F(CompositionEnginePresentTest, worksWithEmptyRequest) {
92     // present() always calls preComposition()
93     EXPECT_CALL(mEngine, preComposition(Ref(mRefreshArgs)));
94 
95     mEngine.present(mRefreshArgs);
96 }
97 
TEST_F(CompositionEnginePresentTest,worksAsExpected)98 TEST_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 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 /*
120  * CompositionEngine::updateCursorAsync
121  */
122 
123 struct CompositionEngineUpdateCursorAsyncTest : public CompositionEngineTest {
124 public:
125     struct Layer {
Layerandroid::compositionengine::__anon882c6eb20111::CompositionEngineUpdateCursorAsyncTest::Layer126         Layer() { EXPECT_CALL(outputLayer, getLayerFE()).WillRepeatedly(ReturnRef(*layerFE)); }
127 
128         StrictMock<mock::OutputLayer> outputLayer;
129         sp<StrictMock<mock::LayerFE>> layerFE = sp<StrictMock<mock::LayerFE>>::make();
130         LayerFECompositionState layerFEState;
131     };
132 
CompositionEngineUpdateCursorAsyncTestandroid::compositionengine::__anon882c6eb20111::CompositionEngineUpdateCursorAsyncTest133     CompositionEngineUpdateCursorAsyncTest() {
134         EXPECT_CALL(*mOutput1, getOutputLayerCount()).WillRepeatedly(Return(0u));
135         EXPECT_CALL(*mOutput1, getOutputLayerOrderedByZByIndex(_)).Times(0);
136 
137         EXPECT_CALL(*mOutput2, getOutputLayerCount()).WillRepeatedly(Return(1u));
138         EXPECT_CALL(*mOutput2, getOutputLayerOrderedByZByIndex(0))
139                 .WillRepeatedly(Return(&mOutput2Layer1.outputLayer));
140 
141         EXPECT_CALL(*mOutput3, getOutputLayerCount()).WillRepeatedly(Return(2u));
142         EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(0))
143                 .WillRepeatedly(Return(&mOutput3Layer1.outputLayer));
144         EXPECT_CALL(*mOutput3, getOutputLayerOrderedByZByIndex(1))
145                 .WillRepeatedly(Return(&mOutput3Layer2.outputLayer));
146     }
147 
148     Layer mOutput2Layer1;
149     Layer mOutput3Layer1;
150     Layer mOutput3Layer2;
151 };
152 
TEST_F(CompositionEngineUpdateCursorAsyncTest,handlesNoOutputs)153 TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoOutputs) {
154     mEngine.updateCursorAsync(mRefreshArgs);
155 }
156 
TEST_F(CompositionEngineUpdateCursorAsyncTest,handlesNoLayersBeingCursorLayers)157 TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesNoLayersBeingCursorLayers) {
158     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));
161 
162     mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
163 
164     mEngine.updateCursorAsync(mRefreshArgs);
165 }
166 
TEST_F(CompositionEngineUpdateCursorAsyncTest,handlesMultipleLayersBeingCursorLayers)167 TEST_F(CompositionEngineUpdateCursorAsyncTest, handlesMultipleLayersBeingCursorLayers) {
168     {
169         InSequence seq;
170         EXPECT_CALL(mOutput2Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
171         EXPECT_CALL(mOutput2Layer1.outputLayer, writeCursorPositionToHWC());
172     }
173 
174     {
175         InSequence seq;
176         EXPECT_CALL(mOutput3Layer1.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
177         EXPECT_CALL(mOutput3Layer1.outputLayer, writeCursorPositionToHWC());
178     }
179 
180     {
181         InSequence seq;
182         EXPECT_CALL(mOutput3Layer2.outputLayer, isHardwareCursor()).WillRepeatedly(Return(true));
183         EXPECT_CALL(mOutput3Layer2.outputLayer, writeCursorPositionToHWC());
184     }
185 
186     mRefreshArgs.outputs = {mOutput1, mOutput2, mOutput3};
187 
188     mEngine.updateCursorAsync(mRefreshArgs);
189 }
190 
191 /*
192  * CompositionEngine::preComposition
193  */
194 
195 struct CompositionTestPreComposition : public CompositionEngineTest {
196     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();
199 };
200 
TEST_F(CompositionTestPreComposition,preCompositionSetsFrameTimestamp)201 TEST_F(CompositionTestPreComposition, preCompositionSetsFrameTimestamp) {
202     const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
203     mEngine.preComposition(mRefreshArgs);
204     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 
TEST_F(CompositionTestPreComposition,preCompositionInvokesLayerPreCompositionWithFrameTimestamp)211 TEST_F(CompositionTestPreComposition, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
212     nsecs_t ts1 = 0;
213     nsecs_t ts2 = 0;
214     nsecs_t ts3 = 0;
215     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)));
221 
222     mRefreshArgs.outputs = {mOutput1};
223     mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
224 
225     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 
TEST_F(CompositionTestPreComposition,preCompositionDefaultsToNoUpdateNeeded)233 TEST_F(CompositionTestPreComposition, preCompositionDefaultsToNoUpdateNeeded) {
234     EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(false));
235     EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
236     EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
237 
238     mEngine.setNeedsAnotherUpdateForTest(true);
239 
240     mRefreshArgs.outputs = {mOutput1};
241     mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
242 
243     mEngine.preComposition(mRefreshArgs);
244 
245     // The call should have cleared the needsAnotherUpdate flag
246     EXPECT_FALSE(mEngine.needsAnotherUpdate());
247 }
248 
TEST_F(CompositionTestPreComposition,preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt)249 TEST_F(CompositionTestPreComposition,
250        preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
251     EXPECT_CALL(*mLayer1FE, onPreComposition(_, _)).WillOnce(Return(true));
252     EXPECT_CALL(*mLayer2FE, onPreComposition(_, _)).WillOnce(Return(false));
253     EXPECT_CALL(*mLayer3FE, onPreComposition(_, _)).WillOnce(Return(false));
254 
255     mRefreshArgs.outputs = {mOutput1};
256     mRefreshArgs.layers = {mLayer1FE, mLayer2FE, mLayer3FE};
257 
258     mEngine.preComposition(mRefreshArgs);
259 
260     EXPECT_TRUE(mEngine.needsAnotherUpdate());
261 }
262 
263 } // namespace
264 } // namespace android::compositionengine
265