1 /*
2  * Copyright (c) 2022 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 #include "display_gralloc.h"
16 #include <cerrno>
17 #include "allocator_manager.h"
18 #include "display_common.h"
19 using namespace OHOS::HDI::DISPLAY;
AllocMem(const AllocInfo * info,BufferHandle ** handle)20 static int32_t AllocMem(const AllocInfo *info, BufferHandle **handle)
21 {
22     DISPLAY_LOGD();
23     DISPLAY_CHK_RETURN((info == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("info is nullptr"));
24     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
25     return AllocatorManager::GetInstance().GetAllocator(info->usage)->AllocMem(*info, handle);
26 }
27 
FreeMem(BufferHandle * handle)28 static void FreeMem(BufferHandle *handle)
29 {
30     DISPLAY_LOGD();
31     DISPLAY_CHK_RETURN_NOT_VALUE((handle == nullptr), DISPLAY_LOGE("handle is nullptr"));
32     AllocatorManager::GetInstance().GetAllocator(handle->usage)->FreeMem(handle);
33 }
34 
Mmap(BufferHandle * handle)35 static void *Mmap(BufferHandle *handle)
36 {
37     DISPLAY_LOGD();
38     DISPLAY_CHK_RETURN((handle == nullptr), nullptr, DISPLAY_LOGE("handle is nullptr"));
39     return AllocatorManager::GetInstance().GetAllocator(handle->usage)->Mmap(*handle);
40 }
41 
Unmap(BufferHandle * handle)42 static int32_t Unmap(BufferHandle *handle)
43 {
44     DISPLAY_LOGD();
45     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
46     return AllocatorManager::GetInstance().GetAllocator(handle->usage)->Unmap(*handle);
47 }
48 
FlushCache(BufferHandle * handle)49 static int32_t FlushCache(BufferHandle *handle)
50 {
51     DISPLAY_LOGD();
52     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
53     return AllocatorManager::GetInstance().GetAllocator(handle->usage)->FlushCache(*handle);
54 }
55 
InvalidateCache(BufferHandle * handle)56 static int32_t InvalidateCache(BufferHandle *handle)
57 {
58     DISPLAY_LOGD();
59     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
60     return AllocatorManager::GetInstance().GetAllocator(handle->usage)->InvalidateCache(*handle);
61 }
62 
63 extern "C" {
GrallocInitialize(GrallocFuncs ** funcs)64 int32_t GrallocInitialize(GrallocFuncs **funcs)
65 {
66     DISPLAY_LOGD();
67     DISPLAY_CHK_RETURN((funcs == nullptr), DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
68     GrallocFuncs *grallocFuncs = reinterpret_cast<GrallocFuncs *>(malloc(sizeof(GrallocFuncs)));
69     DISPLAY_CHK_RETURN((grallocFuncs == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("malloc failed"));
70     (void)memset_s(grallocFuncs, sizeof(GrallocFuncs), 0, sizeof(GrallocFuncs));
71     // initialize gralloc
72     int ret = AllocatorManager::GetInstance().Init();
73     if (ret != DISPLAY_SUCCESS) {
74         DISPLAY_LOGE("failed to initialize allocator manager");
75         free(grallocFuncs);
76         grallocFuncs = nullptr;
77         return DISPLAY_FAILURE;
78     }
79     grallocFuncs->AllocMem = AllocMem;
80     grallocFuncs->FreeMem = FreeMem;
81     grallocFuncs->Mmap = Mmap;
82     grallocFuncs->Unmap = Unmap;
83     grallocFuncs->InvalidateCache = InvalidateCache;
84     grallocFuncs->FlushCache = FlushCache;
85     *funcs = grallocFuncs;
86     DISPLAY_LOGD("gralloc initialize success");
87     return DISPLAY_SUCCESS;
88 }
89 
GrallocUninitialize(GrallocFuncs * funcs)90 int32_t GrallocUninitialize(GrallocFuncs *funcs)
91 {
92     DISPLAY_LOGD();
93     DISPLAY_CHK_RETURN(funcs == nullptr, DISPLAY_PARAM_ERR, DISPLAY_LOGE("funcs is nullptr"));
94     free(funcs);
95     AllocatorManager::GetInstance().DeInit();
96     return DISPLAY_SUCCESS;
97 }
98 }