1 /*
2 * Copyright (c) 2021-2023 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_gralloc_gbm.h"
20
AllocMem(const AllocInfo * info,BufferHandle ** handle)21 int32_t AllocMem(const AllocInfo *info, BufferHandle **handle)
22 {
23 DISPLAY_CHK_RETURN((info == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is null"));
24 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
25 return GbmAllocMem(info, handle);
26 }
27
FreeMem(BufferHandle * handle)28 void FreeMem(BufferHandle *handle)
29 {
30 DISPLAY_CHK_RETURN_NOT_VALUE((handle == NULL), DISPLAY_LOGE("handle is null"));
31 GbmFreeMem(handle);
32 }
33
Mmap(BufferHandle * handle)34 void *Mmap(BufferHandle *handle)
35 {
36 DISPLAY_CHK_RETURN((handle == NULL), NULL, DISPLAY_LOGE("handle is null"));
37 return GbmMmap(handle);
38 }
39
Unmap(BufferHandle * handle)40 int32_t Unmap(BufferHandle *handle)
41 {
42 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
43 return GbmUnmap(handle);
44 }
45
FlushCache(BufferHandle * handle)46 int32_t FlushCache(BufferHandle *handle)
47 {
48 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
49 return GbmFlushCache(handle);
50 }
51
InvalidateCache(BufferHandle * handle)52 int32_t InvalidateCache(BufferHandle *handle)
53 {
54 DISPLAY_CHK_RETURN((handle == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is null"));
55 return GbmInvalidateCache(handle);
56 }
57
GrallocUninitialize(GrallocFuncs * funcs)58 int32_t GrallocUninitialize(GrallocFuncs *funcs)
59 {
60 DISPLAY_CHK_RETURN(funcs == NULL, DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is null"));
61 DISPLAY_DEBUGLOG();
62 #ifdef GRALLOC_GBM_SUPPORT
63 if (GbmGrallocUninitialize() != DISPLAY_SUCCESS) {
64 DISPLAY_LOGE("gbm uninit failed");
65 }
66 #endif
67 free(funcs);
68 return DISPLAY_SUCCESS;
69 }
70
GrallocInitialize(GrallocFuncs ** funcs)71 int32_t GrallocInitialize(GrallocFuncs **funcs)
72 {
73 DISPLAY_DEBUGLOG();
74 DISPLAY_CHK_RETURN((funcs == NULL), DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is null"));
75 GrallocFuncs *grallocFuncs = (GrallocFuncs *)malloc(sizeof(GrallocFuncs));
76 DISPLAY_CHK_RETURN((grallocFuncs == NULL), DISPLAY_NULL_PTR, DISPLAY_LOGE("memset_s failed"));
77 errno_t eok = memset_s(grallocFuncs, sizeof(GrallocFuncs), 0, sizeof(GrallocFuncs));
78 DISPLAY_CHK_RETURN((eok != EOK), DISPLAY_FAILURE, DISPLAY_LOGE("memset_s failed"));
79 // initialize gbm gralloc
80 #ifdef GRALLOC_GBM_SUPPORT
81 int ret = GbmGrallocInitialize();
82 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), ret, DISPLAY_LOGE("gbm initial");
83 free(grallocFuncs));
84 #endif
85 grallocFuncs->AllocMem = AllocMem;
86 grallocFuncs->FreeMem = FreeMem;
87 grallocFuncs->Mmap = Mmap;
88 grallocFuncs->Unmap = Unmap;
89 grallocFuncs->InvalidateCache = InvalidateCache;
90 grallocFuncs->FlushCache = FlushCache;
91 *funcs = grallocFuncs;
92 return DISPLAY_SUCCESS;
93 }
94