1 /*
2 * Copyright (c) 2024 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 #include "display_sync_impl.h"
16
17 #include <cstring>
18 #include "cj_lambda.h"
19 #include "display_sync_error.h"
20 #include "ffi_remote_data.h"
21 #include "securec.h"
22
23 using namespace OHOS::DisplaySync;
24 using namespace OHOS::FFI;
25
26 namespace OHOS::DisplaySync {
RegisterOnFrameCallback(void (* callback)(CIntervalInfo))27 void DisplaySync::RegisterOnFrameCallback(void (*callback)(CIntervalInfo))
28 {
29 uiDisplaySync_->RegisterOnFrameWithData(
30 [callbackCJ = CJLambda::Create(callback)] (Ace::RefPtr<Ace::DisplaySyncData> displaySyncData) {
31 CIntervalInfo intervalInfo;
32 intervalInfo.timestamp = displaySyncData->timestamp_;
33 intervalInfo.targetTimestamp = displaySyncData->targetTimestamp_;
34 if (callbackCJ != nullptr) {
35 callbackCJ(intervalInfo);
36 }
37 });
38 }
39
UnregisterOnFrameCallback(void (* callback)(CIntervalInfo))40 void DisplaySync::UnregisterOnFrameCallback(void (*callback)(CIntervalInfo))
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 uiDisplaySync_->UnregisterOnFrame();
44 FFIDataManager::GetInstance()->RemoveRemoteData(reinterpret_cast<int64_t>(callback));
45 }
46
DisplaySyncImpl()47 DisplaySyncImpl::DisplaySyncImpl()
48 {
49 auto uiDisplaySync = Ace::AceType::MakeRefPtr<Ace::UIDisplaySync>();
50 displaySync_ = std::make_shared<DisplaySync>(uiDisplaySync);
51 if (displaySync_ == nullptr) {
52 return;
53 }
54 }
55
SetExpectedFrameRateRange(CFrameRateRange cRateRange)56 int32_t DisplaySyncImpl::SetExpectedFrameRateRange(CFrameRateRange cRateRange)
57 {
58 const auto uiDisplaySync = displaySync_->GetUIDisplaySync();
59 if (!uiDisplaySync) {
60 return ERR_CJ_INNER_EXCEPTION;
61 }
62
63 Ace::FrameRateRange rateRange(cRateRange.min, cRateRange.max, cRateRange.expected);
64 if (!rateRange.IsValid()) {
65 return ERR_CJ_PARAMETER_ERROR;
66 }
67 uiDisplaySync->SetExpectedFrameRateRange(rateRange);
68 return ERR_CJ_SUCCESS;
69 }
70
On(char * type,void (* callback)(CIntervalInfo))71 int32_t DisplaySyncImpl::On(char* type, void (*callback)(CIntervalInfo))
72 {
73 if (strcmp(type, "frame") != 0) {
74 return ERR_CJ_PARAMETER_ERROR;
75 }
76 displaySync_->RegisterOnFrameCallback(callback);
77 return ERR_CJ_SUCCESS;
78 }
79
Off(char * type,void (* callback)(CIntervalInfo))80 int32_t DisplaySyncImpl::Off(char* type, void (*callback)(CIntervalInfo))
81 {
82 if (strcmp(type, "frame") != 0) {
83 return ERR_CJ_PARAMETER_ERROR;
84 }
85 displaySync_->UnregisterOnFrameCallback(callback);
86 return ERR_CJ_SUCCESS;
87 }
88
Start()89 int32_t DisplaySyncImpl::Start()
90 {
91 const auto uiDisplaySync = displaySync_->GetUIDisplaySync();
92 if (!uiDisplaySync) {
93 return ERR_CJ_INNER_EXCEPTION;
94 }
95 uiDisplaySync->AddToPipelineOnContainer();
96 return ERR_CJ_SUCCESS;
97 }
98
Stop()99 int32_t DisplaySyncImpl::Stop()
100 {
101 const auto uiDisplaySync = displaySync_->GetUIDisplaySync();
102 if (!uiDisplaySync) {
103 return ERR_CJ_INNER_EXCEPTION;
104 }
105 uiDisplaySync->DelFromPipelineOnContainer();
106 return ERR_CJ_SUCCESS;
107 }
108 }
109