1 // Copyright 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "VsyncThread.h"
15
16 #include <gtest/gtest.h>
17
18 #ifndef _WIN32
19 #include <unistd.h>
20 #endif
21
22 namespace gfxstream {
23 namespace {
24
25 // Tests scheduling some basic task, and that they are processed
26 // before the thread completely exits.
TEST(VsyncThread,Basic)27 TEST(VsyncThread, Basic) {
28 const uint64_t kOneSecondNs = 1000000000ULL;
29 const uint64_t k60HzPeriodNs = kOneSecondNs / 60ULL;
30
31 for (int i = 0; i < 2; ++i) {
32 uint64_t count = 0;
33
34 {
35 VsyncThread thread(k60HzPeriodNs);
36 EXPECT_EQ(k60HzPeriodNs, thread.getPeriod());
37 for (int j = 0; j < 2; ++j) {
38 thread.schedule([&count](uint64_t currentCount) {
39 ++count;
40 });
41 }
42 }
43
44 EXPECT_EQ(2, count);
45 }
46 }
47
48 // Tests scheduling tasks and changing the period.
TEST(VsyncThread,ChangePeriod)49 TEST(VsyncThread, ChangePeriod) {
50 const uint64_t kOneSecondNs = 1000000000ULL;
51 const uint64_t k60HzPeriodNs = kOneSecondNs / 60ULL;
52 const uint64_t k144HzPeriodNs = kOneSecondNs / 144ULL;
53
54 for (int i = 0; i < 2; ++i) {
55 uint64_t count = 0;
56
57 {
58 VsyncThread thread(k60HzPeriodNs);
59 EXPECT_EQ(k60HzPeriodNs, thread.getPeriod());
60 thread.schedule([&count](uint64_t currentCount) {
61 ++count;
62 });
63 thread.setPeriod(k144HzPeriodNs);
64 thread.schedule([&count](uint64_t currentCount) {
65 ++count;
66 });
67 }
68
69 EXPECT_EQ(2, count);
70 }
71 }
72
73 } // namespace
74 } // namespace gfxstream
75