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 #include "vsync_adapter_impl.h"
17
18 #include "nweb_log.h"
19 #include "transaction/rs_interfaces.h"
20
21 namespace OHOS::NWeb {
RequestVsync(void * data,std::function<void (int64_t,void *)> NWebVSyncCb)22 VSyncErrorCode VSyncAdapterImpl::RequestVsync(void* data, std::function<void(int64_t, void*)> NWebVSyncCb)
23 {
24 if (!receiver_) {
25 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
26 receiver_ = rsClient.CreateVSyncReceiver("NWeb_" + std::to_string(::getpid()));
27 if (!receiver_) {
28 WVLOG_E("CreateVSyncReceiver failed");
29 return VSyncErrorCode::ERROR;
30 }
31 VsyncError ret = receiver_->Init();
32 if (ret != VsyncError::GSERROR_OK) {
33 WVLOG_E("vsync receiver init failed, ret=%{public}d", ret);
34 receiver_ = nullptr;
35 return VSyncErrorCode::ERROR;
36 }
37
38 frameCb_ = std::make_unique<Rosen::VSyncReceiver::FrameCallback>();
39 if (!frameCb_) {
40 WVLOG_E("vsync frame callback make failed");
41 receiver_ = nullptr;
42 return VSyncErrorCode::ERROR;
43 }
44 frameCb_->userData_ = data;
45 frameCb_->callback_ = NWebVSyncCb;
46 }
47
48 int ret = receiver_->RequestNextVSync(*frameCb_);
49 if (ret != VsyncError::GSERROR_OK) {
50 WVLOG_E("NWebWindowAdapter RequestVsync RequestNextVSync fail, ret=%{public}d", ret);
51 return VSyncErrorCode::ERROR;
52 }
53 return VSyncErrorCode::SUCCESS;
54 }
55 } // namespace OHOS::NWeb