blob: 0c9b4fe53e83cf81a7e833c41d483a6a8866701a [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
19#include <android-base/thread_annotations.h>
20#include <functional>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <string_view>
25#include <unordered_map>
26
27#include "VSyncDispatch.h"
28
29namespace android::scheduler {
30
31// VSyncDispatchTimerQueueEntry is a helper class representing internal state for each entry in
32// VSyncDispatchTimerQueue hoisted to public for unit testing.
33class VSyncDispatchTimerQueueEntry {
34public:
35 // This is the state of the entry. There are 3 states, armed, running, disarmed.
36 // Valid transition: disarmed -> armed ( when scheduled )
37 // Valid transition: armed -> running -> disarmed ( when timer is called)
38 // Valid transition: armed -> disarmed ( when cancelled )
Kevin DuBoisc94ca832019-11-26 12:56:24 -080039 VSyncDispatchTimerQueueEntry(std::string const& name, std::function<void(nsecs_t)> const& fn,
40 nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080041 std::string_view name() const;
42
43 // Start: functions that are not threadsafe.
44 // Return the last vsync time this callback was invoked.
45 std::optional<nsecs_t> lastExecutedVsyncTarget() const;
46
47 // This moves the state from disarmed->armed and will calculate the wakeupTime.
Kevin DuBois2311b1a2019-11-18 16:19:08 -080048 ScheduleResult schedule(nsecs_t workDuration, nsecs_t earliestVsync, VSyncTracker& tracker,
49 nsecs_t now);
Kevin DuBoise4f27a82019-11-12 11:41:41 -080050 // This will update armed entries with the latest vsync information. Entry remains armed.
51 void update(VSyncTracker& tracker, nsecs_t now);
52
53 // This will return empty if not armed, or the next calculated wakeup time if armed.
54 // It will not update the wakeupTime.
55 std::optional<nsecs_t> wakeupTime() const;
56
57 // This moves state from armed->disarmed.
58 void disarm();
59
60 // This moves the state from armed->running.
61 // Store the timestamp that this was intended for as the last called timestamp.
62 nsecs_t executing();
63 // End: functions that are not threadsafe.
64
65 // Invoke the callback with the timestamp, moving the state from running->disarmed.
66 void callback(nsecs_t timestamp);
67 // Block calling thread while the callback is executing.
68 void ensureNotRunning();
69
70private:
Kevin DuBoise4f27a82019-11-12 11:41:41 -080071 std::string const mName;
72 std::function<void(nsecs_t)> const mCallback;
73
74 nsecs_t mWorkDuration;
75 nsecs_t mEarliestVsync;
Kevin DuBoisc94ca832019-11-26 12:56:24 -080076 nsecs_t const mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -080077
78 struct ArmingInfo {
79 nsecs_t mActualWakeupTime;
80 nsecs_t mActualVsyncTime;
81 };
82 std::optional<ArmingInfo> mArmedInfo;
83 std::optional<nsecs_t> mLastDispatchTime;
84
85 std::mutex mRunningMutex;
86 std::condition_variable mCv;
87 bool mRunning GUARDED_BY(mRunningMutex) = false;
88};
89
90/*
91 * VSyncDispatchTimerQueue is a class that will dispatch callbacks as per VSyncDispatch interface
92 * using a single timer queue.
93 */
94class VSyncDispatchTimerQueue : public VSyncDispatch {
95public:
Kevin DuBoisc94ca832019-11-26 12:56:24 -080096 // Constructs a VSyncDispatchTimerQueue.
97 // \param[in] tk A timekeeper.
98 // \param[in] tracker A tracker.
99 // \param[in] timerSlack The threshold at which different similarly timed callbacks
100 // should be grouped into one wakeup.
101 // \param[in] minVsyncDistance The minimum distance between two vsync estimates before the
102 // vsyncs are considered the same vsync event.
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800103 explicit VSyncDispatchTimerQueue(std::unique_ptr<TimeKeeper> tk, VSyncTracker& tracker,
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800104 nsecs_t timerSlack, nsecs_t minVsyncDistance);
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800105 ~VSyncDispatchTimerQueue();
106
107 CallbackToken registerCallback(std::function<void(nsecs_t)> const& callbackFn,
108 std::string callbackName) final;
109 void unregisterCallback(CallbackToken token) final;
110 ScheduleResult schedule(CallbackToken token, nsecs_t workDuration, nsecs_t earliestVsync) final;
111 CancelResult cancel(CallbackToken token) final;
112
113private:
114 VSyncDispatchTimerQueue(VSyncDispatchTimerQueue const&) = delete;
115 VSyncDispatchTimerQueue& operator=(VSyncDispatchTimerQueue const&) = delete;
116
Ady Abraham2139f732019-11-13 18:56:40 -0800117 using CallbackMap =
118 std::unordered_map<CallbackToken, std::shared_ptr<VSyncDispatchTimerQueueEntry>>;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800119
120 void timerCallback();
121 void setTimer(nsecs_t, nsecs_t) REQUIRES(mMutex);
122 void rearmTimer(nsecs_t now) REQUIRES(mMutex);
123 void rearmTimerSkippingUpdateFor(nsecs_t now, CallbackMap::iterator const& skipUpdate)
124 REQUIRES(mMutex);
125 void cancelTimer() REQUIRES(mMutex);
126
127 static constexpr nsecs_t kInvalidTime = std::numeric_limits<int64_t>::max();
128 std::unique_ptr<TimeKeeper> const mTimeKeeper;
129 VSyncTracker& mTracker;
130 nsecs_t const mTimerSlack;
Kevin DuBoisc94ca832019-11-26 12:56:24 -0800131 nsecs_t const mMinVsyncDistance;
Kevin DuBoise4f27a82019-11-12 11:41:41 -0800132
133 std::mutex mutable mMutex;
134 size_t mCallbackToken GUARDED_BY(mMutex) = 0;
135
136 CallbackMap mCallbacks GUARDED_BY(mMutex);
137 nsecs_t mIntendedWakeupTime GUARDED_BY(mMutex) = kInvalidTime;
138};
139
140} // namespace android::scheduler