• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
70 
OH_NativeVSync_RequestFrame(OH_NativeVSync * ohNativeVSync,OH_NativeVSync_FrameCallback callback,void * data)71 int OH_NativeVSync_RequestFrame(OH_NativeVSync *ohNativeVSync, OH_NativeVSync_FrameCallback callback, void* data)
72 {
73     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(ohNativeVSync);
74     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || callback == nullptr) {
75         VLOGE("parameter is nullptr, please check");
76         return -1;
77     }
78     OHOS::Rosen::VSyncReceiver::FrameCallback frameCallback = {
79         .userData_ = data,
80         .callback_ = callback,
81     };
82     return nativeVSync->receiver_->RequestNextVSync(frameCallback);
83 }
84 
OH_NativeVSync_GetPeriod(OH_NativeVSync * nativeVsync,long long * period)85 int OH_NativeVSync_GetPeriod(OH_NativeVSync* nativeVsync, long long* period)
86 {
87     NativeVSync* nativeVSync = OH_NativeVSync_OHNativeVSyncToNativeVSync(nativeVsync);
88     if (nativeVSync == nullptr || nativeVSync->receiver_ == nullptr || period == nullptr) {
89         VLOGE("parameter is nullptr, please check");
90         return -1;
91     }
92     return nativeVSync->receiver_->GetVSyncPeriod(*reinterpret_cast<int64_t*>(period));
93 }
94