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_interface_service.h"
17 #include <dlfcn.h>
18 #include <hdf_base.h>
19 #include "display_log.h"
20 #include "hdf_log.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Display {
25 namespace Buffer {
26 namespace V1_0 {
AllocatorInterfaceImplGetInstance(void)27 extern "C" IAllocatorInterface *AllocatorInterfaceImplGetInstance(void)
28 {
29 return new (std::nothrow) AllocatorInterfaceService();
30 }
31
AllocatorInterfaceService()32 AllocatorInterfaceService::AllocatorInterfaceService()
33 : libHandle_(nullptr),
34 hwiImpl_(nullptr),
35 createHwi_(nullptr),
36 destroyHwi_(nullptr)
37 {
38 int32_t ret = LoadHwi();
39 if (ret == HDF_SUCCESS) {
40 hwiImpl_ = createHwi_();
41 CHECK_NULLPOINTER_RETURN(hwiImpl_);
42 } else {
43 HDF_LOGE("error: LoadHwi failure, lib path:%{public}s", DISPLAY_BUFFER_HWI_LIBRARY_PATH);
44 }
45 }
46
~AllocatorInterfaceService()47 AllocatorInterfaceService::~AllocatorInterfaceService()
48 {
49 if (destroyHwi_ != nullptr && hwiImpl_ != nullptr) {
50 destroyHwi_(hwiImpl_);
51 }
52 if (libHandle_ != nullptr) {
53 dlclose(libHandle_);
54 }
55 }
56
LoadHwi()57 int32_t AllocatorInterfaceService::LoadHwi()
58 {
59 const char *errStr = dlerror();
60 if (errStr) {
61 HDF_LOGI("warning, existing dlerror: %{public}s", errStr);
62 }
63 libHandle_ = dlopen(DISPLAY_BUFFER_HWI_LIBRARY_PATH, RTLD_NOW);
64 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
65
66 createHwi_ = reinterpret_cast<Create_DisplayBufferHwiFunc_t *>(dlsym(libHandle_, "Create_DisplayBufferHwi"));
67 errStr = dlerror();
68 if (errStr) {
69 HDF_LOGE("error: %{public}s", errStr);
70 return HDF_FAILURE;
71 }
72
73 destroyHwi_ = reinterpret_cast<Destroy_DisplayBufferHwiFunc_t *>(dlsym(libHandle_, "Destroy_DisplayBufferHwi"));
74 errStr = dlerror();
75 if (errStr) {
76 HDF_LOGE("error: %{public}s", errStr);
77 return HDF_FAILURE;
78 }
79
80 return HDF_SUCCESS;
81 }
82
AllocMem(const AllocInfo & info,sptr<BufferHandleParcelable> & handle)83 int32_t AllocatorInterfaceService::AllocMem(const AllocInfo &info, sptr<BufferHandleParcelable> &handle)
84 {
85 BufferHandle *buffer = nullptr;
86 CHECK_NULLPOINTER_RETURN_VALUE(hwiImpl_, HDF_FAILURE);
87 int32_t ec = hwiImpl_->AllocMem(info, buffer);
88 if (ec == HDF_SUCCESS) {
89 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
90 }
91 BufferHandleParcelable *hdiBuffer = new BufferHandleParcelable(*buffer);
92 CHECK_NULLPOINTER_RETURN_VALUE(hdiBuffer, HDF_FAILURE);
93 handle = hdiBuffer;
94 return HDF_SUCCESS;
95 }
96 } // namespace V1_0
97 } // namespace Buffer
98 } // namespace Display
99 } // namespace HDI
100 } // namespace OHOS
101