1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong DID 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 applicable 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_impl.h"
16 #include <algorithm>
17 #include <ashmem.h>
18 #include <display_type.h>
19 #include <dlfcn.h>
20 #include <hdf_base.h>
21 #include <mutex>
22 #include <securec.h>
23 #include <sys/mman.h>
24 #include <unistd.h>
25 #include "codec_jpeg_decoder.h"
26 #include "codec_log_wrapper.h"
27 namespace OHOS {
28 namespace VDI {
29 namespace JPEG {
30 RKMppApi *CodecJpegImpl::mppApi_ = nullptr;
31 IDisplayBufferVdi* CodecJpegImpl::displayVdi_ = nullptr;
32 intptr_t CodecJpegImpl::libHandle_ = 0;
33 static std::once_flag g_Initflag;
34 const static std::string g_libDispaly = "libdisplay_buffer_vdi_impl.z.so";
35
36 using CreateDisplayBufferVdi = IDisplayBufferVdi*(*)(void);
37
CodecJpegImpl()38 CodecJpegImpl::CodecJpegImpl()
39 {}
40
~CodecJpegImpl()41 CodecJpegImpl::~CodecJpegImpl()
42 {}
43
Init()44 int32_t CodecJpegImpl::Init()
45 {
46 CODEC_LOGI("enter");
47 std::call_once(g_Initflag, [&] {
48 auto ret = GetMppApi(&mppApi_);
49 if (ret != HDF_SUCCESS) {
50 CODEC_LOGE("GetMppApi ret %{public}d.", ret);
51 return;
52 }
53 libHandle_ = reinterpret_cast<intptr_t>(dlopen(g_libDispaly.c_str(), RTLD_LAZY));
54 if (!libHandle_) {
55 CODEC_LOGE("libHandle_ is null, errno:%{public}d", errno);
56 return;
57 }
58 CreateDisplayBufferVdi displayVdiCreate = (CreateDisplayBufferVdi)dlsym(reinterpret_cast<void*>(libHandle_), "CreateDisplayBufferVdi");
59 if (displayVdiCreate == nullptr) {
60 CODEC_LOGE("displayVdiCreate is nullptr, errno:%{public}d", errno);
61 return;
62 }
63
64 displayVdi_ = displayVdiCreate();
65 });
66 if (mppApi_ == nullptr) {
67 CODEC_LOGE("mppApi_ is nullptr");
68 return HDF_FAILURE;
69 }
70
71 if (displayVdi_ == nullptr) {
72 CODEC_LOGE("displayVdi_ is nullptr");
73 return HDF_FAILURE;
74 }
75
76 return HDF_SUCCESS;
77 }
78
DeInit()79 void CodecJpegImpl::DeInit()
80 {
81 CODEC_LOGI("enter");
82 }
83
AllocateBuffer(BufferHandle **buffer, uint32_t size)84 int32_t CodecJpegImpl::AllocateBuffer(BufferHandle **buffer, uint32_t size)
85 {
86 if (!displayVdi_) {
87 CODEC_LOGE("displayVdi_ is nullptr");
88 return HDF_FAILURE;
89 }
90
91 AllocInfo alloc = {.width = AlignUp(size/2/4, 16), // 2: min size, 8:pixel size of RGBA8888, 16: stride
92 .height = 2, // 2: min size
93 .usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA,
94 .format = PIXEL_FMT_RGBA_8888};
95 return displayVdi_->AllocMem(alloc, *buffer);
96 }
97
FreeBuffer(BufferHandle *buffer)98 int32_t CodecJpegImpl::FreeBuffer(BufferHandle *buffer)
99 {
100 if (!displayVdi_) {
101 CODEC_LOGE("displayVdi_ is nullptr");
102 return HDF_FAILURE;
103 }
104 if (buffer == nullptr) {
105 CODEC_LOGE("buffer is nullptr");
106 return HDF_ERR_INVALID_PARAM;
107 }
108
109 displayVdi_->FreeMem(*buffer);
110 return HDF_SUCCESS;
111 }
112
DeCode(BufferHandle *buffer, BufferHandle *outBuffer, const struct CodecJpegDecInfo &decInfo)113 int32_t CodecJpegImpl::DeCode(BufferHandle *buffer, BufferHandle *outBuffer, const struct CodecJpegDecInfo &decInfo)
114 {
115 if (buffer == nullptr || outBuffer == nullptr) {
116 CODEC_LOGE("buffer is nullptr or outBuffer is nullptr.");
117 return HDF_ERR_INVALID_PARAM;
118 }
119 if (mppApi_ == nullptr) {
120 CODEC_LOGE("mppApi_ is nullptr, please Init first!");
121 return HDF_ERR_INVALID_PARAM;
122 }
123
124 CodecJpegDecoder decoder(mppApi_);
125 auto ret = decoder.DeCode(buffer, outBuffer, decInfo);
126 if (ret != HDF_SUCCESS) {
127 CODEC_LOGE("mppApi_ is nullptr, please Init first!");
128 }
129
130 return ret;
131 }
132 } // namespace JPEG
133 } // namespace VDI
134 } // namespace OHOS
135
136