1 /*
2  * Copyright 2022 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 #undef LOG_TAG
18 #define LOG_TAG "SurfaceFlingerPowerHintTest"
19 
20 #include <chrono>
21 
22 #include "CommitAndCompositeTest.h"
23 
24 #include "DisplayHardware/Hal.h"
25 
26 using namespace std::chrono_literals;
27 using testing::_;
28 using testing::Return;
29 
30 namespace android {
31 namespace {
32 
33 class SurfaceFlingerPowerHintTest : public CommitAndCompositeTest {};
34 
TEST_F(SurfaceFlingerPowerHintTest,sendDurationsIncludingHwcWaitTime)35 TEST_F(SurfaceFlingerPowerHintTest, sendDurationsIncludingHwcWaitTime) {
36     ON_CALL(*mPowerAdvisor, usePowerHintSession()).WillByDefault(Return(true));
37 
38     EXPECT_CALL(*mPowerAdvisor, updateTargetWorkDuration(_)).Times(1);
39     EXPECT_CALL(*mDisplaySurface,
40                 prepareFrame(compositionengine::DisplaySurface::CompositionType::Hwc))
41             .Times(1);
42     EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _, _)).WillOnce([] {
43         constexpr Duration kMockHwcRunTime = 20ms;
44         std::this_thread::sleep_for(kMockHwcRunTime);
45         return hardware::graphics::composer::hal::Error::NONE;
46     });
47     EXPECT_CALL(*mPowerAdvisor, reportActualWorkDuration()).Times(1);
48 
49     const TimePoint frameTime = scheduler::SchedulerClock::now();
50     constexpr Period kMockVsyncPeriod = 15ms;
51     mFlinger.commitAndComposite(frameTime, VsyncId{123}, frameTime + kMockVsyncPeriod);
52 }
53 
TEST_F(SurfaceFlingerPowerHintTest,inactiveOnDisplayDoze)54 TEST_F(SurfaceFlingerPowerHintTest, inactiveOnDisplayDoze) {
55     ON_CALL(*mPowerAdvisor, usePowerHintSession()).WillByDefault(Return(true));
56 
57     mDisplay->setPowerMode(hal::PowerMode::DOZE);
58 
59     EXPECT_CALL(*mPowerAdvisor, updateTargetWorkDuration(_)).Times(0);
60     EXPECT_CALL(*mDisplaySurface,
61                 prepareFrame(compositionengine::DisplaySurface::CompositionType::Hwc))
62             .Times(1);
63     EXPECT_CALL(*mComposer, presentOrValidateDisplay(HWC_DISPLAY, _, _, _, _, _, _)).WillOnce([] {
64         constexpr Duration kMockHwcRunTime = 20ms;
65         std::this_thread::sleep_for(kMockHwcRunTime);
66         return hardware::graphics::composer::hal::Error::NONE;
67     });
68     EXPECT_CALL(*mPowerAdvisor, reportActualWorkDuration()).Times(0);
69 
70     const TimePoint frameTime = scheduler::SchedulerClock::now();
71     constexpr Period kMockVsyncPeriod = 15ms;
72     mFlinger.commitAndComposite(frameTime, VsyncId{123}, frameTime + kMockVsyncPeriod);
73 }
74 
75 } // namespace
76 } // namespace android
77