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