1 /*
2  * Copyright (C) 2021 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 applicable 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 "image_convert_tools.h"
17 
18 #include <chrono>
19 #include <dlfcn.h>
20 #include <string>
21 
22 #include "image_log.h"
23 #include "log_tags.h"
24 
25 namespace OHOS {
26 namespace Media {
27 namespace {
28 #if (defined(__aarch64__) || defined(__x86_64__))
29 const std::string YUV_LIB_PATH = "libyuv.z.so";
30 #else
31 const std::string YUV_LIB_PATH = "libyuv.z.so";
32 #endif
33 const std::string GET_IMAGE_CONVERTER_FUNC = "GetImageYuvConverter";
34 }
35 
36 #ifdef EXT_PIXEL
37 using GetImageConverterFunc = OHOS::OpenSourceLibyuv::ImageYuvConverter (*)();
38 #endif
39 
40 #ifdef EXT_PIXEL
GetInstance()41 ConverterHandle& ConverterHandle::GetInstance()
42 {
43     static ConverterHandle instance;
44     return instance;
45 }
46 
InitConverter()47 void ConverterHandle::InitConverter()
48 {
49     dlHandler_ = dlopen(YUV_LIB_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE);
50     if (dlHandler_ == nullptr) {
51         IMAGE_LOGD("Dlopen failed.");
52         return;
53     }
54     GetImageConverterFunc getConverter = (GetImageConverterFunc)dlsym(dlHandler_, GET_IMAGE_CONVERTER_FUNC.c_str());
55     if (getConverter == nullptr) {
56         IMAGE_LOGD("Function of converter is null.");
57         dlclose(dlHandler_);
58         dlHandler_ = nullptr;
59         return;
60     }
61     converter_ = getConverter();
62     isInited_.store(true);
63     IMAGE_LOGD("Initialize image converter success.");
64 }
65 
DeInitConverter()66 void ConverterHandle::DeInitConverter()
67 {
68     if (dlHandler_) {
69         dlclose(dlHandler_);
70         dlHandler_ = nullptr;
71     }
72     isInited_.store(false);
73 }
74 
GetHandle()75 const OHOS::OpenSourceLibyuv::ImageYuvConverter &ConverterHandle::GetHandle()
76 {
77     if (!isInited_.load()) {
78         InitConverter();
79     }
80     return converter_;
81 }
82 #endif
83 } // namespace Media
84 } // namespace OHOS
85