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 "codec_log_wrapper.h"
17 #include "codec_image_service.h"
18 #include "hitrace_meter.h"
19 #include <unistd.h>
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace Codec {
24 namespace Image {
25 namespace V2_0 {
CodecImageImplGetInstance(void)26 extern "C" ICodecImage *CodecImageImplGetInstance(void)
27 {
28     return new (std::nothrow) CodecImageService();
29 }
30 
CodecImageService()31 CodecImageService::CodecImageService()
32 {
33     jpegImpl_ = std::make_unique<CodecJpegService>();
34     heifEncodeImpl_ = std::make_unique<CodecHeifEncodeService>();
35 }
36 
GetImageCapability(std::vector<CodecImageCapability> & capList)37 int32_t CodecImageService::GetImageCapability(std::vector<CodecImageCapability>& capList)
38 {
39     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecGetImageCapability");
40     return CodecImageConfig::GetInstance()->GetImageCapabilityList(capList);
41 }
42 
Init(enum CodecImageRole role)43 int32_t CodecImageService::Init(enum CodecImageRole role)
44 {
45     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecInit");
46     CODEC_LOGD("servcie impl!");
47     if (role == CODEC_IMAGE_JPEG) {
48         CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null");
49         return jpegImpl_->JpegInit();
50     } else {
51         return HDF_ERR_NOT_SUPPORT;
52     }
53 }
54 
DeInit(enum CodecImageRole role)55 int32_t CodecImageService::DeInit(enum CodecImageRole role)
56 {
57     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDeInit");
58     CODEC_LOGD("servcie impl!");
59     if (role == CODEC_IMAGE_JPEG) {
60         CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null");
61         return jpegImpl_->JpegDeInit();
62     } else {
63         return HDF_ERR_NOT_SUPPORT;
64     }
65 }
66 
DoJpegDecode(const CodecImageBuffer & inBuffer,const CodecImageBuffer & outBuffer,const CodecJpegDecInfo & decInfo)67 int32_t CodecImageService::DoJpegDecode(const CodecImageBuffer& inBuffer, const CodecImageBuffer& outBuffer,
68     const CodecJpegDecInfo& decInfo)
69 {
70     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDoJpegDecode");
71     CODEC_LOGD("servcie impl!");
72     if (inBuffer.fenceFd >= 0) {
73         close(inBuffer.fenceFd);
74     }
75     CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null");
76     return jpegImpl_->DoJpegDecode(inBuffer, outBuffer, decInfo);
77 }
78 
AllocateInBuffer(CodecImageBuffer & inBuffer,uint32_t size,CodecImageRole role)79 int32_t CodecImageService::AllocateInBuffer(CodecImageBuffer& inBuffer, uint32_t size, CodecImageRole role)
80 {
81     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecAllocateInBuffer");
82     CODEC_LOGD("servcie impl, size [%{public}d]", size);
83     CHECK_AND_RETURN_RET_LOG(size != 0, HDF_ERR_INVALID_PARAM, "buffer size is 0");
84     CHECK_AND_RETURN_RET_LOG(size <= CODEC_IMAGE_MAX_BUFFER_SIZE, HDF_ERR_INVALID_PARAM, "buffer size is too large");
85     inBuffer.bufferRole = role;
86     inBuffer.size = size;
87     if (role == CODEC_IMAGE_JPEG) {
88         CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null");
89         return jpegImpl_->AllocateJpegInBuffer(inBuffer, size);
90     } else {
91         return HDF_ERR_NOT_SUPPORT;
92     }
93 }
94 
FreeInBuffer(const CodecImageBuffer & inBuffer)95 int32_t CodecImageService::FreeInBuffer(const CodecImageBuffer& inBuffer)
96 {
97     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecFreeInBuffer");
98     CODEC_LOGI("servcie impl, bufferId [%{public}d]", inBuffer.id);
99     if (inBuffer.fenceFd >= 0) {
100         close(inBuffer.fenceFd);
101     }
102     if (inBuffer.bufferRole == CODEC_IMAGE_JPEG) {
103         CHECK_AND_RETURN_RET_LOG(jpegImpl_ != nullptr, HDF_FAILURE, "jpegImpl_ is null");
104         return jpegImpl_->FreeJpegInBuffer(inBuffer);
105     } else {
106         return HDF_ERR_NOT_SUPPORT;
107     }
108 }
109 
DoHeifEncode(const std::vector<ImageItem> & inputImgs,const std::vector<MetaItem> & inputMetas,const std::vector<ItemRef> & refs,const SharedBuffer & output,uint32_t & filledLen)110 int32_t CodecImageService::DoHeifEncode(const std::vector<ImageItem>& inputImgs,
111                                         const std::vector<MetaItem>& inputMetas,
112                                         const std::vector<ItemRef>& refs,
113                                         const SharedBuffer& output, uint32_t& filledLen)
114 {
115     HITRACE_METER_NAME(HITRACE_TAG_HDF, "HdfCodecDoHeifEncode");
116     CODEC_LOGI("servcie impl!");
117     CHECK_AND_RETURN_RET_LOG(heifEncodeImpl_ != nullptr, HDF_FAILURE, "heifEncodeImpl_ is null");
118     return heifEncodeImpl_->DoHeifEncode(inputImgs, inputMetas, refs, output, filledLen);
119 }
120 } // V2_0
121 } // Image
122 } // Codec
123 } // HDI
124 } // OHOS
125