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 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 <dlfcn.h>
17 #include <poll.h>
18 #include <securec.h>
19 #include <unistd.h>
20 #include "codec_jpeg_core.h"
21 #include "codec_log_wrapper.h"
22 #include "hdf_base.h"
23
24 namespace OHOS {
25 namespace HDI {
26 namespace Codec {
27 namespace Image {
~CodecJpegCore()28 CodecJpegCore::~CodecJpegCore()
29 {
30 if (libHandle_ != nullptr) {
31 dlclose(libHandle_);
32 libHandle_ = nullptr;
33 }
34 }
35
NotifyPowerOn()36 void CodecJpegCore::NotifyPowerOn()
37 {
38 if (JpegHwi_ == nullptr) {
39 CODEC_LOGE("JpegHwi_ is null");
40 return;
41 }
42 if (JpegHwi_->NotifyPowerOn != nullptr) {
43 JpegHwi_->NotifyPowerOn();
44 }
45 }
46
AddVendorLib()47 void CodecJpegCore::AddVendorLib()
48 {
49 CODEC_LOGI("start load jpeg dependency library!");
50 libHandle_ = dlopen(CODEC_JPEG_VDI_LIB_NAME, RTLD_LAZY);
51 if (libHandle_ == nullptr) {
52 CODEC_LOGE("Failed to dlopen %{public}s, error [%{public}s]", CODEC_JPEG_VDI_LIB_NAME, dlerror());
53 return;
54 }
55
56 getCodecJpegHwi_= reinterpret_cast<GetCodecJpegHwi>(dlsym(libHandle_, "GetCodecJpegHwi"));
57 if (getCodecJpegHwi_ == NULL) {
58 CODEC_LOGE("Failed to dlsym GetCodecJpegHwi");
59 dlclose(libHandle_);
60 libHandle_ = nullptr;
61 return;
62 }
63
64 JpegHwi_ = getCodecJpegHwi_();
65 if (JpegHwi_ == nullptr) {
66 CODEC_LOGE("run GetCodecJpegHwi error!");
67 dlclose(libHandle_);
68 libHandle_ = nullptr;
69 return;
70 }
71 CODEC_LOGI("load dependency library success!");
72 }
73
JpegInit()74 int32_t CodecJpegCore::JpegInit()
75 {
76 if (JpegHwi_ == nullptr) {
77 AddVendorLib();
78 }
79 CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
80 return (JpegHwi_->JpegInit)();
81 }
82
JpegDeInit()83 int32_t CodecJpegCore::JpegDeInit()
84 {
85 CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
86 return (JpegHwi_->JpegDeInit)();
87 }
88
AllocateInBuffer(BufferHandle ** buffer,uint32_t size)89 int32_t CodecJpegCore::AllocateInBuffer(BufferHandle **buffer, uint32_t size)
90 {
91 CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
92 return (JpegHwi_->AllocateInBuffer)(buffer, size);
93 }
94
FreeInBuffer(BufferHandle * buffer)95 int32_t CodecJpegCore::FreeInBuffer(BufferHandle *buffer)
96 {
97 CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
98 return (JpegHwi_->FreeInBuffer)(buffer);
99 }
100
DoDecode(BufferHandle * buffer,BufferHandle * outBuffer,const V2_1::CodecJpegDecInfo * decInfo)101 int32_t CodecJpegCore::DoDecode(BufferHandle *buffer, BufferHandle *outBuffer,
102 const V2_1::CodecJpegDecInfo *decInfo)
103 {
104 CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
105 CodecJpegDecInfo *vdiDecInfo = reinterpret_cast<CodecJpegDecInfo *>(const_cast<V2_1::CodecJpegDecInfo *>(decInfo));
106 return (JpegHwi_->DoJpegDecode)(buffer, outBuffer, vdiDecInfo);
107 }
108 } // Image
109 } // Codec
110 } // HDI
111 } // OHOS
112