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