1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "metadata_service.h"
17094332d3Sopenharmony_ci#include <dlfcn.h>
18094332d3Sopenharmony_ci#include <hdf_base.h>
19094332d3Sopenharmony_ci#include <hdf_log.h>
20094332d3Sopenharmony_ci#include "hilog/log.h"
21094332d3Sopenharmony_ci#include "display_log.h"
22094332d3Sopenharmony_ci#include "hdf_trace.h"
23094332d3Sopenharmony_ci
24094332d3Sopenharmony_ci#undef LOG_TAG
25094332d3Sopenharmony_ci#define LOG_TAG "METADATA_SRV"
26094332d3Sopenharmony_ci#undef LOG_DOMAIN
27094332d3Sopenharmony_ci#define LOG_DOMAIN 0xD002515
28094332d3Sopenharmony_ci#undef DISPLAY_TRACE
29094332d3Sopenharmony_ci#define DISPLAY_TRACE HdfTrace trace(__func__, "HDI:DISP:")
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cinamespace OHOS {
32094332d3Sopenharmony_cinamespace HDI {
33094332d3Sopenharmony_cinamespace Display {
34094332d3Sopenharmony_cinamespace Buffer {
35094332d3Sopenharmony_cinamespace V1_1 {
36094332d3Sopenharmony_ciusing namespace OHOS::HDI::Base;
37094332d3Sopenharmony_ciextern "C" IMetadata *MetadataImplGetInstance(void)
38094332d3Sopenharmony_ci{
39094332d3Sopenharmony_ci    return new (std::nothrow) MetadataService();
40094332d3Sopenharmony_ci}
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ciMetadataService::MetadataService()
43094332d3Sopenharmony_ci    : libHandle_(nullptr),
44094332d3Sopenharmony_ci    vdiImpl_(nullptr),
45094332d3Sopenharmony_ci    createVdi_(nullptr),
46094332d3Sopenharmony_ci    destroyVdi_(nullptr)
47094332d3Sopenharmony_ci{
48094332d3Sopenharmony_ci    int32_t ret = LoadVdi();
49094332d3Sopenharmony_ci    if (ret == HDF_SUCCESS) {
50094332d3Sopenharmony_ci        vdiImpl_ = createVdi_();
51094332d3Sopenharmony_ci        CHECK_NULLPOINTER_RETURN(vdiImpl_);
52094332d3Sopenharmony_ci    } else {
53094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: Load buffer VDI failed", __func__);
54094332d3Sopenharmony_ci    }
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_ciMetadataService::~MetadataService()
58094332d3Sopenharmony_ci{
59094332d3Sopenharmony_ci    std::lock_guard<std::mutex> lck(mutex_);
60094332d3Sopenharmony_ci    if (destroyVdi_ != nullptr && vdiImpl_ != nullptr) {
61094332d3Sopenharmony_ci        destroyVdi_(vdiImpl_);
62094332d3Sopenharmony_ci        vdiImpl_ = nullptr;
63094332d3Sopenharmony_ci        destroyVdi_ = nullptr;
64094332d3Sopenharmony_ci    }
65094332d3Sopenharmony_ci    if (libHandle_ != nullptr) {
66094332d3Sopenharmony_ci        dlclose(libHandle_);
67094332d3Sopenharmony_ci        libHandle_ = nullptr;
68094332d3Sopenharmony_ci    }
69094332d3Sopenharmony_ci}
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ciint32_t MetadataService::LoadVdi()
72094332d3Sopenharmony_ci{
73094332d3Sopenharmony_ci    const char* errStr = dlerror();
74094332d3Sopenharmony_ci    if (errStr != nullptr) {
75094332d3Sopenharmony_ci        HDF_LOGD("%{public}s: metadata load vdi, clear earlier dlerror: %{public}s", __func__, errStr);
76094332d3Sopenharmony_ci    }
77094332d3Sopenharmony_ci#ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
78094332d3Sopenharmony_ci    libHandle_ = dlopen(DISPLAY_BUFFER_VDI_DEFAULT_LIBRARY, RTLD_LAZY);
79094332d3Sopenharmony_ci    if (libHandle_ == nullptr) {
80094332d3Sopenharmony_ci        DISPLAY_LOGE("display buffer load vendor vdi default library failed: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
81094332d3Sopenharmony_ci#endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
82094332d3Sopenharmony_ci        libHandle_ = dlopen(DISPLAY_BUFFER_VDI_LIBRARY, RTLD_LAZY);
83094332d3Sopenharmony_ci        DISPLAY_LOGD("display buffer load vendor vdi library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
84094332d3Sopenharmony_ci#ifdef BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
85094332d3Sopenharmony_ci    } else {
86094332d3Sopenharmony_ci        DISPLAY_LOGD("display buffer load vendor vdi default library: %{public}s", DISPLAY_BUFFER_VDI_LIBRARY);
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci#endif // BUFFER_VDI_DEFAULT_LIBRARY_ENABLE
89094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(libHandle_, HDF_FAILURE);
90094332d3Sopenharmony_ci
91094332d3Sopenharmony_ci    createVdi_ = reinterpret_cast<CreateDisplayBufferVdiFunc>(dlsym(libHandle_, "CreateDisplayBufferVdi"));
92094332d3Sopenharmony_ci    if (createVdi_ == nullptr) {
93094332d3Sopenharmony_ci        errStr = dlerror();
94094332d3Sopenharmony_ci        if (errStr != nullptr) {
95094332d3Sopenharmony_ci            HDF_LOGE("%{public}s: metadata CreateDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
96094332d3Sopenharmony_ci        }
97094332d3Sopenharmony_ci        dlclose(libHandle_);
98094332d3Sopenharmony_ci        return HDF_FAILURE;
99094332d3Sopenharmony_ci    }
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    destroyVdi_ = reinterpret_cast<DestroyDisplayBufferVdiFunc>(dlsym(libHandle_, "DestroyDisplayBufferVdi"));
102094332d3Sopenharmony_ci    if (destroyVdi_ == nullptr) {
103094332d3Sopenharmony_ci        errStr = dlerror();
104094332d3Sopenharmony_ci        if (errStr != nullptr) {
105094332d3Sopenharmony_ci            HDF_LOGE("%{public}s: metadata DestroyDisplayBufferVdi dlsym error: %{public}s", __func__, errStr);
106094332d3Sopenharmony_ci        }
107094332d3Sopenharmony_ci        dlclose(libHandle_);
108094332d3Sopenharmony_ci        return HDF_FAILURE;
109094332d3Sopenharmony_ci    }
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ci    return HDF_SUCCESS;
112094332d3Sopenharmony_ci}
113094332d3Sopenharmony_ci
114094332d3Sopenharmony_ciint32_t MetadataService::RegisterBuffer(const sptr<NativeBuffer>& handle)
115094332d3Sopenharmony_ci{
116094332d3Sopenharmony_ci    DISPLAY_TRACE;
117094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
118094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci    BufferHandle* buffer = handle->GetBufferHandle();
121094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
122094332d3Sopenharmony_ci    int32_t ret = vdiImpl_->RegisterBuffer(*buffer);
123094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
124094332d3Sopenharmony_ci    return HDF_SUCCESS;
125094332d3Sopenharmony_ci}
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ciint32_t MetadataService::SetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, const std::vector<uint8_t>& value)
128094332d3Sopenharmony_ci{
129094332d3Sopenharmony_ci    DISPLAY_TRACE;
130094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
131094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
132094332d3Sopenharmony_ci
133094332d3Sopenharmony_ci    BufferHandle* buffer = handle->GetBufferHandle();
134094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
135094332d3Sopenharmony_ci    int32_t ret = vdiImpl_->SetMetadata(*buffer, key, value);
136094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
137094332d3Sopenharmony_ci    return HDF_SUCCESS;
138094332d3Sopenharmony_ci}
139094332d3Sopenharmony_ci
140094332d3Sopenharmony_ciint32_t MetadataService::GetMetadata(const sptr<NativeBuffer>& handle, uint32_t key, std::vector<uint8_t>& value)
141094332d3Sopenharmony_ci{
142094332d3Sopenharmony_ci    DISPLAY_TRACE;
143094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
144094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
145094332d3Sopenharmony_ci
146094332d3Sopenharmony_ci    BufferHandle* buffer = handle->GetBufferHandle();
147094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
148094332d3Sopenharmony_ci    int32_t ret = vdiImpl_->GetMetadata(*buffer, key, value);
149094332d3Sopenharmony_ci    return ret;
150094332d3Sopenharmony_ci}
151094332d3Sopenharmony_ci
152094332d3Sopenharmony_ciint32_t MetadataService::ListMetadataKeys(const sptr<NativeBuffer>& handle, std::vector<uint32_t>& keys)
153094332d3Sopenharmony_ci{
154094332d3Sopenharmony_ci    DISPLAY_TRACE;
155094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
156094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
157094332d3Sopenharmony_ci
158094332d3Sopenharmony_ci    BufferHandle* buffer = handle->GetBufferHandle();
159094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
160094332d3Sopenharmony_ci    int32_t ret = vdiImpl_->ListMetadataKeys(*buffer, keys);
161094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
162094332d3Sopenharmony_ci    return HDF_SUCCESS;
163094332d3Sopenharmony_ci}
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ciint32_t MetadataService::EraseMetadataKey(const sptr<NativeBuffer>& handle, uint32_t key)
166094332d3Sopenharmony_ci{
167094332d3Sopenharmony_ci    DISPLAY_TRACE;
168094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(handle, HDF_FAILURE);
169094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(vdiImpl_, HDF_FAILURE);
170094332d3Sopenharmony_ci
171094332d3Sopenharmony_ci    BufferHandle* buffer = handle->GetBufferHandle();
172094332d3Sopenharmony_ci    CHECK_NULLPOINTER_RETURN_VALUE(buffer, HDF_FAILURE);
173094332d3Sopenharmony_ci    int32_t ret = vdiImpl_->EraseMetadataKey(*buffer, key);
174094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN(ret != HDF_SUCCESS, ret, DISPLAY_LOGE(" fail"));
175094332d3Sopenharmony_ci    return HDF_SUCCESS;
176094332d3Sopenharmony_ci}
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_ci} // V1_1
179094332d3Sopenharmony_ci} // Buffer
180094332d3Sopenharmony_ci} // Display
181094332d3Sopenharmony_ci} // HDI
182094332d3Sopenharmony_ci} // OHOS
183