blob: edcd3ac709f31dfb37c0d26813787686fb847975 [file] [log] [blame]
Ana Krulec241cf832018-08-10 15:03:23 -07001/*
2 * Copyright 2018 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#pragma once
17
Ana Krulec072233f2018-08-10 15:03:23 -070018#include <mutex>
19#include <string>
Ana Krulec241cf832018-08-10 15:03:23 -070020
Ana Krulec241cf832018-08-10 15:03:23 -070021#include "EventThread.h"
Ady Abraham50204dd2019-07-19 15:47:11 -070022#include "TracedOrdinal.h"
Ady Abraham9c53ee72020-07-22 21:16:18 -070023#include "VSyncDispatch.h"
Ana Krulec241cf832018-08-10 15:03:23 -070024
Ady Abraham9c53ee72020-07-22 21:16:18 -070025namespace android::scheduler {
26class CallbackRepeater;
Rachel Leeef2e21f2022-02-01 14:51:34 -080027class VSyncTracker;
Ana Krulec241cf832018-08-10 15:03:23 -070028
Ady Abraham9c53ee72020-07-22 21:16:18 -070029class DispSyncSource final : public VSyncSource {
Ana Krulec241cf832018-08-10 15:03:23 -070030public:
Rachel Leeef2e21f2022-02-01 14:51:34 -080031 DispSyncSource(VSyncDispatch& vSyncDispatch, VSyncTracker& vSyncTracker,
32 std::chrono::nanoseconds workDuration, std::chrono::nanoseconds readyDuration,
33 bool traceVsync, const char* name);
Ana Krulec241cf832018-08-10 15:03:23 -070034
Ady Abraham9c53ee72020-07-22 21:16:18 -070035 ~DispSyncSource() override;
Ana Krulec241cf832018-08-10 15:03:23 -070036
37 // The following methods are implementation of VSyncSource.
Dominik Laskowski6505f792019-09-18 11:10:05 -070038 const char* getName() const override { return mName; }
Ana Krulec241cf832018-08-10 15:03:23 -070039 void setVSyncEnabled(bool enable) override;
40 void setCallback(VSyncSource::Callback* callback) override;
Ady Abraham9c53ee72020-07-22 21:16:18 -070041 void setDuration(std::chrono::nanoseconds workDuration,
42 std::chrono::nanoseconds readyDuration) override;
Rachel Leeef2e21f2022-02-01 14:51:34 -080043 VSyncData getLatestVSyncData() const override;
Ana Krulec241cf832018-08-10 15:03:23 -070044
Ady Abraham5e7371c2020-03-24 14:47:24 -070045 void dump(std::string&) const override;
46
Ana Krulec241cf832018-08-10 15:03:23 -070047private:
Ady Abraham9c53ee72020-07-22 21:16:18 -070048 void onVsyncCallback(nsecs_t vsyncTime, nsecs_t targetWakeupTime, nsecs_t readyTime);
Ana Krulec241cf832018-08-10 15:03:23 -070049
50 const char* const mName;
Ady Abraham50204dd2019-07-19 15:47:11 -070051 TracedOrdinal<int> mValue;
Ana Krulec241cf832018-08-10 15:03:23 -070052
53 const bool mTraceVsync;
Ana Krulec072233f2018-08-10 15:03:23 -070054 const std::string mVsyncOnLabel;
Ana Krulec241cf832018-08-10 15:03:23 -070055
Rachel Leeef2e21f2022-02-01 14:51:34 -080056 const VSyncTracker& mVSyncTracker;
57
Ady Abraham9c53ee72020-07-22 21:16:18 -070058 std::unique_ptr<CallbackRepeater> mCallbackRepeater;
Ana Krulec241cf832018-08-10 15:03:23 -070059
Ana Krulec072233f2018-08-10 15:03:23 -070060 std::mutex mCallbackMutex;
61 VSyncSource::Callback* mCallback GUARDED_BY(mCallbackMutex) = nullptr;
Ana Krulec241cf832018-08-10 15:03:23 -070062
Ady Abraham5e7371c2020-03-24 14:47:24 -070063 mutable std::mutex mVsyncMutex;
Ady Abraham9c53ee72020-07-22 21:16:18 -070064 TracedOrdinal<std::chrono::nanoseconds> mWorkDuration GUARDED_BY(mVsyncMutex);
65 std::chrono::nanoseconds mReadyDuration GUARDED_BY(mVsyncMutex);
Ana Krulec072233f2018-08-10 15:03:23 -070066 bool mEnabled GUARDED_BY(mVsyncMutex) = false;
Ana Krulec241cf832018-08-10 15:03:23 -070067};
68
Ady Abraham9c53ee72020-07-22 21:16:18 -070069} // namespace android::scheduler