blob: 265d89c4b4149f6c8606d9b5c80813347cc9a6e7 [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;
Ady Abraham13bc0be2020-02-27 16:48:20 -080033class PredictedVsyncTracer;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080034
35// TODO (b/145217110): consider renaming.
Kevin DuBois00287382019-11-19 15:11:55 -080036class VSyncReactor : public android::DispSync {
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080037public:
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080038 VSyncReactor(std::unique_ptr<Clock> clock, std::unique_ptr<VSyncDispatch> dispatch,
Dan Stoza027d3652020-05-26 17:26:34 -070039 std::unique_ptr<VSyncTracker> tracker, size_t pendingFenceLimit,
40 bool supportKernelIdleTimer);
Kevin DuBoisf91e9232019-11-21 10:51:23 -080041 ~VSyncReactor();
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080042
Kevin DuBois00287382019-11-19 15:11:55 -080043 bool addPresentFence(const std::shared_ptr<FenceTime>& fence) final;
44 void setIgnorePresentFences(bool ignoration) final;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080045
Ady Abraham0ed31c92020-04-16 11:48:45 -070046 nsecs_t computeNextRefresh(int periodOffset, nsecs_t now) const final;
47 nsecs_t expectedPresentTime(nsecs_t now) final;
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080048
Kevin DuBois00287382019-11-19 15:11:55 -080049 void setPeriod(nsecs_t period) final;
50 nsecs_t getPeriod() final;
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080051
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080052 // TODO: (b/145626181) remove begin,endResync functions from DispSync i/f when possible.
Kevin DuBois00287382019-11-19 15:11:55 -080053 void beginResync() final;
Ady Abraham5dee2f12020-02-05 17:49:47 -080054 bool addResyncSample(nsecs_t timestamp, std::optional<nsecs_t> hwcVsyncPeriod,
55 bool* periodFlushed) final;
Kevin DuBois00287382019-11-19 15:11:55 -080056 void endResync() final;
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080057
Kevin DuBoisf91e9232019-11-21 10:51:23 -080058 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
Kevin DuBois00287382019-11-19 15:11:55 -080059 nsecs_t lastCallbackTime) final;
60 status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback) final;
61 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase) final;
62
63 void dump(std::string& result) const final;
64 void reset() final;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080065
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080066private:
Kevin DuBois02d5ed92020-01-27 11:05:46 -080067 void setIgnorePresentFencesInternal(bool ignoration) REQUIRES(mMutex);
68 void updateIgnorePresentFencesInternal() REQUIRES(mMutex);
Kevin DuBoisc4cdd372020-01-09 14:12:02 -080069 void startPeriodTransition(nsecs_t newPeriod) REQUIRES(mMutex);
70 void endPeriodTransition() REQUIRES(mMutex);
Ady Abraham5dee2f12020-02-05 17:49:47 -080071 bool periodConfirmed(nsecs_t vsync_timestamp, std::optional<nsecs_t> hwcVsyncPeriod)
72 REQUIRES(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080073
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080074 std::unique_ptr<Clock> const mClock;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080075 std::unique_ptr<VSyncTracker> const mTracker;
Kevin DuBois00287382019-11-19 15:11:55 -080076 std::unique_ptr<VSyncDispatch> const mDispatch;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080077 size_t const mPendingLimit;
78
Ady Abraham5e7371c2020-03-24 14:47:24 -070079 mutable std::mutex mMutex;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080080 bool mInternalIgnoreFences GUARDED_BY(mMutex) = false;
81 bool mExternalIgnoreFences GUARDED_BY(mMutex) = false;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080082 std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
Kevin DuBoisf77025c2019-12-18 16:13:24 -080083
84 bool mMoreSamplesNeeded GUARDED_BY(mMutex) = false;
Kevin DuBois02d5ed92020-01-27 11:05:46 -080085 bool mPeriodConfirmationInProgress GUARDED_BY(mMutex) = false;
Kevin DuBoisf77025c2019-12-18 16:13:24 -080086 std::optional<nsecs_t> mPeriodTransitioningTo GUARDED_BY(mMutex);
87 std::optional<nsecs_t> mLastHwVsync GUARDED_BY(mMutex);
88
Kevin DuBoisf91e9232019-11-21 10:51:23 -080089 std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks
90 GUARDED_BY(mMutex);
Ady Abraham13bc0be2020-02-27 16:48:20 -080091
92 const std::unique_ptr<PredictedVsyncTracer> mPredictedVsyncTracer;
Dan Stoza027d3652020-05-26 17:26:34 -070093 const bool mSupportKernelIdleTimer = false;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080094};
95
Kevin DuBois00287382019-11-19 15:11:55 -080096class SystemClock : public Clock {
97 nsecs_t now() const final;
98};
99
Kevin DuBoisb2501ba2019-11-12 14:20:29 -0800100} // namespace android::scheduler