blob: 2415a66a6429ae8502489c37507ebd3ffb1e2ad3 [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
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080019#include <memory>
20#include <mutex>
Kevin DuBoisf91e9232019-11-21 10:51:23 -080021#include <unordered_map>
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080022#include <vector>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080023
24#include <android-base/thread_annotations.h>
Leon Scroggins III67388622023-02-06 20:36:20 -050025#include <ui/DisplayId.h>
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080026#include <ui/FenceTime.h>
27
28#include <scheduler/TimeKeeper.h>
29
Ady Abrahamc585dba2023-11-15 18:41:35 -080030#include "VSyncTracker.h"
Ady Abraham8cb21882020-08-26 18:22:05 -070031#include "VsyncController.h"
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080032
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080033namespace android::scheduler {
34
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080035class Clock;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080036class VSyncDispatch;
37class VSyncTracker;
38
39// TODO (b/145217110): consider renaming.
Ady Abraham8cb21882020-08-26 18:22:05 -070040class VSyncReactor : public VsyncController {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080041public:
Leon Scroggins III67388622023-02-06 20:36:20 -050042 VSyncReactor(PhysicalDisplayId, std::unique_ptr<Clock> clock, VSyncTracker& tracker,
43 size_t pendingFenceLimit, bool supportKernelIdleTimer);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080044 ~VSyncReactor();
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080045
Dominik Laskowski068173d2021-08-11 17:22:59 -070046 bool addPresentFence(std::shared_ptr<FenceTime>) final;
Ady Abraham8cb21882020-08-26 18:22:05 -070047 void setIgnorePresentFences(bool ignore) final;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080048
Ady Abrahamc585dba2023-11-15 18:41:35 -080049 void onDisplayModeChanged(ftl::NonNull<DisplayModePtr>, bool force) final;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080050
Ady Abraham8cb21882020-08-26 18:22:05 -070051 bool addHwVsyncTimestamp(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
52 bool* periodFlushed) final;
Kevin DuBois00287382019-11-19 15:11:55 -080053
Rachel Lee6a9731d2022-06-06 17:08:14 -070054 void setDisplayPowerMode(hal::PowerMode powerMode) final;
55
Kevin DuBois00287382019-11-19 15:11:55 -080056 void dump(std::string& result) const final;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080057
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080058private:
Ady Abraham8cb21882020-08-26 18:22:05 -070059 void setIgnorePresentFencesInternal(bool ignore) REQUIRES(mMutex);
Kevin DuBois02d5ed92020-01-27 11:05:46 -080060 void updateIgnorePresentFencesInternal() REQUIRES(mMutex);
Ady Abrahamc585dba2023-11-15 18:41:35 -080061 void startPeriodTransitionInternal(ftl::NonNull<DisplayModePtr>) REQUIRES(mMutex);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -080062 void endPeriodTransition() REQUIRES(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -080063 bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod)
64 REQUIRES(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080065
Leon Scroggins III67388622023-02-06 20:36:20 -050066 const PhysicalDisplayId mId;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080067 std::unique_ptr<Clock> const mClock;
Ady Abraham5a858552020-03-31 17:54:56 -070068 VSyncTracker& mTracker;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080069 size_t const mPendingLimit;
70
Ady Abraham5e7371c2020-03-24 14:47:24 -070071 mutable std::mutex mMutex;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080072 bool mInternalIgnoreFences GUARDED_BY(mMutex) = false;
73 bool mExternalIgnoreFences GUARDED_BY(mMutex) = false;
Ady Abraham8cb21882020-08-26 18:22:05 -070074 std::vector<std::shared_ptr<android::FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080075
76 bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080077 bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false;
Ady Abrahamc585dba2023-11-15 18:41:35 -080078 DisplayModePtr mModePtrTransitioningTo GUARDED_BY(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080079 std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex);
80
Rachel Lee6a9731d2022-06-06 17:08:14 -070081 hal::PowerMode mDisplayPowerMode GUARDED_BY(mMutex) = hal::PowerMode::ON;
82
Dan Stoza027d3652020-05-26 17:26:34 -070083 const bool mSupportKernelIdleTimer = false;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080084};
85
Kevin DuBois00287382019-11-19 15:11:55 -080086class SystemClock : public Clock {
87 nsecs_t now() const final;
88};
89
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080090} // namespace android::scheduler