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