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
23 #undef LOG_TAG
24 #define LOG_TAG "METADATA_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_1 {
33 using namespace OHOS::HDI::Base;
MetadataImplGetInstance(void)34 extern "C" IMetadata *MetadataImplGetInstance(void)
35 {
36 return new (std::nothrow) MetadataService();
37 }
38
MetadataService()39 MetadataService::MetadataService()
40 : libHandle_(nullptr),
41 vdiImpl_(nullptr),
42 createVdi_(nullptr),
43 destroyVdi_(nullptr)
44 {
45 int32_t ret = LoadVdi();
46 if (ret == HDF_SUCCESS) {
47 vdiImpl_ = createVdi_();
48 CHECK_NULLPOINTER_RETURN(vdiImpl_);
49 } else {
50 HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
51 }
52 }
53
~MetadataService()54 MetadataService::~MetadataService()
55 {
56 if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
57 destroyVdi_(vdiImpl_);
58 }
59 if (libHandle_ != nullptr) {
60 dlclose(libHandle_);
61 }
62 }
63
LoadVdi()64 int32_t MetadataService::LoadVdi()
65 {
66 const char* errStr = dlerror();
67 if (errStr != nullptr) {
68 HDF_LOGI("%{public}s: metadata load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
69 }
70 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
71 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
72 if (libHandle_ == nullptr) {
73 DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
74 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
75 libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
76 DISPLAY_LOGI("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
77 #ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
78 } else {
79 DISPLAY_LOGI("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
80 }
81 #endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
82 CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
83
84 createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
85 if (createVdi_ == nullptr) {
86 errStr = dlerror();
87 if (errStr != nullptr) {
88 HDF_LOGE("%{public}s: metadata CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
89 }
90 dlclose(libHandle_);
91 return HDF_FAILURE;
92 }
93
94 destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
95 if (destroyVdi_ == nullptr) {
96 errStr = dlerror();
97 if (errStr != nullptr) {
98 HDF_LOGE("%{public}s: metadata DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
99 }
100 dlclose(libHandle_);
101 return HDF_FAILURE;
102 }
103
104 return HDF_SUCCESS;
105 }
106
RegisterBuffer(const sptr<NativeBuffer> & handle)107 int32_t MetadataService::RegisterBuffer(const sptr<NativeBuffer>& handle)
108 {
109 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
110 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
111 int32_t ret = vdiImpl_->RegisterBuffer(*handle->GetBufferHandle());
112 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
113 return HDF_SUCCESS;
114 }
115
SetMetadata(const sptr<NativeBuffer> & handle,uint32_t key,const std::vector<uint8_t> & value)116 int32_t MetadataService::SetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, const std::vector<uint8_t>& value)
117 {
118 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
119 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
120 int32_t ret = vdiImpl_->SetMetadata(*handle->GetBufferHandle(), key, value);
121 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
122 return HDF_SUCCESS;
123 }
124
GetMetadata(const sptr<NativeBuffer> & handle,uint32_t key,std::vector<uint8_t> & value)125 int32_t MetadataService::GetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, std::vector<uint8_t>& value)
126 {
127 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
128 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
129 int32_t ret = vdiImpl_->GetMetadata(*handle->GetBufferHandle(), key, value);
130 return ret;
131 }
132
ListMetadataKeys(const sptr<NativeBuffer> & handle,std::vector<uint32_t> & keys)133 int32_t MetadataService::ListMetadataKeys(const sptr<NativeBuffer>& handle, std::vector<uint32_t>& keys)
134 {
135 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
136 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
137 int32_t ret = vdiImpl_->ListMetadataKeys(*handle->GetBufferHandle(), keys);
138 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
139 return HDF_SUCCESS;
140 }
141
EraseMetadataKey(const sptr<NativeBuffer> & handle,uint32_t key)142 int32_t MetadataService::EraseMetadataKey(const sptr<NativeBuffer>& handle, uint32_t key)
143 {
144 CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
145 CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
146 int32_t ret = vdiImpl_->EraseMetadataKey(*handle->GetBufferHandle(), key);
147 DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
148 return HDF_SUCCESS;
149 }
150
151 } // V1_1
152 } // Buffer
153 } // Display
154 } // HDI
155 } // OHOS
156