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