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