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_handle_parcel.h>
17 #include <buffer_handle_utils.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <hdf_sbuf_ipc.h>
21
22 #include "video_layer_stub.h"
23
24 namespace OHOS {
25 namespace HDI {
26 namespace Display {
27 namespace V1_0 {
VideoLayerStub()28 VideoLayerStub::VideoLayerStub() {}
29
Init()30 DispErrCode VideoLayerStub::Init()
31 {
32 layerService_ = std::make_shared<VideoLayerService>();
33 if (layerService_ == nullptr) {
34 HDF_LOGE("%{public}s: layer service start failed", __func__);
35 return DISPLAY_FAILURE;
36 }
37 return DISPLAY_SUCCESS;
38 }
39
LayerStubInitDisplay(MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int32_t VideoLayerStub::LayerStubInitDisplay(MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
43 return HDF_ERR_INVALID_PARAM;
44 }
45 uint32_t devId = data.ReadUint32();
46
47 DispErrCode retCode = layerService_->InitDisplay(devId);
48 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
49 HDF_LOGE("%{public}s: write retcode failed", __func__);
50 return HDF_FAILURE;
51 }
52 return HDF_SUCCESS;
53 }
54
LayerStubDeinitDisplay(MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int32_t VideoLayerStub::LayerStubDeinitDisplay(MessageParcel &data, MessageParcel &reply, MessageOption &option)
56 {
57 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
58 return HDF_ERR_INVALID_PARAM;
59 }
60 uint32_t devId = data.ReadUint32();
61
62 DispErrCode retCode = layerService_->DeinitDisplay(devId);
63 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
64 HDF_LOGE("%{public}s: write retcode failed", __func__);
65 return HDF_FAILURE;
66 }
67 return HDF_SUCCESS;
68 }
69
LayerStubGetDisplayInfo(MessageParcel & data,MessageParcel & reply,MessageOption & option)70 int32_t VideoLayerStub::LayerStubGetDisplayInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option)
71 {
72 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
73 return HDF_ERR_INVALID_PARAM;
74 }
75 uint32_t devId = data.ReadUint32();
76 std::shared_ptr<DisplayInfo> dispInfo = std::make_shared<DisplayInfo>();
77
78 DispErrCode retCode = layerService_->GetDisplayInfo(devId, dispInfo);
79 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
80 HDF_LOGE("%{public}s: write retcode failed", __func__);
81 return HDF_FAILURE;
82 }
83
84 bool flag = reply.WriteUint32(dispInfo->width);
85 flag |= reply.WriteUint32(dispInfo->height);
86 flag |= reply.WriteInt32(dispInfo->rotAngle);
87 if (!flag) {
88 HDF_LOGE("%{public}s: write display info failed", __func__);
89 return HDF_FAILURE;
90 }
91 return HDF_SUCCESS;
92 }
93
LayerStubCreateLayer(MessageParcel & data,MessageParcel & reply,MessageOption & option)94 int32_t VideoLayerStub::LayerStubCreateLayer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
95 {
96 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
97 return HDF_ERR_INVALID_PARAM;
98 }
99 uint32_t devId = data.ReadUint32();
100 uint32_t layerId = 0;
101 LayerInfo pLayerInfo;
102
103 pLayerInfo.width = data.ReadInt32();
104 pLayerInfo.height = data.ReadInt32();
105 pLayerInfo.type = static_cast<LayerType>(data.ReadInt32());
106 pLayerInfo.bpp = data.ReadInt32();
107 pLayerInfo.pixFormat = static_cast<PixelFormat>(data.ReadInt32());
108
109 DispErrCode retCode = layerService_->CreateLayer(devId, pLayerInfo, layerId);
110 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
111 HDF_LOGE("%{public}s: write retcode failed", __func__);
112 return HDF_FAILURE;
113 }
114
115 if (!reply.WriteUint32(layerId)) {
116 HDF_LOGE("%{public}s: write layerId failed", __func__);
117 return HDF_FAILURE;
118 }
119 return HDF_SUCCESS;
120 }
121
LayerStubCloseLayer(MessageParcel & data,MessageParcel & reply,MessageOption & option)122 int32_t VideoLayerStub::LayerStubCloseLayer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
123 {
124 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
125 return HDF_ERR_INVALID_PARAM;
126 }
127 uint32_t devId = data.ReadUint32();
128 uint32_t layerId = data.ReadUint32();
129
130 DispErrCode retCode = layerService_->CloseLayer(devId, layerId);
131 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
132 HDF_LOGE("%{public}s: write retcode failed", __func__);
133 return HDF_FAILURE;
134 }
135 return HDF_SUCCESS;
136 }
137
LayerStubSetLayerVisible(MessageParcel & data,MessageParcel & reply,MessageOption & option)138 int32_t VideoLayerStub::LayerStubSetLayerVisible(MessageParcel &data, MessageParcel &reply, MessageOption &option)
139 {
140 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
141 return HDF_ERR_INVALID_PARAM;
142 }
143 uint32_t devId = data.ReadUint32();
144 uint32_t layerId = data.ReadUint32();
145 bool visible = data.ReadBool();
146
147 DispErrCode retCode = layerService_->SetLayerVisible(devId, layerId, visible);
148 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
149 HDF_LOGE("%{public}s: write retcode failed", __func__);
150 return HDF_FAILURE;
151 }
152 return HDF_SUCCESS;
153 }
154
LayerStubGetLayerVisibleState(MessageParcel & data,MessageParcel & reply,MessageOption & option)155 int32_t VideoLayerStub::LayerStubGetLayerVisibleState(MessageParcel &data, MessageParcel &reply, MessageOption &option)
156 {
157 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
158 return HDF_ERR_INVALID_PARAM;
159 }
160 uint32_t devId = data.ReadUint32();
161 uint32_t layerId = data.ReadUint32();
162 bool visible = false;
163
164 DispErrCode retCode = layerService_->GetLayerVisibleState(devId, layerId, visible);
165 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
166 HDF_LOGE("%{public}s: write retcode failed", __func__);
167 return HDF_FAILURE;
168 }
169
170 if (!reply.WriteBool(visible)) {
171 HDF_LOGE("%{public}s: write visible state failed", __func__);
172 return HDF_FAILURE;
173 }
174 return HDF_SUCCESS;
175 }
176
LayerStubSetLayerRect(MessageParcel & data,MessageParcel & reply,MessageOption & option)177 int32_t VideoLayerStub::LayerStubSetLayerRect(MessageParcel &data, MessageParcel &reply, MessageOption &option)
178 {
179 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
180 return HDF_ERR_INVALID_PARAM;
181 }
182 uint32_t devId = data.ReadUint32();
183 uint32_t layerId = data.ReadUint32();
184 IRect rect;
185
186 rect.x = data.ReadInt32();
187 rect.y = data.ReadInt32();
188 rect.w = data.ReadInt32();
189 rect.h = data.ReadInt32();
190
191 DispErrCode retCode = layerService_->SetLayerRect(devId, layerId, rect);
192 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
193 HDF_LOGE("%{public}s: write retcode failed", __func__);
194 return HDF_FAILURE;
195 }
196 return HDF_SUCCESS;
197 }
198
LayerStubGetLayerRect(MessageParcel & data,MessageParcel & reply,MessageOption & option)199 int32_t VideoLayerStub::LayerStubGetLayerRect(MessageParcel &data, MessageParcel &reply, MessageOption &option)
200 {
201 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
202 return HDF_ERR_INVALID_PARAM;
203 }
204 uint32_t devId = data.ReadUint32();
205 uint32_t layerId = data.ReadUint32();
206 std::shared_ptr<IRect> rect = std::make_shared<IRect>();
207
208 DispErrCode retCode = layerService_->GetLayerRect(devId, layerId, rect);
209 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
210 HDF_LOGE("%{public}s: write retcode failed", __func__);
211 return HDF_FAILURE;
212 }
213
214 bool flag = reply.WriteInt32(rect->x);
215 flag |= reply.WriteInt32(rect->y);
216 flag |= reply.WriteInt32(rect->w);
217 flag |= reply.WriteInt32(rect->h);
218 if (!flag) {
219 HDF_LOGE("%{public}s: write rect failed", __func__);
220 return HDF_FAILURE;
221 }
222 return HDF_SUCCESS;
223 }
224
LayerStubSetLayerZorder(MessageParcel & data,MessageParcel & reply,MessageOption & option)225 int32_t VideoLayerStub::LayerStubSetLayerZorder(MessageParcel &data, MessageParcel &reply, MessageOption &option)
226 {
227 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
228 return HDF_ERR_INVALID_PARAM;
229 }
230 uint32_t devId = data.ReadUint32();
231 uint32_t layerId = data.ReadUint32();
232 uint32_t zorder = data.ReadUint32();
233
234 DispErrCode retCode = layerService_->SetLayerZorder(devId, layerId, zorder);
235 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
236 HDF_LOGE("%{public}s: write retcode failed", __func__);
237 return HDF_FAILURE;
238 }
239 return HDF_SUCCESS;
240 }
241
LayerStubGetLayerZorder(MessageParcel & data,MessageParcel & reply,MessageOption & option)242 int32_t VideoLayerStub::LayerStubGetLayerZorder(MessageParcel &data, MessageParcel &reply, MessageOption &option)
243 {
244 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
245 return HDF_ERR_INVALID_PARAM;
246 }
247 uint32_t devId = data.ReadUint32();
248 uint32_t layerId = data.ReadUint32();
249 uint32_t zorder = 0;
250
251 DispErrCode retCode = layerService_->GetLayerZorder(devId, layerId, zorder);
252 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
253 HDF_LOGE("%{public}s: write retcode failed", __func__);
254 return HDF_FAILURE;
255 }
256
257 if (!reply.WriteUint32(zorder)) {
258 HDF_LOGE("%{public}s: write zorder failed", __func__);
259 return HDF_FAILURE;
260 }
261 return HDF_SUCCESS;
262 }
263
LayerStubSetLayerTransformMode(MessageParcel & data,MessageParcel & reply,MessageOption & option)264 int32_t VideoLayerStub::LayerStubSetLayerTransformMode(MessageParcel &data, MessageParcel &reply, MessageOption &option)
265 {
266 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
267 return HDF_ERR_INVALID_PARAM;
268 }
269 uint32_t devId = data.ReadUint32();
270 uint32_t layerId = data.ReadUint32();
271 TransformType transForm = static_cast<TransformType>(data.ReadUint32());
272
273 DispErrCode retCode = layerService_->SetTransformMode(devId, layerId, transForm);
274 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
275 HDF_LOGE("%{public}s: write retcode failed", __func__);
276 return HDF_FAILURE;
277 }
278 return HDF_SUCCESS;
279 }
280
LayerStubSetLayerBuffer(MessageParcel & data,MessageParcel & reply,MessageOption & option)281 int32_t VideoLayerStub::LayerStubSetLayerBuffer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
282 {
283 if (data.ReadInterfaceToken() != VideoLayerStub::GetDescriptor()) {
284 return HDF_ERR_INVALID_PARAM;
285 }
286 uint32_t devId = data.ReadUint32();
287 uint32_t layerId = data.ReadUint32();
288 BufferHandle *buffer = nullptr;
289
290 buffer = OHOS::ReadBufferHandle(data);
291 if (buffer == nullptr) {
292 HDF_LOGE("%{public}s: read bufferHandle failed", __func__);
293 return DISPLAY_FAILURE;
294 }
295 buffer->virAddr = nullptr;
296 uint32_t fence = data.ReadInt32();
297
298 DispErrCode retCode = layerService_->SetLayerBuffer(devId, layerId, *buffer, fence);
299 (void)FreeBufferHandle(buffer);
300 if (!reply.WriteInt32(static_cast<int32_t>(retCode))) {
301 HDF_LOGE("%{public}s: write retcode failed", __func__);
302 return HDF_FAILURE;
303 }
304 return HDF_SUCCESS;
305 }
306
LayerStubOnRemoteRequest(int cmdId,MessageParcel & data,MessageParcel & reply,MessageOption & option)307 int32_t VideoLayerStub::LayerStubOnRemoteRequest(
308 int cmdId, MessageParcel &data, MessageParcel &reply, MessageOption &option)
309 {
310 switch (cmdId) {
311 case CMD_DISPLAY_LAYER_INIT_DISPLAY: {
312 return LayerStubInitDisplay(data, reply, option);
313 }
314 case CMD_DISPLAY_LAYER_DEINIT_DISPLAY: {
315 return LayerStubDeinitDisplay(data, reply, option);
316 }
317 case CMD_DISPLAY_LAYER_GET_DISPLAY_INFO: {
318 return LayerStubGetDisplayInfo(data, reply, option);
319 }
320 case CMD_DISPLAY_LAYER_CREATE_LAYER: {
321 return LayerStubCreateLayer(data, reply, option);
322 }
323 case CMD_DISPLAY_LAYER_CLOSE_LAYER: {
324 return LayerStubCloseLayer(data, reply, option);
325 }
326 case CMD_DISPLAY_LAYER_SET_LAYER_BUFFER: {
327 return LayerStubSetLayerBuffer(data, reply, option);
328 }
329 case CMD_DISPLAY_LAYER_SET_LAYER_RECT: {
330 return LayerStubSetLayerRect(data, reply, option);
331 }
332 case CMD_DISPLAY_LAYER_GET_LAYER_RECT: {
333 return LayerStubGetLayerRect(data, reply, option);
334 }
335 case CMD_DISPLAY_LAYER_SET_LAYER_ZORDER: {
336 return LayerStubSetLayerZorder(data, reply, option);
337 }
338 case CMD_DISPLAY_LAYER_GET_LAYER_ZORDER: {
339 return LayerStubGetLayerZorder(data, reply, option);
340 }
341 case CMD_DISPLAY_LAYER_SET_LAYER_VISIBLE: {
342 return LayerStubSetLayerVisible(data, reply, option);
343 }
344 case CMD_DISPLAY_LAYER_GET_LAYER_VISIBLE_STATE: {
345 return LayerStubGetLayerVisibleState(data, reply, option);
346 }
347 case CMD_DISPLAY_LAYER_SET_TRANSFORM_MODE: {
348 return LayerStubSetLayerTransformMode(data, reply, option);
349 }
350 default: {
351 HDF_LOGE("%{public}s: not support cmd %{public}d", __func__, cmdId);
352 return HDF_ERR_INVALID_PARAM;
353 }
354 }
355 return HDF_SUCCESS;
356 }
357 } // namespace V1_0
358 } // namespace Display
359 } // namespace HDI
360 } // namespace OHOS
361
362 using namespace OHOS::HDI::Display::V1_0;
363
LayerStubInstance()364 void *LayerStubInstance()
365 {
366 VideoLayerStub *stub = new (std::nothrow) VideoLayerStub();
367 if (stub == nullptr) {
368 HDF_LOGE("%{public}s: display layer stub create failed", __func__);
369 return nullptr;
370 }
371
372 DispErrCode ret = stub->Init();
373 if (ret != DISPLAY_SUCCESS) {
374 delete stub;
375 stub = nullptr;
376 return nullptr;
377 }
378 return reinterpret_cast<void *>(stub);
379 }
380
DestroyLayerStub(void * obj)381 void DestroyLayerStub(void *obj)
382 {
383 delete reinterpret_cast<VideoLayerStub *>(obj);
384 }
385
LayerServiceOnRemoteRequest(void * stub,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)386 int32_t LayerServiceOnRemoteRequest(void *stub, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
387 {
388 if (stub == nullptr) {
389 HDF_LOGE("%{public}s:stub is nullptr", __func__);
390 return HDF_FAILURE;
391 }
392
393 VideoLayerStub *layerStub = reinterpret_cast<VideoLayerStub *>(stub);
394 OHOS::MessageParcel *dataParcel = nullptr;
395 OHOS::MessageParcel *replyParcel = nullptr;
396
397 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
398 HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
399 return HDF_ERR_INVALID_PARAM;
400 }
401
402 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
403 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
404 return HDF_ERR_INVALID_PARAM;
405 }
406
407 OHOS::MessageOption option;
408 return layerStub->LayerStubOnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
409 }
410