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 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() : period_(0), timeStamp_(0), 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 int64_t period_; 53 int64_t timeStamp_; 54 thread_local static inline int64_t periodShared_ = 0; 55 thread_local static inline int64_t timeStampShared_ = 0; 56 57 private: 58 void OnReadable(int32_t fileDescriptor) override; 59 VSyncCallback vsyncCallbacks_; 60 void *userData_; 61 std::mutex mtx_; 62 }; 63 64 #ifdef __OHOS__ 65 class VSyncReceiver : public RefBase { 66 public: 67 // check 68 using FrameCallback = VSyncCallBackListener::FrameCallback; 69 70 VSyncReceiver(const sptr<IVSyncConnection>& conn, 71 const sptr<IRemoteObject>& token = nullptr, 72 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr, 73 const std::string& name = "Uninitialized"); 74 ~VSyncReceiver(); 75 // nocopyable 76 VSyncReceiver(const VSyncReceiver &) = delete; 77 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 78 79 virtual VsyncError Init(); 80 virtual VsyncError RequestNextVSync(FrameCallback callback); 81 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate); 82 virtual VsyncError GetVSyncPeriod(int64_t &period); 83 virtual VsyncError GetVSyncPeriodAndLastTimeStamp(int64_t &period, int64_t &timeStamp, 84 bool isThreadShared = false); GetFd()85 int32_t GetFd() { return fd_; } 86 87 /* transfer the FD to other process(want to use the FD), 88 the current process does not use the FD, so close FD, but not close vsync connection 89 */ 90 void CloseVsyncReceiverFd(); 91 private: 92 VsyncError Destroy(); 93 sptr<IVSyncConnection> connection_; 94 sptr<IRemoteObject> token_; 95 std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_; 96 std::shared_ptr<VSyncCallBackListener> listener_; 97 98 std::mutex initMutex_; 99 bool init_; 100 int32_t fd_; 101 std::string name_; 102 }; 103 #else 104 class VSyncReceiver { 105 public: 106 using FrameCallback = VSyncCallBackListener::FrameCallback; 107 108 VSyncReceiver() = default; 109 virtual ~VSyncReceiver() = default; 110 VSyncReceiver(const VSyncReceiver &) = delete; 111 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 112 113 virtual VsyncError Init() = 0; 114 virtual VsyncError RequestNextVSync(FrameCallback callback) = 0; 115 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate) = 0; 116 }; 117 #endif 118 } // namespace Rosen 119 } // namespace OHOS 120 121 #endif 122