| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 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> | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 23 | #include <unordered_map> | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 24 | #include <vector> | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 25 | #include "DispSync.h" | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 26 | #include "TimeKeeper.h" | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 27 | namespace android::scheduler { | 
 | 28 |  | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 29 | class Clock; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 30 | class VSyncDispatch; | 
 | 31 | class VSyncTracker; | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 32 | class CallbackRepeater; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 33 |  | 
 | 34 | // TODO (b/145217110): consider renaming. | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 35 | class VSyncReactor : public android::DispSync { | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 36 | public: | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 37 |     VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch, | 
 | 38 |                  std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit); | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 39 |     ~VSyncReactor(); | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 40 |  | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 41 |     bool addPresentFence(const std::shared_ptr<FenceTime>& fence) final; | 
 | 42 |     void setIgnorePresentFences(bool ignoration) final; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 43 |  | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 44 |     nsecs_t computeNextRefresh(int periodOffset) const final; | 
 | 45 |     nsecs_t expectedPresentTime() final; | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 46 |  | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 47 |     void setPeriod(nsecs_t period) final; | 
 | 48 |     nsecs_t getPeriod() final; | 
| Kevin DuBois | ee2ad9f | 2019-11-21 11:10:57 -0800 | [diff] [blame] | 49 |  | 
| Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 50 |     // TODO: (b/145626181) remove begin,endResync functions from DispSync i/f when possible. | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 51 |     void beginResync() final; | 
| Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 52 |     bool addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, | 
 | 53 |                          bool* periodFlushed) final; | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 54 |     void endResync() final; | 
| Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 55 |  | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 56 |     status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback, | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 57 |                               nsecs_t lastCallbackTime) final; | 
 | 58 |     status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) final; | 
 | 59 |     status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) final; | 
 | 60 |  | 
 | 61 |     void dump(std::string& result) const final; | 
 | 62 |     void reset() final; | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 63 |  | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 64 | private: | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 65 |     void setIgnorePresentFencesInternal(bool ignoration) REQUIRES(mMutex); | 
 | 66 |     void updateIgnorePresentFencesInternal() REQUIRES(mMutex); | 
| Kevin DuBois | c4cdd37 | 2020-01-09 14:12:02 -0800 | [diff] [blame] | 67 |     void startPeriodTransition(nsecs_t newPeriod) REQUIRES(mMutex); | 
 | 68 |     void endPeriodTransition() REQUIRES(mMutex); | 
| Ady Abraham | 5dee2f1 | 2020-02-05 17:49:47 -0800 | [diff] [blame] | 69 |     bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod) | 
 | 70 |             REQUIRES(mMutex); | 
| Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 71 |  | 
| Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 72 |     std::unique_ptr<Clock> const mClock; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 73 |     std::unique_ptr<VSyncTracker> const mTracker; | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 74 |     std::unique_ptr<VSyncDispatch> const mDispatch; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 75 |     size_t const mPendingLimit; | 
 | 76 |  | 
 | 77 |     std::mutex mMutex; | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 78 |     bool mInternalIgnoreFences GUARDED_BY(mMutex) = false; | 
 | 79 |     bool mExternalIgnoreFences GUARDED_BY(mMutex) = false; | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 80 |     std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex); | 
| Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 81 |  | 
 | 82 |     bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false; | 
| Kevin DuBois | 02d5ed9 | 2020-01-27 11:05:46 -0800 | [diff] [blame] | 83 |     bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false; | 
| Kevin DuBois | f77025c | 2019-12-18 16:13:24 -0800 | [diff] [blame] | 84 |     std::optional<nsecs_t> mPeriodTransitioningTo GUARDED_BY(mMutex); | 
 | 85 |     std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex); | 
 | 86 |  | 
| Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 87 |     std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks | 
 | 88 |             GUARDED_BY(mMutex); | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 89 | }; | 
 | 90 |  | 
| Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame] | 91 | class SystemClock : public Clock { | 
 | 92 |     nsecs_t now() const final; | 
 | 93 | }; | 
 | 94 |  | 
| Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 95 | } // namespace android::scheduler |