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 "buffer_client_producer.h"
17
18 #include "buffer_log.h"
19 #include "buffer_manager.h"
20 #include "buffer_utils.h"
21
22 #define DEFINE_MESSAGE_VARIABLES(arg, ret, opt, LOGE) \
23 MessageOption opt; \
24 MessageParcel arg; \
25 MessageParcel ret; \
26 if (!arg.WriteInterfaceToken(GetDescriptor())) { \
27 LOGE("write interface token failed"); \
28 }
29
30 #define SEND_REQUEST(COMMAND, arguments, reply, option) \
31 do { \
32 int32_t ret = Remote()->SendRequest(COMMAND, arguments, reply, option); \
33 if (ret != ERR_NONE) { \
34 BLOGN_FAILURE("SendRequest return %{public}d", ret); \
35 return GSERROR_BINDER; \
36 } \
37 } while (0)
38
39 #define SEND_REQUEST_WITH_SEQ(COMMAND, arguments, reply, option, sequence) \
40 do { \
41 int32_t ret = Remote()->SendRequest(COMMAND, arguments, reply, option); \
42 if (ret != ERR_NONE) { \
43 BLOGN_FAILURE_ID(sequence, "SendRequest return %{public}d", ret); \
44 return GSERROR_BINDER; \
45 } \
46 } while (0)
47
48 #define CHECK_RETVAL_WITH_SEQ(reply, sequence) \
49 do { \
50 int32_t ret = reply.ReadInt32(); \
51 if (ret != GSERROR_OK) { \
52 BLOGN_FAILURE_ID(sequence, "Remote return %{public}d", ret); \
53 return (GSError)ret; \
54 } \
55 } while (0)
56
57 namespace OHOS {
BufferClientProducer(const sptr<IRemoteObject> & impl)58 BufferClientProducer::BufferClientProducer(const sptr<IRemoteObject>& impl)
59 : IRemoteProxy<IBufferProducer>(impl)
60 {
61 BLOGNI("ctor");
62 }
63
~BufferClientProducer()64 BufferClientProducer::~BufferClientProducer()
65 {
66 BLOGNI("dtor");
67 }
68
RequestBuffer(const BufferRequestConfig & config,BufferExtraData & bedata,RequestBufferReturnValue & retval)69 GSError BufferClientProducer::RequestBuffer(const BufferRequestConfig &config, BufferExtraData &bedata,
70 RequestBufferReturnValue &retval)
71 {
72 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
73
74 WriteRequestConfig(arguments, config);
75
76 SEND_REQUEST(BUFFER_PRODUCER_REQUEST_BUFFER, arguments, reply, option);
77 CHECK_RETVAL_WITH_SEQ(reply, retval.sequence);
78
79 ReadSurfaceBufferImpl(reply, retval.sequence, retval.buffer);
80 bedata.ReadFromParcel(reply);
81 ReadFence(reply, retval.fence);
82 reply.ReadInt32Vector(&retval.deletingBuffers);
83 return GSERROR_OK;
84 }
85
CancelBuffer(int32_t sequence,BufferExtraData & bedata)86 GSError BufferClientProducer::CancelBuffer(int32_t sequence, BufferExtraData &bedata)
87 {
88 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
89
90 arguments.WriteInt32(sequence);
91 bedata.WriteToParcel(arguments);
92
93 SEND_REQUEST_WITH_SEQ(BUFFER_PRODUCER_CANCEL_BUFFER, arguments, reply, option, sequence);
94 CHECK_RETVAL_WITH_SEQ(reply, sequence);
95
96 return GSERROR_OK;
97 }
98
FlushBuffer(int32_t sequence,BufferExtraData & bedata,int32_t fence,BufferFlushConfig & config)99 GSError BufferClientProducer::FlushBuffer(int32_t sequence, BufferExtraData &bedata,
100 int32_t fence, BufferFlushConfig &config)
101 {
102 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
103
104 arguments.WriteInt32(sequence);
105 bedata.WriteToParcel(arguments);
106 WriteFence(arguments, fence);
107 WriteFlushConfig(arguments, config);
108
109 SEND_REQUEST_WITH_SEQ(BUFFER_PRODUCER_FLUSH_BUFFER, arguments, reply, option, sequence);
110 CHECK_RETVAL_WITH_SEQ(reply, sequence);
111
112 return GSERROR_OK;
113 }
114
AttachBuffer(sptr<SurfaceBuffer> & buffer)115 GSError BufferClientProducer::AttachBuffer(sptr<SurfaceBuffer>& buffer)
116 {
117 return GSERROR_NOT_SUPPORT;
118 }
119
DetachBuffer(sptr<SurfaceBuffer> & buffer)120 GSError BufferClientProducer::DetachBuffer(sptr<SurfaceBuffer>& buffer)
121 {
122 return GSERROR_NOT_SUPPORT;
123 }
124
RegisterReleaseListener(OnReleaseFunc func)125 GSError BufferClientProducer::RegisterReleaseListener(OnReleaseFunc func)
126 {
127 return GSERROR_NOT_SUPPORT;
128 }
129
GetQueueSize()130 uint32_t BufferClientProducer::GetQueueSize()
131 {
132 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
133
134 SEND_REQUEST(BUFFER_PRODUCER_GET_QUEUE_SIZE, arguments, reply, option);
135
136 return reply.ReadUint32();
137 }
138
SetQueueSize(uint32_t queueSize)139 GSError BufferClientProducer::SetQueueSize(uint32_t queueSize)
140 {
141 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
142
143 arguments.WriteInt32(queueSize);
144
145 SEND_REQUEST(BUFFER_PRODUCER_SET_QUEUE_SIZE, arguments, reply, option);
146 int32_t ret = reply.ReadInt32();
147 if (ret != GSERROR_OK) {
148 BLOGN_FAILURE("Remote return %{public}d", ret);
149 return (GSError)ret;
150 }
151
152 return GSERROR_OK;
153 }
154
GetName(std::string & name)155 GSError BufferClientProducer::GetName(std::string &name)
156 {
157 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
158
159 SEND_REQUEST(BUFFER_PRODUCER_GET_NAME, arguments, reply, option);
160 int32_t ret = reply.ReadInt32();
161 if (ret != GSERROR_OK) {
162 BLOGN_FAILURE("Remote return %{public}d", ret);
163 return static_cast<GSError>(ret);
164 }
165 if (reply.ReadString(name) == false) {
166 BLOGN_FAILURE("reply.ReadString return false");
167 return GSERROR_BINDER;
168 }
169 name_ = name;
170 return static_cast<GSError>(ret);
171 }
172
GetDefaultWidth()173 int32_t BufferClientProducer::GetDefaultWidth()
174 {
175 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
176
177 SEND_REQUEST(BUFFER_PRODUCER_GET_DEFAULT_WIDTH, arguments, reply, option);
178
179 return reply.ReadInt32();
180 }
181
GetDefaultHeight()182 int32_t BufferClientProducer::GetDefaultHeight()
183 {
184 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
185
186 SEND_REQUEST(BUFFER_PRODUCER_GET_DEFAULT_HEIGHT, arguments, reply, option);
187
188 return reply.ReadInt32();
189 }
190
GetDefaultUsage()191 uint32_t BufferClientProducer::GetDefaultUsage()
192 {
193 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
194
195 SEND_REQUEST(BUFFER_PRODUCER_GET_DEFAULT_USAGE, arguments, reply, option);
196
197 return reply.ReadUint32();
198 }
199
GetUniqueId()200 uint64_t BufferClientProducer::GetUniqueId()
201 {
202 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
203
204 SEND_REQUEST(BUFFER_PRODUCER_GET_UNIQUE_ID, arguments, reply, option);
205
206 return reply.ReadUint64();
207 }
208
CleanCache()209 GSError BufferClientProducer::CleanCache()
210 {
211 DEFINE_MESSAGE_VARIABLES(arguments, reply, option, BLOGE);
212
213 SEND_REQUEST(BUFFER_PRODUCER_CLEAN_CACHE, arguments, reply, option);
214 int32_t ret = reply.ReadInt32();
215 if (ret != GSERROR_OK) {
216 BLOGN_FAILURE("Remote return %{public}d", ret);
217 return (GSError)ret;
218 }
219
220 return GSERROR_OK;
221 }
222 }; // namespace OHOS
223