• 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 
17 #ifndef VSYNC_VSYNC_RECEIVER_H
18 #define VSYNC_VSYNC_RECEIVER_H
19 
20 #include <refbase.h>
21 #include "ivsync_connection.h"
22 #include "file_descriptor_listener.h"
23 
24 #include <atomic>
25 #include <functional>
26 #include <memory>
27 #include <mutex>
28 #include <string>
29 
30 namespace OHOS {
31 namespace Rosen {
32 class VSyncCallBackListener : public OHOS::AppExecFwk::FileDescriptorListener {
33 public:
34     using VSyncCallback = std::function<void(int64_t, void*)>;
35     struct FrameCallback {
36         void *userData_;
37         VSyncCallback callback_;
38     };
VSyncCallBackListener()39     VSyncCallBackListener() : vsyncCallbacks_(nullptr), userData_(nullptr)
40     {
41     }
42 
~VSyncCallBackListener()43     ~VSyncCallBackListener()
44     {
45     }
SetCallback(FrameCallback cb)46     void SetCallback(FrameCallback cb)
47     {
48         std::lock_guard<std::mutex> locker(mtx_);
49         vsyncCallbacks_ = cb.callback_;
50         userData_ = cb.userData_;
51     }
52 
53 private:
54     void OnReadable(int32_t fileDescriptor) override;
55     VSyncCallback vsyncCallbacks_;
56     void *userData_;
57     std::mutex mtx_;
58 };
59 
60 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__gnu_linux__)
61 class VSyncReceiver : public RefBase {
62 public:
63     // check
64     using FrameCallback = VSyncCallBackListener::FrameCallback;
65 
66     VSyncReceiver(const sptr<IVSyncConnection>& conn,
67         const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr,
68         const std::string& name = "Uninitialized");
69     ~VSyncReceiver();
70     // nocopyable
71     VSyncReceiver(const VSyncReceiver &) = delete;
72     VSyncReceiver &operator=(const VSyncReceiver &) = delete;
73 
74     virtual VsyncError Init();
75     virtual VsyncError RequestNextVSync(FrameCallback callback);
76     virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate);
77 
78 private:
79     sptr<IVSyncConnection> connection_;
80     std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_;
81     std::shared_ptr<VSyncCallBackListener> listener_;
82 
83     std::mutex initMutex_;
84     bool init_;
85     int32_t fd_;
86     std::string name_;
87 };
88 #else
89 class VSyncReceiver {
90 public:
91     // check
92     using FrameCallback = VSyncCallBackListener::FrameCallback;
93 
94     VSyncReceiver() = default;
95     virtual ~VSyncReceiver() = default;
96     VSyncReceiver(const VSyncReceiver &) = delete;
97     VSyncReceiver &operator=(const VSyncReceiver &) = delete;
98 
99     virtual VsyncError Init() = 0;
100     virtual VsyncError RequestNextVSync(FrameCallback callback) = 0;
101     virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate) = 0;
102 };
103 #endif
104 } // namespace Rosen
105 } // namespace OHOS
106 
107 #endif
108