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 choreographer->postFrameCallbackDelayed(nullptr, nullptr, vsyncCallback, &animationCb, 0,
56 CALLBACK_ANIMATION);
57 VsyncCallback inputCb;
58 choreographer->postFrameCallbackDelayed(nullptr, nullptr, vsyncCallback, &inputCb, 0,
59 CALLBACK_INPUT);
60 auto startTime = std::chrono::system_clock::now();
61 do {
62 static constexpr int32_t timeoutMs = 1000;
63 int pollResult = looper->pollOnce(timeoutMs);
64 ASSERT_TRUE((pollResult != Looper::POLL_TIMEOUT) && (pollResult != Looper::POLL_ERROR))
65 << "Failed to poll looper. Poll result = " << pollResult;
66 auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(
67 std::chrono::system_clock::now() - startTime);
68 ASSERT_LE(elapsedMs.count(), timeoutMs)
69 << "Timed out waiting for callbacks. inputCb=" << inputCb.callbackReceived()
70 << " animationCb=" << animationCb.callbackReceived();
71 } while (!(inputCb.callbackReceived() && animationCb.callbackReceived()));
72
73 ASSERT_EQ(inputCb.frameTime, animationCb.frameTime)
74 << android::base::StringPrintf("input and animation callback frame times don't match. "
75 "inputFrameTime=%lld animationFrameTime=%lld",
76 inputCb.frameTime.count(),
77 animationCb.frameTime.count());
78
79 ASSERT_LT(inputCb.receivedCallbackTime, animationCb.receivedCallbackTime)
80 << android::base::StringPrintf("input callback was not called first. "
81 "inputCallbackTime=%lld animationCallbackTime=%lld",
82 inputCb.frameTime.count(),
83 animationCb.frameTime.count());
84 }
85
86 } // namespace android