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     auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
46     std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver = rsClient.CreateVSyncReceiver(vsyncName);
47     if (receiver == nullptr) {
48         VLOGE("Create VSyncReceiver failed");
49         return nullptr;
50     }
51     int ret = receiver->Init();
52     if (ret != 0) {
53         VLOGE("VSyncReceiver Init failed, ret:%{public}d", ret);
54         return nullptr;
55     }
56     NativeVSync* nativeVSync = new NativeVSync;
57     nativeVSync->receiver_ = receiver;
58     return OH_NativeVSync_NativeVSyncToOHNativeVSync(nativeVSync);
59 }
60 
OH_NativeVSync_Destroy(OH_NativeVSync *nativeVSync)61 void OH_NativeVSync_Destroy(OH_NativeVSync *nativeVSync)
62 {
63     if (nativeVSync == nullptr) {
64         VLOGE("parameter is nullptr, please check");
65         return;
66     }
67 
68     delete OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVSync);
69     nativeVSync = nullptr;
70 }
71 
OH_NativeVSync_RequestFrame(OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)72 int OH_NativeVSync_RequestFrame(OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
73 {
74     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
75     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
76         VLOGE("parameter is nullptr, please check");
77         return VSYNC_ERROR_INVALID_ARGUMENTS;
78     }
79     OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
80         .userData_ = data,
81         .callback_ = callback,
82     };
83     return nativeVSync->receiver_->RequestNextVSync(frameCallback);
84 }
85 
OH_NativeVSync_RequestFrameWithMultiCallback( OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)86 int OH_NativeVSync_RequestFrameWithMultiCallback(
87     OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
88 {
89     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
90     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
91         VLOGE("parameter is nullptr, please check");
92         return VSYNC_ERROR_INVALID_ARGUMENTS;
93     }
94     OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
95         .userData_ = data,
96         .callback_ = callback,
97     };
98     return nativeVSync->receiver_->RequestNextVSyncWithMultiCallback(frameCallback);
99 }
100 
OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period)101 int OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period)
102 {
103     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVsync);
104     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || period == nullptr) {
105         VLOGE("parameter is nullptr, please check");
106         return VSYNC_ERROR_INVALID_ARGUMENTS;
107     }
108     return nativeVSync->receiver_->GetVSyncPeriod(*reinterpret_cast<int64_t*>(period));
109 }
110 
OH_NativeVSync_DVSyncSwitch(OH_NativeVSync* ohNativeVSync, bool enable)111 int OH_NativeVSync_DVSyncSwitch(OH_NativeVSync* ohNativeVSync, bool enable)
112 {
113     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
114     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr) {
115         VLOGE("parameter is nullptr, please check");
116         return VSYNC_ERROR_INVALID_ARGUMENTS;
117     }
118     return nativeVSync->receiver_->SetNativeDVSyncSwitch(enable);
119 }
120