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 <sys/ioctl.h>
19 #include <linux/types.h>
20 #include <linux/dma-buf.h>
21 #include <securec.h>
22 #include <dlfcn.h>
23 #include <hdf_base.h>
24 #include <hdf_log.h>
25 #include "display_log.h"
26 #include "hdf_trace.h"
27 #include "hdf_remote_service.h"
28 #include "display_buffer_dfx.h"
29
30 #undef LOG_TAG
31 #define LOG_TAG "ALLOC_SRV"
32 #undef LOG_DOMAIN
33 #define LOG_DOMAIN 0xD002515
34 #define BUFF_SIZE 16
35
36 namespace OHOS {
37 namespace HDI {
38 namespace Display {
39 namespace Buffer {
40 namespace V1_0 {
AllocatorImplGetInstance(void)41 extern "C" IAllocator* AllocatorImplGetInstance(void)
42 {
43 return new (std::nothrow) AllocatorService();
44 }
45
AllocatorService()46 AllocatorService::AllocatorService()
47 : libHandle_(nullptr),
48 vdiImpl_(nullptr),
49 createVdi_(nullptr),
50 destroyVdi_(nullptr)
51 {
52 int32_t ret = LoadVdi();
53 if (ret == HDF_SUCCESS) {
54 vdiImpl_ = createVdi_();
55 CHECK_NULLPOINTER_RETURN(vdiImpl_);
56 } else {
57 HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
58 }
59 }
60
~AllocatorService()61 AllocatorService::~AllocatorService()
62 {
63 std::lock_guard<std::mutex> lck(mutex_);
64 if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
65 destroyVdi_(vdiImpl_);
66 vdiImpl_ = nullptr;
67 destroyVdi_ = nullptr;
68 }
69 if (libHandle_ != nullptr) {
70 dlclose(libHandle_);
71 libHandle_ = nullptr;
72 }
73 }
74
LoadVdi()75 int32_t AllocatorService::LoadVdi()
76 {
77 const char* errStr = dlerror();
78 if (errStr != nullptr) {
79 HDF_LOGD("%{public}s: allocator load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
80 }
81 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
82 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
83 if (libHandle_ == nullptr) {
84 DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
85 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
86 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
87 DISPLAY_LOGD("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
88 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
89 } else {
90 DISPLAY_LOGD("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
91 }
92 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
93 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
94
95 createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
96 if (createVdi_ == nullptr) {
97 errStr = dlerror();
98 if (errStr != nullptr) {
99 HDF_LOGE("%{public}s: allocator CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
100 }
101 dlclose(libHandle_);
102 return HDF_FAILURE;
103 }
104
105 destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
106 if (destroyVdi_ == nullptr) {
107 errStr = dlerror();
108 if (errStr != nullptr) {
109 HDF_LOGE("%{public}s: allocator DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
110 }
111 dlclose(libHandle_);
112 return HDF_FAILURE;
113 }
114 return HDF_SUCCESS;
115 }
116
WriteAllocPidToDma(int32_t fd,pid_t remotePid)117 void AllocatorService::WriteAllocPidToDma(int32_t fd, pid_t remotePid)
118 {
119 char pidStr[BUFF_SIZE] = { 0 };
120 if (sprintf_s(pidStr, BUFF_SIZE, "%d", remotePid) >= 0) {
121 ioctl(fd, DMA_BUF_SET_NAME_A, pidStr);
122 }
123 }
124
FreeMemVdi(BufferHandle * handle)125 void AllocatorService::FreeMemVdi(BufferHandle* handle)
126 {
127 DisplayBufferDfx dfxIns("HDI:Display:FreeMemVdi:FreeMem");
128 dfxIns.SetTimer();
129 dfxIns.StartTimeStamp();
130 HdfTrace traceTwo("FreeMem", "HDI:VDI:");
131 vdiImpl_->FreeMem(*handle);
132 #ifndef DISPLAY_COMMUNITY
133 delete handle;
134 handle = nullptr;
135 #endif
136 }
137
AllocMem(const AllocInfo & info,sptr<NativeBuffer> & handle)138 int32_t AllocatorService::AllocMem(const AllocInfo& info, sptr<NativeBuffer>& handle)
139 {
140 pid_t remotePid = HdfRemoteGetCallingPid();
141 HITRACE_METER_FMT(HITRACE_TAG_HDF, "%s: remotePid %d", __func__, remotePid);
142
143 BufferHandle* buffer = nullptr;
144 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
145 {
146 DisplayBufferDfx dfxIns("HDI:Display:AllocatorService:remotePid:" + std::to_string(remotePid));
147 dfxIns.SetTimer();
148 dfxIns.StartTimeStamp();
149 HdfTrace traceOne("AllocMem-VDI", "HDI:VDI:");
150 int32_t ec = vdiImpl_->AllocMem(info, buffer);
151 if (ec != HDF_SUCCESS) {
152 HDF_LOGE("%{public}s: AllocMem failed, ec = %{public}d", __func__, ec);
153 return ec;
154 }
155 }
156
157 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_DEV_ERR_NO_MEMORY);
158 WriteAllocPidToDma(buffer->fd, remotePid);
159
160 handle = new NativeBuffer();
161 if (handle == nullptr) {
162 HDF_LOGE("%{public}s: new NativeBuffer failed", __func__);
163 FreeMemVdi(buffer);
164 return HDF_FAILURE;
165 }
166
167 handle->SetBufferHandle(buffer, true, [this](BufferHandle* freeBuffer) {
168 FreeMemVdi(freeBuffer);
169 });
170 return HDF_SUCCESS;
171 }
172 } // namespace V1_0
173 } // namespace Buffer
174 } // namespace Display
175 } // namespace HDI
176 } // namespace OHOS
177