1 /*
2 * Copyright (C) 2021–2022 Beijing OSWare Technology Co., Ltd
3 * This file contains confidential and proprietary information of
4 * OSWare Technology Co., Ltd
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #include "display_gralloc.h"
19 #include <cerrno>
20 #include "allocator_manager.h"
21 #include "display_common.h"
22 using namespace OHOS::HDI::DISPLAY;
AllocMem(const AllocInfo * info,BufferHandle ** handle)23 static int32_t AllocMem(const AllocInfo *info, BufferHandle **handle)
24 {
25 DISPLAY_LOGD();
26 DISPLAY_CHK_RETURN((info == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is nullptr"));
27 DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
28 return AllocatorManager::GetInstance().GetAllocator(info->usage)->AllocMem(*info, handle);
29 }
30
FreeMem(BufferHandle * handle)31 static void FreeMem(BufferHandle *handle)
32 {
33 DISPLAY_LOGD();
34 DISPLAY_CHK_RETURN_NOT_VALUE((handle == nullptr), DISPLAY_LOGE("handle is nullptr"));
35 AllocatorManager::GetInstance().GetAllocator(handle->usage)->FreeMem(handle);
36 }
37
Mmap(BufferHandle * handle)38 static void *Mmap(BufferHandle *handle)
39 {
40 DISPLAY_LOGD();
41 DISPLAY_CHK_RETURN((handle == nullptr), nullptr, DISPLAY_LOGE("handle is nullptr"));
42 return AllocatorManager::GetInstance().GetAllocator(handle->usage)->Mmap(*handle);
43 }
44
Unmap(BufferHandle * handle)45 static int32_t Unmap(BufferHandle *handle)
46 {
47 DISPLAY_LOGD();
48 DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
49 return AllocatorManager::GetInstance().GetAllocator(handle->usage)->Unmap(*handle);
50 }
51
FlushCache(BufferHandle * handle)52 static int32_t FlushCache(BufferHandle *handle)
53 {
54 DISPLAY_LOGD();
55 DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
56 return AllocatorManager::GetInstance().GetAllocator(handle->usage)->FlushCache(*handle);
57 }
58
InvalidateCache(BufferHandle * handle)59 static int32_t InvalidateCache(BufferHandle *handle)
60 {
61 DISPLAY_LOGD();
62 DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
63 return AllocatorManager::GetInstance().GetAllocator(handle->usage)->InvalidateCache(*handle);
64 }
65
66 extern "C" {
GrallocInitialize(GrallocFuncs ** funcs)67 int32_t GrallocInitialize(GrallocFuncs **funcs)
68 {
69 DISPLAY_LOGD();
70 DISPLAY_CHK_RETURN((funcs == nullptr), DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
71 GrallocFuncs *grallocFuncs = (GrallocFuncs *)malloc(sizeof(GrallocFuncs));
72 if (grallocFuncs == NULL) {
73 DISPLAY_LOGE("failed to malloc memory!");
74 return DISPLAY_FAILURE;
75 }
76 (void)memset_s(grallocFuncs, sizeof(GrallocFuncs), 0, sizeof(GrallocFuncs));
77 // initialize gralloc
78 int ret = AllocatorManager::GetInstance().Init();
79 if (ret != DISPLAY_SUCCESS) {
80 DISPLAY_LOGE("failed to initialize allocator manager");
81 free(grallocFuncs);
82 grallocFuncs = nullptr;
83 return DISPLAY_FAILURE;
84 }
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 DISPLAY_LOGD("gralloc initialize success");
93 return DISPLAY_SUCCESS;
94 }
95
GrallocUninitialize(GrallocFuncs * funcs)96 int32_t GrallocUninitialize(GrallocFuncs *funcs)
97 {
98 DISPLAY_LOGD();
99 DISPLAY_CHK_RETURN(funcs == nullptr, DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
100 free(funcs);
101 AllocatorManager::GetInstance().DeInit();
102 return DISPLAY_SUCCESS;
103 }
104 }