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_GENERATOR_H 18 #define VSYNC_VSYNC_GENERATOR_H 19 20 #include <refbase.h> 21 #include "graphic_common.h" 22 23 #include <mutex> 24 #include <vector> 25 #include <thread> 26 #include <condition_variable> 27 28 namespace OHOS { 29 namespace Rosen { 30 class VSyncGenerator : public RefBase { 31 public: 32 class Callback : public RefBase { 33 public: 34 virtual void OnVSyncEvent(int64_t now, int64_t period) = 0; 35 }; 36 VSyncGenerator() = default; 37 virtual ~VSyncGenerator() noexcept = default; 38 virtual VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) = 0; 39 virtual VsyncError AddListener(int64_t phase, const sptr<Callback>& cb) = 0; 40 virtual VsyncError RemoveListener(const sptr<Callback>& cb) = 0; 41 virtual VsyncError ChangePhaseOffset(const sptr<Callback>& cb, int64_t offset) = 0; 42 virtual bool IsEnable() = 0; 43 }; 44 45 sptr<VSyncGenerator> CreateVSyncGenerator(); 46 void DestroyVSyncGenerator(); 47 48 namespace impl { 49 class VSyncGenerator : public OHOS::Rosen::VSyncGenerator { 50 public: 51 static sptr<OHOS::Rosen::VSyncGenerator> GetInstance() noexcept; 52 static void DeleteInstance() noexcept; 53 54 // nocopyable 55 VSyncGenerator(const VSyncGenerator &) = delete; 56 VSyncGenerator &operator=(const VSyncGenerator &) = delete; 57 VsyncError UpdateMode(int64_t period, int64_t phase, int64_t refrenceTime) override; 58 VsyncError AddListener(int64_t phase, const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override; 59 VsyncError RemoveListener(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb) override; 60 VsyncError ChangePhaseOffset(const sptr<OHOS::Rosen::VSyncGenerator::Callback>& cb, int64_t offset) override; 61 bool IsEnable() override; 62 63 private: 64 friend class OHOS::Rosen::VSyncGenerator; 65 66 struct Listener { 67 int64_t phase_; 68 sptr<OHOS::Rosen::VSyncGenerator::Callback> callback_; 69 int64_t lastTime_; 70 }; 71 72 VSyncGenerator(); 73 ~VSyncGenerator() noexcept override; 74 75 int64_t ComputeNextVSyncTimeStamp(int64_t now, int64_t refrenceTime); 76 std::vector<Listener> GetListenerTimeouted(int64_t now, int64_t refrenceTime); 77 int64_t ComputeListenerNextVSyncTimeStamp(const Listener &listen, int64_t now, int64_t refrenceTime); 78 void ThreadLoop(); 79 void UpdateWakeupDelay(int64_t occurTimestamp, int64_t nextTimeStamp); 80 81 int64_t period_; 82 int64_t phase_; 83 int64_t refrenceTime_; 84 int64_t wakeupDelay_; 85 86 std::vector<Listener> listeners_; 87 88 std::mutex mutex_; 89 std::condition_variable con_; 90 std::mutex waitForTimeoutMtx_; 91 std::condition_variable waitForTimeoutCon_; 92 std::thread thread_; 93 bool vsyncThreadRunning_; 94 static std::once_flag createFlag_; 95 static sptr<OHOS::Rosen::VSyncGenerator> instance_; 96 }; 97 } // impl 98 } // namespace Rosen 99 } // namespace OHOS 100 101 #endif 102