blob: 837eb75005d1ca20daaf310c37827753de66cb2a [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 DuBoisb2501ba2019-11-12 14:20:29 -080026
27namespace 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.
35class VSyncReactor /* TODO (b/140201379): : public android::DispSync */ {
36public:
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
41 bool addPresentFence(const std::shared_ptr<FenceTime>& fence);
42 void setIgnorePresentFences(bool ignoration);
43
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080044 nsecs_t computeNextRefresh(int periodOffset) const;
45 nsecs_t expectedPresentTime();
46
Kevin DuBoisee2ad9f2019-11-21 11:10:57 -080047 void setPeriod(nsecs_t period);
48 nsecs_t getPeriod();
49
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080050 // TODO: (b/145626181) remove begin,endResync functions from DispSync i/f when possible.
51 void beginResync();
52 bool addResyncSample(nsecs_t timestamp, bool* periodFlushed);
53 void endResync();
54
Kevin DuBoisf91e9232019-11-21 10:51:23 -080055 status_t addEventListener(const char* name, nsecs_t phase, DispSync::Callback* callback,
56 nsecs_t lastCallbackTime);
57 status_t removeEventListener(DispSync::Callback* callback, nsecs_t* outLastCallback);
58 status_t changePhaseOffset(DispSync::Callback* callback, nsecs_t phase);
59
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080060private:
Kevin DuBois2fd3cea2019-11-14 08:52:45 -080061 std::unique_ptr<Clock> const mClock;
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080062 std::unique_ptr<VSyncDispatch> const mDispatch;
63 std::unique_ptr<VSyncTracker> const mTracker;
64 size_t const mPendingLimit;
65
66 std::mutex mMutex;
67 bool mIgnorePresentFences GUARDED_BY(mMutex) = false;
68 std::vector<std::shared_ptr<FenceTime>> mUnfiredFences GUARDED_BY(mMutex);
Kevin DuBoisa9fdab72019-11-14 09:44:14 -080069 bool mPeriodChangeInProgress GUARDED_BY(mMutex) = false;
Kevin DuBoisf91e9232019-11-21 10:51:23 -080070 std::unordered_map<DispSync::Callback*, std::unique_ptr<CallbackRepeater>> mCallbacks
71 GUARDED_BY(mMutex);
Kevin DuBoisb2501ba2019-11-12 14:20:29 -080072};
73
74} // namespace android::scheduler