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