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 "DispSync.h" 26 #include "TimeKeeper.h" 27 namespace android::scheduler { 28 29 class Clock; 30 class VSyncDispatch; 31 class VSyncTracker; 32 class CallbackRepeater; 33 class PredictedVsyncTracer; 34 35 // TODO (b/145217110): consider renaming. 36 class VSyncReactor : public android::DispSync { 37 public: 38 VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch, 39 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit, 40 bool supportKernelIdleTimer); 41 ~VSyncReactor(); 42 43 bool addPresentFence(const std::shared_ptr<FenceTime>& fence) final; 44 void setIgnorePresentFences(bool ignoration) final; 45 46 nsecs_t computeNextRefresh(int periodOffset, nsecs_t now) const final; 47 nsecs_t expectedPresentTime(nsecs_t now) final; 48 49 void setPeriod(nsecs_t period) final; 50 nsecs_t getPeriod() final; 51 52 // TODO: (b/145626181) remove begin,endResync functions from DispSync i/f when possible. 53 void beginResync() final; 54 bool addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod, 55 bool* periodFlushed) final; 56 void endResync() final; 57 58 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback, 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; 65 66 private: 67 void setIgnorePresentFencesInternal(bool ignoration) REQUIRES(mMutex); 68 void updateIgnorePresentFencesInternal() REQUIRES(mMutex); 69 void startPeriodTransition(nsecs_t newPeriod) REQUIRES(mMutex); 70 void endPeriodTransition() REQUIRES(mMutex); 71 bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod) 72 REQUIRES(mMutex); 73 74 std::unique_ptr<Clock> const mClock; 75 std::unique_ptr<VSyncTracker> const mTracker; 76 std::unique_ptr<VSyncDispatch> const mDispatch; 77 size_t const mPendingLimit; 78 79 mutable std::mutex mMutex; 80 bool mInternalIgnoreFences GUARDED_BY(mMutex) = false; 81 bool mExternalIgnoreFences GUARDED_BY(mMutex) = false; 82 std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex); 83 84 bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false; 85 bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false; 86 std::optional<nsecs_t> mPeriodTransitioningTo GUARDED_BY(mMutex); 87 std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex); 88 89 std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks 90 GUARDED_BY(mMutex); 91 92 const std::unique_ptr<PredictedVsyncTracer> mPredictedVsyncTracer; 93 const bool mSupportKernelIdleTimer = false; 94 }; 95 96 class SystemClock : public Clock { 97 nsecs_t now() const final; 98 }; 99 100 } // namespace android::scheduler 101