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
CreateAndInitVSyncReceiver(const std::string & vsyncName,uint64_t windowID=0,bool isAssociatedWindow=false)38 std::shared_ptr<OHOS::Rosen::VSyncReceiver> CreateAndInitVSyncReceiver(
39 const std::string& vsyncName,
40 uint64_t windowID = 0,
41 bool isAssociatedWindow = false)
42 {
43 auto& rsClient = OHOS::Rosen::RSInterfaces::GetInstance();
44 std::shared_ptr<OHOS::Rosen::VSyncReceiver> receiver;
45 if (isAssociatedWindow) {
46 receiver = rsClient.CreateVSyncReceiver(vsyncName, 0, nullptr, windowID, true);
47 } else {
48 receiver = rsClient.CreateVSyncReceiver(vsyncName);
49 }
50 if (receiver == nullptr) {
51 VLOGE("Create VSyncReceiver failed");
52 return nullptr;
53 }
54 int ret = receiver->Init();
55 if (ret != 0) {
56 VLOGE("VSyncReceiver Init failed, ret:%{public}d", ret);
57 return nullptr;
58 }
59 return receiver;
60 }
61
OH_NativeVSync_Create(const char * name,unsigned int length)62 OH_NativeVSync* OH_NativeVSync_Create(const char* name, unsigned int length)
63 {
64 if (name == nullptr) {
65 VLOGE("name is nullptr, please check");
66 return nullptr;
67 }
68 std::string vsyncName(name, length);
69 auto receiver = CreateAndInitVSyncReceiver(vsyncName);
70 if (receiver == nullptr) {
71 return nullptr;
72 }
73 NativeVSync* nativeVSync = new NativeVSync;
74 nativeVSync->receiver_ = receiver;
75 return OH_NativeVSync_NativeVSyncToOHNativeVSync(nativeVSync);
76 }
77
OH_NativeVSync_Create_ForAssociatedWindow(uint64_t windowID,const char * name,unsigned int length)78 OH_NativeVSync* OH_NativeVSync_Create_ForAssociatedWindow(uint64_t windowID, const char* name, unsigned int length)
79 {
80 if (name == nullptr) {
81 VLOGE("name is nullptr, please check");
82 return nullptr;
83 }
84 std::string vsyncName(name, length);
85 auto receiver = CreateAndInitVSyncReceiver(vsyncName, windowID, true);
86 if (receiver == nullptr) {
87 VLOGE("receiver is nullptr, please check");
88 return nullptr;
89 }
90 NativeVSync* nativeVSync = new NativeVSync;
91 nativeVSync->receiver_ = receiver;
92 return OH_NativeVSync_NativeVSyncToOHNativeVSync(nativeVSync);
93 }
94
OH_NativeVSync_Destroy(OH_NativeVSync * nativeVSync)95 void OH_NativeVSync_Destroy(OH_NativeVSync *nativeVSync)
96 {
97 if (nativeVSync == nullptr) {
98 VLOGE("parameter is nullptr, please check");
99 return;
100 }
101
102 delete OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVSync);
103 }
104
OH_NativeVSync_RequestFrame(OH_NativeVSync * ohNativeVSync,OH_NativeVSync_FrameCallback callback,void * data)105 int OH_NativeVSync_RequestFrame(OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
106 {
107 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
108 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
109 VLOGE("parameter is nullptr, please check");
110 return VSYNC_ERROR_INVALID_ARGUMENTS;
111 }
112 OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
113 .userData_ = data,
114 .callback_ = callback,
115 };
116 return nativeVSync->receiver_->RequestNextVSync(frameCallback);
117 }
118
OH_NativeVSync_RequestFrameWithMultiCallback(OH_NativeVSync * ohNativeVSync,OH_NativeVSync_FrameCallback callback,void * data)119 int OH_NativeVSync_RequestFrameWithMultiCallback(
120 OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
121 {
122 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
123 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
124 VLOGE("parameter is nullptr, please check");
125 return VSYNC_ERROR_INVALID_ARGUMENTS;
126 }
127 OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
128 .userData_ = data,
129 .callback_ = callback,
130 };
131 return nativeVSync->receiver_->RequestNextVSyncWithMultiCallback(frameCallback);
132 }
133
OH_NativeVSync_GetPeriod(OH_NativeVSync * nativeVsync,long long * period)134 int OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period)
135 {
136 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVsync);
137 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || period == nullptr) {
138 VLOGE("parameter is nullptr, please check");
139 return VSYNC_ERROR_INVALID_ARGUMENTS;
140 }
141 return nativeVSync->receiver_->GetVSyncPeriod(*reinterpret_cast<int64_t*>(period));
142 }
143
OH_NativeVSync_DVSyncSwitch(OH_NativeVSync * ohNativeVSync,bool enable)144 int OH_NativeVSync_DVSyncSwitch(OH_NativeVSync* ohNativeVSync, bool enable)
145 {
146 NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
147 if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr) {
148 VLOGE("parameter is nullptr, please check");
149 return VSYNC_ERROR_INVALID_ARGUMENTS;
150 }
151 return nativeVSync->receiver_->SetNativeDVSyncSwitch(enable);
152 }