• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string libName = HDF_LIBRARY_FULL_PATH(CODEC_JPEG_VDI_NAME);
39     char pathBuf[PATH_MAX] = {'\0'};
40     if (realpath(libName.c_str(), pathBuf) == nullptr) {
41         CODEC_LOGE("realpath failed! path = [%{public}s]", pathBuf);
42         return;
43     }
44     libHandle_ = dlopen(pathBuf, RTLD_LAZY);
45     if (libHandle_ == nullptr) {
46         CODEC_LOGE("Failed to dlopen %{public}s, error [%{public}s]", libName.c_str(), dlerror());
47         return;
48     }
49 
50     getCodecJpegHwi_= reinterpret_cast<GetCodecJpegHwi>(dlsym(libHandle_, "GetCodecJpegHwi"));
51     if (getCodecJpegHwi_ == NULL) {
52         CODEC_LOGE("Failed to dlsym GetCodecJpegHwi");
53         dlclose(libHandle_);
54         return;
55     }
56 
57     JpegHwi_ =  getCodecJpegHwi_();
58     if (JpegHwi_ == nullptr) {
59         CODEC_LOGE("run GetCodecJpegHwi error!");
60         dlclose(libHandle_);
61         return;
62     }
63     CODEC_LOGI("load dependency library success!");
64 }
65 
JpegInit()66 int32_t CodecJpegCore::JpegInit()
67 {
68     if (JpegHwi_ == nullptr) {
69         AddVendorLib();
70     }
71     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
72     return (JpegHwi_->JpegInit)();
73 }
74 
JpegDeInit()75 int32_t CodecJpegCore::JpegDeInit()
76 {
77     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
78     return (JpegHwi_->JpegDeInit)();
79 }
80 
AllocateInBuffer(BufferHandle ** buffer,uint32_t size)81 int32_t CodecJpegCore::AllocateInBuffer(BufferHandle **buffer, uint32_t size)
82 {
83     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
84     return (JpegHwi_->AllocateInBuffer)(buffer, size);
85 }
86 
FreeInBuffer(BufferHandle * buffer)87 int32_t CodecJpegCore::FreeInBuffer(BufferHandle *buffer)
88 {
89     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
90     return (JpegHwi_->FreeInBuffer)(buffer);
91 }
92 
DoDecode(BufferHandle * buffer,BufferHandle * outBuffer,const V1_0::CodecJpegDecInfo * decInfo)93 int32_t CodecJpegCore::DoDecode(BufferHandle *buffer, BufferHandle *outBuffer,
94     const V1_0::CodecJpegDecInfo *decInfo)
95 {
96     CHECK_AND_RETURN_RET_LOG(JpegHwi_ != nullptr, HDF_FAILURE, "JpegHwi_ is null");
97     CodecJpegDecInfo *vdiDecInfo = reinterpret_cast<CodecJpegDecInfo *>(const_cast<V1_0::CodecJpegDecInfo *>(decInfo));
98     return (JpegHwi_->DoJpegDecode)(buffer, outBuffer, vdiDecInfo);
99 }
100 } // Image
101 } // Codec
102 } // HDI
103 } // OHOS
104