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; |
| 52 | bool addResyncSample(nsecs_t timestamp, bool* periodFlushed) final; |
| 53 | void endResync() final; |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 54 | |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 55 | status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback, |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame^] | 56 | nsecs_t lastCallbackTime) final; |
| 57 | status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) final; |
| 58 | status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) final; |
| 59 | |
| 60 | void dump(std::string& result) const final; |
| 61 | void reset() final; |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 62 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 63 | private: |
Kevin DuBois | 2fd3cea | 2019-11-14 08:52:45 -0800 | [diff] [blame] | 64 | std::unique_ptr<Clock> const mClock; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 65 | std::unique_ptr<VSyncTracker> const mTracker; |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame^] | 66 | std::unique_ptr<VSyncDispatch> const mDispatch; |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 67 | size_t const mPendingLimit; |
| 68 | |
| 69 | std::mutex mMutex; |
| 70 | bool mIgnorePresentFences GUARDED_BY(mMutex) = false; |
| 71 | std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex); |
Kevin DuBois | a9fdab7 | 2019-11-14 09:44:14 -0800 | [diff] [blame] | 72 | bool mPeriodChangeInProgress GUARDED_BY(mMutex) = false; |
Kevin DuBois | f91e923 | 2019-11-21 10:51:23 -0800 | [diff] [blame] | 73 | std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks |
| 74 | GUARDED_BY(mMutex); |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Kevin DuBois | 0028738 | 2019-11-19 15:11:55 -0800 | [diff] [blame^] | 77 | class SystemClock : public Clock { |
| 78 | nsecs_t now() const final; |
| 79 | }; |
| 80 | |
Kevin DuBois | b2501ba | 2019-11-12 14:20:29 -0800 | [diff] [blame] | 81 | } // namespace android::scheduler |