• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 class VSyncReceiver : public RefBase {
61 public:
62     // check
63     using FrameCallback = VSyncCallBackListener::FrameCallback;
64 
65     VSyncReceiver(const sptr<IVSyncConnection>& conn,
66         const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr,
67         const std::string& name = "Uninitialized");
68     ~VSyncReceiver();
69     // nocopyable
70     VSyncReceiver(const VSyncReceiver &) = delete;
71     VSyncReceiver &operator=(const VSyncReceiver &) = delete;
72 
73     VsyncError Init();
74     VsyncError RequestNextVSync(FrameCallback callback);
75     VsyncError SetVSyncRate(FrameCallback callback, int32_t rate);
76 
77 private:
78     sptr<IVSyncConnection> connection_;
79     std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_;
80     std::shared_ptr<VSyncCallBackListener> listener_;
81 
82     std::mutex initMutex_;
83     bool init_;
84     int32_t fd_;
85     std::string name_;
86 };
87 } // namespace Rosen
88 } // namespace OHOS
89 
90 #endif
91