From 85f2bc1fc10c7b8c6d2219e7cfe5b5ede736f65a Mon Sep 17 00:00:00 2001 From: Bobie Date: Mon, 11 Mar 2024 23:37:33 +0800 Subject: [PATCH 1/2] compile isolation Signed-off-by: Bobie --- bundle.json | 3 +- common/BUILD.gn | 1 + common/include/utils/dcamera_utils_tools.h | 22 ++++++- common/include/utils/image_converter.h | 61 +++++++++++++++++++ common/src/utils/dcamera_utils_tools.cpp | 46 +++++++++++++- common/test/unittest/common/utils/BUILD.gn | 1 + services/data_process/BUILD.gn | 5 +- .../decoder/decode_data_process.cpp | 5 +- .../scale_convert_process.cpp | 15 +++-- 9 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 common/include/utils/image_converter.h diff --git a/bundle.json b/bundle.json index d750a4c4..a19dd1f1 100644 --- a/bundle.json +++ b/bundle.json @@ -50,8 +50,7 @@ "safwk", "drivers_interface_camera", "access_token", - "av_codec", - "libyuv" + "av_codec" ], "third_party": [ "cJSON", diff --git a/common/BUILD.gn b/common/BUILD.gn index cd2cb54c..a737e814 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -62,6 +62,7 @@ ohos_shared_library("distributed_camera_utils") { external_deps = [ "c_utils:utils", + "distributed_hardware_fwk:distributedhardwareutils", "dsoftbus:softbus_client", "hdf_core:libhdi", "hilog:libhilog", diff --git a/common/include/utils/dcamera_utils_tools.h b/common/include/utils/dcamera_utils_tools.h index bd97dbf0..6386e89c 100644 --- a/common/include/utils/dcamera_utils_tools.h +++ b/common/include/utils/dcamera_utils_tools.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,10 +16,16 @@ #ifndef OHOS_DCAMERA_UTILS_TOOL_H #define OHOS_DCAMERA_UTILS_TOOL_H +#include #include #include #include +#ifdef DCAMERA_MMAP_RESERVE +#include "image_converter.h" +#include "single_instance.h" +#endif + namespace OHOS { namespace DistributedHardware { const std::string BASE_64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -33,6 +39,20 @@ std::string Base64Decode(const std::string& basicString); void DumpBufferToFile(std::string fileName, uint8_t *buffer, size_t bufSize); bool IsBase64(unsigned char c); int32_t IsUnderDumpMaxSize(std::string fileName); + +#ifdef DCAMERA_MMAP_RESERVE +class ConverterHandle { + DECLARE_SINGLE_INSTANCE(ConverterHandle); + +public: + void InitConverter(); + const OHOS::OpenSourceLibyuv::ImageConverter &GetHandle(); + +private: + std::atomic isInited_ = false; + OHOS::OpenSourceLibyuv::ImageConverter converter_ = {0}; +}; +#endif } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DCAMERA_UTILS_TOOL_H diff --git a/common/include/utils/image_converter.h b/common/include/utils/image_converter.h new file mode 100644 index 00000000..9b16c1b0 --- /dev/null +++ b/common/include/utils/image_converter.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_OPEN_SOURCE_LIBYUV_IMAGE_CONVERTER_H +#define OHOS_OPEN_SOURCE_LIBYUV_IMAGE_CONVERTER_H + +#include + +namespace OHOS { +namespace OpenSourceLibyuv { +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum FilterMode { + kFilterNone = 0, + kFilterLinear = 1, + kFilterBilinear = 2, + kFilterBox = 3 +} FilterModeEnum; + +struct ImageConverter { + int32_t (*NV12ToI420)(const uint8_t* src_y, int src_stride_y, const uint8_t* src_uv, int src_stride_uv, + uint8_t* dst_y, int dst_stride_y, uint8_t* dst_u, int dst_stride_u, + uint8_t* dst_v, int dst_stride_v, int width, int height); + int32_t (*I420Scale)(const uint8_t* src_y, int src_stride_y, const uint8_t* src_u, int src_stride_u, + const uint8_t* src_v, int src_stride_v, int src_width, int src_height, + uint8_t* dst_y, int dst_stride_y, uint8_t* dst_u, int dst_stride_u, + uint8_t* dst_v, int dst_stride_v, int dst_width, int dst_height, + enum FilterMode filtering); + int32_t (*I420ToNV21)(const uint8_t* src_y, int src_stride_y, const uint8_t* src_u, int src_stride_u, + const uint8_t* src_v, int src_stride_v, uint8_t* dst_y, int dst_stride_y, + uint8_t* dst_vu, int dst_stride_vu, int width, int height); + int32_t (*I420ToRGBA)(const uint8_t* src_y, int src_stride_y, const uint8_t* src_u, int src_stride_u, + const uint8_t* src_v, int src_stride_v, uint8_t* dst_rgba, int dst_stride_rgba, + int width, int height); + int32_t (*ARGBToNV12)(const uint8_t* src_argb, int src_stride_argb, uint8_t* dst_y, int dst_stride_y, + uint8_t* dst_uv, int dst_stride_uv, int width, int height); +}; + +struct ImageConverter GetImageConverter(void); + +#ifdef __cplusplus +} +#endif + +} // namespace OpenSourceLibyuv +} // namespace OHOS +#endif // OHOS_OPEN_SOURCE_LIBYUV_IMAGE_CONVERTER_H diff --git a/common/src/utils/dcamera_utils_tools.cpp b/common/src/utils/dcamera_utils_tools.cpp index cc90d44a..5fd573a4 100644 --- a/common/src/utils/dcamera_utils_tools.cpp +++ b/common/src/utils/dcamera_utils_tools.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #include "dcamera_utils_tools.h" #include +#include #include #include "distributed_camera_constants.h" @@ -26,6 +27,19 @@ namespace OHOS { namespace DistributedHardware { +namespace { +#if (defined(__aarch64__) || defined(__x86_64__)) +const std::string YUV_LIB_PATH = "/system/lib64/libyuv.z.so"; +#else +const std::string YUV_LIB_PATH = "/system/lib/libyuv.z.so"; +#endif +const std::string GET_IMAGE_CONVERTER_FUNC = "GetImageConverter"; +} + +#ifdef DCAMERA_MMAP_RESERVE +using GetImageConverterFunc = OHOS::OpenSourceLibyuv::ImageConverter (*)(); +#endif + const uint32_t OFFSET2 = 2; const uint32_t OFFSET4 = 4; const uint32_t OFFSET6 = 6; @@ -223,5 +237,35 @@ int32_t IsUnderDumpMaxSize(std::string fileName) return DCAMERA_BAD_VALUE; } } + +#ifdef DCAMERA_MMAP_RESERVE +IMPLEMENT_SINGLE_INSTANCE(ConverterHandle); +void ConverterHandle::InitConverter() +{ + void *dlHandler = dlopen(YUV_LIB_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE); + if (dlHandler == nullptr) { + DHLOGE("Dlopen failed."); + return; + } + GetImageConverterFunc getConverter = (GetImageConverterFunc)dlsym(dlHandler, GET_IMAGE_CONVERTER_FUNC.c_str()); + if (getConverter == nullptr) { + DHLOGE("Function of converter is null, failed reason: %s.", dlerror()); + dlclose(dlHandler); + dlHandler = nullptr; + return; + } + converter_ = getConverter(); + DHLOGI("Initialize image converter success."); +} + +const OHOS::OpenSourceLibyuv::ImageConverter& ConverterHandle::GetHandle() +{ + if (!isInited_.load()) { + InitConverter(); + isInited_.store(true); + } + return converter_; +} +#endif } // namespace DistributedHardware } // namespace OHOS diff --git a/common/test/unittest/common/utils/BUILD.gn b/common/test/unittest/common/utils/BUILD.gn index 0a5a52e3..07628d79 100644 --- a/common/test/unittest/common/utils/BUILD.gn +++ b/common/test/unittest/common/utils/BUILD.gn @@ -54,6 +54,7 @@ ohos_unittest("CommonUtilsTest") { "access_token:libnativetoken", "access_token:libtoken_setproc", "c_utils:utils", + "distributed_hardware_fwk:distributedhardwareutils", "drivers_interface_distributed_camera:libdistributed_camera_provider_proxy_1.0", "hdf_core:libhdi", "safwk:system_ability_fwk", diff --git a/services/data_process/BUILD.gn b/services/data_process/BUILD.gn index cc4aad20..df9bb6b6 100644 --- a/services/data_process/BUILD.gn +++ b/services/data_process/BUILD.gn @@ -86,6 +86,8 @@ ohos_shared_library("distributed_camera_data_process") { if (distributed_camera_common) { cflags += [ "-DDCAMERA_SUPPORT_FFMPEG" ] + } else { + cflags += [ "-DDCAMERA_MMAP_RESERVE" ] } defines = [ @@ -111,9 +113,6 @@ ohos_shared_library("distributed_camera_data_process") { "samgr:samgr_proxy", ] - if (!distributed_camera_common) { - external_deps += [ "libyuv:yuv" ] - } subsystem_name = "distributedhardware" part_name = "distributed_camera" diff --git a/services/data_process/src/pipeline_node/multimedia_codec/decoder/decode_data_process.cpp b/services/data_process/src/pipeline_node/multimedia_codec/decoder/decode_data_process.cpp index 66d68bff..7ecf1233 100644 --- a/services/data_process/src/pipeline_node/multimedia_codec/decoder/decode_data_process.cpp +++ b/services/data_process/src/pipeline_node/multimedia_codec/decoder/decode_data_process.cpp @@ -15,7 +15,6 @@ #include "decode_data_process.h" -#include "libyuv.h" #include "distributed_camera_constants.h" #include "distributed_hardware_log.h" #include "dcamera_hisysevent_adapter.h" @@ -538,7 +537,9 @@ void DecodeDataProcess::CopyDecodedImage(const sptr& surBuf, int3 uint8_t *dstDataY = bufferOutput->Data(); uint8_t *dstDataU = bufferOutput->Data() + dstSizeY; uint8_t *dstDataV = bufferOutput->Data() + dstSizeY + dstSizeUV; - int32_t ret = libyuv::NV12ToI420(srcDataY, alignedWidth, srcDataUV, alignedWidth, + auto converter = ConverterHandle::GetInstance().GetHandle(); + CHECK_AND_RETURN_LOG(converter.NV12ToI420 == nullptr, "converter is null."); + int32_t ret = converter.NV12ToI420(srcDataY, alignedWidth, srcDataUV, alignedWidth, dstDataY, sourceConfig_.GetWidth(), dstDataU, static_cast(sourceConfig_.GetWidth()) >> MEMORY_RATIO_UV, dstDataV, static_cast(sourceConfig_.GetWidth()) >> MEMORY_RATIO_UV, diff --git a/services/data_process/src/pipeline_node/scale_conversion/scale_convert_process.cpp b/services/data_process/src/pipeline_node/scale_conversion/scale_convert_process.cpp index 5d218787..70330cdc 100644 --- a/services/data_process/src/pipeline_node/scale_conversion/scale_convert_process.cpp +++ b/services/data_process/src/pipeline_node/scale_conversion/scale_convert_process.cpp @@ -15,7 +15,6 @@ #include "scale_convert_process.h" -#include "libyuv.h" #include "dcamera_utils_tools.h" #include "distributed_camera_constants.h" #include "distributed_camera_errno.h" @@ -281,7 +280,9 @@ int32_t ScaleConvertProcess::ConvertResolution(ImageUnitInfo& srcImgInfo, ImageU uint8_t *dstDataU = dstBuf->Data() + dstSizeY; uint8_t *dstDataV = dstBuf->Data() + dstSizeY + dstSizeUV; - int32_t ret = libyuv::I420Scale( + auto converter = ConverterHandle::GetInstance().GetHandle(); + CHECK_AND_RETURN_RET_LOG(converter.I420Scale == nullptr, DCAMERA_BAD_VALUE, "converter is invalid."); + int32_t ret = converter.I420Scale( srcDataY, srcImgInfo.width, srcDataU, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, srcDataV, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, @@ -290,7 +291,7 @@ int32_t ScaleConvertProcess::ConvertResolution(ImageUnitInfo& srcImgInfo, ImageU dstDataU, static_cast(dstImgInfo.width) >> MEMORY_RATIO_UV, dstDataV, static_cast(dstImgInfo.width) >> MEMORY_RATIO_UV, dstImgInfo.width, dstImgInfo.height, - libyuv::FilterMode::kFilterNone); + OpenSourceLibyuv::FilterMode::kFilterNone); if (ret != DCAMERA_OK) { DHLOGE("Convert I420 scale failed."); return DCAMERA_BAD_VALUE; @@ -328,7 +329,9 @@ int32_t ScaleConvertProcess::ConvertFormatToNV21(ImageUnitInfo& srcImgInfo, Imag uint8_t *dstDataY = dstImgInfo.imgData->Data(); uint8_t *dstDataUV = dstImgInfo.imgData->Data() + dstSizeY; - int32_t ret = libyuv::I420ToNV21( + auto converter = ConverterHandle::GetInstance().GetHandle(); + CHECK_AND_RETURN_RET_LOG(converter.I420ToNV21 == nullptr, DCAMERA_BAD_VALUE, "converter is invalid."); + int32_t ret = converter.I420ToNV21( srcDataY, srcImgInfo.width, srcDataU, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, srcDataV, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, @@ -362,7 +365,9 @@ int32_t ScaleConvertProcess::ConvertFormatToRGBA(ImageUnitInfo& srcImgInfo, Imag uint8_t *srcDataV = dstBuf->Data() + srcSizeY + srcSizeUV; uint8_t *dstDataRGBA = dstImgInfo.imgData->Data(); - int32_t ret = libyuv::I420ToRGBA( + auto converter = ConverterHandle::GetInstance().GetHandle(); + CHECK_AND_RETURN_RET_LOG(converter.I420ToRGBA == nullptr, DCAMERA_BAD_VALUE, "converter is invalid."); + int32_t ret = converter.I420ToRGBA( srcDataY, srcImgInfo.width, srcDataU, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, srcDataV, static_cast(srcImgInfo.width) >> MEMORY_RATIO_UV, -- Gitee From 739e1b25ee0bb7902619b357f2779ae680e080fa Mon Sep 17 00:00:00 2001 From: Bobie Date: Tue, 12 Mar 2024 20:52:11 +0800 Subject: [PATCH 2/2] add deinit method. Signed-off-by: Bobie --- common/include/utils/dcamera_utils_tools.h | 3 +++ common/src/utils/dcamera_utils_tools.cpp | 21 +++++++++++++------ services/cameraservice/sourceservice/BUILD.gn | 4 ++++ .../distributed_camera_source_service.cpp | 7 +++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/common/include/utils/dcamera_utils_tools.h b/common/include/utils/dcamera_utils_tools.h index 6386e89c..6bcf240e 100644 --- a/common/include/utils/dcamera_utils_tools.h +++ b/common/include/utils/dcamera_utils_tools.h @@ -46,10 +46,13 @@ class ConverterHandle { public: void InitConverter(); + void DeInitConverter(); const OHOS::OpenSourceLibyuv::ImageConverter &GetHandle(); + using DlHandle = void *; private: std::atomic isInited_ = false; + DlHandle dlHandler_ = nullptr; OHOS::OpenSourceLibyuv::ImageConverter converter_ = {0}; }; #endif diff --git a/common/src/utils/dcamera_utils_tools.cpp b/common/src/utils/dcamera_utils_tools.cpp index 5fd573a4..07c2fb26 100644 --- a/common/src/utils/dcamera_utils_tools.cpp +++ b/common/src/utils/dcamera_utils_tools.cpp @@ -242,27 +242,36 @@ int32_t IsUnderDumpMaxSize(std::string fileName) IMPLEMENT_SINGLE_INSTANCE(ConverterHandle); void ConverterHandle::InitConverter() { - void *dlHandler = dlopen(YUV_LIB_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE); - if (dlHandler == nullptr) { + dlHandler_ = dlopen(YUV_LIB_PATH.c_str(), RTLD_LAZY | RTLD_NODELETE); + if (dlHandler_ == nullptr) { DHLOGE("Dlopen failed."); return; } - GetImageConverterFunc getConverter = (GetImageConverterFunc)dlsym(dlHandler, GET_IMAGE_CONVERTER_FUNC.c_str()); + GetImageConverterFunc getConverter = (GetImageConverterFunc)dlsym(dlHandler_, GET_IMAGE_CONVERTER_FUNC.c_str()); if (getConverter == nullptr) { DHLOGE("Function of converter is null, failed reason: %s.", dlerror()); - dlclose(dlHandler); - dlHandler = nullptr; + dlclose(dlHandler_); + dlHandler_ = nullptr; return; } converter_ = getConverter(); + isInited_.store(true); DHLOGI("Initialize image converter success."); } +void ConverterHandle::DeInitConverter() +{ + if (dlHandler_) { + dlclose(dlHandler_); + dlHandler_ = nullptr; + } + isInited_.store(false); +} + const OHOS::OpenSourceLibyuv::ImageConverter& ConverterHandle::GetHandle() { if (!isInited_.load()) { InitConverter(); - isInited_.store(true); } return converter_; } diff --git a/services/cameraservice/sourceservice/BUILD.gn b/services/cameraservice/sourceservice/BUILD.gn index 409b07a2..5fba05f3 100644 --- a/services/cameraservice/sourceservice/BUILD.gn +++ b/services/cameraservice/sourceservice/BUILD.gn @@ -113,6 +113,10 @@ ohos_shared_library("distributed_camera_source") { defines += [ "DUMP_DCAMERA_FILE" ] } + if (!distributed_camera_common) { + cflags = [ "-DDCAMERA_MMAP_RESERVE" ] + } + external_deps = [ "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", diff --git a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp index 0539b824..b8ce514e 100644 --- a/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp +++ b/services/cameraservice/sourceservice/src/distributedcamera/distributed_camera_source_service.cpp @@ -29,6 +29,7 @@ #include "dcamera_hitrace_adapter.h" #include "dcamera_service_state_listener.h" #include "dcamera_source_service_ipc.h" +#include "dcamera_utils_tools.h" #include "distributed_camera_errno.h" #include "distributed_hardware_log.h" @@ -56,6 +57,9 @@ void DistributedCameraSourceService::OnStart() DHLOGE("DistributedCameraSourceService init failed"); return; } +#ifdef DCAMERA_MMAP_RESERVE + ConverterHandle::GetInstance().InitConverter(); +#endif state_ = DCameraServiceState::DCAMERA_SRV_STATE_RUNNING; DHLOGI("start service success."); } @@ -92,6 +96,9 @@ void DistributedCameraSourceService::OnStop() if (hicollieThread_.joinable()) { hicollieThread_.join(); } +#ifdef DCAMERA_MMAP_RESERVE + ConverterHandle::GetInstance().DeInitConverter(); +#endif } int32_t DistributedCameraSourceService::InitSource(const std::string& params, -- Gitee