• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #include "codec_gralloc_wrapper.h"
17 #include <idisplay_gralloc.h>
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "v1_0/display_composer_type.h"
21 #include "v1_0/display_buffer_type.h"
22 #include "v1_0/include/idisplay_buffer.h"
23 
24 using namespace std;
25 using namespace OHOS;
26 
27 #define HDF_LOG_TAG codec_hdi_gralloc
28 
29 #ifdef __cplusplus
30 extern "C"
31 {
32 #endif
33 OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer *g_gralloc = nullptr;
GrAllocatorInit(void)34 int32_t GrAllocatorInit(void)
35 {
36     g_gralloc = OHOS::HDI::Display::Buffer::V1_0::IDisplayBuffer::Get();
37     if (g_gralloc == nullptr) {
38         HDF_LOGE("%{public}s g_gralloc init failed!", __func__);
39         return HDF_FAILURE;
40     }
41     return HDF_SUCCESS;
42 }
43 
GrMemCreate(OHOS::HDI::Display::Buffer::V1_0::AllocInfo alloc,BufferHandle ** bufferHandle)44 static int32_t GrMemCreate(OHOS::HDI::Display::Buffer::V1_0::AllocInfo alloc, BufferHandle **bufferHandle)
45 {
46     if (g_gralloc == nullptr) {
47         HDF_LOGE("%{public}s g_gralloc null", __func__);
48         return HDF_FAILURE;
49     }
50     int32_t err = g_gralloc->AllocMem(alloc, *bufferHandle);
51     if (err != DISPLAY_SUCCESS) {
52         HDF_LOGE("%{public}s AllocMem fail, ret:%{public}d", __func__, err);
53         return HDF_FAILURE;
54     }
55     return HDF_SUCCESS;
56 }
57 
GrMemDestroy(BufferHandle * bufferHandle)58 static int32_t GrMemDestroy(BufferHandle *bufferHandle)
59 {
60     if (g_gralloc == nullptr) {
61         HDF_LOGE("%{public}s g_gralloc null", __func__);
62         return HDF_FAILURE;
63     }
64     g_gralloc->FreeMem(*bufferHandle);
65     return HDF_SUCCESS;
66 }
67 
GrMemMap(BufferHandle * bufferHandle)68 static int32_t GrMemMap(BufferHandle *bufferHandle)
69 {
70     if (g_gralloc == nullptr) {
71         HDF_LOGE("%{public}s g_gralloc null", __func__);
72         return HDF_FAILURE;
73     }
74     g_gralloc->Mmap(*bufferHandle);
75     return HDF_SUCCESS;
76 }
77 
GrMemUnmap(BufferHandle * bufferHandle)78 static int32_t GrMemUnmap(BufferHandle *bufferHandle)
79 {
80     if (g_gralloc == nullptr) {
81         HDF_LOGE("%{public}s g_gralloc null", __func__);
82         return HDF_FAILURE;
83     }
84     g_gralloc->Unmap(*bufferHandle);
85     return HDF_SUCCESS;
86 }
87 
CreateGrShareMemory(BufferHandle ** bufferHandle,CodecCmd cmd,ShareMemory * shareMemory)88 int32_t CreateGrShareMemory(BufferHandle **bufferHandle, CodecCmd cmd, ShareMemory *shareMemory)
89 {
90     if (bufferHandle == NULL || shareMemory == NULL) {
91         HDF_LOGE("%{public}s invalid param", __func__);
92         return HDF_FAILURE;
93     }
94     OHOS::HDI::Display::Buffer::V1_0::AllocInfo alloc = {
95         .width = cmd.width,
96         .height = cmd.height,
97         .usage =  OHOS::HDI::Display::Composer::V1_0::HBM_USE_CPU_READ
98             | OHOS::HDI::Display::Composer::V1_0::HBM_USE_CPU_WRITE
99             | OHOS::HDI::Display::Composer::V1_0::HBM_USE_MEM_DMA,
100         .format = cmd.pixFmt};
101 
102     int32_t ret = GrMemCreate(alloc, bufferHandle);
103     if (ret != HDF_SUCCESS || *bufferHandle == NULL) {
104         HDF_LOGE("%{public}s: Failed to create buffer handle!", __func__);
105         return HDF_FAILURE;
106     }
107     ret = GrMemMap(*bufferHandle);
108     if (ret != HDF_SUCCESS) {
109         HDF_LOGE("%{public}s: Failed to map buffer handle!", __func__);
110         return ret;
111     }
112     shareMemory->virAddr = static_cast<uint8_t *>((*bufferHandle)->virAddr);
113     if (shareMemory->virAddr == NULL) {
114         HDF_LOGE("%{public}s: Failed to map buffer handle!", __func__);
115         return HDF_FAILURE;
116     }
117     shareMemory->size = (*bufferHandle)->size;
118     shareMemory->fd = (*bufferHandle)->fd;
119     return HDF_SUCCESS;
120 }
121 
DestroyGrShareMemory(BufferHandle * bufferHandle)122 int32_t DestroyGrShareMemory(BufferHandle *bufferHandle)
123 {
124     if (bufferHandle == NULL) {
125         HDF_LOGE("%{public}s invalid param", __func__);
126         return HDF_FAILURE;
127     }
128     return GrMemDestroy(bufferHandle);
129 }
130 
OpenGrShareMemory(BufferHandle * bufferHandle,ShareMemory * shareMemory)131 int32_t OpenGrShareMemory(BufferHandle *bufferHandle, ShareMemory *shareMemory)
132 {
133     if (bufferHandle == NULL || shareMemory == NULL) {
134         HDF_LOGE("%{public}s invalid param", __func__);
135         return HDF_FAILURE;
136     }
137 
138     int32_t ret = GrMemMap(bufferHandle);
139     if (ret != HDF_SUCCESS) {
140         HDF_LOGE("%{public}s: Failed to map buffer handle!", __func__);
141         return ret;
142     }
143     shareMemory->virAddr = static_cast<uint8_t *>(bufferHandle->virAddr);
144     if (shareMemory->virAddr == NULL) {
145         HDF_LOGE("%{public}s: Failed to map buffer handle!", __func__);
146         return HDF_FAILURE;
147     }
148     shareMemory->size = bufferHandle->size;
149     shareMemory->fd = bufferHandle->fd;
150     return HDF_SUCCESS;
151 }
152 
ReleaseGrShareMemory(BufferHandle * bufferHandle,ShareMemory * shareMemory)153 int32_t ReleaseGrShareMemory(BufferHandle *bufferHandle, ShareMemory *shareMemory)
154 {
155     if (bufferHandle == NULL || shareMemory == NULL) {
156         HDF_LOGE("%{public}s invalid param", __func__);
157         return HDF_FAILURE;
158     }
159 
160     int32_t ret = GrMemUnmap(bufferHandle);
161     if (ret != HDF_SUCCESS) {
162         HDF_LOGE("%{public}s: Failed to Unmap bufferHandle!", __func__);
163         return ret;
164     }
165     shareMemory->virAddr = NULL;
166     return HDF_SUCCESS;
167 }
168 #ifdef __cplusplus
169 }
170 #endif