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: 35 using VSyncCallback = std::function<void(int64_t, void*)>; 36 using VSyncCallbackWithId = std::function<void(int64_t, int64_t, void*)>; 37 using FdShutDownCallback = std::function<void(int32_t)>; 38 using ReadableCallback = std::function<bool(int32_t)>; 39 struct FrameCallback { 40 void *userData_; 41 VSyncCallback callback_; 42 VSyncCallbackWithId callbackWithId_; 43 }; VSyncCallBackListener()44 VSyncCallBackListener() 45 : vsyncCallbacks_(nullptr), vsyncCallbacksWithId_(nullptr), userData_(nullptr) 46 {} 47 ~VSyncCallBackListener()48 ~VSyncCallBackListener() 49 { 50 } SetCallback(FrameCallback cb)51 void SetCallback(FrameCallback cb) 52 { 53 std::lock_guard<std::mutex> locker(mtx_); 54 userData_ = cb.userData_; 55 vsyncCallbacks_ = cb.callback_; 56 vsyncCallbacksWithId_ = cb.callbackWithId_; 57 } SetName(std::string & name)58 void SetName(std::string &name) 59 { 60 std::lock_guard<std::mutex> locker(mtx_); 61 name_ = name; 62 } SetRNVFlag(bool RNVFlag)63 void SetRNVFlag(bool RNVFlag) 64 { 65 std::lock_guard<std::mutex> locker(mtx_); 66 RNVFlag_ = RNVFlag; 67 } GetRNVFlag()68 bool GetRNVFlag() 69 { 70 std::lock_guard<std::mutex> locker(mtx_); 71 return RNVFlag_; 72 } 73 GetPeriod()74 int64_t GetPeriod() 75 { 76 std::lock_guard<std::mutex> locker(mtx_); 77 return period_; 78 } 79 GetTimeStamp()80 int64_t GetTimeStamp() 81 { 82 std::lock_guard<std::mutex> locker(mtx_); 83 return timeStamp_; 84 } 85 GetPeriodShared()86 int64_t GetPeriodShared() 87 { 88 std::lock_guard<std::mutex> locker(mtx_); 89 return periodShared_; 90 } 91 GetTimeStampShared()92 int64_t GetTimeStampShared() 93 { 94 std::lock_guard<std::mutex> locker(mtx_); 95 return timeStampShared_; 96 } 97 AddCallback(FrameCallback cb)98 void AddCallback(FrameCallback cb) 99 { 100 std::lock_guard<std::mutex> locker(mtx_); 101 frameCallbacks_.push_back(cb); 102 } 103 104 void RegisterFdShutDownCallback(FdShutDownCallback cb); 105 void RegisterReadableCallback(ReadableCallback cb); 106 107 void SetFdClosedFlagLocked(bool fdClosed); 108 109 std::mutex fdMutex_; 110 private: 111 void OnReadable(int32_t fileDescriptor) override; 112 void OnShutdown(int32_t fileDescriptor) override; 113 int64_t CalculateExpectedEndLocked(int64_t now); 114 void HandleVsyncCallbacks(int64_t data[], ssize_t dataCount, int32_t fileDescriptor); 115 VsyncError ReadFdInternal(int32_t fd, int64_t (&data)[3], ssize_t &dataCount); 116 VSyncCallback vsyncCallbacks_; 117 VSyncCallbackWithId vsyncCallbacksWithId_; 118 void *userData_; 119 std::mutex mtx_; 120 std::string name_; 121 bool RNVFlag_ = false; 122 int64_t period_ = 0; 123 int64_t timeStamp_ = 0; 124 thread_local static inline int64_t periodShared_ = 0; 125 thread_local static inline int64_t timeStampShared_ = 0; 126 std::vector<FrameCallback> frameCallbacks_ = {}; 127 bool fdClosed_ = false; 128 FdShutDownCallback fdShutDownCallback_ = nullptr; 129 ReadableCallback readableCallback_ = nullptr; 130 std::mutex cbMutex_; 131 }; 132 133 #ifdef __OHOS__ 134 class VSyncReceiver : public RefBase { 135 public: 136 // check 137 using FrameCallback = VSyncCallBackListener::FrameCallback; 138 139 VSyncReceiver(const sptr<IVSyncConnection>& conn, 140 const sptr<IRemoteObject>& token = nullptr, 141 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr, 142 const std::string& name = "Uninitialized"); 143 ~VSyncReceiver(); 144 // nocopyable 145 VSyncReceiver(const VSyncReceiver &) = delete; 146 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 147 148 virtual VsyncError Init(); 149 virtual VsyncError RequestNextVSync(FrameCallback callback); 150 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate); 151 virtual VsyncError GetVSyncPeriod(int64_t &period); 152 virtual VsyncError GetVSyncPeriodAndLastTimeStamp(int64_t &period, int64_t &timeStamp, 153 bool isThreadShared = false); GetFd()154 int32_t GetFd() { return fd_; } 155 156 /* transfer the FD to other process(want to use the FD), 157 the current process does not use the FD, so close FD, but not close vsync connection 158 */ 159 void CloseVsyncReceiverFd(); 160 virtual VsyncError RequestNextVSync(FrameCallback callback, const std::string &fromWhom, 161 int64_t lastVSyncTS); 162 virtual bool IsRequestedNextVSync(); 163 virtual VsyncError SetVsyncCallBackForEveryFrame(FrameCallback callback, bool isOpen); 164 virtual VsyncError SetUiDvsyncSwitch(bool dvsyncSwitch); 165 virtual VsyncError SetUiDvsyncConfig(int32_t bufferCount); 166 virtual VsyncError RequestNextVSyncWithMultiCallback(FrameCallback callback); 167 virtual VsyncError SetNativeDVSyncSwitch(bool dvsyncSwitch); 168 private: 169 void RegisterFileDescriptorListener(); 170 VsyncError DestroyLocked(); 171 void RemoveAndCloseFdLocked(); 172 sptr<IVSyncConnection> connection_; 173 sptr<IRemoteObject> token_; 174 std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_; 175 std::shared_ptr<VSyncCallBackListener> listener_; 176 177 std::mutex initMutex_; 178 bool init_; 179 int32_t fd_; 180 std::string name_; 181 }; 182 #else 183 class VSyncReceiver { 184 public: 185 using FrameCallback = VSyncCallBackListener::FrameCallback; 186 187 VSyncReceiver() = default; 188 virtual ~VSyncReceiver() = default; 189 VSyncReceiver(const VSyncReceiver &) = delete; 190 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 191 192 virtual VsyncError Init() = 0; 193 virtual VsyncError RequestNextVSync(FrameCallback callback) = 0; 194 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate) = 0; 195 }; 196 #endif 197 } // namespace Rosen 198 } // namespace OHOS 199 200 #endif 201