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 #ifdef __OHOS__ 61 class VSyncReceiver : public RefBase { 62 public: 63 // check 64 using FrameCallback = VSyncCallBackListener::FrameCallback; 65 66 VSyncReceiver(const sptr<IVSyncConnection>& conn, 67 const sptr<IRemoteObject>& token = nullptr, 68 const std::shared_ptr<OHOS::AppExecFwk::EventHandler>& looper = nullptr, 69 const std::string& name = "Uninitialized"); 70 ~VSyncReceiver(); 71 // nocopyable 72 VSyncReceiver(const VSyncReceiver &) = delete; 73 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 74 75 virtual VsyncError Init(); 76 virtual VsyncError RequestNextVSync(FrameCallback callback); 77 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate); 78 virtual VsyncError GetVSyncPeriod(int64_t &period); 79 80 private: 81 sptr<IVSyncConnection> connection_; 82 sptr<IRemoteObject> token_; 83 std::shared_ptr<OHOS::AppExecFwk::EventHandler> looper_; 84 std::shared_ptr<VSyncCallBackListener> listener_; 85 86 std::mutex initMutex_; 87 bool init_; 88 int32_t fd_; 89 std::string name_; 90 }; 91 #else 92 class VSyncReceiver { 93 public: 94 using FrameCallback = VSyncCallBackListener::FrameCallback; 95 96 VSyncReceiver() = default; 97 virtual ~VSyncReceiver() = default; 98 VSyncReceiver(const VSyncReceiver &) = delete; 99 VSyncReceiver &operator=(const VSyncReceiver &) = delete; 100 101 virtual VsyncError Init() = 0; 102 virtual VsyncError RequestNextVSync(FrameCallback callback) = 0; 103 virtual VsyncError SetVSyncRate(FrameCallback callback, int32_t rate) = 0; 104 }; 105 #endif 106 } // namespace Rosen 107 } // namespace OHOS 108 109 #endif 110