blob: e0fb8f9f86db38c9180000fab06e0e39a68ccd57 [file] [log] [blame]
Kevin DuBoise4f27a82019-11-12 11:41:41 -08001/*
2 * Copyright 2019 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#pragma once
18
Kevin DuBoise4f27a82019-11-12 11:41:41 -080019#include <functional>
20#include <memory>
21#include <mutex>
22#include <string>
23#include <string_view>
24#include <unordered_map>
25
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026#include <android-base/thread_annotations.h>
27
Kevin DuBoise4f27a82019-11-12 11:41:41 -080028#include "VSyncDispatch.h"
Leon Scroggins III67388622023-02-06 20:36:20 -050029#include "VsyncSchedule.h"
Kevin DuBoise4f27a82019-11-12 11:41:41 -080030
31namespace android::scheduler {
32
Dominik Laskowskib0054a22022-03-03 09:03:06 -080033class TimeKeeper;
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080034
Kevin DuBoise4f27a82019-11-12 11:41:41 -080035// VSyncDispatchTimerQueueEntry is a helper class representing internal state for each entry in
36// VSyncDispatchTimerQueue hoisted to public for unit testing.
37class VSyncDispatchTimerQueueEntry {
38public:
39 // This is the state of the entry. There are 3 states, armed, running, disarmed.
40 // Valid transition: disarmed -> armed ( when scheduled )
41 // Valid transition: armed -> running -> disarmed ( when timer is called)
42 // Valid transition: armed -> disarmed ( when cancelled )
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080043 VSyncDispatchTimerQueueEntry(std::string name, VSyncDispatch::Callback,
Kevin DuBoisc94ca832019-11-26 12:56:24 -080044 nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080045 std::string_view name() const;
46
47 // Start: functions that are not threadsafe.
48 // Return the last vsync time this callback was invoked.
49 std::optional<nsecs_t> lastExecutedVsyncTarget() const;
50
51 // This moves the state from disarmed->armed and will calculate the wakeupTime.
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080052 ScheduleResult schedule(VSyncDispatch::ScheduleTiming, VSyncTracker&, nsecs_t now);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080053 // This will update armed entries with the latest vsync information. Entry remains armed.
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080054 void update(VSyncTracker&, nsecs_t now);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080055
56 // This will return empty if not armed, or the next calculated wakeup time if armed.
57 // It will not update the wakeupTime.
58 std::optional<nsecs_t> wakeupTime() const;
59
Ady Abraham9c53ee72020-07-22 21:16:18 -070060 std::optional<nsecs_t> readyTime() const;
61
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -080062 std::optional<nsecs_t> targetVsync() const;
63
Kevin DuBoise4f27a82019-11-12 11:41:41 -080064 // This moves state from armed->disarmed.
65 void disarm();
66
67 // This moves the state from armed->running.
68 // Store the timestamp that this was intended for as the last called timestamp.
69 nsecs_t executing();
Kevin DuBois5c18c1c2020-05-27 15:50:50 -070070
71 // Adds a pending upload of the earliestVSync and workDuration that will be applied on the next
72 // call to update()
Ady Abraham9c53ee72020-07-22 21:16:18 -070073 void addPendingWorkloadUpdate(VSyncDispatch::ScheduleTiming);
Kevin DuBois5c18c1c2020-05-27 15:50:50 -070074
75 // Checks if there is a pending update to the workload, returning true if so.
76 bool hasPendingWorkloadUpdate() const;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080077 // End: functions that are not threadsafe.
78
Kevin DuBois2968afc2020-01-14 09:48:50 -080079 // Invoke the callback with the two given timestamps, moving the state from running->disarmed.
Ady Abraham9c53ee72020-07-22 21:16:18 -070080 void callback(nsecs_t vsyncTimestamp, nsecs_t wakeupTimestamp, nsecs_t deadlineTimestamp);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080081 // Block calling thread while the callback is executing.
82 void ensureNotRunning();
83
Ady Abraham5e7371c2020-03-24 14:47:24 -070084 void dump(std::string& result) const;
85
Kevin DuBoise4f27a82019-11-12 11:41:41 -080086private:
Ady Abraham3fcfd8b2022-07-12 12:31:00 -070087 nsecs_t adjustVsyncIfNeeded(VSyncTracker& tracker, nsecs_t nextVsyncTime) const;
88
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080089 const std::string mName;
90 const VSyncDispatch::Callback mCallback;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080091
Ady Abraham9c53ee72020-07-22 21:16:18 -070092 VSyncDispatch::ScheduleTiming mScheduleTiming;
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080093 const nsecs_t mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080094
95 struct ArmingInfo {
96 nsecs_t mActualWakeupTime;
97 nsecs_t mActualVsyncTime;
Ady Abraham9c53ee72020-07-22 21:16:18 -070098 nsecs_t mActualReadyTime;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080099 };
100 std::optional<ArmingInfo> mArmedInfo;
101 std::optional<nsecs_t> mLastDispatchTime;
102
Ady Abraham9c53ee72020-07-22 21:16:18 -0700103 std::optional<VSyncDispatch::ScheduleTiming> mWorkloadUpdateInfo;
Kevin DuBois5c18c1c2020-05-27 15:50:50 -0700104
Ady Abraham5e7371c2020-03-24 14:47:24 -0700105 mutable std::mutex mRunningMutex;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800106 std::condition_variable mCv;
107 bool mRunning GUARDED_BY(mRunningMutex) = false;
108};
109
110/*
111 * VSyncDispatchTimerQueue is a class that will dispatch callbacks as per VSyncDispatch interface
112 * using a single timer queue.
113 */
114class VSyncDispatchTimerQueue : public VSyncDispatch {
115public:
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800116 // Constructs a VSyncDispatchTimerQueue.
117 // \param[in] tk A timekeeper.
118 // \param[in] tracker A tracker.
119 // \param[in] timerSlack The threshold at which different similarly timed callbacks
120 // should be grouped into one wakeup.
121 // \param[in] minVsyncDistance The minimum distance between two vsync estimates before the
122 // vsyncs are considered the same vsync event.
Leon Scroggins III67388622023-02-06 20:36:20 -0500123 VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper>, VsyncSchedule::TrackerPtr,
124 nsecs_t timerSlack, nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800125 ~VSyncDispatchTimerQueue();
126
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800127 CallbackToken registerCallback(Callback, std::string callbackName) final;
128 void unregisterCallback(CallbackToken) final;
129 ScheduleResult schedule(CallbackToken, ScheduleTiming) final;
Ady Abraham011f8ba2022-11-22 15:09:07 -0800130 ScheduleResult update(CallbackToken, ScheduleTiming) final;
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800131 CancelResult cancel(CallbackToken) final;
132 void dump(std::string&) const final;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800133
134private:
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -0800135 VSyncDispatchTimerQueue(const VSyncDispatchTimerQueue&) = delete;
136 VSyncDispatchTimerQueue& operator=(const VSyncDispatchTimerQueue&) = delete;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800137
Ady Abraham2139f732019-11-13 18:56:40 -0800138 using CallbackMap =
139 std::unordered_map<CallbackToken, std::shared_ptr<VSyncDispatchTimerQueueEntry>>;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800140
141 void timerCallback();
142 void setTimer(nsecs_t, nsecs_t) REQUIRES(mMutex);
143 void rearmTimer(nsecs_t now) REQUIRES(mMutex);
144 void rearmTimerSkippingUpdateFor(nsecs_t now, CallbackMap::iterator const& skipUpdate)
145 REQUIRES(mMutex);
146 void cancelTimer() REQUIRES(mMutex);
Ady Abraham011f8ba2022-11-22 15:09:07 -0800147 ScheduleResult scheduleLocked(CallbackToken, ScheduleTiming) REQUIRES(mMutex);
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800148
Ray Chin7a1e7422023-07-17 16:39:22 +0800149 std::mutex mutable mMutex;
150
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800151 static constexpr nsecs_t kInvalidTime = std::numeric_limits<int64_t>::max();
152 std::unique_ptr<TimeKeeper> const mTimeKeeper;
Leon Scroggins III67388622023-02-06 20:36:20 -0500153 VsyncSchedule::TrackerPtr mTracker;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800154 nsecs_t const mTimerSlack;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800155 nsecs_t const mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800156
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800157 size_t mCallbackToken GUARDED_BY(mMutex) = 0;
158
159 CallbackMap mCallbacks GUARDED_BY(mMutex);
160 nsecs_t mIntendedWakeupTime GUARDED_BY(mMutex) = kInvalidTime;
Kevin DuBoisecb1f0d2019-12-12 10:47:41 -0800161
Ady Abraham75398722020-04-07 14:08:45 -0700162 // For debugging purposes
163 nsecs_t mLastTimerCallback GUARDED_BY(mMutex) = kInvalidTime;
164 nsecs_t mLastTimerSchedule GUARDED_BY(mMutex) = kInvalidTime;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800165};
166
167} // namespace android::scheduler