blob: 0dbf8f07810faead7075d93a9b67bd3fb18df5c3 [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 Pique70d91362018-10-18 16:02:55 -070018#include <compositionengine/impl/CompositionEngine.h>
Lloyd Piqueab039b52019-02-13 14:22:42 -080019#include <compositionengine/mock/Layer.h>
20#include <compositionengine/mock/LayerFE.h>
21#include <compositionengine/mock/Output.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070022#include <gtest/gtest.h>
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070023#include <renderengine/mock/RenderEngine.h>
Lloyd Pique70d91362018-10-18 16:02:55 -070024
Lloyd Pique441d5042018-10-18 16:49:51 -070025#include "MockHWComposer.h"
26
Lloyd Pique70d91362018-10-18 16:02:55 -070027namespace android::compositionengine {
28namespace {
29
Lloyd Piqueab039b52019-02-13 14:22:42 -080030using ::testing::_;
31using ::testing::Return;
32using ::testing::SaveArg;
Lloyd Pique441d5042018-10-18 16:49:51 -070033using ::testing::StrictMock;
34
Lloyd Pique70d91362018-10-18 16:02:55 -070035class CompositionEngineTest : public testing::Test {
36public:
Lloyd Piqueab039b52019-02-13 14:22:42 -080037 android::mock::HWComposer* mHwc = new StrictMock<android::mock::HWComposer>();
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070038 renderengine::mock::RenderEngine* mRenderEngine =
39 new StrictMock<renderengine::mock::RenderEngine>();
Lloyd Pique441d5042018-10-18 16:49:51 -070040 impl::CompositionEngine mEngine;
Lloyd Pique70d91362018-10-18 16:02:55 -070041};
42
Lloyd Pique70d91362018-10-18 16:02:55 -070043TEST_F(CompositionEngineTest, canInstantiateCompositionEngine) {
44 auto engine = impl::createCompositionEngine();
45 EXPECT_TRUE(engine.get() != nullptr);
46}
47
Lloyd Pique441d5042018-10-18 16:49:51 -070048TEST_F(CompositionEngineTest, canSetHWComposer) {
49 mEngine.setHwComposer(std::unique_ptr<android::HWComposer>(mHwc));
50
51 EXPECT_EQ(mHwc, &mEngine.getHwComposer());
52}
53
Lloyd Piqueb97e04f2018-10-18 17:07:05 -070054TEST_F(CompositionEngineTest, canSetRenderEngine) {
55 mEngine.setRenderEngine(std::unique_ptr<renderengine::RenderEngine>(mRenderEngine));
56
57 EXPECT_EQ(mRenderEngine, &mEngine.getRenderEngine());
58}
59
Lloyd Piqueab039b52019-02-13 14:22:42 -080060/*
61 * CompositionEngine::preComposition
62 */
63
64class PreCompositionTest : public CompositionEngineTest {
65public:
66 PreCompositionTest() {
67 EXPECT_CALL(*mLayer1, getLayerFE()).WillRepeatedly(Return(mLayer1FE));
68 EXPECT_CALL(*mLayer2, getLayerFE()).WillRepeatedly(Return(mLayer2FE));
69 EXPECT_CALL(*mLayer3, getLayerFE()).WillRepeatedly(Return(mLayer3FE));
70 // getLayerFE() can return nullptr. Ensure that this is handled.
71 EXPECT_CALL(*mLayer4, getLayerFE()).WillRepeatedly(Return(nullptr));
72
73 mRefreshArgs.outputs = {mOutput};
74 mRefreshArgs.layers = {mLayer1, mLayer2, mLayer3, mLayer4};
75 }
76
77 std::shared_ptr<mock::Output> mOutput{std::make_shared<StrictMock<mock::Output>>()};
78 std::shared_ptr<mock::Layer> mLayer1{std::make_shared<StrictMock<mock::Layer>>()};
79 std::shared_ptr<mock::Layer> mLayer2{std::make_shared<StrictMock<mock::Layer>>()};
80 std::shared_ptr<mock::Layer> mLayer3{std::make_shared<StrictMock<mock::Layer>>()};
81 std::shared_ptr<mock::Layer> mLayer4{std::make_shared<StrictMock<mock::Layer>>()};
82 sp<StrictMock<mock::LayerFE>> mLayer1FE{new StrictMock<mock::LayerFE>()};
83 sp<StrictMock<mock::LayerFE>> mLayer2FE{new StrictMock<mock::LayerFE>()};
84 sp<StrictMock<mock::LayerFE>> mLayer3FE{new StrictMock<mock::LayerFE>()};
85
86 CompositionRefreshArgs mRefreshArgs;
87};
88
89TEST_F(PreCompositionTest, preCompositionSetsFrameTimestamp) {
90 const nsecs_t before = systemTime(SYSTEM_TIME_MONOTONIC);
91 CompositionRefreshArgs emptyArgs;
92 mEngine.preComposition(emptyArgs);
93 const nsecs_t after = systemTime(SYSTEM_TIME_MONOTONIC);
94
95 // The frame timestamp should be between the before and after timestamps
96 EXPECT_GE(mEngine.getLastFrameRefreshTimestamp(), before);
97 EXPECT_LE(mEngine.getLastFrameRefreshTimestamp(), after);
98}
99
100TEST_F(PreCompositionTest, preCompositionInvokesLayerPreCompositionWithFrameTimestamp) {
101 nsecs_t ts1 = 0;
102 nsecs_t ts2 = 0;
103 nsecs_t ts3 = 0;
104 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts1), Return(false)));
105 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts2), Return(false)));
106 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(DoAll(SaveArg<0>(&ts3), Return(false)));
107
108 mEngine.preComposition(mRefreshArgs);
109
110 // Each of the onPreComposition calls should used the same refresh timestamp
111 EXPECT_EQ(ts1, mEngine.getLastFrameRefreshTimestamp());
112 EXPECT_EQ(ts2, mEngine.getLastFrameRefreshTimestamp());
113 EXPECT_EQ(ts3, mEngine.getLastFrameRefreshTimestamp());
114}
115
116TEST_F(PreCompositionTest, preCompositionDefaultsToNoUpdateNeeded) {
117 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(false));
118 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
119 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
120
121 mEngine.setNeedsAnotherUpdateForTest(true);
122
123 mEngine.preComposition(mRefreshArgs);
124
125 // The call should have cleared the needsAnotherUpdate flag
126 EXPECT_FALSE(mEngine.needsAnotherUpdate());
127}
128
129TEST_F(PreCompositionTest, preCompositionSetsNeedsAnotherUpdateIfAtLeastOneLayerRequestsIt) {
130 EXPECT_CALL(*mLayer1FE, onPreComposition(_)).WillOnce(Return(true));
131 EXPECT_CALL(*mLayer2FE, onPreComposition(_)).WillOnce(Return(false));
132 EXPECT_CALL(*mLayer3FE, onPreComposition(_)).WillOnce(Return(false));
133
134 mEngine.preComposition(mRefreshArgs);
135
136 EXPECT_TRUE(mEngine.needsAnotherUpdate());
137}
138
Lloyd Pique70d91362018-10-18 16:02:55 -0700139} // namespace
140} // namespace android::compositionengine