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 "metadata_service.h"
17 #include <dlfcn.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include "hilog/log.h"
21 #include "display_log.h"
22 #include "hdf_trace.h"
23
24 #undef LOG_TAG
25 #define LOG_TAG "METADATA_SRV"
26 #undef LOG_DOMAIN
27 #define LOG_DOMAIN 0xD002515
28 #undef DISPLAY_TRACE
29 #define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
30
31 namespace OHOS {
32 namespace HDI {
33 namespace Display {
34 namespace Buffer {
35 namespace V1_1 {
36 using namespace OHOS::HDI::Base;
MetadataImplGetInstance(void)37 extern "C" IMetadata *MetadataImplGetInstance(void)
38 {
39 return new (std::nothrow) MetadataService();
40 }
41
MetadataService()42 MetadataService::MetadataService()
43 : libHandle_(nullptr),
44 vdiImpl_(nullptr),
45 createVdi_(nullptr),
46 destroyVdi_(nullptr)
47 {
48 int32_t ret = LoadVdi();
49 if (ret == HDF_SUCCESS) {
50 vdiImpl_ = createVdi_();
51 CHECK_NULLPOINTER_RETURN(vdiImpl_);
52 } else {
53 HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
54 }
55 }
56
~MetadataService()57 MetadataService::~MetadataService()
58 {
59 std::lock_guard<std::mutex> lck(mutex_);
60 if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
61 destroyVdi_(vdiImpl_);
62 vdiImpl_ = nullptr;
63 destroyVdi_ = nullptr;
64 }
65 if (libHandle_ != nullptr) {
66 dlclose(libHandle_);
67 libHandle_ = nullptr;
68 }
69 }
70
LoadVdi()71 int32_t MetadataService::LoadVdi()
72 {
73 const char* errStr = dlerror();
74 if (errStr != nullptr) {
75 HDF_LOGD("%{public}s: metadata load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
76 }
77 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
78 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
79 if (libHandle_ == nullptr) {
80 DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
81 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
82 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
83 DISPLAY_LOGD("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
84 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
85 } else {
86 DISPLAY_LOGD("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
87 }
88 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
89 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
90
91 createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
92 if (createVdi_ == nullptr) {
93 errStr = dlerror();
94 if (errStr != nullptr) {
95 HDF_LOGE("%{public}s: metadata CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
96 }
97 dlclose(libHandle_);
98 return HDF_FAILURE;
99 }
100
101 destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
102 if (destroyVdi_ == nullptr) {
103 errStr = dlerror();
104 if (errStr != nullptr) {
105 HDF_LOGE("%{public}s: metadata DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
106 }
107 dlclose(libHandle_);
108 return HDF_FAILURE;
109 }
110
111 return HDF_SUCCESS;
112 }
113
RegisterBuffer(const sptr<NativeBuffer> & handle)114 int32_t MetadataService::RegisterBuffer(const sptr<NativeBuffer>& handle)
115 {
116 DISPLAY_TRACE;
117 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
118 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
119
120 BufferHandle* buffer = handle->GetBufferHandle();
121 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
122 int32_t ret = vdiImpl_->RegisterBuffer(*buffer);
123 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
124 return HDF_SUCCESS;
125 }
126
SetMetadata(const sptr<NativeBuffer> & handle,uint32_t key,const std::vector<uint8_t> & value)127 int32_t MetadataService::SetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, const std::vector<uint8_t>& value)
128 {
129 DISPLAY_TRACE;
130 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
131 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
132
133 BufferHandle* buffer = handle->GetBufferHandle();
134 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
135 int32_t ret = vdiImpl_->SetMetadata(*buffer, key, value);
136 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
137 return HDF_SUCCESS;
138 }
139
GetMetadata(const sptr<NativeBuffer> & handle,uint32_t key,std::vector<uint8_t> & value)140 int32_t MetadataService::GetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, std::vector<uint8_t>& value)
141 {
142 DISPLAY_TRACE;
143 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
144 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
145
146 BufferHandle* buffer = handle->GetBufferHandle();
147 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
148 int32_t ret = vdiImpl_->GetMetadata(*buffer, key, value);
149 return ret;
150 }
151
ListMetadataKeys(const sptr<NativeBuffer> & handle,std::vector<uint32_t> & keys)152 int32_t MetadataService::ListMetadataKeys(const sptr<NativeBuffer>& handle, std::vector<uint32_t>& keys)
153 {
154 DISPLAY_TRACE;
155 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
156 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
157
158 BufferHandle* buffer = handle->GetBufferHandle();
159 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
160 int32_t ret = vdiImpl_->ListMetadataKeys(*buffer, keys);
161 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
162 return HDF_SUCCESS;
163 }
164
EraseMetadataKey(const sptr<NativeBuffer> & handle,uint32_t key)165 int32_t MetadataService::EraseMetadataKey(const sptr<NativeBuffer>& handle, uint32_t key)
166 {
167 DISPLAY_TRACE;
168 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
169 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
170
171 BufferHandle* buffer = handle->GetBufferHandle();
172 CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
173 int32_t ret = vdiImpl_->EraseMetadataKey(*buffer, key);
174 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
175 return HDF_SUCCESS;
176 }
177
178 } // V1_1
179 } // Buffer
180 } // Display
181 } // HDI
182 } // OHOS
183