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 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
17 #include "codec_image_config.h"
18 #include "codec_log_wrapper.h"
19 #include "hdf_base.h"
20 #include "hdf_device_desc.h"
21 #include "hdf_sbuf_ipc.h"
22 #include "v1_0/codec_image_jpeg_stub.h"
23
24 using namespace OHOS::HDI::Codec::Image::V1_0;
25 namespace {
26 struct HdfCodecJpegHost {
27 struct IDeviceIoService ioService;
28 OHOS::sptr<OHOS::IRemoteObject> stub;
29 };
30 }
CodecJpegDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)31 static int32_t CodecJpegDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
32 struct HdfSBuf *reply)
33 {
34 auto *hdfCodecJpegHost =
35 CONTAINER_OF(client->device->service, struct HdfCodecJpegHost, ioService);
36
37 OHOS::MessageParcel *dataParcel = nullptr;
38 OHOS::MessageParcel *replyParcel = nullptr;
39 OHOS::MessageOption option;
40
41 int32_t ret = SbufToParcel(data, &dataParcel);
42 if (ret != HDF_SUCCESS) {
43 CODEC_LOGE("invalid data sbuf object to dispatch, error [%{public}d]", ret);
44 return HDF_ERR_INVALID_PARAM;
45 }
46
47 ret = SbufToParcel(reply, &replyParcel);
48 if (ret != HDF_SUCCESS) {
49 CODEC_LOGE("invalid reply sbuf object to dispatch, error [%{public}d]", ret);
50 return HDF_ERR_INVALID_PARAM;
51 }
52
53 return hdfCodecJpegHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
54 }
55
HdfCodecJpegDriverInit(struct HdfDeviceObject * deviceObject)56 static int HdfCodecJpegDriverInit(struct HdfDeviceObject *deviceObject)
57 {
58 CODEC_LOGI("HdfCodecJpegDriverInit enter");
59 CodecImageConfig::GetInstance()->Init(*(deviceObject->property));
60 return HDF_SUCCESS;
61 }
62
HdfCodecJpegDriverBind(struct HdfDeviceObject * deviceObject)63 static int HdfCodecJpegDriverBind(struct HdfDeviceObject *deviceObject)
64 {
65 CODEC_LOGI("HdfCodecJpegDriverBind enter");
66
67 auto *hdfCodecJpegHost = new (std::nothrow) HdfCodecJpegHost;
68 if (hdfCodecJpegHost == nullptr) {
69 CODEC_LOGE("failed to create create HdfCodecJpegHost object");
70 return HDF_FAILURE;
71 }
72
73 hdfCodecJpegHost->ioService.Dispatch = CodecJpegDriverDispatch;
74 hdfCodecJpegHost->ioService.Open = NULL;
75 hdfCodecJpegHost->ioService.Release = NULL;
76
77 auto serviceImpl = ICodecImageJpeg::Get(true);
78 if (serviceImpl == nullptr) {
79 CODEC_LOGE("failed to get of implement service");
80 delete hdfCodecJpegHost;
81 return HDF_FAILURE;
82 }
83
84 hdfCodecJpegHost->stub =
85 OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl, ICodecImageJpeg::GetDescriptor());
86 if (hdfCodecJpegHost->stub == nullptr) {
87 CODEC_LOGE("failed to get stub object");
88 delete hdfCodecJpegHost;
89 return HDF_FAILURE;
90 }
91
92 deviceObject->service = &hdfCodecJpegHost->ioService;
93 return HDF_SUCCESS;
94 }
95
HdfCodecJpegDriverRelease(struct HdfDeviceObject * deviceObject)96 static void HdfCodecJpegDriverRelease(struct HdfDeviceObject *deviceObject)
97 {
98 CODEC_LOGI("HdfCodecJpegDriverRelease enter");
99 if (deviceObject->service == nullptr) {
100 CODEC_LOGE("HdfCodecJpegDriverRelease not initted");
101 return;
102 }
103
104 auto *hdfCodecJpegHost =
105 CONTAINER_OF(deviceObject->service, struct HdfCodecJpegHost, ioService);
106 delete hdfCodecJpegHost;
107 }
108
109 static struct HdfDriverEntry g_codecJpegDriverEntry = {
110 .moduleVersion = 1,
111 .moduleName = "libcodec_jpeg_driver.z.so",
112 .Bind = HdfCodecJpegDriverBind,
113 .Init = HdfCodecJpegDriverInit,
114 .Release = HdfCodecJpegDriverRelease,
115 };
116
117 #ifndef __cplusplus
118 extern "C" {
119 #endif
120 HDF_INIT(g_codecJpegDriverEntry);
121 #ifndef __cplusplus
122 }
123 #endif