• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VSYNC_VSYNC_RECEIVER_H
17 #define VSYNC_VSYNC_RECEIVER_H
18 
19 #include <refbase.h>
20 #include "ivsync_connection.h"
21 #include "file_descriptor_listener.h"
22 
23 #include <atomic>
24 #include <functional>
25 #include <memory>
26 #include <mutex>
27 #include <stdint.h>
28 #include <string>
29 #include <vector>
30 
31 namespace OHOS {
32 namespace Rosen {
33 class VSyncCallBackListener : public OHOS::AppExecFwk::FileDescriptorListener,
34     public std::enable_shared_from_this<VSyncCallBackListener> {
35 public:
36     using VSyncCallback = std::function<void(int64_t, void*)>;
37     using VSyncCallbackWithId = std::function<void(int64_t, int64_t, void*)>;
38     using FdShutDownCallback = std::function<void(int32_t)>;
39     using ReadableCallback = std::function<bool(int32_t)>;
40     struct FrameCallback {
41         void *userData_;
42         VSyncCallback callback_;
43         VSyncCallbackWithId callbackWithId_;
44     };
VSyncCallBackListener()45     VSyncCallBackListener()
46         : vsyncCallbacks_(nullptr), vsyncCallbacksWithId_(nullptr), userData_(nullptr)
47     {}
48 
~VSyncCallBackListener()49     ~VSyncCallBackListener()
50     {
51     }
SetCallback(FrameCallback cb)52     void SetCallback(FrameCallback cb)
53     {
54         std::lock_guard<std::mutex> locker(mtx_);
55         userData_ = cb.userData_;
56         vsyncCallbacks_ = cb.callback_;
57         vsyncCallbacksWithId_ = cb.callbackWithId_;
58     }
SetName(std::string & name)59     void SetName(std::string &name)
60     {
61         std::lock_guard<std::mutex> locker(mtx_);
62         name_ = name;
63     }
SetRNVFlag(bool RNVFlag)64     void SetRNVFlag(bool RNVFlag)
65     {
66         std::lock_guard<std::mutex> locker(mtx_);
67         RNVFlag_ = RNVFlag;
68     }
GetRNVFlag()69     bool GetRNVFlag()
70     {
71         std::lock_guard<std::mutex> locker(mtx_);
72         return RNVFlag_;
73     }
74 
GetPeriod()75     int64_t GetPeriod()
76     {
77         std::lock_guard<std::mutex> locker(mtx_);
78         return period_;
79     }
80 
GetTimeStamp()81     int64_t GetTimeStamp()
82     {
83         std::lock_guard<std::mutex> locker(mtx_);
84         return timeStamp_;
85     }
86 
GetPeriodShared()87     int64_t GetPeriodShared()
88     {
89         std::lock_guard<std::mutex> locker(mtx_);
90         return periodShared_;
91     }
92 
GetTimeStampShared()93     int64_t GetTimeStampShared()
94     {
95         std::lock_guard<std::mutex> locker(mtx_);
96         return timeStampShared_;
97     }
98 
AddCallback(FrameCallback cb)99     void AddCallback(FrameCallback cb)
100     {
101         std::lock_guard<std::mutex> locker(mtx_);
102         frameCallbacks_.push_back(cb);
103     }
104 
105     void RegisterFdShutDownCallback(FdShutDownCallback cb);
106     void RegisterReadableCallback(ReadableCallback cb);
107 
108     void SetFdClosedFlagLocked(bool fdClosed);
109 
110     std::mutex fdMutex_;
111 private:
112     void OnReadable(int32_t fileDescriptor) override;
113     void OnShutdown(int32_t fileDescriptor) override;
114     int64_t CalculateExpectedEndLocked(int64_t now);
115     void HandleVsyncCallbacks(int64_t data[], ssize_t dataCount, int32_t fileDescriptor);
116     VsyncError ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount);
117     VSyncCallback vsyncCallbacks_;
118     VSyncCallbackWithId vsyncCallbacksWithId_;
119     void *userData_;
120     std::mutex mtx_;
121     std::string name_;
122     bool RNVFlag_ = false;
123     int64_t period_ = 0;
124     int64_t timeStamp_ = 0;
125     thread_local static inline int64_t periodShared_ = 0;
126     thread_local static inline int64_t timeStampShared_ = 0;
127     std::vector<FrameCallback> frameCallbacks_ = {};
128     bool fdClosed_ = false;
129     FdShutDownCallback fdShutDownCallback_ = nullptr;
130     ReadableCallback readableCallback_ = nullptr;
131     std::mutex cbMutex_;
132 };
133 
134 #ifdef __OHOS__
135 class VSyncReceiver : public RefBase {
136 public:
137     // check
138     using FrameCallback = VSyncCallBackListener::FrameCallback;
139 
140     VSyncReceiver(const sptr<IVSyncConnection>& conn,
141         const sptr<IRemoteObject>& token = nullptr,
142         const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr,
143         const std::string& name = "Uninitialized");
144     ~VSyncReceiver();
145     // nocopyable
146     VSyncReceiver(const VSyncReceiver &) = delete;
147     VSyncReceiver &operator=(const VSyncReceiver &) = delete;
148 
149     /**
150      * @brief init VSyncReceiver instance.
151      *
152      * @return Returns an error code.
153      */
154     virtual VsyncError Init();
155 
156     /**
157      * @brief request next vsync signal.
158      * if this function been called multi times in one frame, only the last callback will be called.
159      *
160      * @param callback is a callback function which will be called when next vsync signal comes.
161      * @return Returns an error code.
162      */
163     virtual VsyncError RequestNextVSync(FrameCallback callback);
164 
165     /**
166      * @brief set vsync rate.
167      *
168      * @param callback is a callback function which will be called when next vsync signal comes.
169      * @param rate means the frequency of callback function calls.
170      * @return Returns an error code.
171      */
172     virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate);
173 
174     /**
175      * @brief get vsync period.
176      *
177      * @param period is vsync period, the unit is nanoseconds.
178      * @return Returns an error code.
179      */
180     virtual VsyncError GetVSyncPeriod(int64_t &period);
181 
182     /**
183      * @brief get vsync period and last vsync timestamp.
184      *
185      * @param period is vsync period, the unit is nanoseconds.
186      * @param timeStamp is last vsync timestamp, the unit is nanoseconds.
187      * @param isThreadShared means whether the period and timeStamp are thread shared.
188      * @return Returns an error code.
189      */
190     virtual VsyncError GetVSyncPeriodAndLastTimeStamp(int64_t &period, int64_t &timeStamp,
191                                                         bool isThreadShared = false);
192 
193     /**
194      * @brief get the ReceiveFd of the vsync socketpair.
195      *
196      * @return Returns the value of fd.
197      */
GetFd()198     int32_t GetFd() { return fd_; }
199 
200     /* transfer the FD to other process(want to use the FD),
201       the current process does not use the FD, so close FD, but not close vsync connection
202     */
203     void CloseVsyncReceiverFd();
204 
205     /**
206      * @brief request next vsync signal.
207      *
208      * @param callback is a callback function which will be called when next vsync signal comes.
209      * @param fromWhom is the role who request next vsync.
210      * @param lastVSyncTS is last vsync timestamp.
211      * @param requestVsyncTime is the count of request vsync.
212      * @return Returns an error code.
213      */
214     virtual VsyncError RequestNextVSync(FrameCallback callback, const std::string &fromWhom,
215                                         int64_t lastVSyncTS, const int64_t &requestVsyncTime = 0);
216 
217     /**
218      * @brief whether next vsync is requested.
219      *
220      * @return whether next vsync is requested.
221      */
222     virtual bool IsRequestedNextVSync();
223 
224     /**
225      * @brief callback will be called when isOpen is true, and stop when isOpen is false.
226      *
227      * @param callback is a callback function which will be called when next vsync signal comes.
228      * @param isOpen whether the callback function called every frame.
229      * @return Returns an error code.
230      */
231     virtual VsyncError SetVsyncCallBackForEveryFrame(FrameCallback callback, bool isOpen);
232 
233     /**
234      * @brief UI Dvsync feature switch on or switch off.
235      *
236      * @param dvsyncSwitch true: switch on, false: switch off.
237      * @return Returns an error code.
238      */
239     virtual VsyncError SetUiDvsyncSwitch(bool dvsyncSwitch);
240 
241     /**
242      * @brief set Dvsync dynamic configuration.
243      *
244      * @param bufferCount is buffer count.
245      * @param compositeSceneEnable is dvsync compositeScene enable.
246      * @param nativeDelayEnable is dvsync native delay enable.
247      * @param rsDvsyncAnimationList is a animation vector which will enable force rs dvsync feature.
248      * @return Returns an error code.
249      */
250     virtual VsyncError SetUiDvsyncConfig(int32_t bufferCount, bool compositeSceneEnable = false,
251         bool nativeDelayEnable = false, const std::vector<std::string> &rsDvsyncAnimationList = {});
252 
253     /**
254      * @brief request next vsync signal.
255      * if this function been called multi times in one frame, all the callbacks will be called.
256      *
257      * @param callback is a callback function which will be called when next vsync signal comes.
258      * @return Returns an error code.
259      */
260     virtual VsyncError RequestNextVSyncWithMultiCallback(FrameCallback callback);
261 
262     /**
263      * @brief Native Dvsync feature switch on or switch off.
264      *
265      * @param dvsyncSwitch true: switch on, false: switch off.
266      * @return Returns an error code.
267      */
268     virtual VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch);
269 
270     /**
271      * @brief Set Touch Event.
272      *
273      * @param touchType arkui touchType.
274      * @return void.
275      */
276     virtual void SetTouchEvent(int32_t touchType);
277 private:
278     void RegisterFileDescriptorListener(bool hasVsyncThread = false);
279     VsyncError DestroyLocked();
280     void RemoveAndCloseFdLocked();
281     sptr<IVSyncConnection> connection_;
282     sptr<IRemoteObject> token_;
283     std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_;
284     std::shared_ptr<VSyncCallBackListener> listener_;
285 
286     std::mutex initMutex_;
287     bool init_;
288     int32_t fd_;
289     std::string name_;
290 };
291 #else
292 class VSyncReceiver {
293 public:
294     using FrameCallback = VSyncCallBackListener::FrameCallback;
295 
296     VSyncReceiver() = default;
297     virtual ~VSyncReceiver() = default;
298     VSyncReceiver(const VSyncReceiver &) = delete;
299     VSyncReceiver &operator=(const VSyncReceiver &) = delete;
300 
301     virtual VsyncError Init() = 0;
302     virtual VsyncError RequestNextVSync(FrameCallback callback) = 0;
303     virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate) = 0;
304 };
305 #endif
306 } // namespace Rosen
307 } // namespace OHOS
308 
309 #endif
310