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