1 /*
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 
18 #include <mutex>
19 #include <string>
20 
21 #include "DispSync.h"
22 #include "EventThread.h"
23 #include "TracedOrdinal.h"
24 
25 namespace android {
26 
27 class DispSyncSource final : public VSyncSource, private DispSync::Callback {
28 public:
29     DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync, const char* name);
30 
31     ~DispSyncSource() override = default;
32 
33     // The following methods are implementation of VSyncSource.
getName()34     const char* getName() const override { return mName; }
35     void setVSyncEnabled(bool enable) override;
36     void setCallback(VSyncSource::Callback* callback) override;
37     void setPhaseOffset(nsecs_t phaseOffset) override;
38 
39     void dump(std::string&) const override;
40 
41 private:
42     // The following method is the implementation of the DispSync::Callback.
43     void onDispSyncEvent(nsecs_t when, nsecs_t expectedVSyncTimestamp) override;
44 
45     const char* const mName;
46     TracedOrdinal<int> mValue;
47 
48     const bool mTraceVsync;
49     const std::string mVsyncOnLabel;
50     nsecs_t mLastCallbackTime GUARDED_BY(mVsyncMutex) = 0;
51 
52     DispSync* mDispSync;
53 
54     std::mutex mCallbackMutex;
55     VSyncSource::Callback* mCallback GUARDED_BY(mCallbackMutex) = nullptr;
56 
57     mutable std::mutex mVsyncMutex;
58     TracedOrdinal<nsecs_t> mPhaseOffset GUARDED_BY(mVsyncMutex);
59     bool mEnabled GUARDED_BY(mVsyncMutex) = false;
60 };
61 
62 } // namespace android
63