• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 V1_0 {
CodecJpegService()23 CodecJpegService::CodecJpegService()
24 {
25     core_ = std::make_unique<CodecJpegCore>();
26     bufferId_ = 0;
27 }
28 
JpegInit()29 int32_t CodecJpegService::JpegInit()
30 {
31     CODEC_LOGI("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 
JpegDeInit()41 int32_t CodecJpegService::JpegDeInit()
42 {
43     CODEC_LOGI("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 
DoJpegDecode(const CodecImageBuffer & inBuffer,const CodecImageBuffer & outBuffer,const CodecJpegDecInfo & decInfo)53 int32_t CodecJpegService::DoJpegDecode(const CodecImageBuffer& inBuffer, const CodecImageBuffer& outBuffer,
54     const CodecJpegDecInfo& decInfo)
55 {
56     CODEC_LOGI("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 
AllocateJpegInBuffer(CodecImageBuffer & inBuffer,uint32_t size)73 int32_t CodecJpegService::AllocateJpegInBuffer(CodecImageBuffer& inBuffer, uint32_t size)
74 {
75     CODEC_LOGI("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(bufferHandle);
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     std::lock_guard<std::mutex> lk(mutex_);
90     inBuffer.id = GetNextBufferId();
91     bufferHandleMap_.emplace(std::make_pair(inBuffer.id, bufferHandle));
92     CODEC_LOGI("success, bufferId [%{public}d]!", inBuffer.id);
93     return ret;
94 }
95 
FreeJpegInBuffer(const CodecImageBuffer & inBuffer)96 int32_t CodecJpegService::FreeJpegInBuffer(const CodecImageBuffer& inBuffer)
97 {
98     CODEC_LOGI("servcie impl, bufferId [%{public}d]!", inBuffer.id);
99     CHECK_AND_RETURN_RET_LOG(core_ != nullptr, HDF_FAILURE, "core_ is null");
100 
101     uint32_t bufferId = inBuffer.id;
102     std::lock_guard<std::mutex> lk(mutex_);
103     auto entry = bufferHandleMap_.find(bufferId);
104     CHECK_AND_RETURN_RET_LOG(entry != bufferHandleMap_.end(), HDF_FAILURE, "not find bufferId:[%{public}d]", bufferId);
105 
106     BufferHandle *bufferHandle = entry->second;
107     int32_t ret = core_->FreeInBuffer(bufferHandle);
108     CHECK_AND_RETURN_RET_LOG(ret == HDF_SUCCESS, ret, "error = [%{public}d]", ret);
109     bufferHandleMap_.erase(entry);
110     return ret;
111 }
112 
GetNextBufferId(void)113 uint32_t CodecJpegService::GetNextBufferId(void)
114 {
115     bufferId_++;
116     return bufferId_;
117 }
118 
119 } // V1_0
120 } // Image
121 } // Codec
122 } // HDI
123 } // OHOS
124