1 /*
2  * Copyright (c) 2021 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_connection_proxy.h"
17 #include "graphic_common.h"
18 #include "vsync_log.h"
19 
20 namespace OHOS {
21 namespace Rosen {
VSyncConnectionProxy(const sptr<IRemoteObject> & impl)22 VSyncConnectionProxy::VSyncConnectionProxy(const sptr<IRemoteObject>& impl)
23     : IRemoteProxy<IVSyncConnection>(impl)
24 {
25 }
26 
RequestNextVSync()27 VsyncError VSyncConnectionProxy::RequestNextVSync()
28 {
29     return RequestNextVSync("unknown", 0);
30 }
31 
RequestNextVSync(const std::string & fromWhom,int64_t lastVSyncTS)32 VsyncError VSyncConnectionProxy::RequestNextVSync(const std::string& fromWhom, int64_t lastVSyncTS)
33 {
34     MessageOption opt(MessageOption::TF_ASYNC);
35     MessageParcel arg;
36     MessageParcel ret;
37 
38     arg.WriteInterfaceToken(GetDescriptor());
39     int res = Remote()->SendRequest(IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC, arg, ret, opt);
40     if (res != NO_ERROR) {
41         VLOGE("ipc send fail, error:%{public}d", res);
42         return VSYNC_ERROR_BINDER_ERROR;
43     }
44     return VSYNC_ERROR_OK;
45 }
46 
SetUiDvsyncSwitch(bool dvsyncSwitch)47 VsyncError VSyncConnectionProxy::SetUiDvsyncSwitch(bool dvsyncSwitch)
48 {
49     MessageOption opt(MessageOption::TF_ASYNC);
50     MessageParcel arg;
51     MessageParcel ret;
52 
53     arg.WriteInterfaceToken(GetDescriptor());
54     arg.WriteBool(dvsyncSwitch);
55     int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH, arg, ret, opt);
56     if (res != NO_ERROR) {
57         VLOGE("ipc send fail, error:%{public}d", res);
58         return VSYNC_ERROR_UNKOWN;
59     }
60     return VSYNC_ERROR_OK;
61 }
62 
SetNativeDVSyncSwitch(bool dvsyncSwitch)63 VsyncError VSyncConnectionProxy::SetNativeDVSyncSwitch(bool dvsyncSwitch)
64 {
65     MessageOption opt(MessageOption::TF_ASYNC);
66     MessageParcel arg;
67     MessageParcel ret;
68 
69     if (!arg.WriteInterfaceToken(GetDescriptor())) {
70         VLOGE("Failed to write interface token");
71         return VSYNC_ERROR_API_FAILED;
72     }
73     if (!arg.WriteBool(dvsyncSwitch)) {
74         VLOGE("Failed to write dvsyncSwitch:%{public}d", dvsyncSwitch);
75         return VSYNC_ERROR_API_FAILED;
76     }
77     auto remote = Remote();
78     if (remote == nullptr) {
79         VLOGE("remote is null");
80         return VSYNC_ERROR_API_FAILED;
81     }
82     int res = remote->SendRequest(IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH, arg, ret, opt);
83     if (res != NO_ERROR) {
84         VLOGE("ipc send fail, error:%{public}d", res);
85         return VSYNC_ERROR_UNKOWN;
86     }
87     return static_cast<VsyncError>(ret.ReadInt32());
88 }
89 
SetUiDvsyncConfig(int32_t bufferCount)90 VsyncError VSyncConnectionProxy::SetUiDvsyncConfig(int32_t bufferCount)
91 {
92     MessageOption opt(MessageOption::TF_ASYNC);
93     MessageParcel arg;
94     MessageParcel ret;
95 
96     arg.WriteInterfaceToken(GetDescriptor());
97     if (!arg.WriteInt32(bufferCount)) {
98         VLOGE("SetUiDvsyncConfig bufferCount error");
99         return VSYNC_ERROR_UNKOWN;
100     }
101     int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG, arg, ret, opt);
102     if (res != NO_ERROR) {
103         return VSYNC_ERROR_UNKOWN;
104     }
105     return VSYNC_ERROR_OK;
106 }
107 
GetReceiveFd(int32_t & fd)108 VsyncError VSyncConnectionProxy::GetReceiveFd(int32_t &fd)
109 {
110     MessageOption opt;
111     MessageParcel arg;
112     MessageParcel ret;
113 
114     arg.WriteInterfaceToken(GetDescriptor());
115     int res = Remote()->SendRequest(IVSYNC_CONNECTION_GET_RECEIVE_FD, arg, ret, opt);
116     if (res != NO_ERROR) {
117         VLOGE("GetReceiveFd Failed, res = %{public}d", res);
118         return VSYNC_ERROR_BINDER_ERROR;
119     }
120     fd = ret.ReadFileDescriptor();
121     if (fd <= 0) {
122         VLOGE("GetReceiveFd Invalid fd:%{public}d", fd);
123         return VSYNC_ERROR_API_FAILED;
124     }
125     return VSYNC_ERROR_OK;
126 }
127 
SetVSyncRate(int32_t rate)128 VsyncError VSyncConnectionProxy::SetVSyncRate(int32_t rate)
129 {
130     if (rate < -1) {
131         return VSYNC_ERROR_INVALID_ARGUMENTS;
132     }
133     MessageOption opt;
134     MessageParcel arg;
135     MessageParcel ret;
136 
137     arg.WriteInterfaceToken(GetDescriptor());
138     arg.WriteInt32(rate);
139     int res = Remote()->SendRequest(IVSYNC_CONNECTION_SET_RATE, arg, ret, opt);
140     if (res != NO_ERROR) {
141         return VSYNC_ERROR_BINDER_ERROR;
142     }
143     return VSYNC_ERROR_OK;
144 }
145 
Destroy()146 VsyncError VSyncConnectionProxy::Destroy()
147 {
148     MessageOption opt;
149     MessageParcel arg;
150     MessageParcel ret;
151 
152     arg.WriteInterfaceToken(GetDescriptor());
153     int res = Remote()->SendRequest(IVSYNC_CONNECTION_DESTROY, arg, ret, opt);
154     if (res != NO_ERROR) {
155         return VSYNC_ERROR_BINDER_ERROR;
156     }
157     return VSYNC_ERROR_OK;
158 }
159 } // namespace Vsync
160 } // namespace OHOS
161