1 /*
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 <ui/FenceTime.h>
21 #include <memory>
22 #include <mutex>
23 #include <unordered_map>
24 #include <vector>
25 #include "TimeKeeper.h"
26 #include "VsyncController.h"
27 namespace android::scheduler {
28 
29 class Clock;
30 class VSyncDispatch;
31 class VSyncTracker;
32 
33 // TODO (b/145217110): consider renaming.
34 class VSyncReactor : public VsyncController {
35 public:
36     VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker, size_t pendingFenceLimit,
37                  bool supportKernelIdleTimer);
38     ~VSyncReactor();
39 
40     bool addPresentFence(const std::shared_ptr<android::FenceTime>& fence) final;
41     void setIgnorePresentFences(bool ignore) final;
42 
43     void startPeriodTransition(nsecs_t period) final;
44 
45     bool addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
46                              bool* periodFlushed) final;
47 
48     void dump(std::string& result) const final;
49 
50 private:
51     void setIgnorePresentFencesInternal(bool ignore) REQUIRES(mMutex);
52     void updateIgnorePresentFencesInternal() REQUIRES(mMutex);
53     void startPeriodTransitionInternal(nsecs_t newPeriod) REQUIRES(mMutex);
54     void endPeriodTransition() REQUIRES(mMutex);
55     bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod)
56             REQUIRES(mMutex);
57 
58     std::unique_ptr<Clock> const mClock;
59     VSyncTracker& mTracker;
60     size_t const mPendingLimit;
61 
62     mutable std::mutex mMutex;
63     bool mInternalIgnoreFences GUARDED_BY(mMutex) = false;
64     bool mExternalIgnoreFences GUARDED_BY(mMutex) = false;
65     std::vector<std::shared_ptr<android::FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
66 
67     bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false;
68     bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false;
69     std::optional<nsecs_t> mPeriodTransitioningTo GUARDED_BY(mMutex);
70     std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex);
71 
72     const bool mSupportKernelIdleTimer = false;
73 };
74 
75 class SystemClock : public Clock {
76     nsecs_t now() const final;
77 };
78 
79 } // namespace android::scheduler
80