• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "render_service_client/core/feature/hyper_graphic_manager/rs_frame_rate_linker.h"
26 
27 namespace OHOS::NWeb {
28 using SetApsSceneFuncType = bool(*)(
29     std::string, std::string, uint32_t);
30 class VSyncAdapterImpl : public VSyncAdapter {
31 public:
32     VSyncAdapterImpl() = default;
33     ~VSyncAdapterImpl() override;
34     VSyncAdapterImpl(const VSyncAdapterImpl&) = delete;
35     VSyncAdapterImpl& operator=(const VSyncAdapterImpl&) = delete;
36 
37     static VSyncAdapterImpl& GetInstance();
38     VSyncErrorCode RequestVsync(void* data, NWebVSyncCb cb) override;
39     int64_t GetVSyncPeriod() override;
40     void SetFrameRateLinkerEnable(bool enabled) override;
41     void SetFramePreferredRate(int32_t preferredRate) override;
42 
43     void SetOnVsyncCallback(void (*callback)()) override;
44     void SetOnVsyncEndCallback(void (*onVsyncEndCallback)()) override;
45     void SetIsGPUProcess(bool isGPU);
46 
47     void SetScene(const std::string& sceneName, uint32_t state) override;
48     void SetDVSyncSwitch(bool dvsyncSwitch) override;
49 private:
50     static void OnVsync(int64_t timestamp, void* data);
51     void VsyncCallbackInner(int64_t nanoTimestamp);
52     VSyncErrorCode Init();
53     void InitAPSClient();
54     void UninitAPSClient();
55 
56     std::mutex mtx_;
57     bool hasRequestedVsync_ = false;
58     bool hasReportedKeyThread_ = false;
59     std::shared_ptr<Rosen::VSyncReceiver> receiver_ = nullptr;
60     std::shared_ptr<OHOS::AppExecFwk::EventHandler> vsyncHandler_;
61     std::unordered_map<void*, NWebVSyncCb> vsyncCallbacks_;
62     Rosen::VSyncReceiver::FrameCallback frameCallback_ = {
63         .userData_ = this,
64         .callback_ = OnVsync,
65     };
66     std::shared_ptr<Rosen::RSFrameRateLinker> frameRateLinker_;
67     static void (*callback_)();
68     static void (*onVsyncEndCallback_)();
69     bool frameRateLinkerEnable_ = false;
70     bool isGPUProcess_ = false;
71     std::string pkgName_ {""};
72     void* apsClientHandler_ {nullptr};
73     SetApsSceneFuncType setApsSceneFunc_ {nullptr};
74 };
75 } // namespace OHOS::NWeb
76 
77 #endif // VSYNC_ADAPTER_IMPL_H
78