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#include "codec_jpeg_service.h" 16#include "codec_log_wrapper.h" 17 18namespace OHOS { 19namespace HDI { 20namespace Codec { 21namespace Image { 22namespace V2_0 { 23CodecJpegService::CodecJpegService() 24{ 25 core_ = std::make_unique<CodecJpegCore>(); 26 bufferId_ = 0; 27} 28 29int32_t CodecJpegService::JpegInit() 30{ 31 CODEC_LOGD("servcie impl!"); 32 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null"); 33 std::lock_guard<std::mutex> lk(initMutex_); 34 int32_t ret = core_->JpegInit(); 35 if (ret != HDF_SUCCESS) { 36 CODEC_LOGE("error = [%{public}d]", ret); 37 } 38 return ret; 39} 40 41int32_t CodecJpegService::JpegDeInit() 42{ 43 CODEC_LOGD("servcie impl!"); 44 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null"); 45 46 int32_t ret = core_->JpegDeInit(); 47 if (ret != HDF_SUCCESS) { 48 CODEC_LOGE("error = [%{public}d]", ret); 49 } 50 return ret; 51} 52 53int32_t CodecJpegService::DoJpegDecode(const CodecImageBuffer& inBuffer, const CodecImageBuffer& outBuffer, 54 const CodecJpegDecInfo& decInfo) 55{ 56 CODEC_LOGD("servcie impl!"); 57 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null"); 58 CHECK_AND_RETURN_RET_LOG(inBuffer.buffer != nullptr, HDF_FAILURE, "inBuffer.buffer is null"); 59 CHECK_AND_RETURN_RET_LOG(outBuffer.buffer != nullptr, HDF_FAILURE, "outBuffer.buffer is null"); 60 61 BufferHandle *inHandle = inBuffer.buffer->GetBufferHandle(); 62 CHECK_AND_RETURN_RET_LOG(inHandle != nullptr, HDF_FAILURE, "inHandle is null"); 63 BufferHandle *outHandle = outBuffer.buffer->GetBufferHandle(); 64 CHECK_AND_RETURN_RET_LOG(outHandle != nullptr, HDF_FAILURE, "outHandle is null"); 65 66 int32_t ret = core_->DoDecode(inHandle, outHandle, &decInfo); 67 if (ret != HDF_SUCCESS) { 68 CODEC_LOGE("error = [%{public}d]", ret); 69 } 70 return ret; 71} 72 73int32_t CodecJpegService::AllocateJpegInBuffer(CodecImageBuffer& inBuffer, uint32_t size) 74{ 75 CODEC_LOGD("servcie impl!"); 76 CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null"); 77 78 BufferHandle *bufferHandle; 79 int32_t ret = core_->AllocateInBuffer(&bufferHandle, size); 80 inBuffer.fenceFd = -1; 81 CHECK_AND_RETURN_RET_LOG(ret == HDF_SUCCESS, ret, "error = [%{public}d]", ret); 82 inBuffer.buffer = new NativeBuffer(); 83 if (inBuffer.buffer == nullptr) { 84 CODEC_LOGE("jpeg new NativeBuffer fail!"); 85 ret = core_->FreeInBuffer(bufferHandle); 86 CHECK_AND_RETURN_RET_LOG(ret == HDF_SUCCESS, ret, "free bufferhandle fail, error = [%{public}d]", ret); 87 return HDF_FAILURE; 88 } 89 inBuffer.buffer->SetBufferHandle(bufferHandle, true, [this](BufferHandle* freeBuffer) { 90 int32_t err = core_->FreeInBuffer(freeBuffer); 91 if (err != HDF_SUCCESS) { 92 CODEC_LOGE("free bufferhandle fail, error = [%{public}d]", err); 93 } 94 }); 95 std::lock_guard<std::mutex> lk(mutex_); 96 inBuffer.id = GetNextBufferId(); 97 CODEC_LOGI("success, bufferId [%{public}d]!", inBuffer.id); 98 return ret; 99} 100 101int32_t CodecJpegService::FreeJpegInBuffer(const CodecImageBuffer& inBuffer) 102{ 103 // inBuffer has been automatically destructed during AllocateJpegInBuffer 104 return HDF_SUCCESS; 105} 106 107uint32_t CodecJpegService::GetNextBufferId(void) 108{ 109 bufferId_++; 110 return bufferId_; 111} 112 113} // V2_0 114} // Image 115} // Codec 116} // HDI 117} // OHOS 118