blob: a9d536be284f44868a513a02f9906d4a6f6d16ad [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 DuBois00287382019-11-19 15:11:55 -080025#include "TimeKeeper.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070026#include "VsyncController.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;
32
33// TODO (b/145217110): consider renaming.
Ady Abraham8cb21882020-08-26 18:22:05 -070034class VSyncReactor : public VsyncController {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080035public:
Ady Abraham8735eac2020-08-12 16:35:04 -070036 VSyncReactor(std::unique_ptr<Clock> clock, VSyncTracker& tracker, size_t pendingFenceLimit,
37 bool supportKernelIdleTimer);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080038 ~VSyncReactor();
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080039
Dominik Laskowski068173d2021-08-11 17:22:59 -070040 bool addPresentFence(std::shared_ptr<FenceTime>) final;
Ady Abraham8cb21882020-08-26 18:22:05 -070041 void setIgnorePresentFences(bool ignore) final;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080042
Ady Abraham8cb21882020-08-26 18:22:05 -070043 void startPeriodTransition(nsecs_t period) final;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080044
Ady Abraham8cb21882020-08-26 18:22:05 -070045 bool addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
46 bool* periodFlushed) final;
Kevin DuBois00287382019-11-19 15:11:55 -080047
48 void dump(std::string& result) const final;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080049
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080050private:
Ady Abraham8cb21882020-08-26 18:22:05 -070051 void setIgnorePresentFencesInternal(bool ignore) REQUIRES(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080052 void updateIgnorePresentFencesInternal() REQUIRES(mMutex);
Ady Abraham8cb21882020-08-26 18:22:05 -070053 void startPeriodTransitionInternal(nsecs_t newPeriod) REQUIRES(mMutex);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -080054 void endPeriodTransition() REQUIRES(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -080055 bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod)
56 REQUIRES(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080057
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080058 std::unique_ptr<Clock> const mClock;
Ady Abraham5a858552020-03-31 17:54:56 -070059 VSyncTracker& mTracker;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080060 size_t const mPendingLimit;
61
Ady Abraham5e7371c2020-03-24 14:47:24 -070062 mutable std::mutex mMutex;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080063 bool mInternalIgnoreFences GUARDED_BY(mMutex) = false;
64 bool mExternalIgnoreFences GUARDED_BY(mMutex) = false;
Ady Abraham8cb21882020-08-26 18:22:05 -070065 std::vector<std::shared_ptr<android::FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080066
67 bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080068 bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false;
Kevin DuBoisf77025c2019-12-18 16:13:24 -080069 std::optional<nsecs_t> mPeriodTransitioningTo GUARDED_BY(mMutex);
70 std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex);
71
Dan Stoza027d3652020-05-26 17:26:34 -070072 const bool mSupportKernelIdleTimer = false;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080073};
74
Kevin DuBois00287382019-11-19 15:11:55 -080075class SystemClock : public Clock {
76 nsecs_t now() const final;
77};
78
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080079} // namespace android::scheduler