1 /* 2 * Copyright (c) 2022 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_ADAPTER_IMPL_H 17 #define VSYNC_ADAPTER_IMPL_H 18 19 #include <functional> 20 #include <memory> 21 22 #include "event_handler.h" 23 #include "graphic_adapter.h" 24 #include "vsync_receiver.h" 25 #include "foundation/graphic/graphic_2d/rosen/modules/render_service_client/core/ui/rs_frame_rate_linker.h" 26 27 namespace OHOS::NWeb { 28 class VSyncAdapterImpl : public VSyncAdapter { 29 public: 30 VSyncAdapterImpl() = default; 31 ~VSyncAdapterImpl() override; 32 VSyncAdapterImpl(const VSyncAdapterImpl&) = delete; 33 VSyncAdapterImpl& operator=(const VSyncAdapterImpl&) = delete; 34 35 static VSyncAdapterImpl& GetInstance(); 36 VSyncErrorCode RequestVsync(void* data, NWebVSyncCb cb) override; 37 int64_t GetVSyncPeriod() override; 38 void SetFrameRateLinkerEnable(bool enabled) override; 39 void SetFramePreferredRate(int32_t preferredRate) override; 40 41 void SetOnVsyncCallback(void (*callback)()) override; 42 void SetOnVsyncEndCallback(void (*onVsyncEndCallback)()) override; 43 void SetIsGPUProcess(bool isGPU); 44 private: 45 static void OnVsync(int64_t timestamp, void* data); 46 void VsyncCallbackInner(int64_t nanoTimestamp); 47 VSyncErrorCode Init(); 48 49 std::mutex mtx_; 50 bool hasRequestedVsync_ = false; 51 bool hasReportedKeyThread_ = false; 52 std::shared_ptr<Rosen::VSyncReceiver> receiver_ = nullptr; 53 std::shared_ptr<OHOS::AppExecFwk::EventHandler> vsyncHandler_; 54 std::unordered_map<void*, NWebVSyncCb> vsyncCallbacks_; 55 Rosen::VSyncReceiver::FrameCallback frameCallback_ = { 56 .userData_ = this, 57 .callback_ = OnVsync, 58 }; 59 std::shared_ptr<Rosen::RSFrameRateLinker> frameRateLinker_; 60 static void (*callback_)(); 61 static void (*onVsyncEndCallback_)(); 62 bool frameRateLinkerEnable_ = false; 63 bool isGPUProcess_ = false; 64 }; 65 } // namespace OHOS::NWeb 66 67 #endif // VSYNC_ADAPTER_IMPL_H 68