1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include "codec_log_wrapper.h" 17094332d3Sopenharmony_ci#include "codec_image_service.h" 18094332d3Sopenharmony_ci#include "hitrace_meter.h" 19094332d3Sopenharmony_ci#include <unistd.h> 20094332d3Sopenharmony_ci 21094332d3Sopenharmony_cinamespace OHOS { 22094332d3Sopenharmony_cinamespace HDI { 23094332d3Sopenharmony_cinamespace Codec { 24094332d3Sopenharmony_cinamespace Image { 25094332d3Sopenharmony_cinamespace V2_0 { 26094332d3Sopenharmony_ciextern "C" ICodecImage *CodecImageImplGetInstance(void) 27094332d3Sopenharmony_ci{ 28094332d3Sopenharmony_ci return new (std::nothrow) CodecImageService(); 29094332d3Sopenharmony_ci} 30094332d3Sopenharmony_ci 31094332d3Sopenharmony_ciCodecImageService::CodecImageService() 32094332d3Sopenharmony_ci{ 33094332d3Sopenharmony_ci jpegImpl_ = std::make_unique<CodecJpegService>(); 34094332d3Sopenharmony_ci heifEncodeImpl_ = std::make_unique<CodecHeifEncodeService>(); 35094332d3Sopenharmony_ci} 36094332d3Sopenharmony_ci 37094332d3Sopenharmony_ciint32_t CodecImageService::GetImageCapability(std::vector<CodecImageCapability>& capList) 38094332d3Sopenharmony_ci{ 39094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecGetImageCapability"); 40094332d3Sopenharmony_ci return CodecImageConfig::GetInstance()->GetImageCapabilityList(capList); 41094332d3Sopenharmony_ci} 42094332d3Sopenharmony_ci 43094332d3Sopenharmony_ciint32_t CodecImageService::Init(enum CodecImageRole role) 44094332d3Sopenharmony_ci{ 45094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecInit"); 46094332d3Sopenharmony_ci CODEC_LOGD("servcie impl!"); 47094332d3Sopenharmony_ci if (role == CODEC_IMAGE_JPEG) { 48094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null"); 49094332d3Sopenharmony_ci return jpegImpl_->JpegInit(); 50094332d3Sopenharmony_ci } else { 51094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 52094332d3Sopenharmony_ci } 53094332d3Sopenharmony_ci} 54094332d3Sopenharmony_ci 55094332d3Sopenharmony_ciint32_t CodecImageService::DeInit(enum CodecImageRole role) 56094332d3Sopenharmony_ci{ 57094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDeInit"); 58094332d3Sopenharmony_ci CODEC_LOGD("servcie impl!"); 59094332d3Sopenharmony_ci if (role == CODEC_IMAGE_JPEG) { 60094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null"); 61094332d3Sopenharmony_ci return jpegImpl_->JpegDeInit(); 62094332d3Sopenharmony_ci } else { 63094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 64094332d3Sopenharmony_ci } 65094332d3Sopenharmony_ci} 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_ciint32_t CodecImageService::DoJpegDecode(const CodecImageBuffer& inBuffer, const CodecImageBuffer& outBuffer, 68094332d3Sopenharmony_ci const CodecJpegDecInfo& decInfo) 69094332d3Sopenharmony_ci{ 70094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDoJpegDecode"); 71094332d3Sopenharmony_ci CODEC_LOGD("servcie impl!"); 72094332d3Sopenharmony_ci if (inBuffer.fenceFd >= 0) { 73094332d3Sopenharmony_ci close(inBuffer.fenceFd); 74094332d3Sopenharmony_ci } 75094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null"); 76094332d3Sopenharmony_ci return jpegImpl_->DoJpegDecode(inBuffer, outBuffer, decInfo); 77094332d3Sopenharmony_ci} 78094332d3Sopenharmony_ci 79094332d3Sopenharmony_ciint32_t CodecImageService::AllocateInBuffer(CodecImageBuffer& inBuffer, uint32_t size, CodecImageRole role) 80094332d3Sopenharmony_ci{ 81094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecAllocateInBuffer"); 82094332d3Sopenharmony_ci CODEC_LOGD("servcie impl, size [%{public}d]", size); 83094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(size != 0, HDF_ERR_INVALID_PARAM, "buffer size is 0"); 84094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(size <= CODEC_IMAGE_MAX_BUFFER_SIZE, HDF_ERR_INVALID_PARAM, "buffer size is too large"); 85094332d3Sopenharmony_ci inBuffer.bufferRole = role; 86094332d3Sopenharmony_ci inBuffer.size = size; 87094332d3Sopenharmony_ci if (role == CODEC_IMAGE_JPEG) { 88094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null"); 89094332d3Sopenharmony_ci return jpegImpl_->AllocateJpegInBuffer(inBuffer, size); 90094332d3Sopenharmony_ci } else { 91094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 92094332d3Sopenharmony_ci } 93094332d3Sopenharmony_ci} 94094332d3Sopenharmony_ci 95094332d3Sopenharmony_ciint32_t CodecImageService::FreeInBuffer(const CodecImageBuffer& inBuffer) 96094332d3Sopenharmony_ci{ 97094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecFreeInBuffer"); 98094332d3Sopenharmony_ci CODEC_LOGI("servcie impl, bufferId [%{public}d]", inBuffer.id); 99094332d3Sopenharmony_ci if (inBuffer.fenceFd >= 0) { 100094332d3Sopenharmony_ci close(inBuffer.fenceFd); 101094332d3Sopenharmony_ci } 102094332d3Sopenharmony_ci if (inBuffer.bufferRole == CODEC_IMAGE_JPEG) { 103094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null"); 104094332d3Sopenharmony_ci return jpegImpl_->FreeJpegInBuffer(inBuffer); 105094332d3Sopenharmony_ci } else { 106094332d3Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 107094332d3Sopenharmony_ci } 108094332d3Sopenharmony_ci} 109094332d3Sopenharmony_ci 110094332d3Sopenharmony_ciint32_t CodecImageService::DoHeifEncode(const std::vector<ImageItem>& inputImgs, 111094332d3Sopenharmony_ci const std::vector<MetaItem>& inputMetas, 112094332d3Sopenharmony_ci const std::vector<ItemRef>& refs, 113094332d3Sopenharmony_ci const SharedBuffer& output, uint32_t& filledLen) 114094332d3Sopenharmony_ci{ 115094332d3Sopenharmony_ci HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDoHeifEncode"); 116094332d3Sopenharmony_ci CODEC_LOGI("servcie impl!"); 117094332d3Sopenharmony_ci CHECK_AND_RETURN_RET_LOG(heifEncodeImpl_ != nullptr, HDF_FAILURE, "heifEncodeImpl_ is null"); 118094332d3Sopenharmony_ci return heifEncodeImpl_->DoHeifEncode(inputImgs, inputMetas, refs, output, filledLen); 119094332d3Sopenharmony_ci} 120094332d3Sopenharmony_ci} // V2_0 121094332d3Sopenharmony_ci} // Image 122094332d3Sopenharmony_ci} // Codec 123094332d3Sopenharmony_ci} // HDI 124094332d3Sopenharmony_ci} // OHOS 125