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