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