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