1 /*
2 * Copyright (c) 2025 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 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 #include <dlfcn.h>
16 #include <unistd.h>
17 #include "codec_log_wrapper.h"
18 #include "hdf_base.h"
19 #include "hdf_remote_service.h"
20 #include "buffer_helper.h"
21 #include "codec_heif_decode_service.h"
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Codec {
26 namespace Image {
27 namespace V2_1 {
28 using GetCodecHeifDecodeHwi = ICodecHeifDecodeHwi*(*)();
29
CodecHeifDecodeService()30 CodecHeifDecodeService::CodecHeifDecodeService()
31 {
32 isIPCMode_ = (HdfRemoteGetCallingPid() == getpid() ? false : true);
33 }
34
~CodecHeifDecodeService()35 CodecHeifDecodeService::~CodecHeifDecodeService()
36 {
37 heifDecodeHwi_ = nullptr;
38 libHeif_ = nullptr;
39 }
40
LoadVendorLib()41 bool CodecHeifDecodeService::LoadVendorLib()
42 {
43 std::lock_guard<std::mutex> lk(mutex_);
44 if (heifDecodeHwi_) {
45 return true;
46 }
47 if (libHeif_ == nullptr) {
48 void *handle = dlopen(CODEC_HEIF_DECODE_VDI_LIB_NAME, RTLD_LAZY);
49 if (handle == nullptr) {
50 CODEC_LOGE("failed to load vendor lib");
51 return false;
52 }
53 libHeif_ = std::shared_ptr<void>(handle, dlclose);
54 }
55 auto func = reinterpret_cast<GetCodecHeifDecodeHwi>(dlsym(libHeif_.get(), "GetCodecHeifDecodeHwi"));
56 if (func == nullptr) {
57 CODEC_LOGE("failed to load symbol from vendor lib");
58 return false;
59 }
60 heifDecodeHwi_ = func();
61 if (heifDecodeHwi_ == nullptr) {
62 CODEC_LOGE("failed to create heif hardware encoder");
63 return false;
64 }
65 return true;
66 }
67
DoHeifDecode(const std::vector<sptr<Ashmem>> & inputs,const sptr<NativeBuffer> & output,const CodecHeifDecInfo & decInfo)68 int32_t CodecHeifDecodeService::DoHeifDecode(const std::vector<sptr<Ashmem>>& inputs,
69 const sptr<NativeBuffer>& output,
70 const CodecHeifDecInfo& decInfo)
71 {
72 if (!isIPCMode_) {
73 return HDF_FAILURE;
74 }
75 if (!LoadVendorLib()) {
76 return HDF_FAILURE;
77 }
78 sptr<NativeBuffer> registered = OHOS::Codec::Omx::ReWrap(output, true);
79 return (heifDecodeHwi_->DoHeifDecode)(inputs, registered, decInfo);
80 }
81 } // V2_1
82 } // Image
83 } // Codec
84 } // HDI
85 } // OHOS