1 /*
2 * Copyright (c) 2022-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_receiver.h>
17 #include "transaction/rs_interfaces.h"
18 #include "vsync_log.h"
19 #include "native_vsync.h"
20
21 using namespace OHOS;
22
23 namespace {
24 struct NativeVSync {
25 std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver_;
26 };
27 }
OH_NativeVSync_OHNativeVSyncToNativeVSync(OH_NativeVSync * ohNativeVSync)28 static NativeVSync* OH_NativeVSync_OHNativeVSyncToNativeVSync(OH_NativeVSync* ohNativeVSync)
29 {
30 return reinterpret_cast<NativeVSync*>(ohNativeVSync);
31 }
32
OH_NativeVSync_NativeVSyncToOHNativeVSync(NativeVSync * nativeVSync)33 static OH_NativeVSync* OH_NativeVSync_NativeVSyncToOHNativeVSync(NativeVSync* nativeVSync)
34 {
35 return reinterpret_cast<OH_NativeVSync*>(nativeVSync);
36 }
37
OH_NativeVSync_Create(const char * name,unsigned int length)38 OH_NativeVSync* OH_NativeVSync_Create(const char* name, unsigned int length)
39 {
40 if (name == nullptr) {
41 VLOGE("name is nullptr, please check");
42 return nullptr;
43 }
44 std::string vsyncName(name, length);
45 NativeVSync* nativeVSync = new NativeVSync;
46 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
47 std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver = rsClient.CreateVSyncReceiver(vsyncName);
48 int ret = receiver->Init();
49 if (ret != 0) {
50 delete nativeVSync;
51 VLOGE("VSyncReceiver Init failed, ret:%{public}d", ret);
52 return nullptr;
53 }
54 nativeVSync->receiver_ = receiver;
55 return OH_NativeVSync_NativeVSyncToOHNativeVSync(nativeVSync);
56 }
57
OH_NativeVSync_Destroy(OH_NativeVSync * nativeVSync)58 void OH_NativeVSync_Destroy(OH_NativeVSync *nativeVSync)
59 {
60 if (nativeVSync == nullptr) {
61 VLOGE("parameter is nullptr, please check");
62 return;
63 }
64
65 delete OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVSync);
66 }
67
OH_NativeVSync_RequestFrame(OH_NativeVSync * ohNativeVSync,OH_NativeVSync_FrameCallback callback,void * data)68 int OH_NativeVSync_RequestFrame(OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
69 {
70 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
71 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
72 VLOGE("parameter is nullptr, please check");
73 return -1;
74 }
75 OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
76 .userData_ = data,
77 .callback_ = callback,
78 };
79 return nativeVSync->receiver_->RequestNextVSync(frameCallback);
80 }
81
OH_NativeVSync_GetPeriod(OH_NativeVSync * nativeVsync,long long * period)82 int OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period)
83 {
84 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVsync);
85 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || period == nullptr) {
86 VLOGE("parameter is nullptr, please check");
87 return -1;
88 }
89 return nativeVSync->receiver_->GetVSyncPeriod(*reinterpret_cast<int64_t*>(period));
90 }
91