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 #include "VsyncThread.h"
18 
19 #include <utils/ThreadDefs.h>
20 
21 #include <thread>
22 
23 #include "Time.h"
24 
25 namespace aidl::android::hardware::graphics::composer3::impl {
26 namespace {
27 
28 // Returns the timepoint of the next vsync after the 'now' timepoint that is
29 // a multiple of 'vsyncPeriod' in-phase/offset-from 'previousSync'.
30 //
31 // Some examples:
32 //  * vsyncPeriod=50ns previousVsync=500ns now=510ns => 550ns
33 //  * vsyncPeriod=50ns previousVsync=300ns now=510ns => 550ns
34 //  * vsyncPeriod=50ns previousVsync=500ns now=550ns => 550ns
GetNextVsyncInPhase(Nanoseconds vsyncPeriod,TimePoint previousVsync,TimePoint now)35 TimePoint GetNextVsyncInPhase(Nanoseconds vsyncPeriod, TimePoint previousVsync, TimePoint now) {
36     const auto elapsed = Nanoseconds(now - previousVsync);
37     const auto nextMultiple = (elapsed / vsyncPeriod) + 1;
38     return previousVsync + (nextMultiple * vsyncPeriod);
39 }
40 
41 }  // namespace
42 
VsyncThread(int64_t displayId)43 VsyncThread::VsyncThread(int64_t displayId) : mDisplayId(displayId) {
44     mPreviousVsync = std::chrono::steady_clock::now() - mVsyncPeriod;
45 }
46 
~VsyncThread()47 VsyncThread::~VsyncThread() { stop(); }
48 
start(int32_t vsyncPeriodNanos)49 HWC3::Error VsyncThread::start(int32_t vsyncPeriodNanos) {
50     DEBUG_LOG("%s for display:%" PRIu64, __FUNCTION__, mDisplayId);
51 
52     mVsyncPeriod = Nanoseconds(vsyncPeriodNanos);
53 
54     mThread = std::thread([this]() { threadLoop(); });
55 
56     // Truncate to 16 chars (15 + null byte) to satisfy pthread_setname_np max name length
57     // requirement.
58     const std::string name =
59             std::string("display_" + std::to_string(mDisplayId) + "_vsync_thread").substr(15);
60 
61     int ret = pthread_setname_np(mThread.native_handle(), name.c_str());
62     if (ret != 0) {
63         ALOGE("%s: failed to set Vsync thread name: %s", __FUNCTION__, strerror(ret));
64     }
65 
66     struct sched_param param = {
67         .sched_priority = ANDROID_PRIORITY_DISPLAY,
68     };
69     ret = pthread_setschedparam(mThread.native_handle(), SCHED_FIFO, &param);
70     if (ret != 0) {
71         ALOGE("%s: failed to set Vsync thread priority: %s", __FUNCTION__, strerror(ret));
72     }
73 
74     return HWC3::Error::None;
75 }
76 
stop()77 HWC3::Error VsyncThread::stop() {
78     mShuttingDown.store(true);
79     mThread.join();
80 
81     return HWC3::Error::None;
82 }
83 
setCallbacks(const std::shared_ptr<IComposerCallback> & callback)84 HWC3::Error VsyncThread::setCallbacks(const std::shared_ptr<IComposerCallback>& callback) {
85     DEBUG_LOG("%s for display:%" PRIu64, __FUNCTION__, mDisplayId);
86 
87     std::unique_lock<std::mutex> lock(mStateMutex);
88 
89     mCallbacks = callback;
90 
91     return HWC3::Error::None;
92 }
93 
setVsyncEnabled(bool enabled)94 HWC3::Error VsyncThread::setVsyncEnabled(bool enabled) {
95     DEBUG_LOG("%s for display:%" PRIu64 " enabled:%d", __FUNCTION__, mDisplayId, enabled);
96 
97     std::unique_lock<std::mutex> lock(mStateMutex);
98 
99     mVsyncEnabled = enabled;
100 
101     return HWC3::Error::None;
102 }
103 
scheduleVsyncUpdate(int32_t newVsyncPeriod,const VsyncPeriodChangeConstraints & constraints,VsyncPeriodChangeTimeline * outTimeline)104 HWC3::Error VsyncThread::scheduleVsyncUpdate(int32_t newVsyncPeriod,
105                                              const VsyncPeriodChangeConstraints& constraints,
106                                              VsyncPeriodChangeTimeline* outTimeline) {
107     DEBUG_LOG("%s for display:%" PRIu64, __FUNCTION__, mDisplayId);
108 
109     PendingUpdate update;
110     update.period = Nanoseconds(newVsyncPeriod);
111     update.updateAfter = asTimePoint(constraints.desiredTimeNanos);
112 
113     std::unique_lock<std::mutex> lock(mStateMutex);
114     mPendingUpdate.emplace(std::move(update));
115 
116     TimePoint nextVsync = GetNextVsyncInPhase(mVsyncPeriod, mPreviousVsync, update.updateAfter);
117 
118     outTimeline->newVsyncAppliedTimeNanos = asNanosTimePoint(nextVsync);
119     outTimeline->refreshRequired = false;
120     outTimeline->refreshTimeNanos = 0;
121 
122     return HWC3::Error::None;
123 }
124 
updateVsyncPeriodLocked(TimePoint now)125 Nanoseconds VsyncThread::updateVsyncPeriodLocked(TimePoint now) {
126     if (mPendingUpdate && now > mPendingUpdate->updateAfter) {
127         mVsyncPeriod = mPendingUpdate->period;
128         mPendingUpdate.reset();
129     }
130 
131     return mVsyncPeriod;
132 }
133 
threadLoop()134 void VsyncThread::threadLoop() {
135     ALOGI("Vsync thread for display:%" PRId64 " starting", mDisplayId);
136 
137     Nanoseconds vsyncPeriod = mVsyncPeriod;
138 
139     int vsyncs = 0;
140     TimePoint previousLog = std::chrono::steady_clock::now();
141 
142     while (!mShuttingDown.load()) {
143         TimePoint now = std::chrono::steady_clock::now();
144         TimePoint nextVsync = GetNextVsyncInPhase(vsyncPeriod, mPreviousVsync, now);
145 
146         std::this_thread::sleep_until(nextVsync);
147         {
148             std::unique_lock<std::mutex> lock(mStateMutex);
149 
150             mPreviousVsync = nextVsync;
151 
152             // Display has finished refreshing at previous vsync period. Update the
153             // vsync period if there was a pending update.
154             vsyncPeriod = updateVsyncPeriodLocked(mPreviousVsync);
155         }
156 
157         if (mVsyncEnabled) {
158             if (mCallbacks) {
159                 DEBUG_LOG("%s: for display:%" PRIu64 " calling vsync", __FUNCTION__, mDisplayId);
160                 mCallbacks->onVsync(mDisplayId, asNanosTimePoint(nextVsync),
161                                     static_cast<int32_t>(asNanosDuration(vsyncPeriod)));
162             }
163         }
164 
165         static constexpr const int kLogIntervalSeconds = 60;
166         if (now > (previousLog + std::chrono::seconds(kLogIntervalSeconds))) {
167             DEBUG_LOG("%s: for display:%" PRIu64 " send %" PRIu32 " in last %d seconds",
168                       __FUNCTION__, mDisplayId, vsyncs, kLogIntervalSeconds);
169             (void)vsyncs;
170             previousLog = now;
171             vsyncs = 0;
172         }
173         ++vsyncs;
174     }
175 
176     ALOGI("Vsync thread for display:%" PRId64 " finished", mDisplayId);
177 }
178 
179 }  // namespace aidl::android::hardware::graphics::composer3::impl
180