1 /*
2 * Copyright (c) 2021 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 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 "display_gralloc.h"
17 #include <inttypes.h>
18 #include <securec.h>
19 #include "display_common.h"
20 #include "display_gralloc_gbm.h"
21
AllocMem(const AllocInfo * info,BufferHandle ** handle)22 int32_t AllocMem(const AllocInfo *info, BufferHandle **handle)
23 {
24 DISPLAY_CHK_RETURN((info == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is null"));
25 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
26 #ifdef GRALLOC_GBM_SUPPORT
27 if (info->usage & HBM_USE_MEM_DMA) {
28 return GbmAllocMem(info, handle);
29 }
30 #endif
31 DISPLAY_LOGE("the usage is not support 0x%{public}" PRIx64 "", info->usage);
32 return DISPLAY_NOT_SUPPORT;
33 }
34
FreeMem(BufferHandle * handle)35 void FreeMem(BufferHandle *handle)
36 {
37 DISPLAY_CHK_RETURN_NOT_VALUE((handle == NULL), DISPLAY_LOGE("handle is null"));
38 #ifdef GRALLOC_GBM_SUPPORT
39 if (handle->usage & HBM_USE_MEM_DMA) {
40 GbmFreeMem(handle);
41 return;
42 }
43 #endif
44 }
45
Mmap(BufferHandle * handle)46 void *Mmap(BufferHandle *handle)
47 {
48 DISPLAY_CHK_RETURN((handle == NULL), NULL, DISPLAY_LOGE("handle is null"));
49 #ifdef GRALLOC_GBM_SUPPORT
50 if (handle->usage & HBM_USE_MEM_DMA) {
51 return GbmMmap(handle);
52 }
53 #endif
54 return NULL;
55 }
56
Unmap(BufferHandle * handle)57 int32_t Unmap(BufferHandle *handle)
58 {
59 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
60 #ifdef GRALLOC_GBM_SUPPORT
61 if (handle->usage & HBM_USE_MEM_DMA) {
62 return GbmUnmap(handle);
63 }
64 #endif
65 return DISPLAY_NOT_SUPPORT;
66 }
67
FlushCache(BufferHandle * handle)68 int32_t FlushCache(BufferHandle *handle)
69 {
70 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
71 #ifdef GRALLOC_GBM_SUPPORT
72 if (handle->usage & HBM_USE_MEM_DMA) {
73 return GbmFlushCache(handle);
74 }
75 #endif
76 return DISPLAY_NOT_SUPPORT;
77 }
78
InvalidateCache(BufferHandle * handle)79 int32_t InvalidateCache(BufferHandle *handle)
80 {
81 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
82 #ifdef GRALLOC_GBM_SUPPORT
83 if (handle->usage & HBM_USE_MEM_DMA) {
84 return GbmInvalidateCache(handle);
85 }
86 #endif
87 return DISPLAY_NOT_SUPPORT;
88 }
89
GrallocUninitialize(GrallocFuncs * funcs)90 int32_t GrallocUninitialize(GrallocFuncs *funcs)
91 {
92 DISPLAY_CHK_RETURN(funcs == NULL, DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is null"));
93 DISPLAY_LOGD();
94 #ifdef GRALLOC_GBM_SUPPORT
95 if (GbmGrallocUninitialize() != DISPLAY_SUCCESS) {
96 DISPLAY_LOGE("gbm uninit failed");
97 }
98 #endif
99 free(funcs);
100 return DISPLAY_SUCCESS;
101 }
102
GrallocInitialize(GrallocFuncs ** funcs)103 int32_t GrallocInitialize(GrallocFuncs **funcs)
104 {
105 DISPLAY_LOGD();
106 DISPLAY_CHK_RETURN((funcs == NULL), DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is null"));
107 GrallocFuncs *grallocFuncs = (GrallocFuncs *)malloc(sizeof(GrallocFuncs));
108 DISPLAY_CHK_RETURN((grallocFuncs == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("memset_s failed"));
109 errno_t eok = memset_s(grallocFuncs, sizeof(GrallocFuncs), 0, sizeof(GrallocFuncs));
110 DISPLAY_CHK_RETURN((eok != EOK), DISPLAY_FAILURE, DISPLAY_LOGE("memset_s failed"));
111 // initialize gbm gralloc
112 #ifdef GRALLOC_GBM_SUPPORT
113 int ret = GbmGrallocInitialize();
114 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_LOGE("gbm initial");
115 free(grallocFuncs));
116 #endif
117 grallocFuncs->AllocMem = AllocMem;
118 grallocFuncs->FreeMem = FreeMem;
119 grallocFuncs->Mmap = Mmap;
120 grallocFuncs->Unmap = Unmap;
121 grallocFuncs->InvalidateCache = InvalidateCache;
122 grallocFuncs->FlushCache = FlushCache;
123 *funcs = grallocFuncs;
124 return DISPLAY_SUCCESS;
125 }
126