• 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 0xD002515
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", __func__);
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 load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
68     }
69 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
70     libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
71     if (libHandle_ == nullptr) {
72         DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
73 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
74         libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
75         DISPLAY_LOGI("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
76 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
77     } else {
78         DISPLAY_LOGI("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
79     }
80 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
81     CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
82 
83     createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
84     if (createVdi_ == nullptr) {
85         errStr = dlerror();
86         if (errStr != nullptr) {
87             HDF_LOGE("%{public}s: allocator CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
88         }
89         dlclose(libHandle_);
90         return HDF_FAILURE;
91     }
92 
93     destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
94     if (destroyVdi_ == nullptr) {
95         errStr = dlerror();
96         if (errStr != nullptr) {
97             HDF_LOGE("%{public}s: allocator DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
98         }
99         dlclose(libHandle_);
100         return HDF_FAILURE;
101     }
102     return HDF_SUCCESS;
103 }
104 
AllocMem(const AllocInfo & info,sptr<NativeBuffer> & handle)105 int32_t AllocatorService::AllocMem(const AllocInfo& info, sptr<NativeBuffer>& handle)
106 {
107     BufferHandle* buffer = nullptr;
108     CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
109     int32_t ec = vdiImpl_->AllocMem(info, buffer);
110     if (ec != HDF_SUCCESS) {
111         HDF_LOGE("%{public}s: AllocMem failed, ec = %{public}d", __func__, ec);
112         return ec;
113     }
114     CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_DEV_ERR_NO_MEMORY);
115 
116     handle = new NativeBuffer();
117     if (handle == nullptr) {
118         HDF_LOGE("%{public}s: new NativeBuffer failed", __func__);
119         delete handle;
120         vdiImpl_->FreeMem(*buffer);
121         return HDF_FAILURE;
122     }
123 
124     handle->SetBufferHandle(buffer, true, [this](BufferHandle* freeBuffer) {
125         vdiImpl_->FreeMem(*freeBuffer);
126     });
127     return HDF_SUCCESS;
128 }
129 } // namespace V1_0
130 } // namespace Buffer
131 } // namespace Display
132 } // namespace HDI
133 } // namespace OHOS
134