• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include <gtest/gtest.h>
18 #include <gui/DisplayEventReceiver.h>
19 #include <gui/ISurfaceComposer.h>
20 #include <gui/SurfaceComposerClient.h>
21 #include <sys/epoll.h>
22 #include <algorithm>
23 
24 namespace android {
25 namespace {
26 using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
27 
28 class SetFrameRateOverrideTest : public ::testing::Test {
29 protected:
SetUp()30     void SetUp() override {
31         const ISurfaceComposer::VsyncSource vsyncSource = ISurfaceComposer::eVsyncSourceApp;
32         const ISurfaceComposer::EventRegistrationFlags eventRegistration = {
33                 ISurfaceComposer::EventRegistration::frameRateOverride};
34 
35         mDisplayEventReceiver =
36                 std::make_unique<DisplayEventReceiver>(vsyncSource, eventRegistration);
37         EXPECT_EQ(NO_ERROR, mDisplayEventReceiver->initCheck());
38 
39         mEpollFd = epoll_create1(EPOLL_CLOEXEC);
40         EXPECT_GT(mEpollFd, 1);
41 
42         epoll_event event;
43         event.events = EPOLLIN;
44         EXPECT_EQ(0, epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mDisplayEventReceiver->getFd(), &event));
45     }
46 
TearDown()47     void TearDown() override { close(mEpollFd); }
48 
setFrameRateAndListenEvents(uid_t uid,float frameRate)49     void setFrameRateAndListenEvents(uid_t uid, float frameRate) {
50         status_t ret = SurfaceComposerClient::setOverrideFrameRate(uid, frameRate);
51         ASSERT_EQ(NO_ERROR, ret);
52 
53         DisplayEventReceiver::Event event;
54         bool isOverrideFlushReceived = false;
55         mFrameRateOverrides.clear();
56 
57         epoll_event epollEvent;
58         while (epoll_wait(mEpollFd, &epollEvent, 1, 1000) > 0) {
59             while (mDisplayEventReceiver->getEvents(&event, 1) > 0) {
60                 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE) {
61                     mFrameRateOverrides.emplace_back(event.frameRateOverride);
62                 }
63                 if (event.header.type ==
64                     DisplayEventReceiver::DISPLAY_EVENT_FRAME_RATE_OVERRIDE_FLUSH) {
65                     isOverrideFlushReceived = true;
66                 }
67             }
68 
69             if (isOverrideFlushReceived) break;
70         }
71     }
72 
73     std::unique_ptr<DisplayEventReceiver> mDisplayEventReceiver;
74     std::vector<FrameRateOverride> mFrameRateOverrides;
75 
76     int mEpollFd;
77 };
78 
TEST_F(SetFrameRateOverrideTest,SetFrameRateOverrideCall)79 TEST_F(SetFrameRateOverrideTest, SetFrameRateOverrideCall) {
80     uid_t uid = getuid();
81     float frameRate = 30.0f;
82     setFrameRateAndListenEvents(uid, frameRate);
83     // check if the frame rate override we set exists
84     ASSERT_TRUE(std::find_if(mFrameRateOverrides.begin(), mFrameRateOverrides.end(),
85                              [uid = uid, frameRate = frameRate](auto i) {
86                                  return uid == i.uid && frameRate == i.frameRateHz;
87                              }) != mFrameRateOverrides.end());
88 
89     // test removing frame rate override
90     frameRate = 0.0f;
91     setFrameRateAndListenEvents(uid, frameRate);
92     ASSERT_TRUE(std::find_if(mFrameRateOverrides.begin(), mFrameRateOverrides.end(),
93                              [uid = uid, frameRate = frameRate](auto i) {
94                                  return uid == i.uid && frameRate == i.frameRateHz;
95                              }) == mFrameRateOverrides.end());
96 }
97 } // namespace
98 } // namespace android
99