• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <ashmem.h>
16 #include <codec_jpeg_vdi.h>
17 #include <display_type.h>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <memory>
21 #include <securec.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 #include "codec_jpeg_decoder.h"
25 #include "codec_log_wrapper.h"
26 using namespace OHOS::VDI::JPEG;
27 static std::shared_ptr<CodecJpegDecoder> g_JpegDecoder = nullptr;
JpegInit()28 static int32_t JpegInit()
29 {
30     CODEC_LOGI("enter.");
31     if (g_JpegDecoder != nullptr) {
32         CODEC_LOGE("jpeg decoder is inited.");
33         return HDF_ERR_DEVICE_BUSY;
34     }
35     g_JpegDecoder = std::make_shared<CodecJpegDecoder>();
36     return g_JpegDecoder->Init();
37 }
38 
JpegDeInit()39 static int32_t JpegDeInit()
40 {
41     CODEC_LOGI("enter.");
42     if (g_JpegDecoder) {
43         g_JpegDecoder->DeInit();
44     }
45     g_JpegDecoder = nullptr;
46     return HDF_SUCCESS;
47 }
48 
AllocateBuffer(BufferHandle ** buffer,uint32_t size)49 static int32_t AllocateBuffer(BufferHandle **buffer, uint32_t size)
50 {
51     CODEC_LOGI("enter.");
52     if (g_JpegDecoder == nullptr) {
53         CODEC_LOGE("jpeg decoder is not init.");
54         return HDF_ERR_NOPERM;
55     }
56     int fd = OHOS::AshmemCreate(0, size);
57     OHOS::AshmemSetProt(fd, PROT_READ | PROT_WRITE);
58     BufferHandle *bufferHandle = reinterpret_cast<BufferHandle *>(malloc(sizeof(BufferHandle)));
59     if (bufferHandle == nullptr) {
60         CODEC_LOGE("malloc ret nullptr");
61         return HDF_ERR_MALLOC_FAIL;
62     }
63     auto ret = memset_s(bufferHandle, sizeof(BufferHandle), 0, sizeof(BufferHandle));
64     if (ret != EOK) {
65         CODEC_LOGE("memset_s ret err %{public}d", ret);
66         return HDF_ERR_MALLOC_FAIL;
67     }
68 
69     bufferHandle->width = size;
70     bufferHandle->height = 1;
71     bufferHandle->stride = size;
72     bufferHandle->size = size;
73     bufferHandle->format = 0;
74     bufferHandle->usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_SHARE;
75     bufferHandle->fd = fd;
76     *buffer = bufferHandle;
77     return HDF_SUCCESS;
78 }
79 
FreeBuffer(BufferHandle * buffer)80 static int32_t FreeBuffer(BufferHandle *buffer)
81 {
82     CODEC_LOGI("enter.");
83     if (g_JpegDecoder == nullptr) {
84         CODEC_LOGE("jpeg decoder is not init.");
85         return HDF_ERR_NOPERM;
86     }
87     if (buffer == nullptr) {
88         CODEC_LOGE("buffer is null.");
89         return HDF_ERR_INVALID_PARAM;
90     }
91     if (buffer->fd > -1) {
92         close(buffer->fd);
93     }
94     CODEC_LOGD("free bufferhandle %p width[%{public}d] height[%{public}d] ", buffer, buffer->width, buffer->height);
95     free(buffer);
96     return HDF_SUCCESS;
97 }
98 
DoJpegDecode(BufferHandle * buffer,BufferHandle * outBuffer,const struct CodecJpegDecInfo * decInfo,CodecJpegCallbackHwi * callback)99 static int32_t DoJpegDecode(BufferHandle *buffer, BufferHandle *outBuffer, const struct CodecJpegDecInfo *decInfo,
100     CodecJpegCallbackHwi *callback)
101 {
102     CODEC_LOGI("enter.");
103     if (g_JpegDecoder == nullptr) {
104         CODEC_LOGE("jpeg decoder is not init.");
105         return HDF_ERR_NOPERM;
106     }
107     if (buffer == nullptr || outBuffer == nullptr) {
108         CODEC_LOGE("invalid param.");
109         return HDF_ERR_INVALID_PARAM;
110     }
111     auto ret = g_JpegDecoder->DeCode(buffer, outBuffer, *decInfo, callback);
112     if (ret != HDF_SUCCESS) {
113         CODEC_LOGE("jpeg decode err %{public}d.", ret);
114         return HDF_ERR_INVALID_PARAM;
115     }
116     return ret;
117 }
118 
119 static ICodecJpegHwi g_jpegHwi = {.JpegInit = JpegInit,
120                                   .JpegDeInit = JpegDeInit,
121                                   .AllocateInBuffer = AllocateBuffer,
122                                   .FreeInBuffer = FreeBuffer,
123                                   .DoJpegDecode = DoJpegDecode};
124 
GetCodecJpegHwi()125 extern "C" ICodecJpegHwi *GetCodecJpegHwi()
126 {
127     return &g_jpegHwi;
128 }
129