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 <ashmem.h>
17 #include "codec_log_wrapper.h"
18 #include "codec_jpeg_dec.h"
19 namespace OHOS {
20 namespace HDI {
21 namespace Codec {
22 namespace Image {
23 namespace V1_0 {
CodecImageJpegImplGetInstance(void)24 extern "C" ICodecImageJpeg *CodecImageJpegImplGetInstance(void)
25 {
26 return new (std::nothrow) CodecJpegDecoder();
27 }
CodecJpegDecoder()28 CodecJpegDecoder::CodecJpegDecoder()
29 {
30 core_ = std::make_unique<CodecJpegCore>();
31 bufferId_ = 0;
32 }
33
GetImageCapability(std::vector<CodecImageCapability> & capList)34 int32_t CodecJpegDecoder::GetImageCapability(std::vector<CodecImageCapability>& capList)
35 {
36 return CodecImageConfig::GetInstance()->GetImageCapabilityList(capList);
37 }
38
JpegInit()39 int32_t CodecJpegDecoder::JpegInit()
40 {
41 CODEC_LOGI("servcie impl!");
42 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
43
44 int32_t ret = core_->Init();
45 if (ret != HDF_SUCCESS) {
46 CODEC_LOGE("error = [%{public}d]", ret);
47 }
48 return ret;
49 }
50
JpegDeInit()51 int32_t CodecJpegDecoder::JpegDeInit()
52 {
53 CODEC_LOGI("servcie impl!");
54 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
55
56 int32_t ret = core_->DeInit();
57 if (ret != HDF_SUCCESS) {
58 CODEC_LOGE("error = [%{public}d]", ret);
59 }
60 return ret;
61 }
62
DoJpegDecode(const CodecImageBuffer & inBuffer,const CodecImageBuffer & outBuffer,const sptr<ICodecImageCallback> & callbacks,const CodecJpegDecInfo & decInfo)63 int32_t CodecJpegDecoder::DoJpegDecode(const CodecImageBuffer& inBuffer, const CodecImageBuffer& outBuffer,
64 const sptr<ICodecImageCallback>& callbacks, const CodecJpegDecInfo& decInfo)
65 {
66 CODEC_LOGI("servcie impl!");
67 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
68 CHECK_AND_RETURN_RET_LOG(callbacks != nullptr, HDF_ERR_INVALID_PARAM, "callbacks is null");
69 CHECK_AND_RETURN_RET_LOG(inBuffer.buffer != nullptr, HDF_ERR_INVALID_PARAM, "inBuffer.buffer is null");
70 CHECK_AND_RETURN_RET_LOG(outBuffer.buffer != nullptr, HDF_ERR_INVALID_PARAM, "outBuffer.buffer is null");
71
72 BufferHandle *inHandle = inBuffer.buffer->Move();
73 CHECK_AND_RETURN_RET_LOG(inHandle != nullptr, HDF_FAILURE, "inHandle is null");
74 BufferHandle *outHandle = outBuffer.buffer->Move();
75 CHECK_AND_RETURN_RET_LOG(outHandle != nullptr, HDF_FAILURE, "outHandle is null");
76
77 int32_t ret = core_->DoDecode(inHandle, outHandle, &decInfo, callbacks, outBuffer.fenceFd);
78 if (ret != HDF_SUCCESS) {
79 CODEC_LOGE("error = [%{public}d]", ret);
80 }
81 return ret;
82 }
83
AllocateInBuffer(CodecImageBuffer & inBuffer,uint32_t size)84 int32_t CodecJpegDecoder::AllocateInBuffer(CodecImageBuffer& inBuffer, uint32_t size)
85 {
86 CODEC_LOGI("servcie impl!");
87 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
88 CHECK_AND_RETURN_RET_LOG(size != 0, HDF_ERR_INVALID_PARAM, "buffer size is 0");
89 CHECK_AND_RETURN_RET_LOG(size <= CODEC_IMAGE_MAX_BUFFER_SIZE, HDF_ERR_INVALID_PARAM, "buffer size is too large");
90
91 BufferHandle *bufferHandle;
92 int32_t ret = core_->AllocateInBuffer(&bufferHandle, size);
93 CHECK_AND_RETURN_RET_LOG(ret == HDF_SUCCESS, ret, "error = [%{public}d]", ret);
94
95 inBuffer.buffer = new NativeBuffer(bufferHandle);
96 inBuffer.id = GetNextBufferId();
97 bufferHandleMap_.emplace(std::make_pair(inBuffer.id, bufferHandle));
98 CODEC_LOGI("success, bufferId [%{public}d]!", inBuffer.id);
99 return ret;
100 }
101
FreeInBuffer(const CodecImageBuffer & inBuffer)102 int32_t CodecJpegDecoder::FreeInBuffer(const CodecImageBuffer& inBuffer)
103 {
104 CODEC_LOGI("servcie impl, bufferId [%{public}d]!", inBuffer.id);
105 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
106
107 uint32_t bufferId = inBuffer.id;
108 auto entry = bufferHandleMap_.find(bufferId);
109 CHECK_AND_RETURN_RET_LOG(entry != bufferHandleMap_.end(), HDF_FAILURE, "not find bufferId:[%{public}d]", bufferId);
110
111 BufferHandle *bufferHandle = entry->second;
112 int32_t ret = core_->FreeInBuffer(bufferHandle);
113 CHECK_AND_RETURN_RET_LOG(ret == HDF_SUCCESS, ret, "error = [%{public}d]", ret);
114
115 bufferHandleMap_.erase(entry);
116 return ret;
117 }
118
GetNextBufferId(void)119 uint32_t CodecJpegDecoder::GetNextBufferId(void)
120 {
121 std::lock_guard<std::mutex> lk(mutex_);
122 bufferId_++;
123 return bufferId_;
124 }
125
126 } // V1_0
127 } // Image
128 } // Codec
129 } // HDI
130 } // OHOS
131