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     }
33 }
34 
AddVendorLib()35 void CodecJpegCore::AddVendorLib()
36 {
37     CODEC_LOGI("start load jpeg dependency library!");
38     libHandle_ = dlopen(CODEC_JPEG_VDI_LIB_NAME, RTLD_LAZY);
39     if (libHandle_ == nullptr) {
40         CODEC_LOGE("Failed to dlopen %{public}s, error [%{public}s]", CODEC_JPEG_VDI_LIB_NAME, dlerror());
41         return;
42     }
43 
44     getCodecJpegHwi_= reinterpret_cast<GetCodecJpegHwi>(dlsym(libHandle_, "GetCodecJpegHwi"));
45     if (getCodecJpegHwi_ == NULL) {
46         CODEC_LOGE("Failed to dlsym GetCodecJpegHwi");
47         dlclose(libHandle_);
48         return;
49     }
50 
51     JpegHwi_ =  getCodecJpegHwi_();
52     if (JpegHwi_ == nullptr) {
53         CODEC_LOGE("run GetCodecJpegHwi error!");
54         dlclose(libHandle_);
55         return;
56     }
57     CODEC_LOGI("load dependency library success!");
58 }
59 
JpegInit()60 int32_t CodecJpegCore::JpegInit()
61 {
62     if (JpegHwi_ == nullptr) {
63         AddVendorLib();
64     }
65     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
66     return (JpegHwi_->JpegInit)();
67 }
68 
JpegDeInit()69 int32_t CodecJpegCore::JpegDeInit()
70 {
71     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
72     return (JpegHwi_->JpegDeInit)();
73 }
74 
AllocateInBuffer(BufferHandle ** buffer,uint32_t size)75 int32_t CodecJpegCore::AllocateInBuffer(BufferHandle **buffer, uint32_t size)
76 {
77     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
78     return (JpegHwi_->AllocateInBuffer)(buffer, size);
79 }
80 
FreeInBuffer(BufferHandle * buffer)81 int32_t CodecJpegCore::FreeInBuffer(BufferHandle *buffer)
82 {
83     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
84     return (JpegHwi_->FreeInBuffer)(buffer);
85 }
86 
DoDecode(BufferHandle * buffer,BufferHandle * outBuffer,const V2_0::CodecJpegDecInfo * decInfo)87 int32_t CodecJpegCore::DoDecode(BufferHandle *buffer, BufferHandle *outBuffer,
88     const V2_0::CodecJpegDecInfo *decInfo)
89 {
90     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
91     CodecJpegDecInfo *vdiDecInfo = reinterpret_cast<CodecJpegDecInfo *>(const_cast<V2_0::CodecJpegDecInfo *>(decInfo));
92     return (JpegHwi_->DoJpegDecode)(buffer, outBuffer, vdiDecInfo);
93 }
94 } // Image
95 } // Codec
96 } // HDI
97 } // OHOS
98