• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "allocator_service.h"
17 
18 #include <dlfcn.h>
19 #include <hdf_base.h>
20 #include <hdf_log.h>
21 #include "display_log.h"
22 
23 #undef LOG_TAG
24 #define LOG_TAG "ALLOC_SRV"
25 #undef LOG_DOMAIN
26 #define LOG_DOMAIN 0xD002500
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace Display {
31 namespace Buffer {
32 namespace V1_0 {
AllocatorImplGetInstance(void)33 extern "C" IAllocator* AllocatorImplGetInstance(void)
34 {
35     return new (std::nothrow) AllocatorService();
36 }
37 
AllocatorService()38 AllocatorService::AllocatorService()
39     : libHandle_(nullptr),
40     vdiImpl_(nullptr),
41     createVdi_(nullptr),
42     destroyVdi_(nullptr)
43 {
44     int32_t ret = LoadVdi();
45     if (ret == HDF_SUCCESS) {
46         vdiImpl_ = createVdi_();
47         CHECK_NULLPOINTER_RETURN(vdiImpl_);
48     } else {
49         HDF_LOGE("%{public}s: Load buffer VDI failed, lib: %{public}s", __func__, DISPLAY_BUFFER_VDI_LIBRARY);
50     }
51 }
52 
~AllocatorService()53 AllocatorService::~AllocatorService()
54 {
55     if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
56         destroyVdi_(vdiImpl_);
57     }
58     if (libHandle_ != nullptr) {
59         dlclose(libHandle_);
60     }
61 }
62 
LoadVdi()63 int32_t AllocatorService::LoadVdi()
64 {
65     const char* errStr = dlerror();
66     if (errStr != nullptr) {
67         HDF_LOGI("%{public}s: allocator loadvid, clear earlier dlerror: %{public}s", __func__, errStr);
68     }
69     libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
70     CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
71 
72     createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
73     if (createVdi_ == nullptr) {
74         errStr = dlerror();
75         if (errStr != nullptr) {
76             HDF_LOGE("%{public}s: allocator CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
77         }
78         dlclose(libHandle_);
79         return HDF_FAILURE;
80     }
81 
82     destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
83     if (destroyVdi_ == nullptr) {
84         errStr = dlerror();
85         if (errStr != nullptr) {
86             HDF_LOGE("%{public}s: allocator DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
87         }
88         dlclose(libHandle_);
89         return HDF_FAILURE;
90     }
91     return HDF_SUCCESS;
92 }
93 
AllocMem(const AllocInfo & info,sptr<NativeBuffer> & handle)94 int32_t AllocatorService::AllocMem(const AllocInfo& info, sptr<NativeBuffer>& handle)
95 {
96     BufferHandle* buffer = nullptr;
97     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
98     int32_t ec = vdiImpl_->AllocMem(info, buffer);
99     if (ec != HDF_SUCCESS) {
100         HDF_LOGE("%{public}s: AllocMem failed, ec = %{public}d", __func__, ec);
101         return ec;
102     }
103     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_DEV_ERR_NO_MEMORY);
104 
105     handle = new NativeBuffer();
106     if (handle == nullptr) {
107         HDF_LOGE("%{public}s: new NativeBuffer failed", __func__);
108         delete handle;
109         vdiImpl_->FreeMem(*buffer);
110         return HDF_FAILURE;
111     }
112 
113     handle->SetBufferHandle(buffer, true, [this](BufferHandle* freeBuffer) {
114         vdiImpl_->FreeMem(*freeBuffer);
115     });
116     return HDF_SUCCESS;
117 }
118 } // namespace V1_0
119 } // namespace Buffer
120 } // namespace Display
121 } // namespace HDI
122 } // namespace OHOS
123