blob: 29a0a11689b098cf23f6b2f095d22ea8420d0b12 [file] [log] [blame]
Kevin DuBoisb2501ba2019-11-12 14:20:29 -08001/*
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 DuBoisf91e9232019-11-21 10:51:23 -080023#include <unordered_map>
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080024#include <vector>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080025#include "DispSync.h"
Kevin DuBois00287382019-11-19 15:11:55 -080026#include "TimeKeeper.h"
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080027namespace android::scheduler {
28
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080029class Clock;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080030class VSyncDispatch;
31class VSyncTracker;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080032class CallbackRepeater;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080033
34// TODO (b/145217110): consider renaming.
Kevin DuBois00287382019-11-19 15:11:55 -080035class VSyncReactor : public android::DispSync {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080036public:
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080037 VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
38 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080039 ~VSyncReactor();
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080040
Kevin DuBois00287382019-11-19 15:11:55 -080041 bool addPresentFence(const std::shared_ptr<FenceTime>& fence) final;
42 void setIgnorePresentFences(bool ignoration) final;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080043
Kevin DuBois00287382019-11-19 15:11:55 -080044 nsecs_t computeNextRefresh(int periodOffset) const final;
45 nsecs_t expectedPresentTime() final;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080046
Kevin DuBois00287382019-11-19 15:11:55 -080047 void setPeriod(nsecs_t period) final;
48 nsecs_t getPeriod() final;
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080049
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080050 // TODO: (b/145626181) remove begin,endResync functions from DispSync i/f when possible.
Kevin DuBois00287382019-11-19 15:11:55 -080051 void beginResync() final;
52 bool addResyncSample(nsecs_t timestamp, bool* periodFlushed) final;
53 void endResync() final;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080054
Kevin DuBoisf91e9232019-11-21 10:51:23 -080055 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
Kevin DuBois00287382019-11-19 15:11:55 -080056 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 DuBoisf91e9232019-11-21 10:51:23 -080062
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080063private:
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080064 std::unique_ptr<Clock> const mClock;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080065 std::unique_ptr<VSyncTracker> const mTracker;
Kevin DuBois00287382019-11-19 15:11:55 -080066 std::unique_ptr<VSyncDispatch> const mDispatch;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080067 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 DuBoisa9fdab72019-11-14 09:44:14 -080072 bool mPeriodChangeInProgress GUARDED_BY(mMutex) = false;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080073 std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks
74 GUARDED_BY(mMutex);
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080075};
76
Kevin DuBois00287382019-11-19 15:11:55 -080077class SystemClock : public Clock {
78 nsecs_t now() const final;
79};
80
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080081} // namespace android::scheduler