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     if (!arg.WriteInterfaceToken(GetDescriptor())) {
39         VLOGE("Failed to write interface token");
40         return VSYNC_ERROR_API_FAILED;
41     }
42     auto remote = Remote();
43     if (remote == nullptr) {
44         VLOGE("remote is null");
45         return VSYNC_ERROR_API_FAILED;
46     }
47     int res = remote->SendRequest(IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC, arg, ret, opt);
48     if (res != NO_ERROR) {
49         VLOGE("ipc send fail, error:%{public}d", res);
50         return VSYNC_ERROR_BINDER_ERROR;
51     }
52     return VSYNC_ERROR_OK;
53 }
54 
SetUiDvsyncSwitch(bool dvsyncSwitch)55 VsyncError VSyncConnectionProxy::SetUiDvsyncSwitch(bool dvsyncSwitch)
56 {
57     MessageOption opt(MessageOption::TF_ASYNC);
58     MessageParcel arg;
59     MessageParcel ret;
60 
61     if (!arg.WriteInterfaceToken(GetDescriptor())) {
62         VLOGE("Failed to write interface token");
63         return VSYNC_ERROR_API_FAILED;
64     }
65     if (!arg.WriteBool(dvsyncSwitch)) {
66         VLOGE("Failed to write dvsyncSwitch:%{public}d", dvsyncSwitch);
67         return VSYNC_ERROR_API_FAILED;
68     }
69     auto remote = Remote();
70     if (remote == nullptr) {
71         VLOGE("remote is null");
72         return VSYNC_ERROR_API_FAILED;
73     }
74     int res = remote->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH, arg, ret, opt);
75     if (res != NO_ERROR) {
76         VLOGE("ipc send fail, error:%{public}d", res);
77         return VSYNC_ERROR_UNKOWN;
78     }
79     return static_cast<VsyncError>(ret.ReadInt32());
80 }
81 
SetNativeDVSyncSwitch(bool dvsyncSwitch)82 VsyncError VSyncConnectionProxy::SetNativeDVSyncSwitch(bool dvsyncSwitch)
83 {
84     MessageOption opt(MessageOption::TF_ASYNC);
85     MessageParcel arg;
86     MessageParcel ret;
87 
88     if (!arg.WriteInterfaceToken(GetDescriptor())) {
89         VLOGE("Failed to write interface token");
90         return VSYNC_ERROR_API_FAILED;
91     }
92     if (!arg.WriteBool(dvsyncSwitch)) {
93         VLOGE("Failed to write dvsyncSwitch:%{public}d", dvsyncSwitch);
94         return VSYNC_ERROR_API_FAILED;
95     }
96     auto remote = Remote();
97     if (remote == nullptr) {
98         VLOGE("remote is null");
99         return VSYNC_ERROR_API_FAILED;
100     }
101     int res = remote->SendRequest(IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH, arg, ret, opt);
102     if (res != NO_ERROR) {
103         VLOGE("ipc send fail, error:%{public}d", res);
104         return VSYNC_ERROR_UNKOWN;
105     }
106     return static_cast<VsyncError>(ret.ReadInt32());
107 }
108 
SetUiDvsyncConfig(int32_t bufferCount)109 VsyncError VSyncConnectionProxy::SetUiDvsyncConfig(int32_t bufferCount)
110 {
111     MessageOption opt(MessageOption::TF_ASYNC);
112     MessageParcel arg;
113     MessageParcel ret;
114 
115     if (!arg.WriteInterfaceToken(GetDescriptor())) {
116         VLOGE("Failed to write interface token");
117         return VSYNC_ERROR_API_FAILED;
118     }
119     if (!arg.WriteInt32(bufferCount)) {
120         VLOGE("SetUiDvsyncConfig bufferCount error");
121         return VSYNC_ERROR_UNKOWN;
122     }
123     auto remote = Remote();
124     if (remote == nullptr) {
125         VLOGE("remote is null");
126         return VSYNC_ERROR_API_FAILED;
127     }
128     int res = remote->SendRequest(IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG, arg, ret, opt);
129     if (res != NO_ERROR) {
130         return VSYNC_ERROR_UNKOWN;
131     }
132     return static_cast<VsyncError>(ret.ReadInt32());
133 }
134 
GetReceiveFd(int32_t &fd)135 VsyncError VSyncConnectionProxy::GetReceiveFd(int32_t &fd)
136 {
137     MessageOption opt;
138     MessageParcel arg;
139     MessageParcel ret;
140 
141     if (!arg.WriteInterfaceToken(GetDescriptor())) {
142         VLOGE("Failed to write interface token");
143         return VSYNC_ERROR_API_FAILED;
144     }
145     auto remote = Remote();
146     if (remote == nullptr) {
147         VLOGE("remote is null");
148         return VSYNC_ERROR_API_FAILED;
149     }
150     int res = remote->SendRequest(IVSYNC_CONNECTION_GET_RECEIVE_FD, arg, ret, opt);
151     if (res != NO_ERROR) {
152         VLOGE("GetReceiveFd Failed, res = %{public}d", res);
153         return VSYNC_ERROR_BINDER_ERROR;
154     }
155     res = ret.ReadInt32();
156     if (res != VSYNC_ERROR_OK) {
157         return static_cast<VsyncError>(res);
158     }
159     fd = ret.ReadFileDescriptor();
160     if (fd < 0) {
161         VLOGE("GetReceiveFd Invalid fd:%{public}d", fd);
162         return VSYNC_ERROR_API_FAILED;
163     }
164     return VSYNC_ERROR_OK;
165 }
166 
SetVSyncRate(int32_t rate)167 VsyncError VSyncConnectionProxy::SetVSyncRate(int32_t rate)
168 {
169     if (rate < -1) {
170         return VSYNC_ERROR_INVALID_ARGUMENTS;
171     }
172     MessageOption opt;
173     MessageParcel arg;
174     MessageParcel ret;
175 
176     if (!arg.WriteInterfaceToken(GetDescriptor())) {
177         VLOGE("Failed to write interface token");
178         return VSYNC_ERROR_API_FAILED;
179     }
180     if (!arg.WriteInt32(rate)) {
181         VLOGE("Failed to write rate:%{public}d", rate);
182         return VSYNC_ERROR_API_FAILED;
183     }
184     auto remote = Remote();
185     if (remote == nullptr) {
186         VLOGE("remote is null");
187         return VSYNC_ERROR_API_FAILED;
188     }
189     int res = remote->SendRequest(IVSYNC_CONNECTION_SET_RATE, arg, ret, opt);
190     if (res != NO_ERROR) {
191         return VSYNC_ERROR_BINDER_ERROR;
192     }
193     return static_cast<VsyncError>(ret.ReadInt32());
194 }
195 
Destroy()196 VsyncError VSyncConnectionProxy::Destroy()
197 {
198     MessageOption opt;
199     MessageParcel arg;
200     MessageParcel ret;
201 
202     if (!arg.WriteInterfaceToken(GetDescriptor())) {
203         VLOGE("Failed to write interface token");
204         return VSYNC_ERROR_API_FAILED;
205     }
206     auto remote = Remote();
207     if (remote == nullptr) {
208         VLOGE("remote is null");
209         return VSYNC_ERROR_API_FAILED;
210     }
211     int res = remote->SendRequest(IVSYNC_CONNECTION_DESTROY, arg, ret, opt);
212     if (res != NO_ERROR) {
213         return VSYNC_ERROR_BINDER_ERROR;
214     }
215     return static_cast<VsyncError>(ret.ReadInt32());
216 }
217 } // namespace Vsync
218 } // namespace OHOS
219