blob: 382199dc9fa7628217d69da36ae05694684e59fc [file] [log] [blame]
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -08001/*
2 * Copyright 2019 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// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Ana Krulec0c8cd522018-08-31 12:27:28 -070021#undef LOG_TAG
22#define LOG_TAG "SchedulerUnittests"
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
Ana Krulec0c8cd522018-08-31 12:27:28 -070026#include <log/log.h>
27
Ana Krulece588e312018-09-18 12:32:24 -070028#include <mutex>
29
Ana Krulece588e312018-09-18 12:32:24 -070030#include "Scheduler/EventControlThread.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070031#include "Scheduler/EventThread.h"
Steven Thomas2bbaabe2019-08-28 16:08:35 -070032#include "Scheduler/RefreshRateConfigs.h"
Dominik Laskowski98041832019-08-01 18:35:59 -070033#include "TestableScheduler.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070034#include "TestableSurfaceFlinger.h"
Ady Abrahamabc27602020-04-08 17:20:29 -070035#include "mock/DisplayHardware/MockDisplay.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070036#include "mock/MockEventThread.h"
Dominik Laskowski983f2b52020-06-25 16:54:06 -070037#include "mock/MockLayer.h"
Ana Krulec0c8cd522018-08-31 12:27:28 -070038
39using testing::_;
40using testing::Return;
41
42namespace android {
43
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -080044constexpr PhysicalDisplayId PHYSICAL_DISPLAY_ID = 999;
45
Ana Krulec0c8cd522018-08-31 12:27:28 -070046class SchedulerTest : public testing::Test {
47protected:
Ana Krulec85c39af2018-12-26 17:29:57 -080048 class MockEventThreadConnection : public android::EventThreadConnection {
Ana Krulec0c8cd522018-08-31 12:27:28 -070049 public:
Ana Krulec85c39af2018-12-26 17:29:57 -080050 explicit MockEventThreadConnection(EventThread* eventThread)
Ady Abraham0f4a1b12019-06-04 16:04:04 -070051 : EventThreadConnection(eventThread, ResyncCallback(),
52 ISurfaceComposer::eConfigChangedSuppress) {}
Ana Krulec0c8cd522018-08-31 12:27:28 -070053 ~MockEventThreadConnection() = default;
54
55 MOCK_METHOD1(stealReceiveChannel, status_t(gui::BitTube* outChannel));
56 MOCK_METHOD1(setVsyncRate, status_t(uint32_t count));
57 MOCK_METHOD0(requestNextVsync, void());
58 };
59
Ana Krulec0c8cd522018-08-31 12:27:28 -070060 SchedulerTest();
61 ~SchedulerTest() override;
62
Dominik Laskowski983f2b52020-06-25 16:54:06 -070063 Hwc2::mock::Display mDisplay;
64 const scheduler::RefreshRateConfigs mConfigs{{HWC2::Display::Config::Builder(mDisplay, 0)
65 .setVsyncPeriod(16'666'667)
66 .setConfigGroup(0)
67 .build()},
68 HwcConfigIndexType(0)};
69
70 TestableScheduler mScheduler{mConfigs, false};
Dominik Laskowski98041832019-08-01 18:35:59 -070071
72 Scheduler::ConnectionHandle mConnectionHandle;
Ana Krulec0c8cd522018-08-31 12:27:28 -070073 mock::EventThread* mEventThread;
Ana Krulec0c8cd522018-08-31 12:27:28 -070074 sp<MockEventThreadConnection> mEventThreadConnection;
Ana Krulec0c8cd522018-08-31 12:27:28 -070075};
76
77SchedulerTest::SchedulerTest() {
78 const ::testing::TestInfo* const test_info =
79 ::testing::UnitTest::GetInstance()->current_test_info();
80 ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name());
81
Dominik Laskowski98041832019-08-01 18:35:59 -070082 auto eventThread = std::make_unique<mock::EventThread>();
Ana Krulec0c8cd522018-08-31 12:27:28 -070083 mEventThread = eventThread.get();
Ana Krulec85c39af2018-12-26 17:29:57 -080084 EXPECT_CALL(*mEventThread, registerDisplayEventConnection(_)).WillOnce(Return(0));
85
86 mEventThreadConnection = new MockEventThreadConnection(mEventThread);
Ana Krulec0c8cd522018-08-31 12:27:28 -070087
88 // createConnection call to scheduler makes a createEventConnection call to EventThread. Make
89 // sure that call gets executed and returns an EventThread::Connection object.
Ady Abraham0f4a1b12019-06-04 16:04:04 -070090 EXPECT_CALL(*mEventThread, createEventConnection(_, _))
Ana Krulec0c8cd522018-08-31 12:27:28 -070091 .WillRepeatedly(Return(mEventThreadConnection));
92
Dominik Laskowski983f2b52020-06-25 16:54:06 -070093 mConnectionHandle = mScheduler.createConnection(std::move(eventThread));
Dominik Laskowski98041832019-08-01 18:35:59 -070094 EXPECT_TRUE(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -070095}
96
97SchedulerTest::~SchedulerTest() {
98 const ::testing::TestInfo* const test_info =
99 ::testing::UnitTest::GetInstance()->current_test_info();
100 ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name());
101}
102
Ana Krulec0c8cd522018-08-31 12:27:28 -0700103/* ------------------------------------------------------------------------
104 * Test cases
105 */
Ana Krulec0c8cd522018-08-31 12:27:28 -0700106
Ana Krulec0c8cd522018-08-31 12:27:28 -0700107TEST_F(SchedulerTest, invalidConnectionHandle) {
Dominik Laskowski98041832019-08-01 18:35:59 -0700108 Scheduler::ConnectionHandle handle;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700109
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700110 const sp<IDisplayEventConnection> connection =
111 mScheduler.createDisplayEventConnection(handle,
112 ISurfaceComposer::eConfigChangedSuppress);
113
Dominik Laskowski98041832019-08-01 18:35:59 -0700114 EXPECT_FALSE(connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700115 EXPECT_FALSE(mScheduler.getEventConnection(handle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700116
117 // The EXPECT_CALLS make sure we don't call the functions on the subsequent event threads.
118 EXPECT_CALL(*mEventThread, onHotplugReceived(_, _)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700119 mScheduler.onHotplugReceived(handle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700120
121 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700122 mScheduler.onScreenAcquired(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700123
124 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700125 mScheduler.onScreenReleased(handle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700126
Dominik Laskowski98041832019-08-01 18:35:59 -0700127 std::string output;
Ana Krulec0c8cd522018-08-31 12:27:28 -0700128 EXPECT_CALL(*mEventThread, dump(_)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700129 mScheduler.dump(handle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700130 EXPECT_TRUE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700131
132 EXPECT_CALL(*mEventThread, setPhaseOffset(_)).Times(0);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700133 mScheduler.setPhaseOffset(handle, 10);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700134}
135
136TEST_F(SchedulerTest, validConnectionHandle) {
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700137 const sp<IDisplayEventConnection> connection =
138 mScheduler.createDisplayEventConnection(mConnectionHandle,
139 ISurfaceComposer::eConfigChangedSuppress);
140
Dominik Laskowski98041832019-08-01 18:35:59 -0700141 ASSERT_EQ(mEventThreadConnection, connection);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700142 EXPECT_TRUE(mScheduler.getEventConnection(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700143
Dominik Laskowskidcb38bb2019-01-25 02:35:50 -0800144 EXPECT_CALL(*mEventThread, onHotplugReceived(PHYSICAL_DISPLAY_ID, false)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700145 mScheduler.onHotplugReceived(mConnectionHandle, PHYSICAL_DISPLAY_ID, false);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700146
147 EXPECT_CALL(*mEventThread, onScreenAcquired()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700148 mScheduler.onScreenAcquired(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700149
150 EXPECT_CALL(*mEventThread, onScreenReleased()).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700151 mScheduler.onScreenReleased(mConnectionHandle);
Ana Krulec0c8cd522018-08-31 12:27:28 -0700152
Dominik Laskowski98041832019-08-01 18:35:59 -0700153 std::string output("dump");
154 EXPECT_CALL(*mEventThread, dump(output)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700155 mScheduler.dump(mConnectionHandle, output);
Dominik Laskowski98041832019-08-01 18:35:59 -0700156 EXPECT_FALSE(output.empty());
Ana Krulec0c8cd522018-08-31 12:27:28 -0700157
158 EXPECT_CALL(*mEventThread, setPhaseOffset(10)).Times(1);
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700159 mScheduler.setPhaseOffset(mConnectionHandle, 10);
Alec Mouri717bcb62020-02-10 17:07:19 -0800160
161 static constexpr size_t kEventConnections = 5;
162 ON_CALL(*mEventThread, getEventThreadConnectionCount())
163 .WillByDefault(Return(kEventConnections));
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700164 EXPECT_EQ(kEventConnections, mScheduler.getEventThreadConnectionCount(mConnectionHandle));
Ana Krulec0c8cd522018-08-31 12:27:28 -0700165}
Dominik Laskowski98041832019-08-01 18:35:59 -0700166
Dominik Laskowski983f2b52020-06-25 16:54:06 -0700167TEST_F(SchedulerTest, noLayerHistory) {
168 // Layer history should not be created if there is a single config.
169 ASSERT_FALSE(mScheduler.hasLayerHistory());
170
171 TestableSurfaceFlinger flinger;
172 mock::MockLayer layer(flinger.flinger());
173
174 // Content detection should be no-op.
175 mScheduler.registerLayer(&layer);
176 mScheduler.recordLayerHistory(&layer, 0, LayerHistory::LayerUpdateType::Buffer);
177
178 constexpr bool kPowerStateNormal = true;
179 mScheduler.setDisplayPowerState(kPowerStateNormal);
180
181 constexpr uint32_t kDisplayArea = 999'999;
182 mScheduler.onPrimaryDisplayAreaChanged(kDisplayArea);
183
184 mScheduler.chooseRefreshRateForContent();
185 EXPECT_EQ(0, mScheduler.refreshRateChangeCount());
186}
187
Ana Krulec3084c052018-11-21 20:27:17 +0100188} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800189
190// TODO(b/129481165): remove the #pragma below and fix conversion issues
191#pragma clang diagnostic pop // ignored "-Wconversion"