1 /*
2 * Copyright (c) 2022 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 <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_0/allocator_interface_stub.h"
21
22 using namespace OHOS::HDI::Display::Buffer::V1_0;
23
24 struct HdfAllocatorInterfaceHost {
25 struct IDeviceIoService ioService;
26 OHOS::sptr<OHOS::IRemoteObject> stub;
27 };
28
AllocatorInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)29 static int32_t AllocatorInterfaceDriverDispatch(
30 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
31 {
32 auto *hdfAllocatorInterfaceHost =
33 CONTAINER_OF(client->device->service, struct HdfAllocatorInterfaceHost, ioService);
34
35 OHOS::MessageParcel *dataParcel = nullptr;
36 OHOS::MessageParcel *replyParcel = nullptr;
37 OHOS::MessageOption option;
38
39 if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
40 HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
41 return HDF_ERR_INVALID_PARAM;
42 }
43 if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
44 HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
45 return HDF_ERR_INVALID_PARAM;
46 }
47
48 return hdfAllocatorInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
49 }
50
HdfAllocatorInterfaceDriverInit(struct HdfDeviceObject * deviceObject)51 static int HdfAllocatorInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
52 {
53 (void)deviceObject;
54 HDF_LOGI("HdfAllocatorInterfaceDriverInit enter");
55 return HDF_SUCCESS;
56 }
57
HdfAllocatorInterfaceDriverBind(struct HdfDeviceObject * deviceObject)58 static int HdfAllocatorInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
59 {
60 HDF_LOGI("HdfAllocatorInterfaceDriverBind enter");
61
62 auto *hdfAllocatorInterfaceHost = new (std::nothrow) HdfAllocatorInterfaceHost;
63 if (hdfAllocatorInterfaceHost == nullptr) {
64 HDF_LOGE("%{public}s: failed to create create HdfAllocatorInterfaceHost object", __func__);
65 return HDF_FAILURE;
66 }
67
68 hdfAllocatorInterfaceHost->ioService.Dispatch = AllocatorInterfaceDriverDispatch;
69 hdfAllocatorInterfaceHost->ioService.Open = NULL;
70 hdfAllocatorInterfaceHost->ioService.Release = NULL;
71
72 auto serviceImpl = IAllocatorInterface::Get(true);
73 if (serviceImpl == nullptr) {
74 HDF_LOGE("%{public}s: failed to get of implement service", __func__);
75 delete hdfAllocatorInterfaceHost;
76 return HDF_FAILURE;
77 }
78
79 hdfAllocatorInterfaceHost->stub =
80 OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, IAllocatorInterface::GetDescriptor());
81 if (hdfAllocatorInterfaceHost->stub == nullptr) {
82 HDF_LOGE("%{public}s: failed to get stub object", __func__);
83 delete hdfAllocatorInterfaceHost;
84 return HDF_FAILURE;
85 }
86
87 deviceObject->service = &hdfAllocatorInterfaceHost->ioService;
88 return HDF_SUCCESS;
89 }
90
HdfAllocatorInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)91 static void HdfAllocatorInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
92 {
93 HDF_LOGI("HdfAllocatorInterfaceDriverRelease enter");
94 if (deviceObject->service == nullptr) {
95 HDF_LOGE("HdfAllocatorInterfaceDriverRelease not initted");
96 return;
97 }
98
99 auto *hdfAllocatorInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfAllocatorInterfaceHost, ioService);
100 delete hdfAllocatorInterfaceHost;
101 }
102
103 static struct HdfDriverEntry g_allocatorinterfaceDriverEntry = {
104 .moduleVersion = 1,
105 .moduleName = "display_buffer",
106 .Bind = HdfAllocatorInterfaceDriverBind,
107 .Init = HdfAllocatorInterfaceDriverInit,
108 .Release = HdfAllocatorInterfaceDriverRelease,
109 };
110
111 #ifdef __cplusplus
112 extern "C" {
113 #endif
114 HDF_INIT(g_allocatorinterfaceDriverEntry);
115 #ifdef __cplusplus
116 }
117 #endif