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 "allocator_service_impl.h"
17 #include "allocator_service_stub.h"
18 #include "buffer_handle_parcel.h"
19 #include "buffer_handle_utils.h"
20 #include "hdf_base.h"
21 #include "hdf_log.h"
22 #include "hdf_sbuf_ipc.h"
23
24 #define HDF_LOG_TAG HDI_DISP_GALLOC
25
26 namespace OHOS {
27 namespace HDI {
28 namespace Display {
29 namespace V1_0 {
AllocatorService()30 AllocatorService::AllocatorService() : grallocFuncs_(nullptr)
31 {
32 if (GrallocInitialize(&grallocFuncs_) != HDF_SUCCESS) {
33 HDF_LOGE("%{public}s: gralloc init failed", __func__);
34 }
35 }
36
~AllocatorService()37 AllocatorService::~AllocatorService()
38 {
39 if (GrallocUninitialize(grallocFuncs_) != HDF_SUCCESS) {
40 HDF_LOGE("%{public}s: gralloc uninit failed", __func__);
41 }
42 }
43
AllocMem(const AllocInfo & info,BufferHandle * & handle)44 int32_t AllocatorService::AllocMem(const AllocInfo &info, BufferHandle *&handle)
45 {
46 if (grallocFuncs_ == nullptr) {
47 HDF_LOGE("%{public}s: grallocFuncs_ is nullptr", __func__);
48 return HDF_FAILURE;
49 }
50 return grallocFuncs_->AllocMem(&info, &handle);
51 }
52
FreeMem(BufferHandle & handle)53 int32_t AllocatorService::FreeMem(BufferHandle &handle)
54 {
55 if (grallocFuncs_ == nullptr) {
56 HDF_LOGE("%{public}s: grallocFuncs_ is nullptr", __func__);
57 return HDF_FAILURE;
58 }
59 grallocFuncs_->FreeMem(&handle);
60 return HDF_SUCCESS;
61 }
62 } // namespace V1_0
63 } // namespace Display
64 } // namespace HDI
65 } // namespace OHOS
66
67 using namespace OHOS::HDI::Display::V1_0;
68
AllocatorServiceInstance(void)69 void *AllocatorServiceInstance(void)
70 {
71 return reinterpret_cast<void *>(new AllocatorService());
72 }
73
AllocatorServiceRelease(void * servObj)74 void AllocatorServiceRelease(void *servObj)
75 {
76 delete reinterpret_cast<AllocatorService *>(servObj);
77 }
78
AllocatorServiceOnRemoteRequest(void * service,int cmdId,struct HdfSBuf & data,struct HdfSBuf & reply)79 int32_t AllocatorServiceOnRemoteRequest(void *service, int cmdId, struct HdfSBuf &data, struct HdfSBuf &reply)
80 {
81 if (service == nullptr) {
82 HDF_LOGE("%{public}s: stub is nullptr", __func__);
83 return HDF_FAILURE;
84 }
85
86 AllocatorService *allocatorService = reinterpret_cast<AllocatorService *>(service);
87 OHOS::MessageParcel *dataParcel = nullptr;
88 OHOS::MessageParcel *replyParcel = nullptr;
89
90 if (SbufToParcel(&reply, &replyParcel) != HDF_SUCCESS) {
91 HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
92 return HDF_ERR_INVALID_PARAM;
93 }
94
95 if (SbufToParcel(&data, &dataParcel) != HDF_SUCCESS) {
96 HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
97 return HDF_ERR_INVALID_PARAM;
98 }
99
100 OHOS::MessageOption option;
101 return allocatorService->OnRemoteRequest(cmdId, *dataParcel, *replyParcel, option);
102 }
103