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