1 /*
2  * Copyright (C) 2024 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 #define LOG_TAG "Choreographer_test"
18 
19 #include <android-base/stringprintf.h>
20 #include <android/choreographer.h>
21 #include <gtest/gtest.h>
22 #include <gui/Choreographer.h>
23 #include <utils/Looper.h>
24 #include <chrono>
25 #include <future>
26 #include <string>
27 
28 namespace android {
29 class ChoreographerTest : public ::testing::Test {};
30 
31 struct VsyncCallback {
32     std::atomic<bool> completePromise{false};
33     std::chrono::nanoseconds frameTime{0LL};
34     std::chrono::nanoseconds receivedCallbackTime{0LL};
35 
onVsyncCallbackandroid::VsyncCallback36     void onVsyncCallback(const AChoreographerFrameCallbackData* callbackData) {
37         frameTime = std::chrono::nanoseconds{
38                 AChoreographerFrameCallbackData_getFrameTimeNanos(callbackData)};
39         receivedCallbackTime = std::chrono::nanoseconds{systemTime(SYSTEM_TIME_MONOTONIC)};
40         completePromise.store(true);
41     }
42 
callbackReceivedandroid::VsyncCallback43     bool callbackReceived() { return completePromise.load(); }
44 };
45 
vsyncCallback(const AChoreographerFrameCallbackData * callbackData,void * data)46 static void vsyncCallback(const AChoreographerFrameCallbackData* callbackData, void* data) {
47     VsyncCallback* cb = static_cast<VsyncCallback*>(data);
48     cb->onVsyncCallback(callbackData);
49 }
50 
TEST_F(ChoreographerTest,InputCallbackBeforeAnimation)51 TEST_F(ChoreographerTest, InputCallbackBeforeAnimation) {
52     sp<Looper> looper = Looper::prepare(0);
53     Choreographer* choreographer = Choreographer::getForThread();
54     VsyncCallback animationCb;
55     VsyncCallback inputCb;
56 
57     choreographer->postFrameCallbackDelayed(nullptr, nullptr, vsyncCallback, &animationCb, 0,
58                                             CALLBACK_ANIMATION);
59     choreographer->postFrameCallbackDelayed(nullptr, nullptr, vsyncCallback, &inputCb, 0,
60                                             CALLBACK_INPUT);
61 
62     nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
63     nsecs_t currTime;
64     int pollResult;
65     do {
66         pollResult = looper->pollOnce(16);
67         currTime = systemTime(SYSTEM_TIME_MONOTONIC);
68     } while (!(inputCb.callbackReceived() && animationCb.callbackReceived()) &&
69              (pollResult != Looper::POLL_TIMEOUT && pollResult != Looper::POLL_ERROR) &&
70              (currTime - startTime < 3000));
71 
72     ASSERT_TRUE(inputCb.callbackReceived()) << "did not receive input callback";
73     ASSERT_TRUE(animationCb.callbackReceived()) << "did not receive animation callback";
74 
75     ASSERT_EQ(inputCb.frameTime, animationCb.frameTime)
76             << android::base::StringPrintf("input and animation callback frame times don't match. "
77                                            "inputFrameTime=%lld  animationFrameTime=%lld",
78                                            inputCb.frameTime.count(),
79                                            animationCb.frameTime.count());
80 
81     ASSERT_LT(inputCb.receivedCallbackTime, animationCb.receivedCallbackTime)
82             << android::base::StringPrintf("input callback was not called first. "
83                                            "inputCallbackTime=%lld  animationCallbackTime=%lld",
84                                            inputCb.frameTime.count(),
85                                            animationCb.frameTime.count());
86 }
87 
88 } // namespace android